forked from weichenzhao/CS544_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
117 lines (95 loc) · 2.54 KB
/
preprocess.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
import sys
import json
import pickle
import string
import nltk
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA
from sklearn.decomposition import SparsePCA
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from sklearn.metrics import f1_score
tag_dict = list()
seg_dict = dict()
tags = list()
body = list()
code = list()
title = list()
seg_dict_test = dict()
tags_test = list()
body_test = list()
code_test = list()
title_test = list()
def readTag(tagFile):
# print("reading Tag:")
with open(tagFile) as f:
for tag in f:
if len(tag_dict) >= 100:
break
tag_dict.append(tag.split()[0])
# print(tag_dict)
def readFile(trainFile, testFile):
# print("reading trainging file:")
tag = list()
# for f in trainFile:
with open(trainFile) as json_data:
seg_dict = json.load(json_data)
for i in seg_dict:
flag = 0;
ts = seg_dict[i][3].split(" ")
tag = list()
for t in ts:
if t in tag_dict:
tag.append(tag_dict.index(t))
flag = 1
if flag == 1:
tags.append(tag)
title.append(seg_dict[i][0])
body.append(seg_dict[i][1])
code.append(seg_dict[i][2])
# print("reading test file: ")
with open(testFile) as json_data:
seg_dict_test = json.load(json_data)
for i in seg_dict_test:
flag = 0;
ts = seg_dict_test[i][3].split(" ")
tag = list()
for t in ts:
if t in tag_dict:
tag.append(tag_dict.index(t))
flag = 1
if flag == 1:
tags_test.append(tag)
title_test.append(seg_dict_test[i][0])
body_test.append(seg_dict_test[i][1])
code_test.append(seg_dict_test[i][2])
print("###################### test tags: #########################")
print(tags_test)
pkl_file = open('input_20k_100.pkl', 'wb')
data = [tags, body, code, title, tags_test, body_test, code_test, title_test, tag_dict]
pickle.dump(data, pkl_file)
pkl_file.close()
# print(tags)
# print(body)
# print(code)
# print(title)
##################################################### main ##################################################
def main(argv):
tagFile = argv[1]
trainFile = argv[2]
testFile = argv[3]
print("train file: ", trainFile, "")
print("test file: ", testFile, "")
readTag(tagFile)
readFile(trainFile, testFile)
# tf_idf_body()
# tf_idf_code()
# BOW_body()
# BOW_code()
# predict_title()
if __name__ == '__main__':
main(sys.argv)