Note - Natural Language Processing with Python (Chapter1)

from __future__ import division
import nltk
from nltk.book import text1, text2, text5, text6, sent1, sent3

print(text2.common_contexts(['monstrous', 'very']))

print(text2.similar('monstrous'))

print(text2.similar('very'))

print(text2.dispersion_plot(['very']))

text2.generate()

print(len(set(text1)))

# count occurrence of each word on average
print(len(text2) / len(set(text2)))

print(text5.count('lol') / len(text5))

print(sorted(sent1))

lml = ['Hello', 'mei', 'ling', 'li', '!']
print(lml)
lml[1:3] = ['yibo']
print(lml)

test = '*'.join(['yibo', 'and', 'zz'])
print(test)

print(test.split('*'))

freq1 = nltk.FreqDist(text1)
vocabulary = freq1.keys()
print(freq1['whale'])
print(freq1)
print(freq1.values(), vocabulary, freq1['am'])
for item, value in enumerate(freq1):
    print(item, value)
freq1.plot(50, cumulative=True)
print(len(freq1.hapaxes()))

V = set(text1)
long_words = [w for w in V if len(w) > 15]
print(long_words[:15])

freq5 = nltk.FreqDist(text5)
set5 = set(text5)
long_words = sorted([w for w in set5 if len(w) > 7 and freq5[w] > 7])
print(long_words[:20])

print(text1.collocations())

fdist1 = nltk.FreqDist([len(w) for w in text1])
print(fdist1.N())
print(fdist1.items())
print(fdist1[3], fdist1.max(), fdist1.freq(3))

print(text1)
print([w for w in sent1 if w.startswith('s')])
print([w for w in sent1 if 's' not in w])
print([w for w in set(text1) if not w.islower()])

from nltk.misc import babelize_shell

babelize_shell()

nltk.chat.chatbots()

text2.collocations()

print(sent3, sent3.index('the'))

print(len(sorted(set([w.lower() for w in text1]))))
print(len(sorted([w.lower() for w in set(text1)])))

# 21
print(text2[-2:])

# 22
four_letter_words = [w for w in text5 if len(w) == 4]
print(four_letter_words)
freq5 = nltk.FreqDist(four_letter_words)
print(freq5.items())
print(freq5.keys())
print(freq5.most_common())

# 23
for w in text6:
    if w.isupper():
        print(w)

# 24
lst = [w for w in text6 if w.endswith('ize')]
print(lst)
lst = [w for w in text6 if 'pt' in w]
print(lst)
lst = [w for w in text6 if w.istitle()]
print(lst)

# 26
print(sum([len(w) for w in text1]))

# 29
print(set(sent3) < set(sent1))
print(set(text5) < set(text1))

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Meilinger_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值