-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_one.py
61 lines (43 loc) · 1.28 KB
/
task_one.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
task_one.py
This file demonstrates how to create a custom corpus class to work with your own data.
Documentation on working with corpa in NLTK is available at:
https://nltk.googlecode.com/svn/trunk/doc/book/ch02.html
The files accessed by this corpus should be stored on disk in this file structure:
|-- Corpus Root
+--+
|-- README
|-- categories.txt
|-- texta.txt
+-- textb.txt
"""
import pprint
import nltk
from nltk import text
from nltk.corpus import PlaintextCorpusReader
from nltk import FreqDist
CORPUS_ROOT = "/Users/mark/Desktop/nlp-nltk-workshop/corpora/selection_from_gutenberg/"
class MyCorpus(PlaintextCorpusReader):
def __init__(self):
super(MyCorpus, self).__init__(CORPUS_ROOT, '.*')
def text(self):
return self.words()
def vocabulary(self):
return set(self.text())
def lexical_diversity(self):
return len(self) / len(self.vocabulary())
def histogram(self):
return FreqDist(self.text())
def generate_text(cfdist, start, num=15):
for i in xrange(num):
print start,
start = cfdist[start].max()
def __len__(self):
return len(self.text())
pp = pprint.PrettyPrinter(indent=2)
corpus = MyCorpus()
# print corpus
# print corpus.text()
# print corpus.vocabulary()
print corpus.histogram()
pp.pprint(corpus.histogram())