-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_random.py
68 lines (48 loc) · 1.58 KB
/
create_random.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
'''
Created on 30. avg. 2012
@author: matic
'''
import random,string
from math import log
from collections import defaultdict
def generate_random(prefix,words_per_doc):
#ROW ORDERING
if 1==1:
mig_mag_file=open(prefix+"all/documents.lndoc", 'r')
#hevristic_scores="all/HevristicsScores.txt"
every=5
line = mig_mag_file.readline() # Invokes readline() method on file
trains_text={}
trains_class={}
i=0
count=0
while line:
if line!="\n" and i%every==0:
spl=line.split("\t")
print spl
trains_class[count]=spl[1]
trains_text[count]=spl[2].split("\n")[0].split(" ")
#print i,spl[0]
count+=1
i+=1
line = mig_mag_file.readline()
#print "row_perm:",row_perm_rev
mig_mag_file.close()
#TF-IDF and BOW
words = set()
tf_idfs = {}
for train in trains_text.keys():
for word in trains_text[train]:
words.add(word)
word_count=defaultdict(int)
for train_words in trains_text.values():
for word in set(train_words):
word_count[word]+=1
out_file = open(prefix+"random"+str(words_per_doc)+"/documents.lndoc", "wb")
#f = open('trains.tab','w')
for i in range(len(trains_class)):
out_file.write(str(i+1)+"\t"+(random.choice(["MIG","MAG"]))+"\t"+string.join(random.sample(words,words_per_doc)," ")+"\n")
out_file.close()
# print "writting to file",len(sorted_words)
return "random"+str(words_per_doc)
#generate_random(5)