当前位置: 首页 > 工具软件 > ansj_seg > 使用案例 >

python调用 java 的 ansj_seg 分词工具

阎知
2023-12-01

出现错误:

   jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class org.ansj.splitWord.analysis.ToAnalysis not found。

         python调用 java 的 ansj_seg 分词工具的时候

        开始时候参考的是   python 调用 java 的 ansj_seg 分词工具

                          github链接:python引用jar包,找不到类

        代码为(class那里将冒号:改为分号;就可以了):

# -*- coding:utf-8 -*-
# -*- created by: mo -*-
import jpype
import os


# jvmPath = '/usr/lib/java/jdk1.8.0_161/jre/lib/amd64/server/libjvm.so'
jvmPath = 'D:/software/Java/Java8/jre/bin/server/jvm.dll'
# jvmPath = 'D:/software/Java/Java8/jre/bin/server/libjvm.so'
print (jvmPath)

jars_dir_ansj_seg = 'D:/software/IDEA/tools/maven_soft/org/ansj/ansj_seg/5.1.6'
jars_dir_nlp_lang = 'D:/software/IDEA/tools/maven_soft/org/nlpcn/nlp-lang/1.7.7'
jars_dir_tree_split = 'D:/software/IDEA/tools/maven_soft/org/ansj/tree_split/1.5'
jars = [os.path.join(jars_dir_ansj_seg, 'ansj_seg-5.1.6.jar'), os.path.join(jars_dir_nlp_lang, 'nlp-lang-1.7.7.jar'), os.path.join(jars_dir_tree_split, 'tree_split-1.5.jar')]
jvm_cp = "-Djava.class.path={}".format(';'.join(jars))
jpype.startJVM(jvmPath, "-ea", jvm_cp)
SegModel = jpype.JClass('org.ansj.splitWord.analysis.ToAnalysis')
jd = SegModel()
print(jd.parseStr("怎么这么麻烦"))

jpype.shutdownJVM()

结果:

D:/software/Java/Java8/jre/bin/server/jvm.dll
���� 20, 2018 8:59:15 ���� org.ansj.util.MyStaticValue warn
����: not find library.properties in classpath use it by default !
���� 20, 2018 8:59:15 ���� org.ansj.dic.impl.File2Stream info
��Ϣ: path to stream library/ambiguity.dic
���� 20, 2018 8:59:15 ���� org.ansj.library.AmbiguityLibrary error
����: Init ambiguity library error :org.ansj.exception.LibraryException:  path :library/ambiguity.dic file:D:\workspace\python\django_server\nlp_app\segment\library\ambiguity.dic not found or can not to read, path: library/ambiguity.dic
���� 20, 2018 8:59:15 ���� org.ansj.dic.impl.File2Stream info
��Ϣ: path to stream library/default.dic
���� 20, 2018 8:59:15 ���� org.ansj.library.DicLibrary error
����: Init dic library error :org.ansj.exception.LibraryException:  path :library/default.dic file:D:\workspace\python\django_server\nlp_app\segment\library\default.dic not found or can not to read, path: library/default.dic
���� 20, 2018 8:59:15 ���� org.ansj.library.DATDictionary info
��Ϣ: init core library ok use time : 544
���� 20, 2018 8:59:15 ���� org.ansj.library.NgramLibrary info
��Ϣ: init ngram ok use time :276
怎么/r,这么/r,麻烦/an
JVM activity report     :
    classes loaded       : 26
JVM has been shutdown 

 类似资料: