1. 映射数据
按“错误文本 正确文本” 方式填写,中间为Tab键,如:
缸肠 肛肠
人名医院 人民医院
门珍 门诊
闪分泌 内分泌
爱尔眠科 爱尔眼科
笫一 第一
2. 算法
当映射数据量很大时,单纯使用循环、替换的方式不能满足性能要求:
String[] errors = new String[]{"人名医院",...};
String[] corrections = new String[]{"人民医院",...};
String text = "浙江省人名医院贤内科";
for(int i=0;i<errors.length;i++){
text = text.replace(errors[i],corrections[i]);
}
考虑借鉴分词的方法,从文本中把错误词匹配出来,这里要用到trie树,可自行搜索相关文章。
ansj中已经实现有很好用的trie树方法,直接复用:
import org.nlpcn.commons.lang.tire.GetWord;
import org.nlpcn.commons.lang.tire.domain.Forest;
import org.nlpcn.commons.lang.tire.library.Library;
import java.io.*;
public class RuleBasedCorrection {
private Forest forest;
public RuleBasedCorrection(String correctionDic) throws Exception {
File file = new File(correctionDic);
BufferedReader reader = new BufferedReader(new FileReader(file));
forest = Library.makeForest(reader);
}
public String correct(String text){
GetWord udg = forest.getWord(text);
String temp = null;
while((temp = udg.getFrontWords()) != null){
text = text.replace(temp, udg.getParam(0));
}
return text;
}
public static void main(String[] args) throws Exception {
RuleBasedCorrection correction = new RuleBasedCorrection("tmp");
System.out.println(correction.correct("浙江省人名医院余杭区贤内科"));
}
}