This commit is contained in:
test123456654321 2024-10-25 21:12:19 +08:00
parent d57b0fa504
commit a01be4ea6c
43 changed files with 183 additions and 138 deletions

View File

@ -123,8 +123,6 @@ def checklang():
globalconfig["language_setted_2.4.5"] = True
globalconfig["languageuse2"] = x.current
globalconfig["tgtlang4"] = x.current
if globalconfig["tgtlang4"] == "ja":
globalconfig["srclang4"] = "zh"
def checkintegrity():

View File

@ -7,19 +7,10 @@ from myutils.utils import simplehtmlparser
class youdao(cishubase):
@property
def srclang(self):
try:
l = getlangsrc()
return l
except:
return ""
def search(self, word):
url = "https://dict.youdao.com/result?word={}&lang={}".format(
quote(word), self.srclang
quote(word), getlangsrc()
)
text = requests.get(url, proxies=self.proxy).text

View File

@ -970,10 +970,10 @@ class dialog_setting_game_internal(QWidget):
formLayout2.addRow(
"源语言",
getsimplecombobox(
[_["zh"] for _ in static_data["lang_list_all"]],
["自动"] + [_["zh"] for _ in static_data["lang_list_all"]],
savehook_new_data[gameuid],
"private_srclang_2",
internal=[_["code"] for _ in static_data["lang_list_all"]],
internal=["auto"] + [_["code"] for _ in static_data["lang_list_all"]],
),
)
formLayout2.addRow(

View File

@ -27,12 +27,12 @@ def setTablanglz(self):
[
"源语言",
D_getsimplecombobox(
[_["zh"] for _ in static_data["lang_list_all"]],
["自动"]
+ [_["zh"] for _ in static_data["lang_list_all"]],
globalconfig,
"srclang4",
internal=[
_["code"] for _ in static_data["lang_list_all"]
],
internal=["auto"]
+ [_["code"] for _ in static_data["lang_list_all"]],
),
],
[

View File

@ -25,6 +25,8 @@ class commonbase:
_setting_dict = None
typename = None
ocr_cant_auto = False
def langmap(self):
return {}
@ -34,7 +36,10 @@ class commonbase:
@property
def srclang(self):
return self.langmap_.get(self.srclang_1, "")
l = self.srclang_1
if self.ocr_cant_auto and (l == "auto"):
raise Exception(_TR("当前OCR引擎不支持设置语言为自动"))
return self.langmap_.get(l, l)
@property
def srclang_1(self) -> str:
@ -46,7 +51,8 @@ class commonbase:
@property
def tgtlang(self):
return self.langmap_.get(self.tgtlang_1, "")
l = self.tgtlang_1
return self.langmap_.get(l, l)
@property
def config(self):
@ -80,7 +86,7 @@ class commonbase:
[_["code"] for _ in static_data["lang_list_all"]],
)
)
_.update({"cht": "zh"})
_.update({"cht": "zh", "auto": "auto"})
_.update(self.langmap())
return _

View File

@ -0,0 +1,79 @@
from collections import Counter
def inrange(n, s, e):
return n >= s and n <= e
def inranges(n, *argc):
for s, e in argc:
if inrange(n, s, e):
return True
return False
def guess(string: str):
if not string:
return "en"
if string.isascii():
return "en"
checkers = {
"ru": lambda c: inranges(
ord(c),
(0x0400, 0x04FF),
(0x0500, 0x052F),
(0x2DE0, 0x2DFF),
(0xA640, 0xA69F),
(0x1C80, 0x1C8F),
(0x1C90, 0x1CBF),
(0x1D2C, 0x1D5F),
(0x1D780, 0x1D7AF),
),
"ko": lambda c: inranges(
ord(c),
(0x1100, 0x11FF),
(0x3130, 0x318F),
(0xAC00, 0xD7AF),
(0xA960, 0xA97F),
(0xD7B0, 0xD7FF),
),
"ja": lambda c: inranges(
ord(c),
(0x3040, 0x309F),
(0x30A0, 0x30FF),
(0xFF65, 0xFF9F),
(0x31F0, 0x31FF),
(0x3100, 0x312F),
(0x31A0, 0x31BF),
(0x3000, 0x303F),
),
"zh": lambda c: inranges(
ord(c),
(0x4E00, 0x9FA5),
),
"ar": lambda c: inranges(
ord(c),
(0x0600, 0x06FF),
(0x0750, 0x077F),
(0x08A0, 0x08FF),
(0x0870, 0x089F),
(0x1EE00, 0x1EEFF),
(0x0600, 0x0603),
(0x060C, 0x061F),
(0x0660, 0x0669),
(0x06F0, 0x06F9),
),
"en": lambda c: inranges(
ord(c),
(0x0000, 0x00FF),
),
}
cnt = []
for c in string.strip():
for lang in checkers:
if checkers[lang](c):
cnt.append(lang)
if not cnt:
return "en"
common = Counter(cnt).most_common()
return common[0][0]

