org.ansj.recognition.impl.UserDicNatureRecognition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ansj_seg Show documentation
Show all versions of ansj_seg Show documentation
best java chinese word seg !
package org.ansj.recognition.impl;
import org.ansj.domain.Nature;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.library.UserDefineLibrary;
import org.ansj.recognition.Recognition;
import org.nlpcn.commons.lang.tire.domain.Forest;
import org.nlpcn.commons.lang.tire.domain.SmartForest;
/**
* 用户自定义词典的词性优先
*
* @author ansj
*
*/
public class UserDicNatureRecognition implements Recognition {
/**
*
*/
private static final long serialVersionUID = 1L;
private Forest[] forests = new Forest[] { UserDefineLibrary.FOREST };
public UserDicNatureRecognition() {
}
/**
* 传入多本词典,后面的会覆盖前面的结果
*
* @param forests
*/
public UserDicNatureRecognition(Forest... forests) {
this.forests = forests;
}
@Override
public void recognition(Result result) {
for (Term term : result) {
for (int i = forests.length - 1; i > -1; i--) {
String[] params = getParams(forests[i], term.getName());
if (params != null) {
term.setNature(new Nature(params[0]));
break;
}
}
}
}
public static String[] getParams(Forest forest, String word) {
SmartForest temp = forest;
for (int i = 0; i < word.length(); i++) {
temp = temp.get(word.charAt(i));
if (temp == null) {
return null;
}
}
if (temp.getStatus() > 1) {
return temp.getParam();
} else {
return null;
}
}
}