参考书籍《Python自然语言处理》,书籍中的版本是Python2和NLTK2,我使用的版本是Python3和NLTK3

实验环境Windows8.1,已有Python3.4,并安装了NumPy, Matplotlib,参考:http://blog.csdn.net/monkey131499/article/details/50734183

安装NLTK3,Natural Language Toolkit,自然语言工具包,地址:http://www.nltk.org/

安装命令:pip install nltk

代码:

SaintKings-Mac-mini:~ saintking$ sudo pip install nltk

Password:

The directory '/Users/saintking/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/Users/saintking/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Collecting nltk

Requirement already satisfied: six in /Library/Python/2.7/site-packages (from nltk)

Installing collected packages: nltk

Successfully installed nltk-3.2.5

SaintKings-Mac-mini:~ saintking$

安装完成后测试:import nltk

SaintKings-Mac-mini:~ saintking$ python

Python 2.7.10 (default, Jul 30 2016, 18:31:42)

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import nltk

>>> nltk.download()

showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml

没有报错即表明安装成功。

NLTK包含大量的软件、数据和文档,可以进行文本分析和语言结构分析等。数据资源可以自行下载使用。地址:http://www.nltk.org/data.html,数据列表:http://www.nltk.org/nltk_data/

下载NLTK-Data,在Python中输入命令:

>>>import nltk

>>>nltk.download()

弹出新的窗口,用于选择下载的资源

双击行后安装.

>>> import nltk

>>> nltk.download()

showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml

True

>>>

点击File可以更改下载安装的路径。all表示全部数据集合,all-corpora表示只有语料库和没有语法或训练的模型,book表示只有书籍中例子或练习的数据。需要注意一点,就是数据的保存路径,要么在C盘中,要么在Python的根目录下,否则后面程序调用数据的时候会因为找不到而报错。

【注意:软件安装需求:Python、NLTK、NLTK-Data必须安装,NumPy和Matplotlin推荐安装,NetworkX和Prover9可选安装】

简单测试NLTK分词功能:

---

下面看一下NLTK数据的几种方法:

1.加载数据

>>> from nltk.book import *

*** Introductory Examples for the NLTK Book ***

Loading text1, ..., text9 and sent1, ..., sent9

Type the name of the text or sentence to view it.

Type: 'texts()' or 'sents()' to list the materials.

text1: Moby Dick by Herman Melville 1851

text2: Sense and Sensibility by Jane Austen 1811

text3: The Book of Genesis

text4: Inaugural Address Corpus

text5: Chat Corpus

text6: Monty Python and the Holy Grail

text7: Wall Street Journal

text8: Personals Corpus

text9: The Man Who Was Thursday by G . K . Chesterton 1908

>>>

2.搜索文本

>>> print(text1.concordance('monstrous'))

Displaying 11 of 11 matches:

ong the former , one was of a most monstrous size . ... This came towards us ,

ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r

ll over with a heathenish array of monstrous clubs and spears . Some were thick

d as you gazed , and wondered what monstrous cannibal and savage could ever hav

that has survived the flood ; most monstrous and most mountainous ! That Himmal

they might scout at Moby Dick as a monstrous fable , or still worse and more de

th of Radney .'" CHAPTER 55 Of the Monstrous Pictures of Whales . I shall ere l

ing Scenes . In connexion with the monstrous pictures of whales , I am strongly

ere to enter upon those still more monstrous stories of them which are to be fo

ght have been rummaged out of this monstrous cabinet there is no telling . But

of Whale - Bones ; for Whales of a monstrous size are oftentimes cast up dead u

None

>>>

3.相似文本

>>> print(text1.similar('monstrous'))

imperial subtly impalpable pitiable curious abundant perilous

trustworthy untoward singular lamentable few determined maddens

horrible tyrannical lazy mystifying christian exasperate

None

>>>

4.共用词汇的上下文

>>> print(text2.common_contexts(['monstrous','very']))

a_pretty is_pretty a_lucky am_glad be_glad

None

>>>

5.词汇分布图

>>> text4.dispersion_plot(['citizens','democracy','freedom','duties','America'])

6.词汇统计

#encoding=utf-8
import nltk
from nltk.book import *  print('~~~~~~~~~~~~~~~~~~~~~~~~~')
print('文档text3的长度:',len(text3))
print('文档text3词汇和标识符排序:',sorted(set(text3)))
print('文档text3词汇和标识符总数:',len(set(text3)))
print('单个词汇平均使用次数:',len(text3)*1.0/len(set(text3)))
print('单词 Abram在text3中使用次数:',text3.count('Abram'))
print('单词Abram在text3中使用百分率:',text3.count('Abram')*100/len(text3))  

转载于:https://www.cnblogs.com/kylinsblog/p/7755843.html

