How to Write a Spelling Corrector用java 寫拼字檢查器 Java實現 以備查驗

來源:互聯網
上載者:User

標籤:

import java.io.*;
import java.util.*;
import java.util.regex.*;

class Spelling {

private final HashMap<String, Integer> nWords = new HashMap<String, Integer>();

public Spelling(String file) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
Pattern p = Pattern.compile("\\w+");
for(String temp = ""; temp != null; temp = in.readLine()){
Matcher m = p.matcher(temp.toLowerCase());
while(m.find())
nWords.put((temp = m.group()), nWords.containsKey(temp) ? nWords.get(temp) + 1 : 1);
}
in.close();
//System.out.println(nWords.size());
}

private final ArrayList<String> edits(String word) {
ArrayList<String> result = new ArrayList<String>();
for(int i=0; i < word.length(); ++i) result.add(word.substring(0, i) + word.substring(i+1));
for(int i=0; i < word.length()-1; ++i) result.add(word.substring(0, i) + word.substring(i+1, i+2) + word.substring(i, i+1) + word.substring(i+2));
for(int i=0; i < word.length(); ++i) for(char c=‘a‘; c <= ‘z‘; ++c) result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i+1));
for(int i=0; i <= word.length(); ++i) for(char c=‘a‘; c <= ‘z‘; ++c) result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
return result;
}

public final String correct(String word) {
//如果詞袋子裡面含有這個詞直接返回
if(nWords.containsKey(word)) return word;
//沒有這個詞的話,那就認為這個詞拼字錯誤 找到所有的可能的基於這個詞的可能詞彙
ArrayList<String> list = edits(word);
HashMap<Integer, String> candidates = new HashMap<Integer, String>();
//在猜想的詞彙表中如果與字典中的詞重合,那就放進候選列表裡面
for(String s : list) if(nWords.containsKey(s)) candidates.put(nWords.get(s),s);
//如果在候選列表裡面有候選
if(candidates.size() > 0) return candidates.get(Collections.max(candidates.keySet()));
//沒有候選的時候怎麼辦?
for(String s : list)
for(String w : edits(s))
//進行第二次匹配,拿出猜想的可能詞彙,再進行一次猜想, 再不行的話,直接返回原來的word
if(nWords.containsKey(w))
candidates.put(nWords.get(w),w);
return candidates.size() > 0 ? candidates.get(Collections.max(candidates.keySet())) : word;
}

public static void main(String args[]) throws IOException {
if(args.length > 0) System.out.println((new Spelling("big.txt")).correct(args[0]));
}

}

 

 

http://raelcunha.com/spell-correct.php

How to Write a Spelling Corrector用java 寫拼字檢查器 Java實現 以備查驗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.