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(指尖天下),欢迎大伙加入交流学习。

猜你喜欢

转载自yucang52555.iteye.com/blog/2165879