-
Notifications
You must be signed in to change notification settings - Fork 2
/
text_analysis.py
75 lines (60 loc) · 1.89 KB
/
text_analysis.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
from nltk.corpus import inaugural
from numpy import*
from pylab import*
from nltk.book import text4
#count=0
#import nltk
#nltk.download()
#quit()
def getWords():
for word in text4[0:1000]:
if len(word)>5:
print word
#count= count+1
def getFileids():
index=0
for id in inaugural.fileids():
index+=1
if index==2:
print id # print the president name
print len(inaugural.words(id)) #print the # of words
if index==14:
print id # print the president name
print len(inaugural.words(id)) #print the # of words
def getGraphs():
index =0
for id in inaugural.fileids(): #prob(-14)
index+=1
ww= inaugural.raw(id).lower()
num_war=ww.count('war')
num_america=ww.count('america')
num_economy=ww.count('economy')
num_world=ww.count('world')
plot(index, num_war,'mo') #war
plot(index, num_america,'go') #america (increasing)
plot(index, num_economy,'ro') #ecomony
plot(index, num_world,'bo') #world (increasing)
xlabel('index, purple-war, green-america, red-economy, world-blue')
ylabel('the frequency of the words used')
show()
def graphWords():
index=0
for id in inaugural.fileids():
index+=1
nchar=len(inaugural.raw(id))*1.0
nword=len(inaugural.words(id))*1.0
nsent=len(inaugural.sents(id))*1.0
nvoc=len(set(w.lower() for w in inaugural.words(id)))*1.0
a = nchar/nword
b=nword/nsent
c=nword/nvoc
plot(index,a,'mo') #purple color
plot(index,b,'go') #green color
plot(index,c,'ro') #red color
xlabel('index, from Washington to Obama (purple - character/word), (red - word/vocab)')
ylabel('Average numbers (green - word/sentence)')
show()
#graphWords()
#getGraphs()
#getFileids()
#getWords()