Python NLTK的学习(二) 获得文本语料和词汇资源

这部分 主要是解决以下的问题的:

  • 如何获取想要的语料
  • 那些代码适合获取语料
  • 怎么更好的编写获取语料的代码

2 获取文本语料库

2.1 NLTK自带语料库

2.1.1古腾堡计划电子书

NLTK自身在下载的适合也可以选择下载其带的古腾堡计划的电子书作为文本语料库,我们也可以从其网站上【http://www.gutenberg.org/】有选择的浏览。

from nltk.corpus import gutenberg
gutenberg.fileids()
for fileid in gutenberg.fileids():
    num_chars = len(gutenberg.raw(fileid))#获得原始文本
    num_words = len(gutenberg.words(fileid))#单词数
    num_sents = len(gutenberg.sents(fileid))#文本划分成句子,每个句子是一个列表
    num_vocab = len(set([w.lower() for w in gutenberg.words(fileid)]))
    print(int(num_chars/num_words), int(num_words/num_sents), int(num_words/num_vocab), fileid)

2.1.2 网络和聊天文本

from nltk.corpus import webtext
for fileid in webtext.fileids():
    print(fileid,webtext.raw(fileid)[:65],'…')

2.1.3 布朗语料库

是一个内容非常丰富的语料库

from nltk.corpus import brown
news_text = brown.words(categories='news')
fdist = nltk.FreqDist([w.lower() for w in news_text])
modals = ['can','could','may','might','must','will']
for m in modals:
    print(m,':',fdist[m])

又或者进行不同文本之间的比较

 cfd = nltk.ConditionalFreqDist(
    (genre, word)
    for genre in brown.categories()
    for word in brown.words(categories=genre))
genres = ['news', 'religion', 'hobbies', 'science_fiction', 'romance', 'humor']
modals = ['can', 'could', 'may', 'might', 'must', 'will']
cfd.tabulate(conditions=genres, samples=modals)
---
                  can could   may might  must  will 
           news    93    86    66    38    50   389 
       religion    82    59    78    12    54    71 
        hobbies   268    58   131    22    83   264 
science_fiction    16    49     4    12     8    16 
        romance    74   193    11    51    45    43 
          humor    16    30     8     8     9    13 

2.1.4 路透社语料库

路透社语料库包含 10,788 个新闻文档, 共计 130 万字。 这些文档分成 90 个主题, 按照“训练” 和“测试” 分为两组。因此, fileid 为“test/14826” 的文档属于测试组。这样分割是为了训练和测试算法的,这种算法自动检测文档的主题,我们将在第 6 章中看到。

 Resource reuters not found.
  Please use the NLTK Downloader to obtain the resource:

明明我感觉自己把NLTK的语料都下载了,但是总是提示我找不到路透社的这份语料
在这里插入图片描述

2.1.5就职演说语料库

%matplotlib inline
cfd = nltk.ConditionalFreqDist(
    (target,fileid[:4])
    for fileid in inaugural.fileids()
    for w in inaugural.words(fileid)
    for target in ['america','citizen']
    if w.lower().startswith(target))
cfd.plot()

在这里插入图片描述

2.1.6 载入用户语料库

如果你有自己收集的文本文件, 并且想使用前面讨论的方法访问它们, 你可以很容易地在 NLTK 中的 PlaintextCorpusReader 帮助下载入它们。 检查你的文件在文件系统中的位置; 在下面的例子中, 我们假定你的文件在/usr/share/dict 目录下。 不管是什么位置, 将变量orpus_root的值设置为这个目录。 PlaintextCorpusReader 初始化函数的第二个参数可以是一个如[‘a.txt’, ‘test/b.txt’]这样的 fileids 链表, 或者一个匹配所有fileids 的模式。如:’[abc]/.*.txt’。

>>> from nltk.corpus import PlaintextCorpusReader
>>> corpus_root = '/usr/share/dict' 
>>> wordlists = PlaintextCorpusReader(corpus_root, '.*') 
>>> wordlists.fileids()
['README', 'connectives', 'propernames', 'web2', 'web2a', 'words']
>>> wordlists.words('connectives')
['the', 'of', 'and', 'to', 'a', 'in', 'that', 'is', ...]
  • 这部分的实践只成功了一半,应该是我选择的本地文件的原因。但是可以成功的读取到这个文件了。

2.2 条件概率分布

当语料文本被分为几类(文体、 主题、 作者等) 时, 我们可以计算每个类别独立的频率分布。这将允许我们研究类别之间的系统性差异。

2.2.1 条件和事件

这两个概念比较好理解,条件,顾名思义,就是我们做出的限制,事件,就是根据这个限制发现的语法单元。

2.2.2 按文体计数词汇

这里NLTK 给出的函数是ConditionalFreqDist()

from nltk.corpus import brown
cfd = nltk.ConditionalFreqDist((genre,word)
                              for genre in brown.categories()
                              for word in brown.words(categories=genre))
                              
cfd               
<ConditionalFreqDist with 15 conditions>
---
cfd.conditions()
['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies', 'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance', 'science_fiction']       
---
cfd['news']
FreqDist({'the': 5580, ',': 5188, '.': 4030, 'of': 2849, 'and': 2146, 'to': 2116, 'a': 1993, 'in': 1893, 'for': 943, 'The': 806, ...})
---
更小的范围:两个文体
gente_word = [(genre,word)
             for genre in ['news','romance']
             for word in brown.words(categories=genre)]
下面的操作类似

2.2.4 以图的形式展示数据

这里的用法我们在上面使用过,其实就是将数据形象化的展示出来。

from nltk.corpus import inaugural
cfd=nltk.ConditionalFreqDist(
    (target,fileid[:4])
    for fileid in inaugural.fileids()
    for w in inaugural.words(fileid)
    for target in ['america','citizen']
    if w.lower().startswith(target))
cfd.plot()

如上述代码,当我们使用ConditionalFreqDist()获得了条件概率的配对信息时,就可以利用plot()图式化的展示数据。更可以对plot()进行参数的设置,来调整你想要显示或隐藏的数据内容。

2.2.5 使用双连词产生随机文本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值