基于HanLP分词菜鸟使用教程
官网
http://www.hanlp.com/
依赖
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.7</version>
</dependency>
分词包下载
https://github.com/hankcs/HanLP/tree/1.x
下载后整合项目 代码可以自动识别到文件
省市县 地理位置分词拆解 提取 地名ns
package ustils;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import java.io.IOException;
import java.util.List;
public class HanLpUtils {
public static void main(String[] args) throws IOException {
// System.out.println(HanLP.segment("你好,欢迎使用HanLP汉语处理包!"));
String word = "2020年广州市天河区天河南二路2号输电二所无人机巡视作业招标公告";
//[2020/m, 年/q, 广州市/ns, 天河区/ns, 天/q, 河南/ns, 二/m, 路/n, 2/m, 号/q, 输电/vn, 二/m, 所/u, 无人机/n, 巡视/v, 作业/vn, 招标/v, 公告/n]
List<Term> termList= HanLP.segment( word);
for (Term term : termList) {
final Nature nature = term.nature;
System.out.println("nature=====>"+nature);
final boolean ns = term.nature.startsWith("ns");
if(ns){
System.out.println("找到你想要的词------> "+term.word);
}else {
System.out.println("现在不需要以后有需要------> "+term.word);
}
}
System.out.println(termList.get(0));
System.out.println(termList.toString());
// PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer("data/model/perceptron/pku199801/cws.bin",
// HanLP.Config.PerceptronPOSModelPath,
// HanLP.Config.PerceptronNERModelPath);
// System.out.println(analyzer.analyze("上海华安工业(集团)公司董事长谭旭光和秘书胡花蕊来到美国纽约现代艺术博物馆参观"));
// String[] testCase = new String[]{
// "武胜县新学乡政府大楼门前锣鼓喧天",
// "蓝翔给宁夏固原市彭阳县红河镇黑牛沟村捐赠了挖掘机",
// };
// Segment segment = HanLP.newSegment().enablePlaceRecognize(true);
// for (String sentence : testCase)
// {
// List<Term> termList = segment.seg(sentence);
// System.out.println(termList);
// }
}
}
运行结果
nature=====>m
现在不需要以后有需要------> 2020
nature=====>q
现在不需要以后有需要------> 年
nature=====>ns
找到你想要的词------> 广州市
nature=====>ns
找到你想要的词------> 天河区
nature=====>q
现在不需要以后有需要------> 天
nature=====>ns
找到你想要的词------> 河南
nature=====>m
现在不需要以后有需要------> 二
nature=====>n
现在不需要以后有需要------> 路
nature=====>m
现在不需要以后有需要------> 2
nature=====>q
现在不需要以后有需要------> 号
nature=====>vn
现在不需要以后有需要------> 输电
nature=====>m
现在不需要以后有需要------> 二
nature=====>u
现在不需要以后有需要------> 所
nature=====>n
现在不需要以后有需要------> 无人机
nature=====>v
现在不需要以后有需要------> 巡视
nature=====>vn
现在不需要以后有需要------> 作业
nature=====>v
现在不需要以后有需要------> 招标
nature=====>n
现在不需要以后有需要------> 公告
2020/m
[2020/m, 年/q, 广州市/ns, 天河区/ns, 天/q, 河南/ns, 二/m, 路/n, 2/m, 号/q, 输电/vn, 二/m, 所/u, 无人机/n, 巡视/v, 作业/vn, 招标/v, 公告/n]