ANTICHAT

ANTICHAT (https://forum.antichat.xyz/index.php)
-   Python (https://forum.antichat.xyz/forumdisplay.php?f=205)
-   -   translate (https://forum.antichat.xyz/showthread.php?t=1510292)

Dickson 08.05.2024 06:59

This is a simple translator. it has gui interface and console interface

Цитата:

Сообщение от Спойлер



code:

Цитата:

Сообщение от Спойлер


main.py:





Код:

import
requests

headers
=
{
'accept'
:
'application/json, text/plain, */*'
,
'accept-language'
:
'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
,
'content-type'
:
'application/json'
,
'origin'
:
'https://www.reverso.net'
,
'priority'
:
'u=1, i'
,
'referer'
:
'https://www.reverso.net/'
,
'sec-ch-ua'
:
'"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"'
,
'sec-ch-ua-mobile'
:
'?0'
,
'sec-ch-ua-platform'
:
'"Windows"'
,
'sec-fetch-dest'
:
'empty'
,
'sec-fetch-mode'
:
'cors'
,
'sec-fetch-site'
:
'same-site'
,
'user-agent'
:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
,
'x-reverso-origin'
:
'translation.web'
,
}
def
translate
(
word
)
:
json_data
=
{
'format'
:
'text'
,
'from'
:
'rus'
,
'to'
:
'eng'
,
'input'
:
word
,
'options'
:
{
'sentenceSplitter'
:
True
,
'origin'
:
'translation.web'
,
'contextResults'
:
True
,
'languageDetection'
:
True
,
}
,
}
response
=
requests
.
post
(
'https://api.reverso.net/translate/v1/translation'
,
headers
=
headers
,
json
=
json_data
)
if
response
.
status_code
==
200
:
data
=
response
.
json
(
)
translation
=
data
[
'translation'
]
print
(
translation
)
else
:
print
(
f"Ошибка:{response.status_code}"
)
while
True
:
word
=
input
(
"Введите слово для перевода (или 'q' для выхода): "
)
if
word
.
lower
(
)
==
'q'
:
break
translate
(
word
)




Цитата:

Сообщение от Спойлер


main.py:





Код:

import
requests
from
PySide6
.
QtWidgets
import
(
QApplication
,
QMainWindow
,
QPushButton
,
QLineEdit
,
QComboBox
,
QLabel
,
QVBoxLayout
,
QHBoxLayout
,
QWidget
)
import
sys
from
PySide6
.
QtGui
import
QLinearGradient
,
QBrush
,
QPalette
,
QColor

headers
=
{
'accept'
:
'application/json, text/plain, */*'
,
'accept-language'
:
'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
,
'content-type'
:
'application/json'
,
'origin'
:
'https://www.reverso.net'
,
'priority'
:
'u=1, i'
,
'referer'
:
'https://www.reverso.net/'
,
'sec-ch-ua'
:
'"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"'
,
'sec-ch-ua-mobile'
:
'?0'
,
'sec-ch-ua-platform'
:
'"Windows"'
,
'sec-fetch-dest'
:
'empty'
,
'sec-fetch-mode'
:
'cors'
,
'sec-fetch-site'
:
'same-site'
,
'user-agent'
:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
,
'x-reverso-origin'
:
'translation.web'
,
}
def
translate
(
word
,
from_lang
,
to_lang
)
:
json_data
=
{
'format'
:
'text'
,
'from'
:
from_lang
,
'to'
:
to_lang
,
'input'
:
word
,
'options'
:
{
'sentenceSplitter'
:
True
,
'origin'
:
'translation.web'
,
'contextResults'
:
True
,
'languageDetection'
:
True
,
}
,
}
response
=
requests
.
post
(
'https://api.reverso.net/translate/v1/translation'
,
headers
=
headers
,
json
=
json_data
)
if
response
.
status_code
==
200
:
data
=
response
.
json
(
)
translations
=
data
[
'translation'
]
if
isinstance
(
translations
,
list
)
:
return
'\n'
.
join
(
translations
)
else
:
return
translations
else
:
return
f"Ошибка:{response.status_code}"
class
TranslatorWindow
(
QMainWindow
)
:
def
__init__
(
self
)
:
super
(
)
.
__init__
(
)
self
.
setWindowTitle
(
'Переводчик'
)
self
.
setGeometry
(
100
,
100
,
400
,
100
)
self
.
central_widget
=
QWidget
(
)
self
.
setCentralWidget
(
self
.
central_widget
)
self
.
layout
=
QVBoxLayout
(
self
.
central_widget
)
self
.
input_layout
=
QHBoxLayout
(
)
self
.
layout
.
addLayout
(
self
.
input_layout
)
self
.
from_lang_combo
=
QComboBox
(
)
self
.
from_lang_combo
.
addItems
(
[
"rus"
,
"eng"
,
"deu"
,
"fra"
,
"spa"
,
"ita"
,
"por"
,
"jpn"
,
"zho"
]
)
self
.
from_lang_combo
.
setCurrentText
(
"rus"
)
self
.
input_layout
.
addWidget
(
self
.
from_lang_combo
)
self
.
to_lang_combo
=
QComboBox
(
)
self
.
to_lang_combo
.
addItems
(
[
"rus"
,
"eng"
,
"deu"
,
"fra"
,
"spa"
,
"ita"
,
"por"
,
"jpn"
,
"zho"
]
)
self
.
to_lang_combo
.
setCurrentText
(
"eng"
)
self
.
input_layout
.
addWidget
(
self
.
to_lang_combo
)
self
.
translate_button
=
QPushButton
(
'Перевести'
)
self
.
translate_button
.
clicked
.
connect
(
self
.
on_translate_button_click
)
self
.
layout
.
addWidget
(
self
.
translate_button
)
self
.
input_field
=
QLineEdit
(
)
self
.
input_field
.
setPlaceholderText
(
'Введите текст для перевода'
)
self
.
layout
.
addWidget
(
self
.
input_field
)
self
.
translation_label
=
QLabel
(
)
self
.
layout
.
addWidget
(
self
.
translation_label
)
def
on_translate_button_click
(
self
)
:
word
=
self
.
input_field
.
text
(
)
if
word
:
result
=
translate
(
word
,
self
.
from_lang_combo
.
currentText
(
)
,
self
.
to_lang_combo
.
currentText
(
)
)
self
.
translation_label
.
setText
(
result
)
if
__name__
==
'__main__'
:
app
=
QApplication
(
sys
.
argv
)
translator
=
TranslatorWindow
(
)
translator
.
show
(
)
sys
.
exit
(
app
.
exec
(
)
)





Fott 08.05.2024 16:54

https://forum.antichat.xyz/attachmen...8d44bbe66a.png

https://forum.antichat.xyz/attachmen...15dbf55968.png

И чем тебе гуи на сайте не угодило?

1715172840484.pngFott · 8 Май 2024 в 15:54' data-fancybox="lb-post-1493162" data-lb-caption-extra-html="" data-lb-sidebar-href="" data-single-image="1" data-src="https://www.blast.hk/attachments/239572/" style="cursor: pointer;" title="1715172840484.png">
https://forum.antichat.xyz/attachmen...2332dfcfd1.png

VRush 08.05.2024 17:23

захотел перевести "q" на русский, прога закрылась

Dickson 08.05.2024 22:14

Цитата:

Сообщение от Fott

И чем тебе гуи на сайте не угодило?

гуи написал чисто для себя, для удобства, поэтому я приложил еще и консольную версию. вообще думал телеграм бота написать, потом как то впадлу стало

Ebatte_Sratte 04.07.2024 01:33

from deep_translator import GoogleTranslator

это не лучше?

Dickson 06.07.2024 18:29

Цитата:

Сообщение от Ebatte_Sratte

from deep_translator import GoogleTranslator
это не лучше?

Не знаю, написал чисто для того, чтоб обучится парсингу

goodflex 19.07.2024 21:16

зачем


Время: 02:38