aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/zh
diff options
context:
space:
mode:
authorCashew <52880648+cashewnuttynuts@users.noreply.github.com>2024-06-01 08:58:41 +0900
committerGitHub <noreply@github.com>2024-05-31 23:58:41 +0000
commitb6341f312d8332ccff0d928d936e9290da0e9584 (patch)
treeafbe836ced6f9fdee9423bdf3b4384769b025283 /ext/js/language/zh
parent76ca08bd59f0e8bfa1bb20ac813f48e7ab241265 (diff)
Add isTextLookupWorthy function for Chinese (#743)
* add is Chinese check move * fix lint * fix lint * fixes --------- Co-authored-by: Darius Jahandarie <djahandarie@gmail.com> Co-authored-by: Stefan Vukovic <stefanvukovic44@gmail.com>
Diffstat (limited to 'ext/js/language/zh')
-rw-r--r--ext/js/language/zh/chinese.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/ext/js/language/zh/chinese.js b/ext/js/language/zh/chinese.js
new file mode 100644
index 00000000..086d2f0a
--- /dev/null
+++ b/ext/js/language/zh/chinese.js
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2024 Yomitan Authors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import {CJK_IDEOGRAPH_RANGES, CJK_PUNCTUATION_RANGE, FULLWIDTH_CHARACTER_RANGES, isCodePointInRanges} from '../CJK-util.js';
+
+/** @type {import('CJK-util').CodepointRange} */
+const BOPOMOFO_RANGE = [0x3100, 0x312f];
+/** @type {import('CJK-util').CodepointRange} */
+const BOPOMOFO_EXTENDED_RANGE = [0x31a0, 0x31bf];
+/** @type {import('CJK-util').CodepointRange} */
+const IDEOGRAPHIC_SYMBOLS_AND_PUNCTUATION_RANGE = [0x16fe0, 0x16fff];
+/** @type {import('CJK-util').CodepointRange} */
+const SMALL_FORM_RANGE = [0xfe50, 0xfe6f];
+/** @type {import('CJK-util').CodepointRange} */
+const VERTICAL_FORM_RANGE = [0xfe10, 0xfe1f];
+
+
+/**
+ * Chinese character ranges, roughly ordered in order of expected frequency.
+ * @type {import('CJK-util').CodepointRange[]}
+ */
+const CHINESE_RANGES = [
+ ...CJK_IDEOGRAPH_RANGES,
+ CJK_PUNCTUATION_RANGE,
+
+ ...FULLWIDTH_CHARACTER_RANGES,
+
+ BOPOMOFO_RANGE,
+ BOPOMOFO_EXTENDED_RANGE,
+ IDEOGRAPHIC_SYMBOLS_AND_PUNCTUATION_RANGE,
+ SMALL_FORM_RANGE,
+ VERTICAL_FORM_RANGE,
+];
+
+
+/**
+ * @param {string} str
+ * @returns {boolean}
+ */
+export function isStringPartiallyChinese(str) {
+ if (str.length === 0) { return false; }
+ for (const c of str) {
+ if (isCodePointInRanges(/** @type {number} */ (c.codePointAt(0)), CHINESE_RANGES)) {
+ return true;
+ }
+ }
+ return false;
+}