1 回答

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
我曾經(jīng)不得不構(gòu)建一個(gè)文本處理器來解析多種語言,包括非常隨意的語言和非常正式的語言。需要確定的一件事是某些單詞是否相關(guān)(例如標(biāo)題中的名詞與一系列事物相關(guān) - 有時(shí)用復(fù)數(shù)形式標(biāo)記。)
IIRC,我們支持的所有語言中 70-90% 的單數(shù)和復(fù)數(shù)單詞形式的“編輯距離”小于 3 或 4。(最終添加了多個(gè)詞典來提高準(zhǔn)確性,因?yàn)椤熬嚯x”本身就會(huì)產(chǎn)生許多誤報(bào)。)另一個(gè)有趣的發(fā)現(xiàn)是,單詞越長,距離等于或小于 3 的距離就越有可能意味著意義上的關(guān)系。
這是我們使用的庫的示例:
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.
添加回答
舉報(bào)