The corpora with NLTK

寻找文件路径的代码

# -*- coding: utf-8 -*-
"""
Spyder Editor This is a temporary script file.
""" import nltk,sys,os
print(nltk.__file__) if sys.platform.startswith('win'):
# Common locations on Windows:
sys.path += [
str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
os.path.join(sys.prefix, str('nltk_data')),
os.path.join(sys.prefix, str('lib'), str('nltk_data')),
os.path.join(os.environ.get(str('APPDATA'), str('C:\\')), str('nltk_data'))
]
else:
# Common locations on UNIX & OS X:
sys.path += [
str('/usr/share/nltk_data'),
str('/usr/local/share/nltk_data'),
str('/usr/lib/nltk_data'),
str('/usr/local/lib/nltk_data')
]

nltk的corpus语料库是一个所有语言的数据集合。大多数语料库是TXT文本存储,少数为xml和其它格式,

In this part of the tutorial, I want us to take a moment to peak into the corpora we all downloaded! The NLTK corpus is a massive dump of all kinds of natural language data sets that are definitely worth taking a look at.

Almost all of the files in the NLTK corpus follow the same rules for accessing them by using the NLTK module, but nothing is magical about them. These files are plain text files for the most part, some are XML and some are other formats, but they are all accessible by you manually, or via the module and Python. Let's talk about viewing them manually.

Depending on your installation, your nltk_data directory might be hiding in a multitude of locations. To figure out where it is, head to your Python directory, where the NLTK module is. If you do not know where that is, use the following code:

import nltk
print(nltk.__file__)

Run that, and the output will be the location of the NLTK module's __init__.py. Head into the NLTK directory, and then look for the data.py file.

The important blurb of code is:

if sys.platform.startswith('win'):
# Common locations on Windows:
path += [
str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
os.path.join(sys.prefix, str('nltk_data')),
os.path.join(sys.prefix, str('lib'), str('nltk_data')),
os.path.join(os.environ.get(str('APPDATA'), str('C:\\')), str('nltk_data'))
]
else:
# Common locations on UNIX & OS X:
path += [
str('/usr/share/nltk_data'),
str('/usr/local/share/nltk_data'),
str('/usr/lib/nltk_data'),
str('/usr/local/lib/nltk_data')
]

There, you can see the various possible directories for the nltk_data. If you're on Windows, chances are it is in your appdata, in the local directory. To get there, you will want to open your file browser, go to the top, and type in %appdata%

Next click on roaming, and then find the nltk_data directory. In there, you will have your corpora file. The full path is something like:

corpora在windows的路径
C:\Users\yourname\AppData\Roaming\nltk_data\corpora

语料库包括书籍,聊天记录,电影影评

Within here, you have all of the available corpora, including things like books, chat logs, movie reviews, and a whole lot more.

Now, we're going to talk about accessing these documents via NLTK.
As you can see, these are mostly text documents, so you could just use
normal Python code to open and read documents. That said, the NLTK
module has a few nice methods for handling the corpus, so you may find
it useful to use their methology. Here's an example of us opening the
Gutenberg Bible, and reading the first few lines:

古腾堡圣经(Gutenberg Bible),亦称四十二行圣经, 是《圣经》拉丁文公认翻译的印刷品,由翰尼斯·古腾堡于1454年到1455年在德国美因兹(Mainz)采用活字印刷术印刷的。这个圣经是最著名的古版书,他的产生标志着西方图书批量生产的开始

# -*- coding: utf-8 -*-
"""
Spyder Editor This is a temporary script file.
""" from nltk.tokenize import sent_tokenize
from nltk.corpus import gutenberg #sample text
sample=gutenberg.raw("bible-kjv.txt")
tok=sent_tokenize(sample)
for x in range(5):
print (tok[x])

Appdata是什么意思?

意思就是说包括系统程序运行时需要的文件,不建议删除!

