|
IK在进行分词的时候,如果分词结果中没有全词匹配的词,如果业务需求需要有全词,那么我们就要把全词加入到分词结果中,假如对”中华人民共和国万岁”进行分词,分词结果如下:
如果我们搜索的时候把”中华人民共和国万岁”放入了IK的词库中,就会产生有些数据没有这个分词结果,通过全词去搜索的时候是搜索不到这个数据的,那么就需要把整个词放入分词结果中。
修改AnalyzeContext.class类的outputToResult方法,该方法的作用是推送分词结果到结果集合:
void outputToResult(){
int index = 0;
for( ; index <= this.cursor ;){
if(CharacterUtil.CHAR_USELESS == this.charTypes[index]){
index++;
continue;
}
LexemePath path = this.pathMap.get(index);
if(path != null){
Lexeme l = path.pollFirst();
while(l != null){
this.results.add(l);
index = l.getBegin() + l.getLength();
l = path.pollFirst();
if(l != null){
for(;index < l.getBegin();index++){
this.outputSingleCJK(index);
}
}
}
}else{
this.outputSingleCJK(index);
index++;
}
}
Lexeme singleCharLexeme = new Lexeme(0 , 0 , this.available , Lexeme.TYPE_CNCHAR);
boolean hasMax = true;
for (Lexeme lex : results) {
if (singleCharLexeme.equals(lex)) {
hasMax = false;
}
}
if (hasMax) {
this.results.add(singleCharLexeme);
}
this.pathMap.clear();
}
分词测试:
 |