-
Notifications
You must be signed in to change notification settings - Fork 1
/
nltk-test.py
141 lines (118 loc) · 3.89 KB
/
nltk-test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import nltk
import string
sample_file = open('murakami-norwood.txt')
print "\n\n"
print '-'*79
print "NLTK EXAMPLES for excerpt from 'Norwegian Wood' by Harumi Murakami"
print "by Tricia Decker for Hackbright Academy Fall 2015 Lightning Talk"
print '-'*79
print '\n'
print "CONVERT .txt file to raw text"
print "-"*79
raw_text = sample_file.read()
print raw_text[19:350] + ' ...'
user_pause = raw_input("> Press ENTER to continue.")
# no_punc_text = raw_text.strip().translate(string.maketrans("", ""), string.punctuation)
raw_no_punc = ''
for char in raw_text:
if char.isalpha() or char == " " or char == '\n':
raw_no_punc += char
raw_no_punc
# TOKENIZE by word
print '\n'
print "TOKENIZE by word:"
print "-"*79
tokens = nltk.word_tokenize(raw_text)
print tokens[6:61]
text = nltk.Text(tokens)
user_pause = raw_input("> Press ENTER to continue.")
# FILTER OUT PUNCTUATION
no_punc_tokens = [token for token in tokens if token.isalpha()]
# PRODUCE TEXT from tokens
text = nltk.Text(tokens)
# VOCABULARY
print '\n'
print "Create a VOCABULARY:"
print "-"*79
text_vocab = sorted(set(text))
print text_vocab[125:175]
user_pause = raw_input("> Press ENTER to continue.")
# collocations are bigrams that occur more often that we'd expect
# ex: red wine
print '\n'
print "Find COLLOCATIONS, bigrams that occur more often than we'd expect:"
print "(ex: 'red wine')"
print "-"*79
text.collocations() # returns None
# cols = [w for w in cols if w.isalpha()]
user_pause = raw_input("> Press ENTER to continue.")
print "\n"
# provide context for words with concordance
print "Provide context for words using CONCORDANCE."
print "-"*79
print "RESULTS for text.concordance('forest'):"
text.concordance('forest')
print "-"*79
print "RESULTS for text.concordance('canyon'):"
text.concordance('canyon')
user_pause = raw_input("> Press ENTER to continue.")
# search for other words used in similar context with similar
print '\n'
print "Search for other words used in SIMILAR context:"
print "-"*79
print "RESULTS for text.similar('forest')"
text.similar('forest')
print "-"*79
print "RESULTS for text.similar('doctor')"
text.similar('doctor')
user_pause = raw_input("> Press ENTER to continue.")
# WHAT MAKES TEXT DISTINCT?
fdist = nltk.FreqDist(tokens)
fdist.most_common(50)
# Create a Frequency Distribution of the tokens
print '\n'
print "What makes text distinct? Use Frequency Distributions"
print "-"*79
fdist2 = nltk.FreqDist(no_punc_tokens)
# Display the most common tokens and their frequencies
print "Fifty most-common words:"
print fdist2.most_common(50)
print
# Plot individual word frequencies, most common 50
print "Plot individual word frequencies, 50 most-common words:"
user_pause = raw_input("> Press ENTER to continue.")
fdist2.plot(50, cumulative=False)
print
# Plot cumulative word frequencies, most common 50
print "Plot cumulative word frequencies, 50 most-common words:"
user_pause = raw_input("> Press ENTER to continue.")
fdist2.plot(50, cumulative=True)
print
# examine longer words (7 characters or more)
long_words = [w for w in no_punc_tokens if len(w) > 7]
fdist3 = nltk.FreqDist(long_words)
print "Plot cumulative word frequencies, words with length > 7 characters:"
user_pause = raw_input("> Press ENTER to continue.")
fdist3.plot(30, cumulative=True)
print
# infrequent words
# hap_all = fdist2.hapaxes()
print "Examine infrequent words with HAPAXES:"
print '-'*79
hap_long = fdist3.hapaxes()
print "Sample of 20 words from 152 infrequent words:"
print hap_long[10:30]
user_pause = raw_input("> Press ENTER to continue.")
print
# meaningful --> frequent and not too long or short
meaningful = [w for w in no_punc_tokens if len(w) > 7 and fdist2[w] > 3]
print
print "MEANINGFUL WORDS: for example, words with length > 7 and frequency > 3"
print "-"*79
for w in set(meaningful):
print w
print
print "CONCORDANCE for 'wrinkles':"
print '-'*79
print text.concordance('wrinkles')
user_pause = raw_input("> Press ENTER to finish.")