Appdata下有三个子文件夹local,locallow,roaming,当你解压缩包时如果不指定路径,系统就把压缩包解到local\temp文件夹下,存放了一些解压文件,
安装软件时就从这里调取数据特别是一些制图软件,体积非常大,占用很多空间。locallow是用来存放共享数据,这两个文件夹下的文件就用优化大师清理,一般都可以清理无用的文件。
roaming文件夹也是存放一些使用程序后产生的数据文件,
如 空间听音乐,登入 的号码等而缓存的一些数据,这些数据优化大师是清理不掉的,
可以打开roaming文件夹里的文件全选定点击删除,删除不掉的就选择跳过,不过当你再使用程序时,这个文件夹又开始膨胀,又会缓存数据.
from nltk.tokenize import sent_tokenize, PunktSentenceTokenizer
from nltk.corpus import gutenberg # sample text
sample = gutenberg.raw("bible-kjv.txt") tok = sent_tokenize(sample) for x in range(5):
print(tok[x])

One of the more advanced data sets in here is "wordnet." Wordnet is a collection of words, definitions, examples of their use, synonyms, antonyms, and more. We'll dive into using wordnet next.

自然语言20_The corpora with NLTK的更多相关文章

  1. 自然语言处理(1)之NLTK与PYTHON

    自然语言处理(1)之NLTK与PYTHON 题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间.碰巧这几天在亚马逊上找书时发现了 ...

  2. 自然语言23_Text Classification with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/text-classification-nltk-tutorial/?compl ...

  3. 自然语言19.1_Lemmatizing with NLTK(单词变体还原)

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/lemmatizing-nltk-tutorial/?completed=/na ...

  4. 自然语言14_Stemming words with NLTK

    https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ # -*- ...

  5. 自然语言13_Stop words with NLTK

    https://www.pythonprogramming.net/stop-words-nltk-tutorial/?completed=/tokenizing-words-sentences-nl ...

  6. 自然语言处理2.1——NLTK文本语料库

    1.获取文本语料库 NLTK库中包含了大量的语料库,下面一一介绍几个: (1)古腾堡语料库:NLTK包含古腾堡项目电子文本档案的一小部分文本.该项目目前大约有36000本免费的电子图书. >&g ...

  7. python自然语言处理函数库nltk从入门到精通

    1. 关于Python安装的补充 若在ubuntu系统中同时安装了Python2和python3,则输入python或python2命令打开python2.x版本的控制台:输入python3命令打开p ...

  8. Python自然语言处理实践: 在NLTK中使用斯坦福中文分词器

    http://www.52nlp.cn/python%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E5%AE%9E%E8%B7%B5-% ...

  9. 推荐《用Python进行自然语言处理》中文翻译-NLTK配套书

    NLTK配套书<用Python进行自然语言处理>(Natural Language Processing with Python)已经出版好几年了,但是国内一直没有翻译的中文版,虽然读英文 ...

随机推荐

  1. RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)

    工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...

  2. FTP服务器

    FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制文件的双向传输. 下载"文件就是从 ...

  3. input用法,永远等待,直到用户输入值赋值给一个东西。

    input用法,永远等待,直到用户输入值赋值给一个东西. n1 = input('请输入用户名:') n1 = input('请输入密码:') print(n1) print(n1)

  4. C语言创始人

    丹尼斯·麦卡利斯泰尔·里奇(英语:Dennis MacAlistair Ritchie,1941年9月9日-2011年10月12日[3]),生于美国纽约州布朗克斯维尔(Bronxville),著名的美 ...

  5. MySQL热备脚本

    MYSQL 备份方法:    热备:    http://blog.itpub.net/27099995/viewspace-1295099/    http://blog.csdn.net/dban ...

  6. Android 判断当前屏幕是横屏还是竖屏

       记录学习 /**  * 返回当前屏幕是否为竖屏.  * @param context  * @return 当且仅当当前屏幕为竖屏时返回true,否则返回false.  */  public s ...

  7. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

  8. 使用IDEA和gradle搭建Spring MVC和MyBatis开发环境

    1. 概述 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具. 它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐 ...

  9. java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'XXXXX' bean

    今天启动srpingmvc项目的时候出现了这个异常, 原因: 在同个项目中,我复制了其中一个 Controller 作为备份 却忘记修改  @RequestMapping("/xxx&quo ...

  10. 寻找数组中的第K大的元素,多种解法以及分析

    遇到了一个很简单而有意思的问题,可以看出不同的算法策略对这个问题求解的优化过程.问题:寻找数组中的第K大的元素. 最简单的想法是直接进行排序,算法复杂度是O(N*logN).这么做很明显比较低效率,因 ...