文本纠错-基于词典匹配的文本纠错

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("浙江省人名医院余杭区贤内科"));
    }
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值