-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopwords.py
216 lines (189 loc) · 7.14 KB
/
popwords.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import nltk
import pickle
import sqlite3
def connectDB():
connect = sqlite3.connect('beta.sqlite')
cur = connect.cursor()
return connect, cur
connect, cur = connectDB()
def tobepop(cur): #figure out the extra rows in sents that have to be populated in words
cur.execute('select max(id) from words;')
st = cur.fetchone()
if not st or st==None:
return 0,0
cur.execute('select max(id) from sents;')
end = cur.fetchone()
return st[0]+1, end[0]
def Noun(word, cur, m):
cur.execute('select id from words where noun1 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun1=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun2 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun2=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun3 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun3=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun4 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun4=? where id=?;', (word, d[0]))
return
def Pronoun(word, cur, m):
cur.execute('select id from words where noun1 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun1=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun2 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun2=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun3 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun3=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where noun4 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set noun4=? where id=?;', (word, d[0]))
return
def Adjective(word,cur, m):
cur.execute('select id from words where adj1 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set adj1=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where adj2 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set adj2=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where adj3 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set adj3=? where id=?;', (word, d[0]))
return
def Modal(word,cur, m):
cur.execute('select id from words where modal IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set modal=? where id=?;', (word, d[0]))
return
def Verb(word,cur, m):
cur.execute('select id from words where verb1 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set verb1=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where verb2 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set verb2=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where verb3 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set verb3=? where id=?;', (word, d[0]))
return
def wh(word,cur, m):
cur.execute('select id from words where wh IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set wh=? where id=?', (word, d[0]))
return
def Adverb(word,cur, m):
cur.execute('select id from words where adv1 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set adv1=? where id=?;', (word, d[0]))
return
cur.execute('select id from words where adv2 IS NULL and new = 1;')
d = cur.fetchone()
if d:
cur.execute('update words set adv2=? where id=?;', (word, d[0]))
return
weights = {} #the dictionary of weights assigned. This is updated for every new input.
ratings = {'N':8, 'V': 10, 'P':6, 'J': 0.8, 'R':0.4, 'W':6, 'M': 6} #relative ratings of the parts of speech.
def finddenom(worddict, cur): #function to find the total weight of the current sentence.
total = 0
for word in worddict:
if word in wordval:
weights[word] = 1/wordval[word] #weight = 1/(its occurence in the corpora)
else:
weights[word]=0.1
for i in worddict:
pos = worddict[i]
pos = pos[0]
if pos in ratings:
weights[i] = weights[i]*ratings[pos] #weights * rating of the POS.
total = total+ weights[i]
print('in finddenom')
cur.execute('update words set denom=? where new=?;', (total, 1))
return total, weights
def stop_remove(sent):
from nltk.corpus import stopwords
stop=stopwords.words('english')
final={}
for i in sent:
if i in ['.', '?', ',','!']:
continue
if i not in stop:
final[i]=sent[i]
return final
cats = { 'N' : Noun,
'J' : Adjective,
'M' : Modal,
'V' : Verb,
'W' : wh,
'R' : Adverb,
'P' : Pronoun,
}
st, end = tobepop(cur)
if st==0:
exit()
for i in range(st, end+1):
cur.execute('select Input from sents where id>? and id=?;', (0, i))
inp = cur.fetchone()[0]
wordlist = nltk.word_tokenize(inp)
worddict = nltk.pos_tag(wordlist) #pos tagged dictionary of the words of the sentence. #duplicates removed.
worddict = dict(worddict)
wordlist = list(worddict.keys()) #duplicates have been removed now, both in wordlist and worddict.
newdict = stop_remove(worddict)
index = 0
q = 'insert into words(id) values(' + str(i) + ')'
print(i)
cur.execute(q)
cur.execute('update words set new=? where id=?;', (1, i))
cur.execute('select id from words where new=1;')
m = cur.fetchone()
m = m[0]
while(index<len(wordlist)):
#cur.execute('select id from words where new = 1;')
#Id = cur.fetchone()
#Id = Id[0]
word = wordlist[index]
char = worddict[word]
char = char[0] #current word's pos tag.
if char[0] not in cats.keys():
index = index+1
continue
cats[char](word, cur, m) #function call corresponding to the pos tag to insert the word in the right column in a new record in the database,
index = index+1
connect.commit()
#cur.execute('select * from words where id = 2')
output = open('density.txt', 'rb') #file containing the dictionary of frequency of words in the corpora is opened.
wordval = pickle.load(output)
inpval, weights = finddenom(worddict, cur) #find the weight of the current sentence, for future use.
cur.execute('update words set sentkey=? where id=?;', (i, i))
cur.execute('update words set new=? where id=?;', (0, i))
connect.commit()
connect.close()