-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path17_nltk4_basicanalysis.py
More file actions
40 lines (32 loc) · 1.24 KB
/
Copy path17_nltk4_basicanalysis.py
File metadata and controls
40 lines (32 loc) · 1.24 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
35
36
37
38
39
40
# Let's do some quick analysis
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)
# Let's make everything lowercase and get rid of punctuation:
filteredWords = []
for word in words:
if word.isalnum():
filteredWords.append(word.lower())
'''
We could do this in a single line too:
filteredWords = [word.lower() for word in words if word.isalnum()]
'''
# We can see these are now all lowercase and no punctuation is
# in the list anymore:
print(filteredWords[:25])
# Let's check the total number of words:
length = len(filteredWords)
print(f"The Adventures of Sherlock Holmes contain {length} words.")
# We can get the number of unique words by using a "set"
uniqueWords = set(filteredWords)
uniqueWordLength = len(uniqueWords)
print(f"Of {length} words, {uniqueWordLength} of them are unique.")
# Let's get the lexical diversity:
lexicalDiversity = uniqueWordLength/length
print(f"It has a lexical diversity of {lexicalDiversity}")