Improve AI

This commit is contained in:
syuilo
2018-09-02 23:40:03 +09:00
parent 9872ef158b
commit 53a07d0d06
6 changed files with 25 additions and 20 deletions

View File

@ -1,10 +1,10 @@
import { hiraganaToKatagana, hankakuToZenkaku } from './japanese';
import { katakanaToHiragana, hankakuToZenkaku } from './japanese';
export default function(text: string, words: string[]): boolean {
if (text == null) return false;
text = hankakuToZenkaku(hiraganaToKatagana(text));
words = words.map(word => hiraganaToKatagana(word));
text = katakanaToHiragana(hankakuToZenkaku(text));
words = words.map(word => katakanaToHiragana(word));
return words.some(word => text.includes(word));
}

View File

@ -1,6 +1,12 @@
// Utilities for Japanese
const kanaMap: string[][] = [
['ガ', 'ガ'], ['ギ', 'ギ'], ['グ', 'グ'], ['ゲ', 'ゲ'], ['ゴ', 'ゴ'],
['ザ', 'ザ'], ['ジ', 'ジ'], ['ズ', 'ズ'], ['ゼ', 'ゼ'], ['ゾ', 'ゾ'],
['ダ', 'ダ'], ['ヂ', 'ヂ'], ['ヅ', 'ヅ'], ['デ', 'デ'], ['ド', 'ド'],
['バ', 'バ'], ['ビ', 'ビ'], ['ブ', 'ブ'], ['ベ', 'ベ'], ['ボ', 'ボ'],
['パ', 'パ'], ['ピ', 'ピ'], ['プ', 'プ'], ['ペ', 'ペ'], ['ポ', 'ポ'],
['ヴ', 'ヴ'], ['ヷ', 'ヷ'], ['ヺ', 'ヺ'],
['ア', 'ア'], ['イ', 'イ'], ['ウ', 'ウ'], ['エ', 'エ'], ['オ', 'オ'],
['カ', 'カ'], ['キ', 'キ'], ['ク', 'ク'], ['ケ', 'ケ'], ['コ', 'コ'],
['サ', 'サ'], ['シ', 'シ'], ['ス', 'ス'], ['セ', 'セ'], ['ソ', 'ソ'],
@ -11,12 +17,6 @@ const kanaMap: string[][] = [
['ヤ', 'ヤ'], ['ユ', 'ユ'], ['ヨ', 'ヨ'],
['ラ', 'ラ'], ['リ', 'リ'], ['ル', 'ル'], ['レ', 'レ'], ['ロ', 'ロ'],
['ワ', 'ワ'], ['ヲ', 'ヲ'], ['ン', 'ン'],
['ガ', 'ガ'], ['ギ', 'ギ'], ['グ', 'グ'], ['ゲ', 'ゲ'], ['ゴ', 'ゴ'],
['ザ', 'ザ'], ['ジ', 'ジ'], ['ズ', 'ズ'], ['ゼ', 'ゼ'], ['ゾ', 'ゾ'],
['ダ', 'ダ'], ['ヂ', 'ヂ'], ['ヅ', 'ヅ'], ['デ', 'デ'], ['ド', 'ド'],
['バ', 'バ'], ['ビ', 'ビ'], ['ブ', 'ブ'], ['ベ', 'ベ'], ['ボ', 'ボ'],
['パ', 'パ'], ['ピ', 'ピ'], ['プ', 'プ'], ['ペ', 'ペ'], ['ポ', 'ポ'],
['ヴ', 'ヴ'], ['ヷ', 'ヷ'], ['ヺ', 'ヺ'],
['ァ', 'ァ'], ['ィ', 'ィ'], ['ゥ', 'ゥ'], ['ェ', 'ェ'], ['ォ', 'ォ'],
['ッ', 'ッ'], ['ャ', 'ャ'], ['ュ', 'ュ'], ['ョ', 'ョ'],
['ー', 'ー']

View File

@ -1,10 +1,10 @@
import { hiraganaToKatagana, hankakuToZenkaku } from './japanese';
import { hankakuToZenkaku, katakanaToHiragana } from './japanese';
export default function(text: string, words: string[]): boolean {
export default function(text: string, words: (string | RegExp)[]): boolean {
if (text == null) return false;
text = hankakuToZenkaku(hiraganaToKatagana(text));
words = words.map(word => hiraganaToKatagana(word));
text = katakanaToHiragana(hankakuToZenkaku(text));
words = words.map(word => typeof word == 'string' ? katakanaToHiragana(word) : word);
return words.some(word => {
/**
@ -19,16 +19,20 @@ export default function(text: string, words: string[]): boolean {
// ただそのままだと「セーラー」などの本来「ー」が含まれているワードも「ー」が除去され
// 「セーラ」になり、「セーラー」を期待している場合はマッチしなくなり期待する動作にならなくなるので、
// 期待するワードの末尾にもともと「ー」が含まれている場合は(対象のテキストの「ー」をすべて除去した後に)「ー」を付けてあげる
.replace(/ー+$/, '') + (word[word.length - 1] == 'ー' ? 'ー' : '')
.replace(/+$/, '')
.replace(/ー+$/, '') + ((typeof word == 'string' && word[word.length - 1] == 'ー') ? 'ー' : '')
.replace(/+$/, '')
.replace(/。$/, '')
.replace(/デス$/, '')
.replace(/です$/, '')
.replace(/^藍/, '')
.replace(/^チャン/, '')
.replace(/^ちゃん/, '')
.replace(/、+$/, '');
}
return (text == word) || (cleanup(text) == word);
if (typeof word == 'string') {
return (text == word) || (cleanup(text) == word);
} else {
return (word.test(text)) || (word.test(cleanup(text)));
}
});
}