• python安装 hanlp +使用 坑坑坑。。。填填填。。。


    刚开始用都不知道要怎么安装,包括什么文件。百度了下发现正常安装就可以用,不用下多余的东西,但是但是但是但是但是但是但是但是

    用pycharm安装不行,pip安装不行,也是见鬼了。

    解决方法:

    1、手动安装JPype1

    pip install D:softJPype1-0.6.2-cp36-cp36m-win_amd64.whl 

    这个安装包可以在这里下  https://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype

    注意如果出现 JPype1-0.6.3-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.是版本的问题,找到自己对应的版本
    可看如下搭配更换版本: cp27 → CPython 2.7 cp34 → CPython 3.4 cp35 → CPython 3.5 cp36 → CPython 3.6 and win32 → 32-bit version of ms-windows win_amd64 → 64-bit version of ms-windows

    2、安装pyhanlp

     pip install pyhanlp

    3、找到pyhanlp安装目录

    进入到如   。。。。pythonLibsite-packagespyhanlpstatic目录

    找到hanlp.properties文件修改root路径把上面青色路径加到root中(绝对路径)

    4、测试是否可以用(cmd中输入hanlp segment 如果出现如下输出就证明可以用了)

    c:>hanlp segment
    欢迎新老师
    欢迎/v新/a老/a师n

    ----------------------------------------------------------------------------------------------------如果可以用了就不用再看下面的了-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

     5、按照上面步骤不可用,接着这么做

    https://pan.baidu.com/s/1UGHNatDivCuSaqiPvb29GA

    用上面的网址下载解压找到里面的hanlp-1.7.2.jar和data文件夹

    覆盖到。。。。pythonLibsite-packagespyhanlpstatic路径中去

    6.检查是否可用

    终于可以用了  :)

    7、以上是在cmd中测试的可以运行,但是到了pycharm中就不行了

    调试代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from jpype import *
     
     
    startJVM(getDefaultJVMPath(), "-Djava.class.path=G:TextAnalysislibshanlp-portable-1.3.1.jar;G:TextAnalysislibs")
    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()

    报错,还是一串乱码。。。。。。。。。。。。

    然后到cmd中运行报错的中文是:::

    十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.HanLP$Config <clinit>
    严重: 没有找到hanlp.properties,可能会导致找不到data
    ========Tips========
    请将hanlp.properties放在下列目录:
    Web项目则请放到下列目录:
    Webapp/WEB-INF/lib
    Webapp/WEB-INF/classes
    Appserver/lib
    JRE/lib
    并且编辑root=PARENT/path/to/your/data
    现在HanLP将尝试从D:pythonworkspace2sourcea读取data……
    十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.corpus.io.IOUtil readBytes
    警告: 读取data/dictionary/CoreNatureDictionary.txt.bin时发生异常java.io.FileNotFoundException: datadictionaryCoreNatureDictionary.txt.bin (系统找不到指定的路径。)
    十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.dictionary.CoreDictionary load
    警告: 核心词典data/dictionary/CoreNatureDictionary.txt不存在!java.io.FileNotFoundException: datadictionaryCoreNatureDictionary.txt (系统找不到指定的路径。)
    Traceback (most recent call last):
    File "t2.py", line 14, in <module>
    print(HanLP.segment(my_words).toString())
    jpype._jexception.ExceptionInInitializerErrorPyRaisable: java.lang.ExceptionInInitializerError
    
     

    解决:

    1、配置hanlp.properties环境变量

    2、把5中下载的data文件夹复制到项目中去

    3、修改hanlp.properties文件,将目录改为 root=。。/你的项目目录/data(这一步我还没用,就已经可以正常运行程序了

     


    参考:

    https://blog.csdn.net/wenwen360360/article/details/80971071 
    https://blog.csdn.net/qq_34333481/article/details/89206083

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

    https://www.cnblogs.com/ziyiang/articles/9885764.html

  • 相关阅读:
    RMAN还原时注意set newname时文件名不要有空格
    注意Vietnamese_CI_AS排序规则下的特殊字符大小敏感问题
    ORA-04028: cannot generate diana for object xxx
    Linux传统Huge Pages与Transparent Huge Pages再次学习总结
    SQL Server 死锁的告警监控
    PlateSpin备份服务器时SQL Server的一些活动信息
    MS SQL xp_instance_regwrite设置注册表疑惑
    AutoAudit研究学习
    The Windows account sa does not exist and cannot be provisioned as a SQL Server system administrator
    Innotop简单介绍
  • 原文地址:https://www.cnblogs.com/51python/p/11031133.html
Copyright © 2020-2023  润新知