View File

@ -939,9 +939,11 @@ def createurl(url: str):
def createenglishlangmap():
return dict(
mp = dict(
zip(
[_["code"] for _ in static_data["lang_list_all"]],
[_["en"] for _ in static_data["lang_list_all"]],
)
)
mp.update({"auto": ""})
return mp

View File

@ -185,6 +185,8 @@ def question(dialog: QDialog):
class OCR(baseocr):
ocr_cant_auto = True
def langmap(self):
return {"cht": "cht"}

View File

@ -3,6 +3,8 @@ from ocrengines.baseocrclass import baseocr
class OCR(baseocr):
ocr_cant_auto = True
def langmap(self):
return {
"ja": "jpn",

View File

@ -48,6 +48,8 @@ def question(dialog: QDialog):
class OCR(baseocr):
ocr_cant_auto = True
def langmap(self):
return {"cht": "cht"}

View File

@ -11,6 +11,8 @@ import json
class OCR(baseocr):
ocr_cant_auto = True
def ocr(self, imagebinary):
self.checkempty(["APPId", "APISecret", "APIKey"])

View File

@ -16,8 +16,9 @@ class TS(basetrans):
path = "/translate"
constructed_url = endpoint + path
params = {"api-version": "3.0", "from": self.srclang, "to": self.tgtlang}
params = {"api-version": "3.0", "to": self.tgtlang}
if self.srclang != "auto":
params.update({"from": self.srclang})
headers = {
"Ocp-Apim-Subscription-Key": key,

View File

@ -55,7 +55,7 @@ class TS(basetrans):
json_data = {
"query": query,
"from": self.srclang,
"from": self.parse_maybe_autolang(query),
"to": self.tgtlang,
"reference": "",
"corpusIds": [],

View File

@ -8,6 +8,7 @@ import functools
from myutils.config import globalconfig, translatorsetting
from myutils.utils import stringfyerror, autosql, PriorityQueue, SafeFormatter
from myutils.commonbase import ArgsEmptyExc, commonbase
from myutils.languageguesser import guess
class Interrupted(Exception):
@ -75,6 +76,12 @@ class basetrans(commonbase):
return alternatedict(translatorsetting[self.typename]["args"])
def parse_maybe_autolang(self, content):
if self.srclang != "auto":
return self.srclang
gs = guess(content)
return self.langmap_.get(gs, gs)
############################################################
_globalconfig_key = "fanyi"
_setting_dict = translatorsetting

View File

@ -166,7 +166,10 @@ class Bing(Tse):
data = r.json()
time.sleep(sleep_seconds)
self.query_count += 1
try:
return data[0] if is_detail_result else data[0]["translations"][0]["text"]
except:
raise Exception(r.maybejson)
class TS(basetrans):
@ -177,95 +180,14 @@ class TS(basetrans):
self.engine = Bing()
def translate(self, content):
src = self.parse_maybe_autolang(content)
try:
return self.engine.bing_api(
content,
self.srclang,
src,
self.tgtlang,
proxies=self.proxy,
if_use_cn_host=True,
)
except:
return self.engine.bing_api(
content, self.srclang, self.tgtlang, proxies=self.proxy
)
# class TS(basetrans):
# def langmap(self):
# return {"zh":"zh-Hans","cht":"zh-Hant"}
# def inittranslator(self):
# self.ss=requests.session()
# headers = {
# 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
# 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
# 'cache-control': 'no-cache',
# 'pragma': 'no-cache',
# 'sec-ch-ua': '"Microsoft Edge";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
# 'sec-ch-ua-arch': '"x86"',
# 'sec-ch-ua-bitness': '"64"',
# 'sec-ch-ua-full-version': '"105.0.1343.53"',
# 'sec-ch-ua-full-version-list': '"Microsoft Edge";v="105.0.1343.53", "Not)A;Brand";v="8.0.0.0", "Chromium";v="105.0.5195.127"',
# 'sec-ch-ua-mobile': '?0',
# 'sec-ch-ua-model': '""',
# 'sec-ch-ua-platform': '"Windows"',
# 'sec-ch-ua-platform-version': '"10.0.0"',
# 'sec-ch-ua-wow64': '?0',
# 'sec-fetch-dest': 'document',
# 'sec-fetch-mode': 'navigate',
# 'sec-fetch-site': 'same-origin',
# 'sec-fetch-user': '?1',
# 'upgrade-insecure-requests': '1',
# 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53',
# }
# response = self.ss.get('https://cn.bing.com/translator/' ,headers=headers)
# text=response.text
# res=re.compile('var params_AbusePreventionHelper = (.*?);').findall(text)[0]
# self.key=str(eval(res)[0])
# self.token=str(eval(res)[1])
# iid = 'translator.5028'
# ig = re.compile('IG:"(.*?)"').findall(text)[0]
# self.IG=ig
# self.iid=iid
# def translate(self,content):
# print(content)
# headers = {
# 'authority': 'cn.bing.com',
# 'accept': '*/*',
# 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
# 'cache-control': 'no-cache',
# 'content-type': 'application/x-www-form-urlencoded',
# 'origin': 'https://cn.bing.com',
# 'pragma': 'no-cache',
# 'referer': 'https://cn.bing.com/translator/',
# 'sec-ch-ua': '"Microsoft Edge";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
# 'sec-ch-ua-arch': '"x86"',
# 'sec-ch-ua-bitness': '"64"',
# 'sec-ch-ua-full-version': '"105.0.1343.53"',
# 'sec-ch-ua-full-version-list': '"Microsoft Edge";v="105.0.1343.53", "Not)A;Brand";v="8.0.0.0", "Chromium";v="105.0.5195.127"',
# 'sec-ch-ua-mobile': '?0',
# 'sec-ch-ua-platform': '"Windows"',
# 'sec-ch-ua-platform-version': '"10.0.0"',
# 'sec-fetch-dest': 'empty',
# 'sec-fetch-mode': 'cors',
# 'sec-fetch-site': 'same-origin',
# 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53',
# 'x-edge-shopping-flag': '1',
# }
# response = self.ss.post('https://cn.bing.com/ttranslatev3?isVertical=1&&IG='+self.IG+'&IID='+self.iid,headers=headers, data={
# 'fromLang':self.srclang,'text':content,'to':self.tgtlang,'token':self.token,'key':self.key
# })#data=data )
# js=response.json()
# ch=js[0]['translations'][0]['text']
# return ch
return self.engine.bing_api(content, src, self.tgtlang, proxies=self.proxy)

View File

@ -20,7 +20,7 @@ class TS(basetransdev):
def translate(self, content):
self.Page_navigate(
"https://www.deepl.com/en/translator#{}/{}/{}".format(
self.srclang, self.tgtlang, quote(content)
self.parse_maybe_autolang(content), self.tgtlang, quote(content)
)
)
return self.wait_for_result(

View File

@ -2,7 +2,7 @@ from translator.basetranslator_dev import basetransdev
import time
class TS(basetransdev):
target_url = "https://fanyi.caiyunapp.com/#/"
target_url = "https://fanyi.caiyunapp.com"
def translate(self, content):
self.Runtime_evaluate(

View File

@ -2,7 +2,7 @@ from translator.basetranslator_dev import basetransdev
class TS(basetransdev):
target_url = "https://fanyi.youdao.com/"
target_url = "https://fanyi.youdao.com/#/TextTranslate"
def translate(self, content):
self.Runtime_evaluate(

View File

@ -400,9 +400,10 @@ def trans(TextList, k_access_key, k_secret_key, src, tgt, proxy):
service = Service(k_service_info, k_api_info)
body = {
"TargetLanguage": tgt,
"SourceLanguage": src,
"TextList": [TextList],
}
if src != "auto":
body.update({"SourceLanguage": src})
res = service.json("translate", {}, json.dumps(body), proxy)
return res

View File

@ -10,7 +10,9 @@ class TS(basetrans):
apikey = self.multiapikeycurrent["apikey"]
url = self.config["apiurl"] + "/v3/translate?version=2018-05-01"
headers = {"Content-Type": "application/json"}
data = {"text": [query], "source": self.srclang, "target": self.tgtlang}
data = {"text": [query], "target": self.tgtlang}
if self.srclang != "auto":
data.update({"source": self.srclang})
response = self.proxysession.post(
url, auth=("apikey", apikey), headers=headers, data=json.dumps(data)

View File

@ -137,6 +137,7 @@ from translator.basetranslator import basetrans
class TS(basetrans):
def langmap(self):
return {
"zh": "chi",
@ -153,6 +154,10 @@ class TS(basetrans):
self.engine._ = None
def translate(self, content):
return self.engine.reverso_api(
content, self.srclang, self.tgtlang, proxies=self.proxy
content,
self.parse_maybe_autolang(content),
self.tgtlang,
proxies=self.proxy,
)

View File

@ -154,5 +154,8 @@ class TS(basetrans):
def translate(self, content):
return self.engine.reverso_api(
content, self.srclang, self.tgtlang, proxies=self.proxy
content,
self.parse_maybe_autolang(content),
self.tgtlang,
proxies=self.proxy,
)

View File

@ -2004,7 +2004,7 @@
"minlength": 0,
"maxlength": 1000,
"maxoriginlength": 1000,
"srclang4": "ja",
"srclang4": "auto",
"tgtlang4": "zh",
"transkiroku": false,
"transkirokuuse": "baidu",

View File

@ -808,5 +808,6 @@
"日志": "سجل .",
"查看": "عرض .",
"标记": "علامة",
"去除标记": "إزالة علامة"
"去除标记": "إزالة علامة",
"当前OCR引擎不支持设置语言为自动": "التعرف الضوئي على الحروف المحرك الحالي لا يدعم وضع اللغة التلقائي"
}

View File

@ -808,5 +808,6 @@
"日志": "日誌",
"查看": "查看",
"标记": "標記",
"去除标记": "去除標記"
"去除标记": "去除標記",
"当前OCR引擎不支持设置语言为自动": "當前OCR引擎不支持設定語言為自動"
}

View File

@ -808,5 +808,6 @@
"日志": "deník",
"查看": "viz",
"标记": "značka",
"去除标记": "Odstranit značení"
"去除标记": "Odstranit značení",
"当前OCR引擎不支持设置语言为自动": "Aktuální OCR engine nepodporuje nastavení jazyka na automatické"
}

View File

@ -808,5 +808,6 @@
"日志": "Journal",
"查看": "siehe",
"标记": "Zeichen",
"去除标记": "Markierung entfernen"
"去除标记": "Markierung entfernen",
"当前OCR引擎不支持设置语言为自动": "Die aktuelle OCR-Engine unterstützt die Einstellung der Sprache auf automatisch nicht"
}

View File

@ -808,5 +808,6 @@
"日志": "journal",
"查看": "see",
"标记": "sign",
"去除标记": "Remove marking"
"去除标记": "Remove marking",
"当前OCR引擎不支持设置语言为自动": "The current OCR engine does not support setting the language to automatic"
}

View File

@ -808,5 +808,6 @@
"日志": "Diario",
"查看": "Ver",
"标记": "Marcado",
"去除标记": "Eliminar la marca"
"去除标记": "Eliminar la marca",
"当前OCR引擎不支持设置语言为自动": "El motor OCR actual no admite configurar el lenguaje a automático"
}

View File

@ -808,5 +808,6 @@
"日志": "Logs",
"查看": "Voir",
"标记": "Marquage",
"去除标记": "Enlever les marques"
"去除标记": "Enlever les marques",
"当前OCR引擎不支持设置语言为自动": "Le moteur OCR actuel ne prend pas en charge le réglage de la langue sur auto"
}

View File

@ -808,5 +808,6 @@
"日志": "diario",
"查看": "vedi",
"标记": "segno",
"去除标记": "Rimuovi la marcatura"
"去除标记": "Rimuovi la marcatura",
"当前OCR引擎不支持设置语言为自动": "Il motore OCR corrente non supporta l'impostazione automatica della lingua"
}

View File

@ -808,5 +808,6 @@
"日志": "ログ#ログ#",
"查看": "表示#ヒョウジ#",
"标记": "マークアップ",
"去除标记": "タグを削除"
"去除标记": "タグを削除",
"当前OCR引擎不支持设置语言为自动": "現在のOCRエンジンでは言語の自動設定はサポートされていません"
}

