UNPKG

mstf-kit

Version:

一个现代化的 JavaScript/TypeScript 工具库,提供了丰富的常用工具函数

153 lines (152 loc) 3.16 kB
const UYGHUR_CHARS = [ "ئ", "ا", "ە", "ب", "پ", "ت", "ج", "چ", "خ", "د", "ر", "ز", "ژ", "س", "ش", "غ", "ف", "ق", "ك", "گ", "ڭ", "ل", "م", "ن", "ھ", "و", "ۇ", "ۆ", "ۈ", "ۋ", "ې", "ى", "ي" ]; const UYGHUR_COMMON_WORDS = [ "مەن", "سەن", "ئۇ", "بىز", "سىلەر", "ئۇلار", // 人称代词 "ۋە", "بىلەن", "ئۈچۈن", "لېكىن", "چۈنكى", // 连词 "بار", "يوق", "كەلدى", "كەتتى", "ياخشى", "چوڭ", // 常用词 "قانداق", "نېمە", "قاچان", "قەيەر", "كىم" // 疑问词 ]; const CHINESE_CHAR_PATTERN = /[\u4e00-\u9fa5]/; function isUyghurText(text, threshold = 0.4) { if (!text || text.trim().length === 0) { return false; } const cleanText = text.replace(/[\s\p{P}]/gu, ""); if (cleanText.length === 0) { return false; } let uyghurCharCount = 0; for (const char of cleanText) { if (UYGHUR_CHARS.includes(char)) { uyghurCharCount++; } } const uyghurCharRatio = uyghurCharCount / cleanText.length; const containsCommonWords = UYGHUR_COMMON_WORDS.some((word) => text.includes(word)); if (uyghurCharRatio >= threshold) { return true; } else if (uyghurCharRatio >= 0.3 && containsCommonWords) { return true; } return false; } function extractUyghurText(text) { if (!text || text.trim().length === 0) { return []; } const uyghurPattern = new RegExp(`[${UYGHUR_CHARS.join("")}]+`, "g"); const matches = text.match(uyghurPattern); return matches ? matches.filter((match) => match.length > 1) : []; } function compareUyghurAndChineseRatio(text, threshold = 1.5) { if (!text || text.trim().length === 0) { return { uyghurCharCount: 0, chineseCharCount: 0, ratio: 0, isUyghurDominant: false, dominantScript: "Balanced" }; } const cleanText = text.replace(/[\s\p{P}]/gu, ""); if (cleanText.length === 0) { return { uyghurCharCount: 0, chineseCharCount: 0, ratio: 0, isUyghurDominant: false, dominantScript: "Balanced" }; } let uyghurCharCount = 0; let chineseCharCount = 0; for (const char of cleanText) { if (UYGHUR_CHARS.includes(char)) { uyghurCharCount++; } else if (CHINESE_CHAR_PATTERN.test(char)) { chineseCharCount++; } } const ratio = chineseCharCount > 0 ? uyghurCharCount / chineseCharCount : uyghurCharCount > 0 ? Infinity : 0; let dominantScript; const isUyghurDominant2 = ratio >= threshold; if (ratio >= threshold) { dominantScript = "Uyghur"; } else if (ratio <= 1 / threshold) { dominantScript = "Chinese"; } else { dominantScript = "Balanced"; } return { uyghurCharCount, chineseCharCount, ratio, isUyghurDominant: isUyghurDominant2, dominantScript }; } function isUyghurDominant(text, ratio = 1.5) { const result = compareUyghurAndChineseRatio(text, ratio); return result.isUyghurDominant; } export { compareUyghurAndChineseRatio, extractUyghurText, isUyghurDominant, isUyghurText };