6. Example1/5
Brown Corpusのニュースカテゴリ内で、法助動詞(話し手の
気持ちを叙述する助動詞)の出現回数を列挙する。
>>> 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], end=' ’)
can: 94 could: 87 may: 93 might: 38 must: 53 will: 389
7. Practice 1/3
Brown Corpusのあるカテゴリ内で、wh-単語の出現
回数を列挙する。
>>> fiction_text = brown.words(categories='fiction')
>>> fdist = nltk.FreqDist(w.lower() for w in
news_text)
>>> wh_words = list(set([w.lower() for w in
fiction_text if w.lower().startswith('wh')]))
>>> for wh in wh_words:
... print(wh + ':', fdist[wh])
11. Example 2/5
人権宣言コーパスの複数の言語について、単語の長さの累積頻度
分布プロットを作成する。
>> from nltk.corpus import udhr
>>> languages = ['Chickasaw', 'English', 'German_Deutsch',
... 'Greenlandic_Inuktikut', 'Hungarian_Magyar', 'Ibibio_Efik']
>>> cfd = nltk.ConditionalFreqDist(
... (lang, len(word))
... for lang in languages
... for word in udhr.words(lang + '-Latin1'))
>>> cfd.plot(cumulative=True)
15. Example 3/5
米国大統領就任演説中、americaおよびcitizenとい
う単語の出現回数を、就任年度に対してプロットす
る。
>>> 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))
22. Example 4/5
単語motorcarについてWordNetを参照する。
>>>from nltk.corpus import wordnet as wn
>>> wn.synsets('motorcar')
[Synset('car.n.01')]
>>> wn.synset('car.n.01').definition()
'a motor vehicle with four wheels; usually propelled
by an internal combustion engine’
>>> wn.synset('car.n.01').lemma_names()
['car', 'auto', 'automobile', 'machine', 'motorcar']
26. Extra
Brown Corpus内でn回以上出現する単語を列挙する。
>>> fd = nltk.FreqDist(w.lower() for w in
brown.words())
>>> words = [vocab for vocab in fd.keys() if
fd[vocab] >= 3]
>>> len(words)
20615