-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtract_mas_dens.py
155 lines (132 loc) · 4.56 KB
/
tract_mas_dens.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# adj
# texto entero
import nltk
from scipy import stats
from numpy import array
import matplotlib.pyplot as plt
from nltk.corpus import PlaintextCorpusReader
corpus_root = r'C:\Users\guille\Google Drive\NLTK\DEF\ficheros_in\Tratactus\ing\GRADOS\grado_4'
newcorpus = PlaintextCorpusReader(corpus_root, '.*')
tokens = nltk.word_tokenize(newcorpus.raw())
text = nltk.Text(tokens)
stopwords = nltk.corpus.stopwords.words('english')
from nltk import word_tokenize
from nltk.tag import pos_tag
V = ['VB', 'VBZ', 'VBP', 'VBD', 'VBG']
N = ['NN', 'NNS', 'NNP', 'NNPS']
ADV = ['RB', 'RBR', 'RBS']
ADJ = ['JJ', 'JJR', 'JJS']
wLen = [] # number of words
vLen = [] # number of verbs
advLen = [] # number of adverbs
adjLen = [] # number of adjectives
vLen, nLen, advLen, adjLen, wLen = ([] for i in range(5))
for fileid in newcorpus.fileids():
tokens = word_tokenize(newcorpus.raw(fileid))
words = [t for t in tokens if t.isalpha()]
taggedW = pos_tag(words)
verbs, nouns, advs, adjs = ([] for i in range(4))
for (w,tag) in taggedW:
if tag in V: verbs.append(w)
elif tag in N: nouns.append(w)
elif tag in ADV: advs.append(w)
elif tag in ADJ: adjs.append(w)
wLen.append(len(words))
vLen.append(len(verbs))
nLen.append(len(nouns))
advLen.append(len(advs))
adjLen.append(len(adjs))
plotData0 = [(wLen, vLen), (wLen, nLen), (wLen, adjLen)]
yaxisLabels = ['V x 1000', 'N x 1000', 'ADJ x 1000']
plt.figure(figsize=(7.5,7.5))
for (pane, data) in enumerate(plotData0):
X, Y = data[0], data[1]
slope, intercept = stats.linregress(X, Y)[0:2]
rX = slope*array(X) + intercept
plt.subplot(2, 2, pane+1)
plt.scatter(X, Y)
plt.plot(X, rX, 'r',
label='slope={},\nintercept={}'.format(
round(slope,2),
round(intercept,2)))
plt.ylim(plt.xlim())
wTicks = [int(tk/1000) for tk in plt.gca().get_xticks()]
plt.gca().set_xticklabels(wTicks)
plt.gca().set_yticklabels(wTicks)
offset = (plt.gca().get_xticks()[1]-plt.gca().get_xticks()[0])/10
for pt in range(len(X)):
plt.annotate(str(pt), # scifiCorpus[i]
xy=(X[pt], Y[pt]),
xytext=(X[pt]+offset, Y[pt]))
plt.xlabel('words x 1000')
plt.ylabel(yaxisLabels[pane])
plt.legend(loc='upper left', fontsize='small')
plt.grid()
plt.tight_layout()
plt.show()
# adverbios
# texto entero
import nltk
from scipy import stats
from numpy import array
import matplotlib.pyplot as plt
from nltk.corpus import PlaintextCorpusReader
corpus_root = r'C:\Users\guille\Google Drive\NLTK\DEF\ficheros_in\Tratactus\ing\GRADOS\grado_4'
newcorpus = PlaintextCorpusReader(corpus_root, '.*')
tokens = nltk.word_tokenize(newcorpus.raw())
text = nltk.Text(tokens)
stopwords = nltk.corpus.stopwords.words('english')
from nltk import word_tokenize
from nltk.tag import pos_tag
V = ['VB', 'VBZ', 'VBP', 'VBD', 'VBG']
N = ['NN', 'NNS', 'NNP', 'NNPS']
ADV = ['RB', 'RBR', 'RBS']
ADJ = ['JJ', 'JJR', 'JJS']
wLen = [] # number of words
vLen = [] # number of verbs
advLen = [] # number of adverbs
adjLen = [] # number of adjectives
vLen, nLen, advLen, adjLen, wLen = ([] for i in range(5))
for fileid in newcorpus.fileids():
tokens = word_tokenize(newcorpus.raw(fileid))
words = [t for t in tokens if t.isalpha()]
taggedW = pos_tag(words)
verbs, nouns, advs, adjs = ([] for i in range(4))
for (w,tag) in taggedW:
if tag in V: verbs.append(w)
elif tag in N: nouns.append(w)
elif tag in ADV: advs.append(w)
elif tag in ADJ: adjs.append(w)
wLen.append(len(words))
vLen.append(len(verbs))
nLen.append(len(nouns))
advLen.append(len(advs))
adjLen.append(len(adjs))
plotData0 = [(wLen, vLen), (wLen, nLen), (wLen, advLen)]
yaxisLabels = ['V x 1000', 'N x 1000', 'ADV x 1000']
plt.figure(figsize=(7.5,7.5))
for (pane, data) in enumerate(plotData0):
X, Y = data[0], data[1]
slope, intercept = stats.linregress(X, Y)[0:2]
rX = slope*array(X) + intercept
plt.subplot(2, 2, pane+1)
plt.scatter(X, Y)
plt.plot(X, rX, 'r',
label='slope={},\nintercept={}'.format(
round(slope,2),
round(intercept,2)))
plt.ylim(plt.xlim())
wTicks = [int(tk/1000) for tk in plt.gca().get_xticks()]
plt.gca().set_xticklabels(wTicks)
plt.gca().set_yticklabels(wTicks)
offset = (plt.gca().get_xticks()[1]-plt.gca().get_xticks()[0])/10
for pt in range(len(X)):
plt.annotate(str(pt), # scifiCorpus[i]
xy=(X[pt], Y[pt]),
xytext=(X[pt]+offset, Y[pt]))
plt.xlabel('words x 1000')
plt.ylabel(yaxisLabels[pane])
plt.legend(loc='upper left', fontsize='small')
plt.grid()
plt.tight_layout()
plt.show()