• NLTK的安装与简单测试


    1.NLTK简介

    Natural Language Toolkit,自然语言处理工具包,在NLP领域中,最常使用的一个Python库。NLTK是一个开源的项目,包含:Python模块,数据集和教程,用于NLP的研究和开发。NLTK由Steven Bird和Edward  Loper在宾夕法尼亚大学计算机和信息科学系开发。NLTK包括图形演示和示例数据。其提供的教程解释了工具包支持的语言处理任务背后的基本概念。

    2. NLTK安装

    NIKE的安装比较简单,可以通过系统命令框输入:pip install nltk 安装;也可以在pycharm中点击File->setting->project->+安装,如下图

    3.NIKE的调试

    NIKE安装好后还需要安装相应的数据包,但是通过上面的安装数据包是没有安装成功的,因此需要另外通过操作安装。安装前,我们先测试一下NLTK功能,输入下面代码,对这段话进行分词。

    import nltk
    text = nltk.word_tokenize("PierreVinken , 59 years old , will join as a nonexecutive director on Nov. 29 .")
    print(text)

     出现如下错误:

    ...
        raise LookupError(resource_not_found)
    LookupError: 
    **********************************************************************
      Resource punkt not found.
      Please use the NLTK Downloader to obtain the resource:
    
      >>> import nltk
      >>> nltk.download('punkt')
      
      For more information see: https://www.nltk.org/data.html
    
      Attempted to load tokenizers/punkt/english.pickle
    
      Searched in:
        - 'C:\Users\Administrator/nltk_data'
        - 'C:\Users\Administrator\Desktop\meatwice\venv\nltk_data'
        - 'C:\Users\Administrator\Desktop\meatwice\venv\share\nltk_data'
        - 'C:\Users\Administrator\Desktop\meatwice\venv\lib\nltk_data'
        - 'C:\Users\Administrator\AppData\Roaming\nltk_data'
        - 'C:\nltk_data'
        - 'D:\nltk_data'
        - 'E:\nltk_data'
        - ''

    原因:NLTK需要的数据包punkt未安装。

    解决方法:在命令框中进入python交互模式,或者在pycharm建立.py文件,输入以下代码:

    import nltk
    nltk.download()  

    系统将会自动跳出NLTK数据包的下载界面,然后再根据错误提示选择punkt数据包安装,然后等待安装。

    
      >>> import nltk
      >>> nltk.download('punkt')
     
    
    

     安装成功后,开始调试,输入下面代码

    import nltk
    text=nltk.word_tokenize("brad pitt, 54 years old , will join as a nonexecutive actor on Nov. 29 .")
    print(text)

    运行结果:

     总结:通过测试,实现了对“brad pitt, 54 years old , will join as a nonexecutive actor on Nov. 29 .”的分词,成功的实现对NLTK的简单使用。

  • 相关阅读:
    Mac下eclipse安装SVN插件
    python中函数参数*args和**kw的区别
    Linux下安装JDK1.8
    SpringBoot Profiles特性
    一张图讲解单机FastDFS图片服务器安装步骤(修订版)
    一张图讲解最少机器搭建FastDFS高可用分布式集群安装说明
    Zookeeper作为配置中心使用说明
    一张图秒懂微服务网络架构
    TestNG的静态方法mock的步骤
    Java中indexOf的用法
  • 原文地址:https://www.cnblogs.com/maxxu11/p/12422334.html
Copyright © 2020-2023  润新知