`
yucang52555
  • 浏览: 68060 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ansj_seg源码分析之用户自定义词性覆盖系统原有词性

阅读更多
    今天遇到一个需求,需要用用户自定义词库的词性标注,替换系统原有词库的词性标注。也就是玉壶自定义词性覆盖系统原有词性。
    废话不多说,直接上代码:
package org.ansj.demo;

import java.io.IOException;
import java.util.List;

import org.ansj.domain.Term;
import org.ansj.recognition.NatureRecognition;
import org.ansj.splitWord.analysis.ToAnalysis;

/**
 * 词性标注
 * 
 * @author ansj
 * 
 */
public class NatureDemo {
	public static void main(String[] args) throws IOException {
		List<Term> terms = ToAnalysis.parse("Ansj中文分词是一个真正的ict的实现.并且加入了自己的一些数据结构和算法的分词.实现了高效率和高准确率的完美结合!");
		new NatureRecognition(terms).recognition();
		System.out.println(terms);
	}
}

    1:List<Term> terms = ToAnalysis.parse("Ansj中文分词是一个真正的ict的实现.并且加入了自己的一些数据结构和算法的分词.实现了高效率和高准确率的完美结合!");用于对字符串进行分词,获得词元列表。
    2:new NatureRecognition(terms).recognition();标注词性。

在标注词性的方法里面修改如下:
将原有方法:
/**
	 * 传入一组。词对词语进行。词性标注
	 * @param words
	 * @param offe
	 * @return
	 */
	public static List<Term> recognition(List<String> words, int offe) {
		List<Term> terms = new ArrayList<Term>(words.size());
		int tempOffe = 0;
		String[] params = null;
		for (String word : words) {
			// 获得词性 , 先从系统辞典。在从用户自定义辞典
			AnsjItem ansjItem = DATDictionary.getItem(word);
			TermNatures tn = null;
			if (ansjItem.termNatures != TermNatures.NULL) {
				tn = ansjItem.termNatures;
			} else if ((params = UserDefineLibrary.getParams(word)) != null) {
				tn = new TermNatures(new TermNature(params[0], 1));
			} else if(WordAlert.isEnglish(word)){
				tn = TermNatures.EN ;
			} else if(WordAlert.isNumber(word)){
				tn = TermNatures.M ;
			} else{
				tn = TermNatures.NULL ;
			}
			terms.add(new Term(word, offe + tempOffe, tn));
			tempOffe += word.length();
		}
		new NatureRecognition(terms).recognition();
		return terms;
	}

替换为:
/**
	 * 传入一组。词对词语进行。词性标注
	 * @param words
	 * @param offe
	 * @return
	 */
	public static List<Term> recognition(List<String> words, int offe) {
		List<Term> terms = new ArrayList<Term>(words.size());
		int tempOffe = 0;
		String[] params = null;
		for (String word : words) {
			// 获得词性 , 先从系统辞典。在从用户自定义辞典
			AnsjItem ansjItem = DATDictionary.getItem(word);
			TermNatures tn = null;
			if ((params = UserDefineLibrary.getParams(word)) != null) {
				tn = new TermNatures(new TermNature(params[0], 1));
			} else if (ansjItem.termNatures != TermNatures.NULL) {
				tn = ansjItem.termNatures;
			} else if(WordAlert.isEnglish(word)){
				tn = TermNatures.EN ;
			} else if(WordAlert.isNumber(word)){
				tn = TermNatures.M ;
			} else{
				tn = TermNatures.NULL ;
			}
			terms.add(new Term(word, offe + tempOffe, tn));
			tempOffe += word.length();
		}
		new NatureRecognition(terms).recognition();
		return terms;
	}

在用户自定义词库中添加一条记录:
数据结构	userDefine	521

就可以看到分词结果变化如下:
由原来的:
[Ansj/en, 中文/nz, 分词/n, 是/v, 一个/m, 真正/d, 的/uj, ict/en, 的/uj, 实现/v, ./m, 并且/c, 加入/v, 了/ul, 自己/r, 的/uj, 一些/m, 数据结构/gi, 和/c, 算法/n, 的/uj, 分词/n, ./m, 实现/v, 了/ul, 高效率/nz, 和/c, 高/a, 准确率/n, 的/uj, 完美/a, 结合/v, !]

变为:
[Ansj/en, 中文/nz, 分词/n, 是/v, 一个/m, 真正/d, 的/uj, ict/en, 的/uj, 实现/v, ./m, 并且/c, 加入/v, 了/ul, 自己/r, 的/uj, 一些/m, 数据结构/userDefine, 和/c, 算法/n, 的/uj, 分词/n, ./m, 实现/v, 了/ul, 高效率/nz, 和/c, 高/a, 准确率/n, 的/uj, 完美/a, 结合/v, !]


可以看到“数据结构”的此行已经变成了我们自己定义的词性。

程序猿行业技术生活交流群:181287753(指尖天下),欢迎大伙加入交流学习。
0
0
分享到:
评论
5 楼 yucang52555 2015-01-23  
yeyefengiswo 写道
我手动添加了词库且有与default.dic重合的部分,但是还是获取不到自定义的词性,获取的仍然是原始词性

这是因为源码里面的词性匹配顺序,默认先匹配系统词库中的词性。改一下源代码的加载顺序就好了。
4 楼 yeyefengiswo 2015-01-21  
大神,你出现下了,其他的词也可以吗?比如"教授"一词
3 楼 yeyefengiswo 2015-01-20  
我手动添加了词库且有与default.dic重合的部分,但是还是获取不到自定义的词性,获取的仍然是原始词性
2 楼 yucang52555 2014-12-23  
qindongliang1922 写道
词性,是自己定义的词库,ansj原本不带这种功能么

ansj自带有词性标注,但是ansj默认先识别核心词库,如果出现用户自定义词库与核心词库冲突,则以核心词库为准,此文是针对这一需求修改。
1 楼 qindongliang1922 2014-12-12  
词性,是自己定义的词库,ansj原本不带这种功能么

相关推荐

Global site tag (gtag.js) - Google Analytics