1 回答

TA貢獻1765條經驗 獲得超5個贊
我曾經不得不構建一個文本處理器來解析多種語言,包括非常隨意的語言和非常正式的語言。需要確定的一件事是某些單詞是否相關(例如標題中的名詞與一系列事物相關 - 有時用復數形式標記。)
IIRC,我們支持的所有語言中 70-90% 的單數和復數單詞形式的“編輯距離”小于 3 或 4。(最終添加了多個詞典來提高準確性,因為“距離”本身就會產生許多誤報。)另一個有趣的發現是,單詞越長,距離等于或小于 3 的距離就越有可能意味著意義上的關系。
這是我們使用的庫的示例:
const fastLevenshtein = require('fast-levenshtein');
console.log('Deburred Distances:')
console.log('Score 1:', fastLevenshtein.get('Schlie?f?cher', 'Schlie?fach'));
// -> 3
console.log('Score 2:', fastLevenshtein.get('Blumtach', 'Blumt?cher'));
// -> 3
console.log('Score 3:', fastLevenshtein.get('schlie?f?cher', 'Schliessfaech'));
// -> 7
console.log('Score 4:', fastLevenshtein.get('not-it', 'Schliessfaech'));
// -> 12
console.log('Score 5:', fastLevenshtein.get('not-it', 'Schiesse'));
// -> 8
/**
* Additional strategy for dealing with other various languages:
* "Deburr" the strings to omit diacritics before checking the distance:
*/
const deburr = require('lodash.deburr');
console.log('Deburred Distances:')
console.log('Score 1:', deburr(fastLevenshtein.get('Schlie?f?cher', 'Schlie?fach')));
// -> 3
console.log('Score 2:', deburr(fastLevenshtein.get('Blumtach', 'Blumt?cher')));
// -> 3
console.log('Score 3:', deburr(fastLevenshtein.get('schlie?f?cher', 'Schliessfaech')));
// -> 7
// Same in this case, but helpful in other similar use cases.
添加回答
舉報