91名师指路-头部
91名师指路

java ikanalyzer分词

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:引入pom

<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>


二:编写测试代码

package com.mszl.product.controller;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class TestIk {

public static void main(String[] args) {
String str="我是中国人";
Map<String, String> map=segMore(str);
System.out.println(map);
}

public static Map<String, String> segMore(String text) {
Map<String, String> map = new HashMap<>();
map.put("智能分词", segText(text, true));
map.put("最大分词", segText(text, false));
return map;
}

private static String segText(String text, boolean useSmart) {
StringBuilder result = new StringBuilder();
IKSegmenter ik = new IKSegmenter(new StringReader(text), useSmart);
try {
Lexeme word = null;
while((word=ik.next())!=null) {
result.append(word.getLexemeText()).append(" ");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return result.toString();
}


}

执行结果:

{最大分词=我 是 中国人 中国 国人 , 智能分词=我 是 中国人 }


参考资料:

https://blog.csdn.net/XnCSD/article/details/81516032

https://www.cnblogs.com/lishanyang/p/6017155.html




2022-05-30 15:32:12     阅读(940)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部