《用Python进行自然语言处理》笔记2

计算语言:简单的统计

一、频率分布

⑴ FreqDist(test)

将文本名称作为参数,形成字典,得到每个标识符的频率分布


>>> fd1=FreqDist(text1)
>>> fd1
FreqDist({
    
    ',': 18713, 'the': 13721, '.': 6862, 'of': 6536, 'and': 6024, 'a': 4569, 'to': 4542, ';': 4072, 'in': 3916, 'that': 2982, ...})
>>> fd1['is']   //查找'is'出现的次数
1695

⑵ hapaxes()

对只出现一次词的统计

 fd1.hapaxes()
['Herman', 'Melville', ']', 'ETYMOLOGY', 'Late', 'Consumptive', 'School', 'threadbare', 'lexicons', 'mockingly', 'flags', 'mortality', 'signification', '...]

如何把握文本的主题和风格:
⑶ plot()

展现变量的趋势变化

 fd1.plot(50,cumulative=False)   //取频率前50的词,图像递减

在这里插入图片描述

二、细粒度的选择词

高频词和低频词一般都难以表示出文章的意思,因此通过对文章长词的分析,或许能找到更多的特征和信息量。
P性质定为文本词汇表长度中超过 15个字符的词
表示我们感兴趣的词汇。它的含义是:此集合中所有 w都满足 w 是集合V(词汇表)的一 个元素且w 有性质 P。
(1) a. {w | w ∈ V & P(w)}
b. [w for w in V if p(w)]

它产生一个链表,而不是集合,这意味 着可能会有相同的元素。

>>> V=set(text1)
>>> long_words=[w for w in V if len(w)>15]
>>> sorted(long_words)    // 产生的是链表,可能会有相同的元素  
['CIRCUMNAVIGATION', 'Physiognomically', 'apprehensiveness', 'cannibalistically', 'characteristically', 'circumnavigating', 'circumnavigation', 'circumnavigations', 'comprehensiveness', 'hermaphroditical', 'indiscriminately', 'indispensableness', 'irresistibleness', 'physiognomically', 'preternaturalness', 'responsibilities', 'simultaneousness', 'subterraneousness', 'supernaturalness', 'superstitiousness', 'uncomfortableness', 'uncompromisedness', 'undiscriminating', 'uninterpenetratingly']

三、词语搭配和双连词(bigrams)

利用函数bigrams()实现对文本词汇中双连词的提取


>>> list(bigrams(['more','is','said','than','done']))
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')

Tip:bigrams函数返回一个"generator"对象,这是一种Python数据类型,类似于List,但仅在需要时创建,因此要把其形式转换为列表。
进行二元分词,然而我们需要去找到更频繁出现的双连词:
(1)collocation_list()
该函数可以为我们找到一篇文章中高频出现的双连词。

>>> text4.collocation_list()
['United States', 'fellow citizens', 'four years', 'years ago', 'Federal Government', 'General Government', 'American people', 'Vice President', 'Old World', 'Almighty God', 'Fellow citizens', 'Chief Magistrate', 'Chief Justice', 'God bless', 'every citizen', 'Indian tribes', 'public debt', 'one another', 'foreign nations', 'political parties']

Tip:原书中采用的是collocations()函数,但是运行后回出现错误:ValueError: too many values to unpack (expected 2)
通过分析该函数的源码发现该方法调用了collocations_list()方法,然而调用的这个函数返回值是普通列表,无法使用:
for w1,w2 in ...

四、计数及其他东西

(1)FreqDist()

该函数实现对文本中词长的计数,形成字典记录每个数字出现的次数

>>> [len(w) for w in text1]  //对文章中词长统计,并形成链表导出
[1, 4, 4, 2, 6, 8, 4, 1, 9, 1, 1, 8, 2, 1, 4, 11, 5, 2, 1, 7, 6, 1, 3, 4, 5, 2, 10, 2, 4, 1, 5, 1, 4, 1, 3, 5, 1, 1, 3, 3, 3, 1, 2, 3, 4, 7, 3, 3, 8,...]
>>> fdist=FreqDist([len(w) for w in text1])  
>>> fdist     
FreqDist({
    
    3: 50223, 1: 47933, 4: 42345, 2: 38513, 5: 26597, 6: 17111, 7: 14399, 8: 9966, 9: 6428, 10: 3528, ...})
>>> fdist.keys()
dict_keys([1, 4, 2, 6, 8, 9, 11, 5, 7, 3, 10, 12, 13, 14, 16, 15, 17, 18, 20])

nltk频率分布类中定义的函数
在这里插入图片描述

五、相关的决策与控制

(1) [w for w in text if condition ]

>>> [w for w in sent7 if len(w)<4 ]
[',', '61', 'old', ',', 'the', 'as', 'a', '29', '.']
>>> [w for w in sent7 if len(w)==4]
['will', 'join', 'Nov.']

在这里插入图片描述
比如:

>>> sorted([w for w in set(text1) if w.endswith('ableness')])
['comfortableness', 'honourableness', 'immutableness', 'indispensableness', 'indomitableness', 'intolerableness', 'palpableness', 'reaso

Guess you like

Origin blog.csdn.net/CHAINQWE/article/details/107082550