-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path17_nltk3_textobject2.py
More file actions
34 lines (28 loc) · 1.1 KB
/
Copy path17_nltk3_textobject2.py
File metadata and controls
34 lines (28 loc) · 1.1 KB
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
# Text objects come with a lot of interesting features:
import nltk
# Same prep as before
textfile = open("holmes.txt","r",encoding="utf8")
holmesstring = textfile.read()
textfile.close()
startpoint = holmesstring.find('*** START OF THIS PROJECT GUTENBERG EBOOK')
endpoint = holmesstring.find('*** END OF THIS PROJECT GUTENBERG EBOOK')
holmesstring = holmesstring[startpoint:endpoint]
words = nltk.word_tokenize(holmesstring)
holmestext = nltk.Text(words)
# Find words that appear in a similar context:
# the bigger the corpus, the more meaningful this will be
print("Similar words to apartment")
holmestext.similar("apartment")
# Make a lexical dispersion plot:
# Give this a list of words. To let the code continue, you
# will have to close the window that opens.
holmestext.dispersion_plot(["murder", "death"])
# Getting collocations:
holmestext.collocations()
# Getting word frequencies (you can also provide
# a list of words):
frequencies = nltk.FreqDist(holmestext)
print(frequencies['and'])
# You can get most common words, or words that occur once:
print(frequencies.most_common(1))
#print(frequencies.hapaxes())