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
(
)
)
|
|