Python与自然语言处理搭建环境相关推荐

  1. Python Web开发-django搭建环境

    这段时间 不太忙,想着整理一下python开发相关的内容,so 就现在,说整就整,白羊座行动派! Django简介 Django,发音为[`dʒæŋɡəʊ],是用python语言写的开源web开发框架 ...

  2. 自然语言处理和python_Python与自然语言处理搭建环境

    参考书籍<Python自然语言处理>,书籍中的版本是Python2和NLTK2,我使用的版本是Python3和NLTK3 安装NLTK3,Natural Language Toolkit, ...

  3. 大数据Python基础——第一章 搭建环境

    一. 从百度Python官网处下载最新版或者其他版本的Python软件. 1.附下载链接:Download Python | Python.orgThe official home of the Py ...

  4. python 创建软连接_Python搭建环境

    介绍完python之后,相信大家对这门编程语言有一定的了解了吧.那么接下来我们就要为自己的计算机装上python,然后就开始我们愉快的python学习之路咯. Python是一门跨平台语言,它可以在我 ...

  5. sources root pycharm 怎么设置_使用python语言开发ROOT之搭建环境方法探索

    这里指的是使用python调用ROOT的模块,编写程序用来处理数据的环境搭建方法. 经过研究,一般有四种方案 环境变量法,主要依靠ROOT本身,环境变量是关键 预编译包,但是该方法没有证实成功搭建py ...

  6. 新手零基础学习Python第一步,搭建开发环境!

    如何在电脑上面搭建Python开发环境?本文会解答这个问题. Python是一门计算机编程语言,通过给计算机下达精确的指令以完成相应的任务或者事情. 人类掌握Python后,就可以利用这个工具告诉计算 ...

  7. python在windows的开发环境_Python合集之Python开发环境在Windows系统里面搭建

    在上一个合集里面我们了解到了Python的基础信息及学习了Python对我们有什么用处,那么今天我们来了解一下,Python的开发环境该如何搭建.(注:Python的开发环境可以在Windows.MA ...

  8. 模拟器真机环境_Appium+python自动化(二)- 环境搭建—下(超详解)

    上一篇android测试开发环境已经准备好, 那么接下来就是appium的环境安装和搭建了. 菲哥和小伙伴们开个玩笑,不要觉得自人品不好,就不会成功那都是骗人的.搭建环境和人品半毛钱关系也没有,搭建环 ...

  9. python在win10怎么搭建_Win10系统如何搭建Python 3.5.2开发环境

    Win10系统如何搭建Python 3.5.2开发环境 具体方法如下: 1.首先从Python官网下载安装软件,如下图,输入IP地址; 2.找到Download,单击下面Python 3.5.2进入下 ...

最新文章

  1. 深度:中国智能制造分析报告
  2. 【NOIP2002】【codevs1098】均分纸牌
  3. 因子和,因子数,1到n的因子和,1到n的因子数(积性函数)
  4. linux下安装python(安装python 3.6稳定版成功亲测)
  5. 毕业设计-springboot+vue公租房租赁管理系统
  6. 老电脑xp系统最流畅的浏览器_1步打造极限精简的win10系统,让老电脑像新机般流畅,再也不卡了...
  7. 高通骁龙430系列-MSM8937 ( Cortex-A53架构)
  8. 软考中级网络工程师学习笔记(知识点汇总)普通版
  9. 大数据处理的四大步骤
  10. 穷举暴力破解密码(真香版)
  11. vs2010 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  12. elasticsearch配置告警方案问题记录
  13. Unresolved reference ‘sklearn‘问题|scikit-learn包安装
  14. Oracle中SCOTT用户的 emp、dept、bonus、salgrade表的意思及其属性的意思
  15. 【数论】 排列组合中的隔板问题
  16. 零打碎敲学Android(三)—俄罗斯,你为什么是方块的
  17. 机器学习02——回归问题中的损失函数 (L2损失L1损失Huber损失函数)
  18. 常用连接嵌入式设备的工具方法
  19. leetcode股票问题
  20. FastDFS - 快速的分布式文件系统

热门文章

  1. 华为USG6550 MIB CPU MEM
  2. 烂泥:团购网站的购买流程
  3. 第二层$.get()、$.post() 方法使用(三)
  4. 《HTML5 开发实例大全》——1.26 使用鼠标光标拖动网页中的文字
  5. 留言板小程序开发笔记3
  6. 标准exception类层次图
  7. Silverlight 属性样式、控件模板、视觉状态
  8. 工业相机和普通相机的区别详解_机器视觉网:工业相机与普通相机的四大区别...
  9. 快速上手Tomcat
  10. std::cin 溢出_溢出:坦诚地工作与焦虑如何影响我的团队