Python与自然语言处理搭建环境
参考书籍《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))
Python与自然语言处理搭建环境的更多相关文章
- Python+NLTK自然语言处理学习(一):环境搭建
Python+NLTK自然语言处理学习(一):环境搭建 参考黄聪的博客地址:http://www.cnblogs.com/huangcong/archive/2011/08/29/2157437.ht ...
- Python 搭建环境踩过的那些坑
实践出真知,学习 Python 第六天,准备开始尝试写一些 Python 的实例.之前学习过程主要是参照的 廖雪峰老师的教程.搭建环境的过程,被很多坑围绕着. 版本选择 版本选择 Python 3.5 ...
- Appium python自动化测试系列之appium环境搭建(二)
2.1 基础环境搭建 当我们学习新的一项技术开始基本都是从环境搭建开始,本书除了第一章节也是的,如果你连最基础的环境都没有那么我们也没必要去说太多,大概介绍一下: 1.因为appium是支持andr ...
- 第一篇:Win10系统搭建Python+Django+Nginx+MySQL 开发环境详解(完美版)
Win10+Python+Django+Nginx+MySQL 开发环境搭建详解 PaulTsao 说明:本文由作者原创,仅供内部参考学习与交流,转载引用请注明出处,用于商业目的请联系作者本人. Wi ...
- pycharm+python+Django之web开发环境的搭建(windows)
转载:https://blog.csdn.net/yjx2323999451/article/details/53200243/ pycharm+python+Django之web开发环境的搭建(wi ...
- Appium+python自动化(二)- 环境搭建—下(超详解)
简介 宏哥的人品还算说得过去,虽然很久没有搭建环境了,但是换了新电脑设备,一气呵成,将android的测试开发环境已经搭建准备完毕.上一篇android测试开发环境已经准备好, 那么接下来就是appi ...
- Appium+python自动化(一)- 环境搭建—上(超详解)
简介 今天是高考各地由于降水,特别糟糕,各位考生高考加油,全国人民端午节快乐.最近整理了一下自动化的东西,先前整理的python接口自动化已经接近尾声.即将要开启新的征程和篇章(Appium& ...
- python web自动化测试框架搭建(功能&接口)——环境搭建
自动化测试框架一般需要实现以下通用功能 执行前准备 结束后清理 执行步骤输出 执行结果输出 错误.失败截图 测试报告 发送邮件 日志 需要的软件和python第三方库有: 通用: JDK Eclips ...
- 基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境
基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境 前言一.环境准备环境介绍软件下载VMware下安装UbuntuUbuntu下Anaconda的安 ...
随机推荐
- Linux系统 multitail 同时跟踪查看多个日志文件
一.问题描述: 平时我们在排查问题的时候往往需要查看日志文件来定位问题,有些程序可能关联的多个程序,因此需要打开多个窗口使用 tailf 或者tail -f 方式查看日志 但是需要不停的切换窗口来看不 ...
- 关于css布局的一点记录
1 关于css实现水平垂直居中的一些方法: .css的定位:用margin,padding,position position:absolute; //绝对定位,一般父级元素采用relative来 ...
- 考试T1护花
传送门 这题的提议似乎有什么问题,只要约翰选好了要抓那头牛,他就不会吃草了,站在原地傻等? 这题就是贪心,但在用cmp中比较单位时间吃草数量时,要用double型,不然可能会有点一样... 还有就是主 ...
- 第5天,python之路
已经过了两三天了,东西一点没来写,今天慢慢的写吧. 还是会按Alex Li的流程来学习,编写 写出自己的东西 常用的数据类型 简单的有,str ,int , 常用的: 列表list[], 字 ...
- python手册学习笔记1
笔记1 > http://www.pythondoc.com/pythontutorial3/controlflow.html 参数传递 Python中sys.argv的用法 调用解释器时,脚本 ...
- javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: ResultSet is from UPDATE. No Data.
Java jpa调用存储过程,抛出异常如下: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCEx ...
- element 动态合并表格
前言 element 官方的例子太简单了,不满足实际的需求 数据肯定是动态的,合并的行数,列数都是动态的,该如何知道每一行的合并数呢 需求 动态合并表格,数据来源于数据库 正文 一开始,我的数据源是单 ...
- mybatis什么时候必须指定jdbcType
#{property,javaType=int,jdbcType=NUMERIC}如果一个列允许 null 值,并且会传递值 null 的参数,就必须要指定 JDBC Type
- Salesforce学习之路(十一)Aura组件属性<aura:attribute />
1. <aura:attribute />语法 Aura组件属性类似与Apex中类的成员变量(或者说Java中类的成员变量).他们是组件在特定的实例上设置的类型化字段,可以使用表达式语法从 ...
- Python 面向对象-下篇
面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...