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的安 ...
随机推荐
- Data Compression Category
Data Compression is an approach to compress the origin dataset and save spaces. According to the Eco ...
- 网络安全-主动信息收集篇第二章-三层网络发现之ping
第三层网络扫描基于TCP/IP.ICMP协议. 优点:可路由.速度比较快 缺点:相对于二层网络扫描较慢,容易被边界防火墙过滤 所有扫描发现技术,都会有相应的对抗办法,所以无论是来自二层的网络扫描还是来 ...
- 7.22 NOIP模拟7
又是炸掉的一次考试 T1.方程的解 本次考试最容易骗分的一道题,但是由于T2花的时间太多,我竟然连a+b=c都没判..暴力掉了40分. 首先a+b=c,只有一组解. 然后是a=1,b=1,答案是c-1 ...
- CSPS模拟 77
%%两位AK爷zkt和skyh T1 位间独立,分别讨论 T2 维护标记,代替移位 T3 同一点对,多种联通,没法搞. 发现最多四路连通,考虑容斥. 显然的奇加偶减. 发现统计某种颜色的点之间的联通数 ...
- C++ 11新标准实现POJ No.1002-487-3279
487-3279(重复的电话号码查询)(标签:优先队列,哈希表) 题目描述 企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一个容易记住的单词或者短语.例如,你需要给滑铁卢大学 ...
- 201871010114-李岩松《面向对象程序设计(java)》第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 【Linux系列】Centos 7安装以及网络配置(一)
目的 本文主要介绍以下两点: 一. 如何在Oracle VM VirtualBox安装centos(已有VirtualBox) 二. 如何在内网里实现虚拟机访问外网.物理主机以及物理主机访问虚拟机 一 ...
- Vue学习笔记:Vue组件的核心概念(下)
1.双向绑定和单向数据流: 本质上还是单向数据流 视图<——>数据 v-model:仅仅是一个简写,用更少代码去实现功能. 自定义事件 .sync 修饰符 2.虚拟DOM及KEY属性作用 ...
- [JQuery] JQuery学习笔记
1.2019年10月20日14:43:48 学习HOW2J. 2.JQuery是一个javascript的框架,是对javascript的一种封装, 通过JQuery可以非常方便的操作html的元素 ...
- vue项目iframe的传值问题
前言 项目需要,我需要引入一个已经封装好的浏览器插件.插件只能以html的方式调用, 所以.我把插件的使用封装了一个html页面.vue项目则利用iframe的方式引入. 到这里我就遇到了一个问题,那 ...