Files
ChatLab/electron/main/nlp/stopwords.ts
T

807 lines
9.8 KiB
TypeScript

import type { SupportedLocale } from './types'
/**
* 停用词表
* 用于过滤无意义的高频词
*/
/** 中文停用词 */
export const CHINESE_STOPWORDS = new Set([
// 代词
'我',
'你',
'他',
'她',
'它',
'我们',
'你们',
'他们',
'她们',
'它们',
'自己',
'别人',
'大家',
'谁',
'什么',
'哪',
'哪里',
'哪儿',
'这',
'那',
'这个',
'那个',
'这些',
'那些',
'这里',
'那里',
'这儿',
'那儿',
'这样',
'那样',
// 助词
'的',
'地',
'得',
'了',
'着',
'过',
'吗',
'呢',
'吧',
'啊',
'呀',
'哇',
'哦',
'嗯',
'噢',
'喔',
'呃',
'唉',
'哎',
'嘛',
// 介词
'在',
'从',
'到',
'向',
'往',
'把',
'被',
'给',
'跟',
'和',
'与',
'对',
'比',
'为',
'因',
'由',
'以',
'按',
'用',
'让',
// 连词
'和',
'与',
'或',
'或者',
'而',
'并',
'并且',
'但',
'但是',
'可是',
'然而',
'不过',
'只是',
'如果',
'要是',
'假如',
'虽然',
'尽管',
'即使',
'所以',
'因此',
'于是',
'那么',
'因为',
'由于',
'既然',
'为了',
'以便',
// 副词
'不',
'没',
'没有',
'很',
'太',
'最',
'更',
'也',
'都',
'就',
'才',
'又',
'再',
'还',
'却',
'只',
'只是',
'已',
'已经',
'曾',
'曾经',
'正',
'正在',
'将',
'将要',
'会',
'能',
'可以',
'可能',
'应该',
'必须',
'一定',
'大概',
'也许',
'或许',
'其实',
'确实',
'真的',
'当然',
'一直',
'总是',
'经常',
'常常',
'往往',
'偶尔',
'几乎',
'差不多',
'简直',
'反正',
'终于',
// 量词
'个',
'只',
'条',
'件',
'位',
'种',
'些',
'点',
'下',
'次',
// 数词
'一',
'二',
'三',
'四',
'五',
'六',
'七',
'八',
'九',
'十',
'百',
'千',
'万',
'亿',
'两',
'几',
'多',
'少',
'第',
'每',
// 动词(常见无实意动词)
'是',
'有',
'在',
'做',
'去',
'来',
'说',
'看',
'想',
'要',
'能',
'会',
'让',
'给',
'叫',
'用',
'打',
'把',
'被',
'到',
// 其他常见词
'上',
'下',
'前',
'后',
'里',
'外',
'中',
'内',
'左',
'右',
'东',
'南',
'西',
'北',
'时',
'时候',
'现在',
'今天',
'明天',
'昨天',
'年',
'月',
'日',
'号',
'点',
'分',
'秒',
'周',
'星期',
// 网络聊天常见无意义词
'好',
'好的',
'行',
'可以',
'嗯嗯',
'哈',
'呵',
'额',
'恩',
'昂',
'ok',
'OK',
'好吧',
'知道',
'知道了',
'谢谢',
'感谢',
'抱歉',
'不好意思',
// 语气词和程度词(虽然词性是名词/动词,但在聊天中无实际意义)
'感觉',
'有点',
'可能',
'应该',
'好像',
'觉得',
'认为',
'看看',
'看到',
'说',
'问',
'找',
'弄',
'搞',
'搞定',
'整',
'干',
'做',
'来',
'去',
'有',
'没有',
'没',
'是不是',
'有没有',
'能不能',
'会不会',
'要不要',
'怎样',
'如何',
'为何',
'为什么',
'怎么',
'怎么样',
'怎么办',
'东西',
'事情',
'事',
'问题',
'时候',
'地方',
'情况',
'样子',
'意思',
'一下',
'一点',
'一些',
'一样',
'一起',
'一直',
'一般',
'一定',
'差不多',
])
/** 英文停用词 */
export const ENGLISH_STOPWORDS = new Set([
// Articles
'a',
'an',
'the',
// Pronouns
'i',
'me',
'my',
'myself',
'we',
'our',
'ours',
'ourselves',
'you',
'your',
'yours',
'yourself',
'yourselves',
'he',
'him',
'his',
'himself',
'she',
'her',
'hers',
'herself',
'it',
'its',
'itself',
'they',
'them',
'their',
'theirs',
'themselves',
'what',
'which',
'who',
'whom',
'this',
'that',
'these',
'those',
// Prepositions
'in',
'on',
'at',
'by',
'for',
'with',
'about',
'against',
'between',
'into',
'through',
'during',
'before',
'after',
'above',
'below',
'to',
'from',
'up',
'down',
'out',
'off',
'over',
'under',
'again',
'further',
// Conjunctions
'and',
'but',
'or',
'nor',
'so',
'yet',
'both',
'either',
'neither',
'not',
'only',
'own',
'same',
'than',
'too',
'very',
'just',
// Be verbs
'am',
'is',
'are',
'was',
'were',
'be',
'been',
'being',
// Have verbs
'have',
'has',
'had',
'having',
// Do verbs
'do',
'does',
'did',
'doing',
// Modal verbs
'will',
'would',
'shall',
'should',
'can',
'could',
'may',
'might',
'must',
// Other common words
'if',
'then',
'else',
'when',
'where',
'why',
'how',
'all',
'each',
'every',
'both',
'few',
'more',
'most',
'other',
'some',
'such',
'no',
'any',
'now',
'here',
'there',
'of',
'as',
// Contractions (without apostrophe)
'dont',
'doesnt',
'didnt',
'wont',
'wouldnt',
'cant',
'couldnt',
'shouldnt',
'isnt',
'arent',
'wasnt',
'werent',
'havent',
'hasnt',
'hadnt',
// Chat common words
'ok',
'okay',
'yes',
'no',
'yeah',
'yep',
'nope',
'sure',
'thanks',
'thank',
'please',
'sorry',
'hi',
'hello',
'hey',
'bye',
'goodbye',
'well',
'like',
'know',
'think',
'want',
'need',
'get',
'got',
'go',
'going',
'come',
'coming',
'make',
'made',
'take',
'took',
'see',
'saw',
'look',
'looking',
'say',
'said',
'tell',
'told',
'ask',
'asked',
'let',
'put',
'keep',
'give',
'gave',
'find',
'found',
'try',
'tried',
// Time words
'today',
'tomorrow',
'yesterday',
'now',
'then',
'always',
'never',
'sometimes',
'often',
'usually',
'still',
'already',
'soon',
'later',
])
/** 日语停用词 */
export const JAPANESE_STOPWORDS = new Set([
// 助詞
'の',
'に',
'は',
'を',
'た',
'が',
'で',
'て',
'と',
'し',
'れ',
'さ',
'ある',
'いる',
'も',
'する',
'から',
'な',
'こと',
'として',
'い',
'や',
'れる',
'など',
'なっ',
'ない',
'この',
'ため',
'その',
'あっ',
'よう',
'また',
'もの',
'という',
'あり',
'まで',
'られ',
'なる',
'へ',
'か',
'だ',
'これ',
'によって',
'により',
'おり',
'より',
'による',
'ず',
'なり',
'られる',
'において',
'ば',
'なかっ',
'なく',
'しかし',
'について',
'せ',
'だっ',
'その後',
'できる',
'それ',
'う',
'ので',
'なお',
'のみ',
'でき',
'き',
'つ',
'における',
'および',
'いう',
'さらに',
'でも',
'ら',
'たり',
'その他',
'に関する',
'たち',
'ます',
'ん',
'なら',
'に対して',
// 代名詞
'私',
'僕',
'俺',
'自分',
'あなた',
'彼',
'彼女',
'それ',
'これ',
'あれ',
'ここ',
'そこ',
'あそこ',
'どこ',
'みんな',
'皆',
// 接続詞
'そして',
'しかし',
'でも',
'だから',
'それで',
'だけど',
'けど',
'ところで',
'さて',
'つまり',
'すなわち',
'ただし',
'もし',
'また',
// 副詞
'とても',
'すごく',
'もう',
'まだ',
'よく',
'ちょっと',
'ちょと',
'もっと',
'やっぱり',
'やはり',
'たぶん',
'きっと',
'ぜんぜん',
'ほんとに',
'ほんと',
'かなり',
'だいたい',
'ほとんど',
'まあ',
'なんか',
'なんとなく',
// 感動詞・フィラー
'あ',
'ああ',
'えー',
'うん',
'えっ',
'おお',
'へー',
'ふーん',
'はい',
'いいえ',
'うーん',
'まあ',
'ねえ',
'ほら',
'あのね',
'えっと',
'その',
// 動詞(高頻度)
'いる',
'ある',
'する',
'なる',
'できる',
'いく',
'くる',
'みる',
'おもう',
'いう',
'やる',
'くれる',
'もらう',
'あげる',
'しまう',
'おく',
// 形容詞(高頻度)
'いい',
'ない',
'よい',
'すごい',
'おおきい',
'ちいさい',
// 助動詞
'です',
'ます',
'でした',
'ました',
'ません',
'だった',
'でしょう',
// チャットでの高頻語
'www',
'ww',
'lol',
'ok',
'おけ',
'りょ',
'おつ',
'わら',
'笑',
'それな',
'たしかに',
'マジ',
'まじ',
'ガチ',
'がち',
])
/**
* 获取停用词集合
* @param locale 语言
* @returns 停用词集合
*/
export function getStopwords(locale: string): Set<string> {
const normalizedLocale = normalizeStopwordLocale(locale)
if (normalizedLocale.startsWith('zh')) {
return CHINESE_STOPWORDS
}
if (normalizedLocale === 'ja-JP') {
return JAPANESE_STOPWORDS
}
return ENGLISH_STOPWORDS
}
/**
* 判断是否为停用词
* @param word 词语
* @param locale 语言
* @returns 是否为停用词
*/
export function isStopword(word: string, locale: string): boolean {
const normalizedLocale = normalizeStopwordLocale(locale)
const stopwords = getStopwords(normalizedLocale)
const normalizedWord = normalizedLocale === 'en-US' ? word.toLowerCase() : word
return stopwords.has(normalizedWord)
}
/**
* 规范化停用词处理使用的 locale。
* 这里额外兜底一次,避免上游误传数字或其他异常值时直接导致运行时崩溃。
*/
function normalizeStopwordLocale(locale: string): SupportedLocale {
if (typeof locale !== 'string') {
return 'en-US'
}
if (locale.startsWith('zh')) {
return 'zh-CN'
}
if (locale === 'ja-JP') {
return 'ja-JP'
}
return 'en-US'
}