必应接入(MaterialTranslator.java):
- TranslationSource 加 BING("必应翻译"),SOURCES 加 BING 殿后
- translateViaBing 按用户给的 URL cn.bing.com/translator/?ref=TThis&text=...
从 HTML 里锚定 id="translation" div 取译文 + unescapeHtml 实体反转义
- indexOfAttr(属性顺序不固定,只认 id 值;兼容单引号变体)
- unescapeHtml(& < > " ' ' — – + 数字实体)
质量差自动路由必应(translateChatText):
- isLowQualityTranslation 三类判定:①译文==原文 ②不含 CJK ③长度比>3或<0.2
- 命中则切必应兜底(必应译文含 CJK 且≠原文才采用),仅聊天整句,材料名单字不触发
百度源(用户指定 mtpe-individual/transText):探测返回 SPA 落地页无数据,
v2transapi 1022 反爬——暂未接入,留待用户后续提供可用端点
This commit is contained in:
@@ -30,11 +30,12 @@ public class MaterialTranslator {
|
|||||||
.connectTimeout(Duration.ofSeconds(3))
|
.connectTimeout(Duration.ofSeconds(3))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// 翻译源优先级:国内接口优先
|
// 翻译源优先级:国内接口优先,必应殿后(HTML 接口,仅兜底)
|
||||||
private static final List<TranslationSource> SOURCES = Arrays.asList(
|
private static final List<TranslationSource> SOURCES = Arrays.asList(
|
||||||
TranslationSource.YOUDAO,
|
TranslationSource.YOUDAO,
|
||||||
TranslationSource.JIANXIN,
|
TranslationSource.JIANXIN,
|
||||||
TranslationSource.GOOGLE
|
TranslationSource.GOOGLE,
|
||||||
|
TranslationSource.BING
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,7 +44,8 @@ public class MaterialTranslator {
|
|||||||
private enum TranslationSource {
|
private enum TranslationSource {
|
||||||
YOUDAO("有道翻译"),
|
YOUDAO("有道翻译"),
|
||||||
JIANXIN("简心翻译"),
|
JIANXIN("简心翻译"),
|
||||||
GOOGLE("Google翻译");
|
GOOGLE("Google翻译"),
|
||||||
|
BING("必应翻译");
|
||||||
|
|
||||||
final String name;
|
final String name;
|
||||||
|
|
||||||
@@ -146,6 +148,9 @@ public class MaterialTranslator {
|
|||||||
case GOOGLE:
|
case GOOGLE:
|
||||||
result = translateViaGoogle(text);
|
result = translateViaGoogle(text);
|
||||||
break;
|
break;
|
||||||
|
case BING:
|
||||||
|
result = translateViaBing(text);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (result != null && !result.isEmpty()) {
|
if (result != null && !result.isEmpty()) {
|
||||||
return result;
|
return result;
|
||||||
@@ -250,6 +255,103 @@ public class MaterialTranslator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 必应翻译(免费网页接口,非 API,第123批再续6 用户指定)。
|
||||||
|
* URL: https://cn.bing.com/translator/?ref=TThis&text=内容&from=&to=
|
||||||
|
* 返回不是 JSON,是整页 HTML——译文在 id="translation" 的 div 里,
|
||||||
|
* 形如 <div id="translation" ...>译文</div>(属性顺序/多余属性不固定,
|
||||||
|
* 只锚定 id="translation" 起始标签,取其到下一个 </div> 之间的文本)。
|
||||||
|
* 文本经 HTML 实体编码(& < > ' " 等),须反转义。
|
||||||
|
* from 留空=自动检测源语言,to=zh 固定中文简体。
|
||||||
|
*/
|
||||||
|
private static String translateViaBing(String text) {
|
||||||
|
try {
|
||||||
|
String encoded = URLEncoder.encode(text, StandardCharsets.UTF_8);
|
||||||
|
String url = "https://cn.bing.com/translator/?ref=TThis&text=" + encoded
|
||||||
|
+ "&from=&to=";
|
||||||
|
|
||||||
|
HttpRequest req = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
||||||
|
+ " AppleWebKit/537.36 (KHTML, like Gecko)"
|
||||||
|
+ " Chrome/120.0.0.0 Safari/537.36")
|
||||||
|
.header("Accept-Language", "zh-CN,zh;q=0.9")
|
||||||
|
.timeout(Duration.ofSeconds(5))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (resp.statusCode() == 200) {
|
||||||
|
String body = resp.body();
|
||||||
|
if (body == null || body.isEmpty()) return null;
|
||||||
|
// 锚定 id="translation" 的起始标签(属性顺序不固定,只认 id 值)
|
||||||
|
int idx = indexOfAttr(body, "id", "translation");
|
||||||
|
if (idx < 0) return null;
|
||||||
|
// 跳过起始标签到内容开头
|
||||||
|
int tagEnd = body.indexOf('>', idx);
|
||||||
|
if (tagEnd < 0) return null;
|
||||||
|
int contentStart = tagEnd + 1;
|
||||||
|
// 内容到下一个 </div> 为止(译文内不含嵌套 div;若有嵌套会截断,
|
||||||
|
// 但聊天译文基本是纯文本,可接受)
|
||||||
|
int divEnd = body.indexOf("</div>", contentStart);
|
||||||
|
if (divEnd < 0) return null;
|
||||||
|
String raw = body.substring(contentStart, divEnd).trim();
|
||||||
|
if (raw.isEmpty()) return null;
|
||||||
|
return unescapeHtml(raw);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 静默降级
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 HTML 中定位形如 <tag ... id="value" ...> 的属性(属性顺序不固定)。
|
||||||
|
* @return 匹配到的标签起始下标;未命中返回 -1。
|
||||||
|
*/
|
||||||
|
private static int indexOfAttr(String html, String attrName, String attrValue) {
|
||||||
|
String needle = attrName + "=\"" + attrValue + "\"";
|
||||||
|
int idx = html.indexOf(needle);
|
||||||
|
if (idx < 0) {
|
||||||
|
// 单引号变体
|
||||||
|
idx = html.indexOf(attrName + "='" + attrValue + "'");
|
||||||
|
if (idx < 0) return -1;
|
||||||
|
}
|
||||||
|
// 回溯到标签开头 '<'
|
||||||
|
int tagStart = html.lastIndexOf('<', idx);
|
||||||
|
return tagStart >= 0 ? tagStart : idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** HTML 实体反转义(聊天译文常见 & < > " ' 数字实体) */
|
||||||
|
private static String unescapeHtml(String s) {
|
||||||
|
if (s == null) return null;
|
||||||
|
String out = s
|
||||||
|
.replace("&", "&")
|
||||||
|
.replace("<", "<")
|
||||||
|
.replace(">", ">")
|
||||||
|
.replace(""", "\"")
|
||||||
|
.replace("'", "'")
|
||||||
|
.replace("'", "'")
|
||||||
|
.replace(" ", " ")
|
||||||
|
.replace("—", "—")
|
||||||
|
.replace("–", "–");
|
||||||
|
// 数字实体 Ӓ / 😀
|
||||||
|
try {
|
||||||
|
java.util.regex.Matcher m = java.util.regex.Pattern
|
||||||
|
.compile("&#(\\d+);").matcher(out);
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int pos = 0;
|
||||||
|
while (m.find()) {
|
||||||
|
sb.append(out, pos, m.start());
|
||||||
|
sb.appendCodePoint(Integer.parseInt(m.group(1)));
|
||||||
|
pos = m.end();
|
||||||
|
}
|
||||||
|
sb.append(out.substring(pos));
|
||||||
|
out = sb.toString();
|
||||||
|
} catch (Exception ignored) { }
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== JSON解析工具 ====================
|
// ==================== JSON解析工具 ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -324,11 +426,20 @@ public class MaterialTranslator {
|
|||||||
private static final int CHAT_CACHE_MAX = 500;
|
private static final int CHAT_CACHE_MAX = 500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 聊天整句机翻(英→中)。复用 translateWithFallback 的
|
* 聊天整句机翻(非简中→中)。复用 translateWithFallback 的
|
||||||
* YOUDAO→JIANXIN→GOOGLE 降级链(与领地系统同一套 API 流程)。
|
* YOUDAO→JIANXIN→GOOGLE→BING 降级链(与领地系统同一套 API 流程)。
|
||||||
|
*
|
||||||
|
* ★ 第123批再续6(用户定调「质量差的时候用必应」):现有源译文质量差时
|
||||||
|
* (乱码/与原文相同/疑似纯英文回显),自动加译必应并以其为准——
|
||||||
|
* 必应对德语/日语等非英语语种质量明显更好(用户截图实锤:同一句德语
|
||||||
|
* 有道译成「德泽活动是一间小木屋」,必应译出正确中文)。
|
||||||
|
* 判定「质量差」的口径(满足任一即切必应):
|
||||||
|
* ① 译文==原文(源没翻出来);
|
||||||
|
* ② 译文不含任何 CJK 统一表意文字(源把非拉丁原文当英文回显);
|
||||||
|
* ③ 译文与原文长度比 >3 或 <0.2(极端异常)。
|
||||||
* 结果独立缓存(上限500条,满则整体清空重来),不污染材料名缓存。
|
* 结果独立缓存(上限500条,满则整体清空重来),不污染材料名缓存。
|
||||||
* 同步阻塞 HTTP(每源 3s 超时),调用方必须放在异步线程执行。
|
* 同步阻塞 HTTP(每源 3~5s 超时),调用方必须放在异步线程执行。
|
||||||
* 返回 null 表示翻译失败或译文无意义(如与原文相同)。
|
* 返回 null 表示翻译失败或译文无意义。
|
||||||
*/
|
*/
|
||||||
public static String translateChatText(String text) {
|
public static String translateChatText(String text) {
|
||||||
if (text == null) return null;
|
if (text == null) return null;
|
||||||
@@ -337,6 +448,14 @@ public class MaterialTranslator {
|
|||||||
String hit = chatCache.get(key);
|
String hit = chatCache.get(key);
|
||||||
if (hit != null) return hit;
|
if (hit != null) return hit;
|
||||||
String zh = translateWithFallback(key);
|
String zh = translateWithFallback(key);
|
||||||
|
// 现有源质量差 → 必应兜底(仅聊天整句;材料名单字不触发)
|
||||||
|
if (isLowQualityTranslation(key, zh)) {
|
||||||
|
String bing = translateViaBing(key);
|
||||||
|
if (bing != null && !bing.isEmpty() && !bing.equals(key)
|
||||||
|
&& containsCjk(bing)) {
|
||||||
|
zh = bing; // 必应译文更可信,以其为准
|
||||||
|
}
|
||||||
|
}
|
||||||
if (zh != null && !zh.isEmpty() && !zh.equals(key)) {
|
if (zh != null && !zh.isEmpty() && !zh.equals(key)) {
|
||||||
if (chatCache.size() >= CHAT_CACHE_MAX) chatCache.clear();
|
if (chatCache.size() >= CHAT_CACHE_MAX) chatCache.clear();
|
||||||
chatCache.put(key, zh);
|
chatCache.put(key, zh);
|
||||||
@@ -345,6 +464,29 @@ public class MaterialTranslator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 译文质量判定:乱码/回显/极端长度比 → 质量差 */
|
||||||
|
private static boolean isLowQualityTranslation(String original, String translated) {
|
||||||
|
if (translated == null || translated.isEmpty()) return true;
|
||||||
|
if (translated.equals(original)) return true; // ① 没翻出来
|
||||||
|
if (!containsCjk(translated)) return true; // ② 非中文回显
|
||||||
|
int olen = original.codePointCount(0, original.length());
|
||||||
|
int tlen = translated.codePointCount(0, translated.length());
|
||||||
|
if (olen > 0 && (tlen > olen * 3 || tlen < olen * 0.2)) {
|
||||||
|
return true; // ③ 极端异常
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 是否含 CJK 统一表意文字(中文/日文汉字等) */
|
||||||
|
private static boolean containsCjk(String s) {
|
||||||
|
if (s == null) return false;
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
char c = s.charAt(i);
|
||||||
|
if (c >= '一' && c <= '鿿') return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 机翻屏蔽关键词(98批,mod 客户端/模组相关) ====================
|
// ==================== 机翻屏蔽关键词(98批,mod 客户端/模组相关) ====================
|
||||||
|
|
||||||
/** 机翻屏蔽关键词(小写)。来源:插件数据目录 mod_keywords.txt,缺失时回退 jar 内置模板 */
|
/** 机翻屏蔽关键词(小写)。来源:插件数据目录 mod_keywords.txt,缺失时回退 jar 内置模板 */
|
||||||
|
|||||||
Reference in New Issue
Block a user