forked from rebeccastandig/TheMATCProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
536 lines (452 loc) · 14.9 KB
/
model.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import redis
import hashlib
import random
import os
r_server = redis.from_url(os.environ.get('REDISCLOUD_URL'))
def md5_hash(password):
return hashlib.md5(password).hexdigest()
def get_list(key):
# returns list via key
# returns empty list if key hadn't been created prior to checking
return r_server.lrange(key, 0, -1)
def get_num_list(key):
# returns len(list), retrieved via key
# returns 0 if key hadn't been created prior to checking
return r_server.llen(key)
def get_string_num(key):
# returns string via key.
# value of key can be either string or number
# doesn't return anything if key hadn't been created prior to checking
return r_server.get(key)
#### Basic Setting Values ####
def set_sent_pos_tag(pos, sentences):
# sets sent_(POS)_tag
# pos must be string
# sentence(s) must be list in ['sent', 'sent2'] format
sent_pos_tag = 'sent_%s_tag'%pos
tag_pos = 'tag_%s'%pos
r_server.rpush(sent_pos_tag, tag_pos)
for sentence in sentences:
r_server.rpush(sent_pos_tag, sentence)
def set_user(user):
# sets user_(name)
# user must be string
user_name = 'user_%s'%user
user_name_pw = 'user_%s_pw'%user
user_name_pts = 'user_%s_pts'%user
r_server.rpush(user_name, user)
r_server.rpush(user_name, user_name_pw)
r_server.rpush(user_name, user_name_pts)
r_server.incr(user_name_pts, 0)
add_all_users(user)
add_all_points(user)
def set_user_pw(user, pw):
# sets user_(name)_pw
# user & pw must be strings
user_name_pw = 'user_%s_pw'%user
password = md5_hash(pw)
r_server.set(user_name_pw, password)
#### End Basic Setting Values ####
#### Basic Setting & Appending ####
def add_all_words(game_word_list):
# sets & appends to words.
# requires game words to be in a list, in '(word)' format
prior_word_list = get_list('words')
for word in game_word_list:
word_word = 'word_%s'%word
if word_word in prior_word_list:
pass
else:
r_server.rpush('words', word_word)
def add_all_tags(pos_tag_list):
# sets & appends to tags.
# requires POS_tags to be in a list, in '(POS)' format
prior_tags_list = get_list('tags')
for pos in pos_tag_list:
tag_pos = 'tag_%s'%pos
if tag_pos in prior_tags_list:
pass
else:
r_server.rpush('tags', tag_pos)
def add_all_users(user):
# sets & appends to users.
# user must be string
prior_users_list = get_list('users')
user_name = 'user_%s'%user
if user_name in prior_users_list:
pass
else:
r_server.rpush('users', user_name)
def add_all_points(user):
# sets & appends to points
# user must be string
prior_points_list = get_list('points')
user_name_pts = 'user_%s_pts'%user
if user_name_pts in prior_points_list:
pass
else:
r_server.rpush('points', user_name_pts)
def add_all_sentences(pos_list):
# sets & appends to sentences
# pos must be list, in '(POS)' format
prior_sent_list = get_list('sentences')
for pos in pos_list:
sent_pos_tag = 'sent_%s_tag'%pos
if sent_pos_tag in prior_sent_list:
pass
else:
r_server.rpush('sentences', sent_pos_tag)
def add_word_tweets(word, tweets):
# sets & appends to word_(word)_tweets
# tweets must be list, in '(tweet)' format
# word must be string
word_word_tweets = "word_%s_tweets"%word
prior_tweet_list = get_list(word_word_tweets)
for tweet in tweets:
if tweet in prior_tweet_list:
pass
elif tweet == "":
pass
else:
r_server.rpush(word_word_tweets, tweet)
def add_user_pts(user_name_pts, pts):
# sets & adds user_(name)_pts
# user must be string 'user_(name)_pts'
# pts must be number, can be negative to decrease
r_server.incr(user_name_pts, pts)
# leaving pts open so i can write diff function that will discriminate when to add 10 vs 5
def add_tag_word_tag_pos(word, pos, user):
# sets & appends to tag_word_(word)_tag_(POS)
# word & pos must be strings
# user must be string in 'user_(name)' format
tag_word_word_tag_pos = 'tag_word_%s_%s'%(word, pos)
prior_user_list = get_list(tag_word_word_tag_pos)
if user in prior_user_list:
pass
else:
r_server.rpush(tag_word_word_tag_pos, user)
def add_tweet_tag_word_tag_pos(word, pos, tweet):
# sets & appends to tweet_tag_word_(word)_tag_(POS)
# word, tweet, & pos must be string
tweet_tag_word_word_tag_pos = 'tweet_tag_word_%s_%s'%(word, pos)
prior_tweet_list = get_list(tweet_tag_word_word_tag_pos)
if tweet not in prior_tweet_list:
r_server.rpush(tweet_tag_word_word_tag_pos, tweet)
def add_tagged_words_pos(pos, word_list):
# sets & appends to tagged_words_tag_(POS)
# pos must be string
# word(s) must be in a list, in 'word_(word)' format
tagged_words_tag_pos = 'tagged_words_%s'%pos
prior_word_list = get_list(tagged_words_tag_pos)
for word in word_list:
if word in prior_word_list:
pass
else:
r_server.rpush(tagged_words_tag_pos, word)
def add_user_words_tagged(user, word_list):
# sets & appends to user_(name)_words_tagged
# user must be string
# word(s) must be list, in 'tag_word_(word)_tag_(POS)' format
user_name_words_tagged = 'user_%s_words_tagged'%user
prior_word_list = get_list(user_name_words_tagged)
for word in word_list:
if word in prior_word_list:
pass
else:
r_server.rpush(user_name_words_tagged, word)
def add_final_tag(word, tag_list):
# sets & appends to final_tag_word_(word)
# word must be string
# tag(s) must be list, in 'tag_(POS)' format
final_tag_word_word = 'final_tag_word_%s'%word
prior_tag_list = get_list(final_tag_word_word)
for tag in tag_list:
if tag not in prior_tag_list:
r_server.rpush(final_tag_word_word, tag)
def set_game_words_tweets(word_list, tweet):
# ***need to check to make sure no words are '(pos)_tweets' or anything like that before adding to word list.***
add_all_words(word_list)
for word in word_list:
add_word_tweets(word, tweet)
print "success"
#### End Basic Setting & Appending ####
#### Flask Uses ####
def check_if_user(user):
# checks to see if user_(name) exists for user.
# returns True or False
user_name = 'user_%s'%user
return r_server.exists(user_name)
def check_alphanum(string):
# checks if a string is A-Z, a-z, 0-9 characters only; ord() must be b/t 48 and 122
for item in string:
alphanum = True
if ord(item) < 48:
alphanum = False
return alphanum
elif ord(item) > 122:
alphanum = False
return alphanum
return alphanum
def auth_login(user_name, pw):
name_key = 'user_%s'%user_name
user_info = get_list(name_key)
authenticated = False
if user_info:
if user_name == user_info[0]:
password = get_string_num(user_info[1])
if md5_hash(pw) == password:
authenticated = True
return authenticated
else:
return authenticated
def get_corpus_pos():
# sec 1 of corpus = list of words tagged w/pos
# so it'll be a dictionary with the key == POS, value == list of words
corpus = "Part of Speech, Words \n"
tag_list = get_list('tags')
for tag in tag_list:
key = 'tagged_words_%s'%tag
# returns ['word_(word)','word_(diff word)']
tagged_words = get_list(key)
# returns '(pos)' or '(diff pos)'
tag_name = tag.lstrip('tag_')
word_list = ""
if len(tagged_words) > 0:
for word_word in tagged_words:
# returns '(word)'
word = word_word.lstrip('word_')
word_list += "%s,"%word
corpus += "%s,%s \n"%(tag_name, word_list)
return corpus
def get_corpus_words():
# sec 2 of corpus = list of POS for words
# so it'll be a dict with the key == word, value == list of POS
corpus = "Word, Parts of Speech \n"
word_list = get_list('words')
for word_word in word_list:
key = 'final_tag_%s'%word_word
# returns ['tag_(pos)', 'tag_(diff pos)']
tag_list = get_list(key)
# returns '(word)'
word = word_word.lstrip('word_')
pos_list = ""
if len(tag_list) > 0:
for tag_tag in tag_list:
# returns '(tag)'
tag = tag_tag.lstrip('tag_')
pos_list += "%s,"%tag
corpus += "%s,%s \n"%(word, pos_list)
return corpus
def get_pos():
# returns list of tags ['tag_NP', 'tag_V', etc]
tag_list = get_list('tags')
tags = []
for tag in tag_list:
# returns ['(pos)','(pos)']
pos = tag.lstrip('tag_')
tags.append(pos)
tags.sort()
return tags
def get_words():
# returns list of words ['word', 'glvod', etc]
word_list = get_list('words')
words = []
for word_word in word_list:
key = 'final_tag_%s'%word_word
tag_list = get_list(key)
if len(tag_list) > 0:
word = word_word.lstrip('word_')
words.append(word)
words.sort()
return words
def get_tags_by_word(word):
key = 'final_tag_word_%s'%word
# returns list of tags ['tag_NP', 'tag_V', etc]
tag_list = get_list(key)
tags = []
for tag_item in tag_list:
tag = tag_item.lstrip('tag_')
tags.append(tag)
tags.sort()
return tags
def get_words_by_tag(tag):
key = 'tagged_words_%s'%tag
# returns list of words ['word_gvold', 'word_blah', etc]
word_list = get_list(key)
words = []
for word_item in word_list:
word = word_item.lstrip('word_')
words.append(word)
words.sort()
return words
#### Game Functions ####
def get_words_tweets_game():
# get sentences and tags assoc - done
# get random word from 'words', then get 'word'
# also get tweets associated with that word
# choose random tweet to return
# also return tweet list, just in case
# returns list of words ['word_glvod', 'word_cruls', etc]
words = get_list('words')
word_list = []
for word_word in words:
# returns '(word)'
word = word_word.lstrip('word_')
# word_list will look like ['word', 'diff word', etc]
word_list.append(word)
random_num = random.randint(0, (len(word_list)-1))
word_for_game = word_list[random_num]
word_word_tweets = 'word_%s_tweets'%word_for_game
tweet_list = get_list(word_word_tweets)
random_num_again = random.randint(0, len(tweet_list)-1)
tweet_for_game = tweet_list[random_num_again]
return word_for_game, tweet_for_game, tweet_list
def get_pos_sentences():
sentences = get_list('sentences')
list_of_sents = []
for sent_pos_tag in sentences:
tag_and_sentences = get_list(sent_pos_tag)
list_of_sents.append(tag_and_sentences)
# list_of_sents looks like [['tag_(POS)', "sent 1", "sent2"], ['tag_(diff POS)', 'sent1', 'sent2']]
return list_of_sents
def break_pos_sents(list_of_sents):
pos_sentences = []
for pos in range(len(list_of_sents)):
random_index = random.randint(1, len(list_of_sents[pos][1:])-1)
sentence = list_of_sents[pos][random_index]
tag = list_of_sents[pos][0]
pos_sentences.append((tag, sentence))
return pos_sentences
def get_another_tweet(word, tweet):
# word and tweet must string
word_word_tweets = 'word_%s_tweets'%word
tweet_list = get_list(word_word_tweets)
return tweet_list
def tag_word_game(word, pos, user, tweet):
# pos comes in as tag_(POS)
# tags a word from the game with tag_POS
if pos != 'tag_U':
# tag_word_(word)_tag_(POS)
user_name = 'user_%s'%user
add_tag_word_tag_pos(word, pos, user_name)
# tweet_tag_word_(word)_tag_(POS)
add_tweet_tag_word_tag_pos(word, pos, [tweet])
# user_(name)_words_tagged
tag_word_tag_pos = 'tag_word_%s_%s'%(word, pos)
add_user_words_tagged(user, [tag_word_tag_pos])
def add_pts_game(word, pos, user):
# word, pos, and user must be strings
# adds 10 pts to every user once tag verified 5x
# adds 5 pts to every new user who continues to verify tag thereafter
# also adds words to their final tags if verified
if pos != 'U':
tag_word_word_tag_pos = "tag_word_%s_%s"%(word, pos)
users_tagged_as_pos = get_list(tag_word_word_tag_pos)
current_user = 'user_%s'%user
if len(users_tagged_as_pos) == 5 and current_user not in users_tagged_as_pos:
# give each user 10 pts
for user_name in users_tagged_as_pos:
user_name_pts = "%s_pts"%user_name
add_user_pts(user_name_pts, 10)
word_word = "word_%s"%word
word_list = [word_word]
tag_tag = "%s"%pos
tag_list = [tag_tag]
add_tagged_words_pos(pos, word_list)
add_final_tag(word, tag_list)
elif len(users_tagged_as_pos) > 5:
# give each user 5 pts
for user_name in users_tagged_as_pos:
user_name_pts = "%s_pts"%user_name
add_user_pts(user_name_pts, 5)
tag_tag = "%s"%pos
tag_list = [tag_tag]
add_final_tag(word, tag_list)
word_word = "word_%s"%word
word_list = [word_word]
add_tagged_words_pos(pos, word_list)
else:
# don't give them any points if not verified yet
pass
def get_top_scores():
# gets the top 25 scores, in order (highest to lowest)
points_list = get_list('points')
top_list = []
if len(points_list) > 25:
# create the first 25 points to compare all other points to
for user_name_pts in points_list[:25]:
their_pts = int(get_string_num(user_name_pts))
user_name = user_name_pts.rstrip('pts').lstrip('user').rstrip('_').lstrip('_')
top_list.append((their_pts, user_name))
top_list.sort()
top_list.reverse()
# looking at the sorted list from highest to lowest
for user_name_pts in points_list[25:]:
their_pts = int(get_string_num(user_name_pts))
num_times_added = 0
for scores in top_list:
if their_pts > scores[0] and num_times_added == 0:
user_name = user_name_pts.rstrip('pts').lstrip('user').rstrip('_').lstrip('_')
if user_name != scores[1]:
top_list.append((their_pts, user_name))
num_times_added += 1
top_list.sort()
top_list.reverse()
if len(top_list) > 25:
top_list = top_list[:25]
return top_list
else:
return top_list
else:
# if the len(points_list) <= 25
for user_name_pts in points_list:
their_pts = int(get_string_num(user_name_pts))
user_name = user_name_pts.rstrip('pts').lstrip('user').rstrip('_').lstrip('_')
top_list.append((their_pts, user_name))
top_list.sort()
top_list.reverse()
return top_list
#### End Game Functions ####
#### Visualization Functions ####
def final_tags_pos(pos):
# gets the words tagged with pos, & num tweets each word appears in where it is tagged with that pos
# pos must be string
words_and_length_list = []
tagged_words_tag_pos = "tagged_words_tag_%s"%pos
# get list of words (format: 'word_(word)') from tagged_words_tag_pos
tagged_words = get_list(tagged_words_tag_pos)
for word_word in tagged_words:
# get number of tweets word is in
tweet_tag_word_word_tag_pos = 'tweet_tag_%s_tag_%s'%(word_word, pos)
num_tweets = get_num_list(tweet_tag_word_word_tag_pos)
word = word_word.lstrip('word_')
words_and_length_list.append((word, num_tweets))
return words_and_length_list
#### End Visualization Functions ####
#### Begin Keen IO Functions ####
def get_num_tagged(user):
#gets the number of words tagged by user (given as string)
key = 'user_%s_words_tagged'%user
num_tagged = r_server.llen(key)
return num_tagged
def get_num_final_tagged(user):
#gets the number of words given final tags by user (given as string)
key = 'user_%s_words_tagged'%user
num_tagged = r_server.lrange(key, 0, -1)
num_final_tagged = 0
for tag_word_word_tag_pos in num_tagged:
split_key = tag_word_word_tag_pos.split('_')
word = split_key[2]
tag_pos = "tag_%s"%split_key[4]
key = 'final_tag_word_%s'%word
if r_server.exists(key) == True:
final_tags = r_server.lrange(key, 0, -1)
if tag_pos in final_tags:
num_final_tagged += 1
return num_final_tagged
#### End Keen IO Functions ####
#### End Flask Uses ####
def main():
pass
if __name__ == "__main__":
main()