使用Matlab实现英文单词的形近词查找
引言
最近在背考博詞匯,考博詞匯是12000詞左右.
短時(shí)間內(nèi)去記憶這么多單詞是很有挑戰(zhàn)性的.我個(gè)人習(xí)慣用的方法是:
第三個(gè)方法就是用"形近詞"去記憶.
這個(gè)方法我感覺是很好用的, 就跟有個(gè)朋友給你介紹新朋友一樣.
而且, 因?yàn)樵诒硢卧~的時(shí)候常常很容易將一個(gè)單詞與他的形近詞相混淆, 所以, 在背單詞的時(shí)候如果能查找形近詞該多好啊.
把一些形近詞放到一起, 便于記憶,也便于辨析.
比如:
complexity complicacy complicate complicity simplicity網(wǎng)上雖然有不少英語學(xué)習(xí)愛好者和研究教育機(jī)構(gòu)整理的形近詞詞庫,遺憾的是,并沒有一款詞典能夠提供形近詞查詢功能.金山詞霸等詞典軟件雖然可以使用通配符, 但是, 通過通配符去找形近詞需要你自己去擬定通配規(guī)則, 很麻煩.
考察了一下,做了個(gè)簡(jiǎn)單的小程序,這下子,大大輔助我背單詞了.
方法
這個(gè)問題,其實(shí)就是一個(gè)詞形近似度的問題[4].其實(shí),跟spell checking[3]根據(jù)詞形相近度自動(dòng)提供spell suggestion和spell correction是一樣的問題,
用正則表達(dá)式也可以實(shí)現(xiàn), 不過, 需要你自己定義n多的變形規(guī)則.很麻煩.
在這里,我主要是通過計(jì)算兩詞之間的編輯距離(edit distance)[1]來判斷.
在Matlab下做了一個(gè)toy程序,用經(jīng)典的Levenshtein Distance[2]算法(算法細(xì)節(jié)參考Wikipedia及相關(guān)文章)實(shí)現(xiàn)了形近詞查找功能. word-to-word匹配用了動(dòng)態(tài)規(guī)劃,所以速度很快.
另外,還有Jaro–Winkler distance[5], 還有發(fā)音相似度(phonetic distance)[6]都可以考慮使用, 我這里暫時(shí)只用了L氏距離.
關(guān)于L氏算法的一個(gè)直觀的實(shí)例:
用法
運(yùn)行算法之后, 會(huì)對(duì)word list進(jìn)行一次遍歷,計(jì)算詞典里每個(gè)詞跟你要查詢?cè)~之間的edit distance, 最后, 用一個(gè)閾值過濾出最相近的單詞.
效果
詞庫用的是四六級(jí)詞庫.
edit distance取3.
從四六級(jí)詞庫里插simple的形近詞,有4個(gè)(算上它自己):
代碼
主函數(shù):
%% this is the main function % Input : % wordToMatch - input word % distThresh - edit distance threshold, usually use 3 % dicPath - file path of the word list file, txt format with every % line of a single word % % Output : % command window output % % Created by visionfans @ 2011.07.20 function findSimilarWords(wordToMatch, distThresh, dicPath )global word;word = wordToMatch;%% check parametersswitch nargincase 0,error('Wrong arguments!');case 1,distThresh = 3;dicPath = '46.txt';end%% load word listwordList = loadWordList(dicPath);%% calculate edit distanceeditDist = cellfun(@calcEditDist,wordList);%% filter the similar wordssimilarWords = wordList(editDist < distThresh);%% display resultsfprintf('There are %d similar words with "%s" : \n', length(similarWords), word);cellfun(@(x)fprintf('\t%s\n', x),similarWords); endL氏距離計(jì)算函數(shù): %% this function is used to calculate the Levenshtein Edit Distance % % S1 and S2 are two words you want to calculate their edit distance % % Created by visionfans @ 2011.07.20 function dist = calcEditDist(s1,s2)?? ?global word;if nargin == 1s2 = word;end%% calculate the edit distance with DPm = length(s1);n = length(s2);if m*n == 0dist = Inf;return;endtable = zeros(m,n);table(:,1) = 0:m-1;table(1,:) = 0:n-1;for i=2:mfor j=2:nif s1(i-1)==s2(j-1)table(i,j) = table(i-1,j-1);elsetable(i,j) = 1 + min([(min(table(i-1,j),table(i,j-1))),table(i-1,j-1)]);endendend%% set resultdist = table(m,n);return; end
詞典加載函數(shù):
%% this function is used to load the dictionary file % The dictionary file is a text file with the format of every line be a % single word. % % You can find a word list file with adequate common words here: %?? ?Kevin's Word List Page - http://wordlist.sourceforge.net/ % % Created by visionfans @ 2011.07.20 function wordList = loadWordList(dictPath)fprintf('Loading word list ...\n');fid = fopen(dictPath);i = 1;tline = fgetl(fid);while ischar(tline)wordList{i,1} = tline;tline = fgetl(fid);i = i+1;endfclose(fid); end補(bǔ)充
有很多更全的詞庫文件,在這里[7]可以找到很多.
感謝Jukuu網(wǎng)工程師YNYS提供四六級(jí)word list, Thanks.
已經(jīng)長(zhǎng)傳到這里[8], 有興趣的同學(xué)可以做測(cè)試.
-----------------------------------------------------------------------個(gè)人使用,所以懶得改C了.
References
[1] Edit distance , http://nlp.stanford.edu/IR-book/html/htmledition/edit-distance-1.html
[2] Levenshtein distance - Wikipedia, the free encyclopedia, http://en.wikipedia.org/wiki/Levenshtein_distance
[3] How to Write a Spelling Corrector, http://norvig.com/spell-correct.html
[4] Approximate string matching - Wikipedia, the free encyclopedia, http://en.wikipedia.org/wiki/Approximate_string_matching
[5] Jaro–Winkler distance - Wikipedia, the free encyclopedia, http://en.wikipedia.org/wiki/Jaro-Winkler_distance
[6] Soundex - Wikipedia, the free encyclopedia, http://en.wikipedia.org/wiki/Soundex
[7] Dictionary & Glossary Links; Downloadable Word Lists, http://www.net-comber.com/wordurls.html
[8] 四六級(jí)詞匯表, http://download.csdn.net/source/3455828
總結(jié)
以上是生活随笔為你收集整理的使用Matlab实现英文单词的形近词查找的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 直播 | 白硕:区块链技术与数据隐私讲座
- 下一篇: Mac启用Root用户