在Python中调用Java扩展包:HanLP

转自:https://www.biaodianfu.com/python-calls-hanlp-jar.html

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from jpype import *
 
 
startJVM(getDefaultJVMPath(), "-Djava.class.path=G:\TextAnalysis\libs\hanlp-portable-1.3.1.jar;G:\TextAnalysis\libs")
HanLP = JClass('com.hankcs.hanlp.HanLP')
 
my_words = u'HanLP是一个致力于向生产环境普及NLP技术的开源Java工具包'
 
# 标准分词
print(HanLP.segment(my_words).toString())
 
#索引分词
IndexTokenizer = JClass('com.hankcs.hanlp.tokenizer.IndexTokenizer')
print(IndexTokenizer.segment(my_words).toString())
 
# 关键词提取
document = u"JPype是一个能够让 Python 代码方便地调用 Java 代码的工具," \
           u"JPype并没有像IKVM(一款可以在.NET环境中运行JAVA代码的工具)那样实现自己的JVM," \
           u"而是以pipe方式调用原生JVM。如果要使用JPype就需要先安装JDK。"
print(HanLP.extractKeyword(document, 3).toString())
 
# 自动摘要
print(HanLP.extractSummary(document, 2).toString())
 
shutdownJVM()

 

posted on 2018-10-30 22:22  ziyi_ang  阅读(403)  评论(0)    收藏  举报

导航