View File

@ -808,5 +808,6 @@
"日志": "로그",
"查看": "보기",
"标记": "태그",
"去除标记": "태그 제거"
"去除标记": "태그 제거",
"当前OCR引擎不支持设置语言为自动": "현재 OCR 엔진은 자동 언어 설정을 지원하지 않습니다."
}

View File

@ -808,5 +808,6 @@
"日志": "dagboek",
"查看": "zie",
"标记": "teken",
"去除标记": "Markering verwijderen"
"去除标记": "Markering verwijderen",
"当前OCR引擎不支持设置语言为自动": "De huidige OCR-engine ondersteunt niet het instellen van de taal op automatisch"
}

View File

@ -808,5 +808,6 @@
"日志": "dziennik",
"查看": "zobacz",
"标记": "znak",
"去除标记": "Usuń oznaczenie"
"去除标记": "Usuń oznaczenie",
"当前OCR引擎不支持设置语言为自动": "Obecny silnik OCR nie obsługuje ustawienia języka na automatyczne"
}

View File

@ -808,5 +808,6 @@
"日志": "diário",
"查看": "ver",
"标记": "signo",
"去除标记": "Remover a marcação"
"去除标记": "Remover a marcação",
"当前OCR引擎不支持设置语言为自动": "O mecanismo OCR atual não suporta configurar o idioma para automático"
}

