-
Notifications
You must be signed in to change notification settings - Fork 1
/
kiwi.py
executable file
·445 lines (379 loc) · 13.2 KB
/
kiwi.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/python
import sys
import web
from web import form
import nltk
from nltk import *
from nltk.corpus import PlaintextCorpusReader
from nltk.stem import *
from nltk.corpus import cmudict
from re import match
import json
import operator
import couchdb
import ast
import httplib
urls = (
'/', 'register',
'/start', 'hello',
'/rev', 'revision',
'/save', 'save',
'/register', 'register',
'/login', 'login',
'/verify', 'verify',
'/getAllDocs', 'getDocs',
'/load' , 'load',
'/render', 'renderPage',
'/contact', 'contact'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
myform = web.form.Form(web.form.Textarea('content', rows=10, cols=30))
couch = couchdb.Server()
cmu = cmudict.dict()
store = web.session.DiskStore('sessions')
session = web.session.Session(app, store, initializer= {'loggedin' : 'false'})
##Here is the Users database. It holds usernames along with (hashed) passwords.
##Knowing whether or not they're logged in is done through cookies and isAuth.
usersDB = None
if ('users' not in couch):
usersDB = couch.create('users')
else:
usersDB = couch['users']
#returns True if you are authorized and False if you are not. I swear, this is secure. :<
#It is, it's not as though you can access isAuth client-side
def isAuth(user, token):
if user not in usersDB:
print "user wasn't actually in database"
return False
else:
if (token == usersDB[user]['hashedPassword']):
print "token matched"
return True
else:
print "token failed"
return False
class verify:
def POST(self):
i = web.input()
username = str(i.username).lower()
token = str(i.token)
if (isAuth(username, token)):
return "verified"
else:
return "not verified"
class save:
def POST(self):
i = web.input()
userID = str(i.username).lower()
password = str(i.password)
title = str(i.title)
rawText = str(i.text)
if ( isAuth(userID, password) ):
saveContent(userID, title, rawText, parseContent(rawText))
return 'success'
else:
return 'failure'
class load:
def POST(self):
i = web.input()
userID = str(i.username).lower()
password = str(i.password)
title = str(i.title)
if ( isAuth(userID, password) ):
content = loadContent(userID, title)
return json.dumps(content)
else:
return 'failure'
class getDocs:
def POST(self):
i = web.input()
userID = str(i.username).lower()
password = str(i.password)
if (isAuth(userID, password)):
titles = docNames(userID)
return json.dumps(titles)
else:
return 'failure'
class register:
def GET(self):
return render.register(usersDB)
def POST(self):
print web.input()
i = web.input()
userID = str(i.username).lower()
password = str(i.password)
if userID in usersDB:
return 'already exists'
else:
usersDB[userID] = {'name' : userID, 'hashedPassword' : password}
addDbForUser(userID)
return 'woo yeah'
return 'wat'
class contact:
def GET(self):
return render.contact()
class login:
def POST(self):
print web.input()
i = web.input()
userID = str(i.username).lower()
password = str(i.password)
if userID in usersDB:
if usersDB[userID]['hashedPassword'] == password:
return 'great'
else:
return 'no match'
else:
print "username not in DB"
return 'no match'
return 'watlol'
class renderPage:
def POST(self):
i = web.input()
form = myform()
form.validates()
returnDict = str(i.parsedText)
print "ok"
returnDict = json.loads(returnDict)
returnJson = json.dumps(returnDict['text'])
statistics = json.dumps(returnDict['statistics'])
return render.edit(returnJson, statistics)
class testAdd:
def GET(self):
i = web.input(userID=None)
userID = str(i.userID)
addDbForUser(userID)
def addDbForUser(userID):
userID = "user_" + userID
db = couch.create(userID)
#Add all the design documents
path = "./couchdbviews/views"
dirList=os.listdir(path)
for fname in dirList:
pathToFile = path + "/" + fname
f = open(pathToFile, 'r')
rawString = f.read()
dictObj = ast.literal_eval(rawString)
designDocId = str(dictObj['_id'])
designDoc = json.dumps(dictObj)
print designDoc
relativePath = "/" + userID + "/" + designDocId
print relativePath
connection = httplib.HTTPConnection('127.0.0.1:5984')
connection.request('PUT',relativePath , designDoc)
result = connection.getresponse()
print result.__dict__
def saveContent(userID, title, rawContent, parsedContentDict):
print "saving"
userID = "user_" + userID
db = couch[userID]
doc = { }
doc['rawText'] = rawContent
doc['parsedContent'] = parsedContentDict
db[title] = doc
def loadContent(userID, title):
userID = "user_" + userID
db = couch[userID]
doc = db[title]
return doc
def docNames(userID):
userID = 'user_' + userID
db = couch[userID]
docs = []
for title in db:
if not (title[:8] == '_design/' ):
docs.append( {'title' : title} )
return docs
def parseContent(content):
tokens = WordPunctSpaceTokenizer().tokenize(content)
finder = ParseBigramCollocationsAndWords(tokens)
repetitions = DetectRepetitions(finder, tokens)
stemmer = LancasterStemmer()
wrds = []
stats = {}
sentenceLengths = []
topWords = {}
topWords["label"] = ["frequency"]
wordFreq = {}
totalSyllables = 0
totalWords = 0
currentSentenceLength = 0
index = 0
for token in tokens:
if token in ["!", ".", "?"]:
sentenceLengths.append(currentSentenceLength)
currentsentenceLength = 0
elif token not in [",","-", ":", "'", '"']:
currentSentenceLength += 1
if token not in ['the','s','.',';',',',"'",":", "`","(",")", " "]:
if token not in wordFreq:
wordFreq[token] = 1
else:
wordFreq[token] += 1
if token not in ['.',';',',',"'",":", "`","(",")", " "]:
totalSyllables = totalSyllables + syllableCount(token)
totalWords = totalWords + 1
makeJson = {}
makeJson['properties'] = {}
makeJson['properties']['stem'] = stemmer.stem(token)
makeJson['specialChar'] = False
makeJson['properties']['repetitions'] = False
if (token == '"'):
makeJson['word'] = "\""
elif ("""\r\n""" in token):
makeJson = {}
makeJson['word'] = "paragraphbreak"
makeJson['specialChar'] = True
elif index in repetitions:
makeJson['word'] = token
makeJson['properties']['repetitions'] = True
elif (token == " "):
makeJson['word'] = "space"
makeJson['specialChar'] = True
else:
makeJson['word'] = token
wrds.append(makeJson)
index = index + 1
gradeLevel = FleshKincaid(totalWords, len(sentenceLengths), totalSyllables)
print gradeLevel
sorted_x = sorted(wordFreq.iteritems(), key=operator.itemgetter(1))
sorted_x.reverse()
top5 = []
max5 = len(sorted_x)
if max5 > 5:
max5 = 5
for i in range(max5):
top5.append(sorted_x[i])
values = []
for top in top5:
dic = {"label" : top[0] ,"values" : [top[1]]}
values.append(dic)
topWords["values"] = values
returnval = json.dumps({"text" : wrds})
stats['topWords'] = {'json' : topWords , 'type': 'barGraph', 'displayName': 'Top Five Words' }
stats['gradeLevel'] = {'json' : gradeLevel, 'type': 'word', 'displayName': 'Grade Level'}
stats['vocabBreadth'] = {'json' : len(wordFreq) , 'type': 'word', 'displayName': 'Vocabulary Breadth'}
return {"text": wrds , "statistics": stats}
class tester:
def GET(self):
contentList = []
JSON1 = json.dumps({'word' : 'hello', 'properties' : {'stem': 'hello', 'repetition' : False}})
JSON2 = json.dumps({'word' : 'world', 'properties' : {'stem': 'world', 'repetition' : False}})
contentList.append(JSON1)
contentList.append(JSON2)
return render.edit(contentList)
class revision:
def POST(self):
i = web.input(text=None)
content = i.text
returnDict = parseContent(content)
returnJson = json.dumps(returnDict)
return returnJson
class hello:
def GET(self):
form = myform()
return render.hello(form)
def POST(self):
form = myform()
form.validates()
content = form['content'].value
returnDict = parseContent(content)
returnJson = json.dumps(returnDict['text'])
statistics = json.dumps(returnDict['statistics'])
return render.edit(returnJson, statistics)
class WordPunctSpaceTokenizer(RegexpTokenizer):
def __init__(self):
RegexpTokenizer.__init__(self, r'\s+|\w+|[^\w\s]+', discard_empty=False)
class ParseBigramCollocationsAndWords(BigramCollocationFinder):
def __init__(cls, words, window_size=2):
cls.PhrasesIndexes = {}
cls.StemmedPhrasesIndexes = {}
cls.breadth = 0
wfd = FreqDist()
bfd = FreqDist()
stemmer = LancasterStemmer()
if window_size < 2:
raise ValueError, "Specify window_size at least 2"
index = 0
for window in nltk.util.ingrams(words, window_size, pad_right=True):
w1 = window[0].lower()
'''
try:
window = window[:list(window).index(w1, 1)]
except ValueError:
continue
'''
if index == 0:
if not w1 in ['the','s','.',';',',',"'",":", "`","(",")", "`", '"', " " ]:
cls.PhrasesIndexes[w1] = [0]
cls.StemmedPhrasesIndexes[stemmer.stem(w1)] = [0]
wfd.inc(w1)
try:
w2 = window[1]
except ValueError:
break
if w2 is not None:
w2 = w2.lower()
if not cls.PhrasesIndexes.has_key(w2):
cls.PhrasesIndexes[w2] = [index + 1]
else:
cls.PhrasesIndexes[w2].append(index + 1)
stemmedw2 = stemmer.stem(w2)
if not stemmedw2 in ['the','s','.',';',',',"'",":", "`","(",")", "`", '"', " "]:
if not cls.StemmedPhrasesIndexes.has_key(stemmedw2):
cls.StemmedPhrasesIndexes[stemmedw2] = [index + 1]
else:
cls.StemmedPhrasesIndexes[stemmedw2].append(index + 1)
if not cls.PhrasesIndexes.has_key(w1 + " " + w2):
cls.PhrasesIndexes[w1 + " " + w2] = [index]
#cls.StemmedPhrasesIndexes[w1 + " " + w2] = [index]
else:
cls.PhrasesIndexes[w1 + " " + w2].append(index)
#cls.StemmedPhrasesIndexes[w1 + " " + w2].append(index)
bfd.inc((w1,w2))
index = index + 1
cls.breadth = len(cls.StemmedPhrasesIndexes)
BigramCollocationFinder.__init__(cls, wfd, bfd)
def DetectRepetitions(finder, tokens):
repetitions = []
indexesofrepetitions = []
for phrase in finder.StemmedPhrasesIndexes:
if not phrase in ['the','s','.',';',',',"'",":", "`","(",")"] :
phraseindexes = finder.StemmedPhrasesIndexes[phrase]
if len(phraseindexes) > 1:
i = 1
while i < len(phraseindexes):
diff = phraseindexes[i] - phraseindexes[i - 1]
if diff < 250:
repetitions = repetitions + [tokens[phraseindexes[i-1]] + " " + tokens[phraseindexes[i]]]
if not phraseindexes[i-1] in indexesofrepetitions:
indexesofrepetitions = indexesofrepetitions + [phraseindexes[i - 1]]
if not phraseindexes[i] in indexesofrepetitions:
indexesofrepetitions = indexesofrepetitions + [phraseindexes[i]]
i = i + 1
return indexesofrepetitions
def FleshKincaid(totalWords, totalSentences, totalSyllables):
if ( (not (totalWords == 0)) and (not (totalSentences == 0)) ):
return int(0.39 * (totalWords / totalSentences) + 11.8 * (totalSyllables / totalWords) - 15.59)
else:
return 0
def syllableCount(word):
reduced = reduce(word)
if (not len(reduced)) or (not reduced in cmu):
return manual_syllable_count(reduced)
return len([x for x in list(''.join(list(cmu[reduced])[-1])) if match(r'\d', x)])
def manual_syllable_count(word):
count = 0
prev = None
vowels = ['a', 'e', 'i', 'o', 'u']
for letter in word:
if ((prev not in vowels) and (letter in vowels)):
count = count + 1
prev = letter
return count
def reduce(word):
return ''.join([x for x in word.lower() if match(r'\w', x)])
if __name__ == "__main__":
# main(sys.argv[1])
app.run()