-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeClassifierFile.py
More file actions
78 lines (65 loc) · 2.07 KB
/
Copy pathmakeClassifierFile.py
File metadata and controls
78 lines (65 loc) · 2.07 KB
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
#!/usr/local/bin/python
import sys
import json
def getJSON(fileName):
f = open(fileName)
jsonData = json.load(f)
f.close()
return jsonData
def getLabel(articleReturn):
if articleReturn > 0.05:
return "Really Good"
elif articleReturn > 0.01:
return "Good"
elif articleReturn > -0.01:
return "OK"
elif articleReturn > -0.05:
return "Bad"
else:
return "Really Bad"
def removeTags(text, openTag, closeTag):
flag = True
while openTag in text and closeTag in text and flag:
start = text.find(openTag)
end = text.find(closeTag)
if start < end:
text = text[:start] + text[end+1:]
else:
flag = False
return text.replace(openTag, '').replace(closeTag, '')
def cleanText(text):
return ' '.join(removeTags(removeTags(text, '<', '>'), '{', '}').replace("&", "and").split())
def writeClassifier(jsonData, fileName):
f = open(fileName, 'w')
f.write("<dataset>\n")
size = len(jsonData.keys())
for article in jsonData.keys():
articleReturn = jsonData[article]
articleText = cleanText(article.strip().encode('utf8'))
f.write('\t<item label="' + getLabel(articleReturn) + '">\n')
f.write("\t\t<content>" + articleText + "</content>\n")
f.write("\t</item>\n")
f.write("</dataset>\n")
f.close()
def splitData(jsonData, testPercentage):
trainData = {}
testData = {}
trainPercentage = 1.0 - testPercentage
count = 0
size = float(len(jsonData.keys()))
for article in jsonData.keys():
percentage = count / size
if percentage < trainPercentage:
trainData[article] = jsonData[article]
else:
testData[article] = jsonData[article]
count += 1
return (trainData, testData)
inputFile = sys.argv[1]
trainFile = sys.argv[2]
testFile = sys.argv[3]
testPercentage = float(sys.argv[4])
jsonData = getJSON(inputFile)
trainData, testData = splitData(jsonData, testPercentage)
writeClassifier(trainData, trainFile)
writeClassifier(testData, testFile)