/
GoodTuringEdit.py
executable file
·1135 lines (977 loc) · 45.6 KB
/
GoodTuringEdit.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#####THERE IS PROBABLY A BETTER WAY TO USE PANDAS DATAFRAME CELL VALUES AS STRINGS
##### SEE DEF FORMAT(LINE) FOR WORKAROUND
from __future__ import division
from operator import *
from collections import Counter
import csv
import nltk
from nltk.corpus import stopwords
from nltk import FreqDist
from nltk.stem import *
from nltk.corpus import wordnet as wn
import string
import gensim
import json
from gensim import models
import xlsxwriter
import pandas as pd
def func1():
return [["1", "2", "3", "4"],
["one", "two", "three", "four"]]
def func2():
return [["one", "two", "three", "four"],
["1", "2", "3", "4"]]
def func3():
return [["a", "b", "c", "d"],
["one", "two", "three", "four"]]
def func4():
return [["z", "y", "x", "w"],
["one", "two", "three", "four"]]
def getInputFile(fileName):#-------------------------------------GET_INPUT_FILE
#var dictionary
file = [] #file as a list of entries
#fileName #name of input file
#sortBy #what to sort the data by:
#0: ideaId
#1: raw idea
#2: userId
#3: unix time
#4: theme
#read each line and add to file
for line in csv.reader(open(fileName), skipinitialspace = True):
file.append(line)
return file
def get_fof(count):#----------------------------------------------------GET_FOF
#in: Counter containing frequency of element bins
#out: Counter containing the frequencies of frequencies of element bins
return Counter(map(itemgetter(1), count.items()))
def estimateNewIdea(count, line):#----------------------------ESTIMATE_NEW_IDEA
freq = get_fof(count) #Counter counting frequeny or frequencies
n1 = freq[1] #frequency of "1"
nc1 = freq[2] #frequency of 1+1
c = 1
n = float(sum(map(itemgetter(1), count.items()))) #total number of items
line.append(str((n1/n)*100))
#line.append(str((nc1 * (c+1))/n))
def calculateCat(count, output, timeSlice, new, sliceTotal):#-----CALCULATE-CAT
total = float(sum(map(itemgetter(1), count.items()))) #total number of items
overallNew = 0
line = output[timeSlice]
for x in range(timeSlice + 1):
if(x != 0):
overallNew += output[x][2]
#calculate per slice
perSlice = (new/sliceTotal)*100 #percent of new per slice
line.append(str(perSlice))
#calculate overall
overall = (overallNew/total)*100 #percent of new overall
line.append(str(overall))
def stem(unique):#---------------------------------------------------------STEM
stem = SnowballStemmer("english")
newList = list()
for word in unique:
newList.append(stem.stem(word))
return newList
def lem(unique):#-----------------------------------------------------------LEM
lem = WordNetLemmatizer()
newList = list()
for word in unique:
newList.append(lem.lemmatize(word))
return newList
def format(line):#-------------------------------------------------------FORMAT
stopwords = nltk.corpus.stopwords.words('english') #list of stopwords
useless = ["would", "could", "in", "use"];
real = list()
listOfWords = []
text = line["content"]
text = text.str.strip('"') #eliminate quotes
text = text.str.split() #cut stop words
for word in text:
for realWord in word:
listOfWords.append(realWord.lower())
real += [word for word in listOfWords if
word not in stopwords and
word not in useless]
text = sorted(real)
if(VERSION < 4):
line[1] = lem(line[1])
line[1] = stem(line[1])
text = lem(text)
return text
#def createScatterPlot(datasets, methods, thresholds):
# workbook = xlsxwriter.Workbook('Data Output/ExcelTesting.xlsx')
# worksheet = workbook.add_worksheet()
# chart5 = workbook.add_chart({'type': 'scatter',
# 'subtype':'smooth'})
# chart5.add_series({
# 'name': 'test',
# 'categories
# })
#for set in datasets:
# for method in methods:
# for threshold in thresholds:
# i = 1
# chart5.add_series({
# 'name':
# 'categories': "=Sheet1!$" + i + "
# })
def nSim(inputSyn, comparisons):
avg = 0
for existing in comparisons:
existingSyn = existing
#get synset of existingSyn
if(wn.synsets(existing)):
existingSyn = wn.synsets(existing)[0]
elif(wn.morphy(existing)):
existingSyn = wn.morphy(existing)[0]
else:
#no synset; skip
continue
#compare input and existing
sim = existingSyn.path_similarity(inputSyn)
if(sim == None):
sim = 0
avg = (avg + sim)/2
return avg
def getCombos(groupList):
combos = set()
print "START"
x = 0
while(x < len(groupList) - 1):
print "\tX: ", x
word1 = groupList[x]
y = 1
while(y < len(groupList)):
print "\tY: ", y
word2 = groupList[y]
print "\tWord: ", word1
print "\tNextWord: ", word2
if(word1 == word2):
print "\tequal"
else:
c = word1 + "-" + word2
combos.update([c])
print "\tCombo: ", c
y = y + 1
x = x + 1
return combos
def getGroup(count, word, threshold, wordsSeen, groups):
word = "".join(l for l in word if l not in string.punctuation)
best = 0
group = word
#searchForExisting
if(wordsSeen.has_key(word)):
return wordsSeen.get(word)
#get synset of word
if(wn.synsets(word)):
wordSyn = wn.synsets(word)[0]
elif(wn.morphy(word)):
wordSyn = wn.morphy(word)[0]
else:
#no synset; use word
wordsSeen.update({word: group})
if(groups.has_key(group)):
newValue = groups.get(group)
newValue.update([word])
groups.update({group: newValue})
else:
newValue = set()
newValue.update([word])
groups.update({group: newValue})
wordsSeen.update({word: group})
return word
#compare to each group
# is there a way to compare one word to many words?
for super_word in count.keys():
#get synset of group being tested against
comparisons = groups.get(super_word)
sim = nSim(wordSyn, comparisons)
if(sim >= threshold and sim > best):
group = super_word
best = sim
wordsSeen.update({word: group})
if(groups.has_key(group)):
newValue = groups.get(group)
newValue.update([word])
groups.update({group: newValue})
else:
newValue = set()
newValue.update([word])
groups.update({group: newValue})
wordsSeen.update({word: group})
return group
def getGroup2(count, word):
#creates a dictionary/hash table of synonyms
#nltk version
#later try PyDictionary
word = "".join(l for l in word if l not in string.punctuation)
group = word
#search hash table for existing entry (word)
if(wordsSeen.has_key(word)):
return wordsSeen.get(word)
#get synset
if(wn.synsets(word)):
wordSyn = wn.synsets(word)
elif(wn.morphy(word)):
wordSyn = wn.morphy(word)
else:
#no synset; use word
wordsSeen.update({word: group})
return word
#not entirly accurate
#if a synonym is found, update group to that value
#problem1: multiple synonyms may exist with differnt values
#which to use?
#update them to be the same?
#problem2: synonymns' values to update to may change halfway through
#search in a separate loop and then update? --> problem 1 still an issue, but
#maybe less so if synonymns usually don't have different values
for syn in wordSyn:
for lem in syn.lemmas():
if(wordsSeen.has_key(lem.name())):
group = wordsSeen.get(lem.name())
else:
wordsSeen.update({lem.name(): group})
return group
#if not found:
#search english dictionary
#if found in english dictionary:
#add word and all synonymns with word as value
#return word
#if not found in english dictionary:
#stub: print --> see what comes out and then decide
#throw away? --> probably a typo
#return word? --> lots of words are not in the dictionary
def getGroup3(count, word, model, threshold, wordsSeen, groups):
word = "".join(l for l in word if l not in string.punctuation)
best = 0
group = word
#print word
#searchForExisting
if(wordsSeen.has_key(word)):
return wordsSeen.get(word)
for super_word in count.keys():
comparisons = groups.get(super_word) # assume that comparisons is an array of words that make up the super_word
try:
#print "\t", item, " vs. ", word
#sim = model.similarity(item, word)
sim = model.n_similarity([word], comparisons)
#print "\t\t", sim
except:
continue
if(sim >= threshold and sim > best):
#if this word is similar to an already existing one, add it as that group
group = super_word
best = sim
#print "\tBest: ", best
#print "\tGroup: ", group
if(groups.has_key(group)):
newValue = groups.get(group)
newValue.update([word])
groups.update({group: newValue})
else:
newValue = set()
newValue.update([word])
groups.update({group: newValue})
wordsSeen.update({word: group})
return group
def countUniqueWords(file):
stopwords = nltk.corpus.stopwords.words('english')
useless = ["would", "could", "in", "use"]
listOfWords = []
count = Counter()
newIdeaCount = 0
for line in file:
words = line[1].strip('"')
words = line[1].split()
listOfWords.append([w.lower() for w in words if
w.lower() not in stopwords and
w.lower() not in useless])
listOfWords = sorted(listOfWords)
for word in listOfWords:
group = getGroup2(count, word)
oldCount = count[group]
count[group] += 1
newCount = count[group]
if newCount == 1: #was this a new bin?
newIdeaCount += 1
print "# of new Ideas: ", newIdeaCount
def evaluate(file):#---------------------------------------------------EVALUATE
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
file.sort(key=itemgetter(2))
if(INTERVAL_MODE == 'time'):
start = int(file[0][2]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
output.append(["Time Slice", #file header
"New Category?",
"# of New Categories",
"Total Ideas in Time Slice",
"Probability of New Bin",
"%New Categories in Time Slice",
"%New Categories Overall",
"Counter"])
while(i < len(file)): #while more ideas still exist
line = file[i] #a line in a file
if(INTERVAL_MODE == 'time'):
current = int(line[2])
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
oldCount = count[line[3]]
count[line[3]] += 1 #add to counter
newCount = count[line[3]]
if newCount == 1: #was this a new bin?
newIdea = 'true' #if yes,
newIdeaCount += 1
totalIdeas += 1
i += 1 #increment while loop
else: #at the end of each time slice
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[timeSlice])
calculateCat(count, output, timeSlice, newIdeaCount, totalIdeas)
timeSlice += 1 #increment time slice id
start = end #update time slice markers
end = start + INTERVAL
newIdea = 'false'
newIdeaCount = 0
totalIdeas = 0
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output)
calculateCat(count, output, timeSlice, newIdeaCount, totalIdeas)
return output
def evaluate2(file):#-------------------------------------------------EVALUATE2
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
file.sort(key=itemgetter(3))
if(INTERVAL_MODE == 'time'):
start = int(file[0][3]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
output.append(["Time Slice", #file header
"New Category?",
"# of New Categories",
"Total Ideas in Time Slice",
"Probability of New Bin",
"%New Categories in Time Slice",
"%New Categories Overall",
"Counter"])
while(i < len(file)): #while more ideas still exist
line = file[i] #a line in a file
if(INTERVAL_MODE == 'time'):
current = int(line[3])
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
format(line) #convert idea content to list of relivant words
for word in line[1]:
oldCount = count[word]
count[word] += 1
newCount = count[word]
if newCount == 1: #was this a new bin?
newIdea = 'true' #if yes,
newIdeaCount += 1
totalIdeas += 1
i += 1
else: #at the end of each time slice
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[timeSlice])
calculateCat(count, output, timeSlice, newIdeaCount, totalIdeas)
timeSlice += 1 #increment time slice id
start = end #update time slice markers
end = start + INTERVAL
newIdea = 'false'
newIdeaCount = 0
totalIdeas = 0
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[timeSlice])
calculateCat(count, output, timeSlice, newIdeaCount, totalIdeas)
return output
def evaluate3_1(file, realOutput):#---------------------------------EVALUATE3.1
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
file.sort(key=itemgetter(3))
if(INTERVAL_MODE == 'time'):
start = int(file[0][3]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
realOutput.append(["Time Slice", #file header
"New Category?",
"# of New Categories",
"Total Ideas in Time Slice",
"Probability of New Bin",
"%New Categories in Time Slice",
"%New Categories Overall",
"Counter"])
while(i < len(file)): #while more ideas still exist
line = file[i] #a line in a file
output = list([]) #output array
if(INTERVAL_MODE == 'time'):
current = int(line[3])
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
format(line) #convert idea content to list of relivant words
for word in line[1]: #add to counter
oldCount = count[word]
count[word] += 1
newCount = count[word]
if newCount == 1: #was this a new bin?
newIdea = 'true' #if yes,
newIdeaCount += 1
totalIdeas += 1
i += 1
else: #at the end of each time slice
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output)
calculateCat(count, output, 0, newIdeaCount, totalIdeas)
timeSlice += 1 #increment time slice id
realOutput.append(output[0])
start = end #update time slice markers
end = start + INTERVAL
newIdea = 'false'
newIdeaCount = 0
totalIdeas = 0
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[0])
calculateCat(count, output, 0, newIdeaCount, totalIdeas)
def evaluate3(file):#-------------------------------------------------EVALUATE3
file.sort(key=itemgetter(4))
currentTheme = file[0][4] #sortBy category
ideasByTheme = []
output = []
for line in file:
themeName = line[4]
print "themeName:", themeName
print "currentTheme:", currentTheme
if (themeName == currentTheme):
print "append"
ideasByTheme.append(line)
else:
print "else"
ideasByTheme.sort(key=itemgetter(3))
output.append([currentTheme, "", "", "", "", ""])
print "EVALUATE"
evaluate3_1(ideasByTheme, output)
ideasByTheme = []
currentTheme = themeName
ideasByTheme.append(line)
if(len(ideasByTheme) > 0):
ideasByTheme.sort(key=itemgetter(3))
output.append([currentTheme, "", "", "", "", ""])
evaluate3_1(ideasByTheme, output)
ideasByTheme = []
currentTheme = themeName
ideasByTheme.append(line)
return output
def evaluate4(file, logs, method, threshold, dataset):#-------------------EVALUATE4
groupsDictOut = []
groupsDictOut.append(["Super_Word", "Value"])
wordsSeen = {}
groups = {}
itemsSeen = {}
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
model = models.Word2Vec.load("text8Model")
#file.sort(key=itemgetter(3))
logs.append(["--------------- START EXECUTION ---------------"])
if(INTERVAL_MODE == 'time'):
start = int(file[0][3]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
#output.append(["Time Slice", #file header
# "New Category?",
# "# of New Categories",
# "Total Ideas in Time Slice",
# "Probability of New Bin",
# "%New Categories in Time Slice",
# "%New Categories Overall",
# "Counter"])
logs.append([str(timeSlice) + "------ TIME_SLICE" + str(timeSlice) + " -----"])
while(i < len(file)+1): #while more ideas still exist
line = file[i:i+1] #a line in a file
if(INTERVAL_MODE == 'time'):
current = int(line["timeStamp"])###NOT TESTED!!!
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
line = format(line) #line becomes just array content rather than the whole line
logs.append(["\tIDEA " + str(i) + " -----"])
# print groupType
groupList = []
for word in line: #add to counter
if(method == "nltk"):
group = getGroup(count, word, threshold, wordsSeen, groups)
elif(method == "word2vec"):
group = getGroup3(count, word, model, threshold, wordsSeen, groups)
oldCount = count[group]
count[group] += 1
newCount = count[group]
#groupList.append(group)
if newCount == 1: #was this a new bin?
newIdea = 'true' #if yes,
newIdeaCount += 1
totalIdeas += 1
logs.append(["\t\t" + word + ":\t" + group + "--> \t" + str(newCount)])
for group in groupList:
print group
#COMBOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#groupList = getCombos(groupList)
#for combo in groupList:
# oldCount = count[combo]
# count[combo] += 1
# newCount = count[combo]
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# if(newCount == 1 and newIdea == "false"):
# newIdeaCount += 1
# logs.append(["\t\t" + str(line[1]) + ":\t" + combo + "--> \t" + str(newCount)])
i += 1
else: #at the end of each time slice
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[timeSlice - 1])
calculateCat(count, output, timeSlice - 1, newIdeaCount, totalIdeas)
timeSlice += 1 #increment time slice id
logs.append([str(timeSlice) + "------ TIME_SLICE" + str(timeSlice) + " -----"])
start = end #update time slice markers
end = start + INTERVAL
newIdea = 'false'
newIdeaCount = 0
totalIdeas = 0
if(totalIdeas > 0):
output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output[timeSlice -1])
calculateCat(count, output, timeSlice - 1, newIdeaCount, totalIdeas)
#print count
dictionary_to_save = "Dictionaries/Experiment/wordkey_%s_%s_%.2f" %(dataset, method, threshold)
dictionary_out = open(dictionary_to_save, 'w')
dictionary_out.write(json.dumps(wordsSeen, indent=2))
dictionary_out.close()
for key in groups.keys():
groupsDictOut.append([key, groups.get(key)])
writeOut(groupsDictOut, "Dictionaries/Experiment/groupkey_%s_%s_%.2f.csv" %(dataset, method, threshold))
return output
def evaluate5_1(file, realOutput, model):#---------------------------------EVALUATE5.1
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
file.sort(key=itemgetter(3))
if(INTERVAL_MODE == 'time'):
start = int(file[0][3]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
while(i < len(file)): #while more ideas still exist
line = file[i] #a line in a file
if(INTERVAL_MODE == 'time'):
current = int(line[3])
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
format(line) #convert idea content to list of relivant words
for word in line[1]: #add to counter
group = getGroup3(count, word, model)
oldCount = count[group]
count[group] += 1
newCount = count[group]
if newCount == 1: #was this a new bin?
newIdea = 'true' #if yes,
newIdeaCount += 1
totalIdeas += 1
i += 1
else: #at the end of each time slice
if(totalIdeas > 0):
#output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output)
#calculateCat(count, output, 0, newIdeaCount, totalIdeas)
timeSlice += 1 #increment time slice id
#realOutput.append(output[0])
for item in output:
realOutput.append(item)
start = end #update time slice markers
end = start + INTERVAL
newIdea = 'false'
newIdeaCount = 0
totalIdeas = 0
if(totalIdeas > 0):
#output.append([timeSlice, newIdea, newIdeaCount, totalIdeas])
estimateNewIdea(count, output)
#calculateCat(count, output, 0, newIdeaCount, totalIdeas)
def evaluate5(file, logs):#-------------------------------------------------EVALUATE5
file.sort(key=itemgetter(4))
currentTheme = file[0][4] #sortBy category
ideasByTheme = []
row= []
output = []
model = models.Word2Vec.load("MODEL")
for line in file:
themeName = line[4]
logs.append("themeName:" + themeName)
if (themeName == currentTheme):
ideasByTheme.append(line)
continue
else:
ideasByTheme.sort(key=itemgetter(3))
row.append(currentTheme)
#output.append([currentTheme, "", "", "", "", ""])
evaluate5_1(ideasByTheme, row, model)
output.append(row)
ideasByTheme = []
row = []
currentTheme = themeName
ideasByTheme.append(line)
continue
if(len(ideasByTheme) > 0):
ideasByTheme.sort(key=itemgetter(3))
#output.append([currentTheme, "", "", "", "", ""])
evaluate5_1(ideasByTheme, row, model)
output.append(row)
ideasByTheme = []
row = []
currentTheme = themeName
ideasByTheme.append(line)
return output
def evaluate6(file, logs):#-------------------------------------------------EVALUATE6
#datasets = ['SuperBoring',
# 'Boring',
# 'Normal',
# 'NewAtEnd',
# 'Exciting'] #the list of input filenames
datasets = ['ideas_remember_names_with_ratings']
# versions = [evaluate4(file, logs, "getGroup", 0.5)] #array of functions to try out
# evaluate4(file, logs, "getGroup", 0.9),
# evaluate4(file, logs, "getGroup3", 0.5),
# evaluate4(file, logs, "getGroup3", 0.9)]
#methods = ["nltk", "word2vec"]
methods = ["word2vec"]
# method_names = {"getGroup": 'nltk', 'getGroup3': 'word2vec'}
thresholds = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
#thresholds = [0.6]
vText = ['nltk 0.5','nltk 0.9', #strings associated with the function of
'word2vec 0.5','word2vec 0.9']
#"versions[]"
output = [] #output to be sent to csv
outLine = [] #one line of output taken from "returnedOut"
# i = 0
for dataset in datasets: #for each dataset
print "datasets: ", dataset
input_file = data = pd.read_csv("Input/%s.csv" %dataset)
if(dataset == "ideas_fabric_display_with_ratings_themes"):
for theme, theme_data in input_file.groupby("theme"):
print "theme: ", theme
for method in methods:
print "method: ", method
for threshold in thresholds:
print "threshold: ", threshold
#SORTBY TIME HERE -- NOT TESTED
#theme_data.sort("timeStamp")
returnedOut = evaluate4(theme_data, logs, method, threshold, dataset) #output from this version
for line in returnedOut: #rotates returned output to desired
#format and save to "output[]"
outLine = []
outLine.append(theme)
outLine.append("%s_%.2f" %(method, threshold))
outLine.append(line[0])
outLine.append(line[4])
outLine.append(line[5])
print "\t\t\t", outLine
output.append(outLine)
# j = j+1
# i = i+1
output_file = "Data Output/Experiment/%s_results.csv" %dataset
writeOut(output, output_file)
output = []
elif(dataset == "ideas_wedding_with_ratings" or
dataset == "ideas_fabric_display_with_ratings" or
dataset == "ideas_remember_names_with_ratings"):
output.append(["dataset", "method", #header
"timeSlice", "GT_predict", "new_TRUE"])
for author, author_data in input_file.groupby("authorID"):
print "author: ", author
for method in methods:
print "method: ", method
for threshold in thresholds:
print "threshold: ", threshold
#SORTBY TIME HERE -- NOT TESTED
#theme_data.sort("timeStamp")
returnedOut = evaluate4(author_data, logs, method, threshold, dataset) #output from this version
for line in returnedOut: #rotates returned output to desired
#format and save to "output[]"
outLine = []
outLine.append(author)
outLine.append("%s_%.2f" %(method, threshold))
outLine.append(line[0])
outLine.append(line[4])
outLine.append(line[5])
print "\t\t\t", outLine
output.append(outLine)
# j = j+1
# i = i+1
output_file = "Data Output/Experiment/%s_results_author.csv" %dataset
writeOut(output, output_file)
output = []
def errorLog(errorCode):#---------------------------------------------ERROR_LOG
if(errorCode == 0):
return "ERROR: NO VERSION SPECIFIED"
else:
return "THERE WAS AN ERROR"
def writeOut(output, fileName):#--------------------------------------WRITE_OUT
#var dictionary
#filename #file to write to
#output #data to write
#writer #csv writer object
with open(fileName, 'w') as file:
writer = csv.writer(file)
for row in output:
writer.writerow(row)
if(output == logs):
print "Log file written to ", fileName
else:
print "Results written to ", fileName
logs.append(["Results written to " + fileName])
def authorRealTime(file, logs, method, threshold, dataset):#-------------------EVALUATE4
groupsDictOut = []
groupsDictOut.append(["Super_Word", "Value"])
wordsSeen = {}
groups = {}
itemsSeen = {}
#var dictionary
#file #raw input file
#start #starting point for intervals
#end #end point for current interval
count = Counter() #counter for frequencies
timeSlice = 1 #timeSlice ID
newIdea = 'false' #true/false:
#new bin in the time slice?
newIdeaCount = 0 #number of new ideas so far
totalIdeas = 0 #total ideas per timeSlice
output = [] #output array
i = 0 #index for while loop
model = models.Word2Vec.load("text8Model")
#file.sort(key=itemgetter(3))
logs.append(["--------------- START EXECUTION ---------------"])
if(INTERVAL_MODE == 'time'):
start = int(file[0][3]) #unix time of first row
end = start + INTERVAL
elif(INTERVAL_MODE == 'count'):
start = 0
end = start + INTERVAL
#output.append(["Time Slice", #file header
# "New Category?",
# "# of New Categories",
# "Total Ideas in Time Slice",
# "Probability of New Bin",
# "%New Categories in Time Slice",
# "%New Categories Overall",
# "Counter"])
logs.append([str(timeSlice) + "------ TIME_SLICE" + str(timeSlice) + " -----"])
while(i < len(file)+1): #while more ideas still exist
newLine = file[i:i+1] #a line in a file
if(INTERVAL_MODE == 'time'):
current = int(newLine["timeStamp"])###NOT TESTED!!!
elif(INTERVAL_MODE == 'count'):
current = i
if(current <= end): #for each time slice
timeSliceData.append(newLine)
i = i+1
else:
#runAuthor()
for line in timeSliceData:
line = format(line) #line becomes just array content rather than the whole line
logs.append(["\tIDEA " + str(i) + " -----"])
# print groupType
groupList = []
for word in line: #add to counter
if(method == "nltk"):
group = getGroup(count, word, threshold, wordsSeen, groups)
elif(method == "word2vec"):
group = getGroup3(count, word, model, threshold, wordsSeen, groups)
oldCount = count[group]
count[group] += 1
newCount = count[group]
#groupList.append(group)
if newCount == 1: #was this a new bin?