View File

@ -808,5 +808,6 @@
"日志": "Журнал",
"查看": "Посмотреть",
"标记": "Маркировка",
"去除标记": "Удалить метку"
"去除标记": "Удалить метку",
"当前OCR引擎不支持设置语言为自动": "Текущий движок OCR не поддерживает настройку языка автоматически"
}

View File

@ -808,5 +808,6 @@
"日志": "journal",
"查看": "se",
"标记": "tecken",
"去除标记": "Ta bort märkning"
"去除标记": "Ta bort märkning",
"当前OCR引擎不支持设置语言为自动": "Den aktuella OCR-motorn stöder inte inställning av språket till automatiskt"
}

View File

@ -808,5 +808,6 @@
"日志": "บันทึกประจำวัน",
"查看": "ดู",
"标记": "เครื่องหมาย",
"去除标记": "ลบเครื่องหมาย"
"去除标记": "ลบเครื่องหมาย",
"当前OCR引擎不支持设置语言为自动": "เครื่องยนต์ OCR ปัจจุบันไม่รองรับการตั้งค่าภาษาเป็นอัตโนมัติ"
}

View File

@ -808,5 +808,6 @@
"日志": "Günlük",
"查看": "Gördün mü?",
"标记": "imza",
"去除标记": "İşaret Çıkar"
"去除标记": "İşaret Çıkar",
"当前OCR引擎不支持设置语言为自动": "Ağımdaki OCR motoru dili otomatik olarak ayarlamasını desteklemez"
}

View File

@ -808,5 +808,6 @@
"日志": "журнал",
"查看": "див.",
"标记": "знак",
"去除标记": "Вилучити позначення"
"去除标记": "Вилучити позначення",
"当前OCR引擎不支持设置语言为自动": "Поточний рушій OCR не підтримує встановлення мови як автоматичного"
}

View File

@ -808,5 +808,6 @@
"日志": "Đăng nhập",
"查看": "Xem thêm",
"标记": "Đánh dấu",
"去除标记": "Xóa đánh dấu"
"去除标记": "Xóa đánh dấu",
"当前OCR引擎不支持设置语言为自动": "Công cụ OCR hiện tại không hỗ trợ thiết lập ngôn ngữ tự động"
}

View File

@ -808,5 +808,6 @@
"日志": "",
"查看": "",
"标记": "",
"去除标记": ""
"去除标记": "",
"当前OCR引擎不支持设置语言为自动": ""
}