使用Matlab实现英文单词的形近词查找
引言
最近在背考博詞匯,考博詞匯是12000詞左右.
短時間內去記憶這么多單詞是很有挑戰性的.我個人習慣用的方法是:
第三個方法就是用"形近詞"去記憶.
這個方法我感覺是很好用的, 就跟有個朋友給你介紹新朋友一樣.
而且, 因為在背單詞的時候常常很容易將一個單詞與他的形近詞相混淆, 所以, 在背單詞的時候如果能查找形近詞該多好啊.
把一些形近詞放到一起, 便于記憶,也便于辨析.
比如:
complexity complicacy complicate complicity simplicity網上雖然有不少英語學習愛好者和研究教育機構整理的形近詞詞庫,遺憾的是,并沒有一款詞典能夠提供形近詞查詢功能.金山詞霸等詞典軟件雖然可以使用通配符, 但是, 通過通配符去找形近詞需要你自己去擬定通配規則, 很麻煩.
考察了一下,做了個簡單的小程序,這下子,大大輔助我背單詞了.
方法
這個問題,其實就是一個詞形近似度的問題[4].其實,跟spell checking[3]根據詞形相近度自動提供spell suggestion和spell correction是一樣的問題,
用正則表達式也可以實現, 不過, 需要你自己定義n多的變形規則.很麻煩.
在這里,我主要是通過計算兩詞之間的編輯距離(edit distance)[1]來判斷.
在Matlab下做了一個toy程序,用經典的Levenshtein Distance[2]算法(算法細節參考Wikipedia及相關文章)實現了形近詞查找功能. word-to-word匹配用了動態規劃,所以速度很快.
另外,還有Jaro–Winkler distance[5], 還有發音相似度(phonetic distance)[6]都可以考慮使用, 我這里暫時只用了L氏距離.
關于L氏算法的一個直觀的實例:
用法
運行算法之后, 會對word list進行一次遍歷,計算詞典里每個詞跟你要查詢詞之間的edit distance, 最后, 用一個閾值過濾出最相近的單詞.
效果
詞庫用的是四六級詞庫.
edit distance取3.
從四六級詞庫里插simple的形近詞,有4個(算上它自己):
代碼
主函數:
%% 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氏距離計算函數: %% 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
詞典加載函數:
%% 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補充
有很多更全的詞庫文件,在這里[7]可以找到很多.
感謝Jukuu網工程師YNYS提供四六級word list, Thanks.
已經長傳到這里[8], 有興趣的同學可以做測試.
-----------------------------------------------------------------------個人使用,所以懶得改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] 四六級詞匯表, http://download.csdn.net/source/3455828
總結
以上是生活随笔為你收集整理的使用Matlab实现英文单词的形近词查找的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 直播 | 白硕:区块链技术与数据隐私讲座
- 下一篇: Mac启用Root用户