-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.py
213 lines (182 loc) · 5.75 KB
/
tmp.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
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import app.parser.getData as importArticles
import app.parser.articleRetrieval.getArticles as getContent
import app.parser.sentences as sent
import app.parser.getChunks as gc
import app.analytics.tag as tag
import app.parser.articleRetrieval.wikipediaParse as wp
import app.analytics.features as fe
import app.analytics.functions.hasDate as hd
import app.analytics.filterSentences as fl
from sklearn import tree, feature_extraction, svm
from sklearn.feature_extraction.text import CountVectorizer
from multiprocessing import Pool
import numpy as np
import datetime
import networkx as nx
import matplotlib.pyplot as plt
np.seterr(divide='ignore',invalid='ignore')
trainArticles= open('data/singleShort.txt','r').readlines()#=importArticles.getData('train')
testArticles = open('data/singleShortTest.txt','r').readlines()#= importArticles.getData('test')
print len(trainArticles)
print len(testArticles)
listOfYears = []
testArticleLookupDict = {}
for title in range(0,len(testArticles)):
#print (eval(testArticles[title])['title'])
testArticleLookupDict[eval(testArticles[title])['title']] = title
clf = svm.SVC(probability=True)#tree.DecisionTreeClassifier()
titles = []
weights = []
G = nx.Graph()#G is an empty graph
#A
def getArticle(article):
singleSets = []
try:
chunks = gc.getChunks(article[1])
tags = tag.getTags(article[1],chunks)
#if tags == []:
# continue # check this is right. go to next itteration
"""The Stanford Open IE tags"""
subject = tags['subject']
relation = tags['relation']
objects = tags['object']
objects = objects.split()
content = wp.getArticle(subject)
rawSentences = sent.getSentences(content)
sentences = []
for sentence in rawSentences:
if(hd.hasDate(sentence) != []):
sentences.append(sentence)
listOfYears.append(article[0])
SS = {'title':article[1], 'sentences':sentences, 'year':article[0]}
singleSets.append(SS)
except:
pass
return singleSets
#B
def generateTrainDataPoints(tpl):
X = tpl[0]
Y = tpl[1]
doubleSets = []
I = eval(trainArticles[X])
J = eval(trainArticles[Y])
sentencesI = fl.filter(I['sentences'],I['title'])
sentencesJ = fl.filter(J['sentences'],J['title'])
if(I['year'] < J['year']):
b = 1
else:
b = 0
val = ({'title1':I['title'],'sentences1':sentencesI,\
'title2':J['title'],'sentences2': sentencesJ,\
'year':b, 'vocab':set(sentencesI + sentencesJ)})
return val
def generateTestDataPoints(tpl):
X = tpl[0]
Y = tpl[1]
doubleSets = []
I = eval(testArticles[X])
J = eval(testArticles[Y])
if(I['year'] < J['year']):
b = 1
else:
b = 0
val = ({'title1':I['title'],'sentences1':I['sentences'],\
'title2':J['title'],'sentences2': J['sentences'],\
'year':b, 'vocab':set(I['sentences'] + J['sentences'])})
return val
def getFeature(item):
yr = item['year']
vec = fe.get(item['sentences1'],item['sentences2'])
titles = ([item['title1'],item['title2']])
return ([vec,titles,yr])
#C
def train(features):
X = [item[0] for item in features]
Y = [item[2] for item in features]
clf.fit(X,Y)
def test(features):
correct = 0
for feature in features:
predict = clf.predict(np.array([feature[0]]))
prob = clf.predict_proba(np.array([feature[0]]))
prob = prob[0][predict][0]
title1 = feature[1][0]
title2 = feature[1][1]
#print "title1 = " + str(title1)
#print prob
#print str(title1) + "," + str(title2) + "," + str(float(prob))
G.add_edge(str(title1),str(title2), weight=float(prob))
if(feature[2] == predict):
correct +=1
print "Accuracy = " + str(correct) + '/' + str(len(features))
#print datetime.datetime.now()
p = Pool(10)
#Used to get Article Content
#articles = (p.map(getArticle,trainData))
mapping = []
for i in range(len(trainArticles)):
for j in range(i+1, len(trainArticles)):
mapping.append([i,j])
print datetime.datetime.now()
doubleSets = p.map(generateTrainDataPoints,mapping)
print datetime.datetime.now()
trainFeatures = p.map(getFeature,doubleSets)
print datetime.datetime.now()
train(trainFeatures)
print datetime.datetime.now()
print "Training Complete. Now For Testing"
mapping = []
for i in range(len(testArticles)):
for j in range(i+1, len(testArticles)):
mapping.append([i,j])
print datetime.datetime.now()
doubleSets = p.map(generateTestDataPoints,mapping)
print datetime.datetime.now()
testFeatures = p.map(getFeature,doubleSets)
print datetime.datetime.now()
test(testFeatures)
print datetime.datetime.now()
print datetime.datetime.now()
Data = []
for t in testArticles:
Data.append(eval(t))
def graph(ttl):
T = nx.bfs_tree(G,ttl)
edgeAccuracy = 0
years = []
for e in T.edges():
t1 = e[0]
t2 = e[1]
y1 = 0
y2 = 0
for d in Data:
if(d['title'] == t1):
y1 = d['year']
if(d['title'] == t2):
y2 = d['year']
#print e
#print str(y1) + " | " + str(y2)
if(y1 > y2):
edgeAccuracy += 1
if(years == []):
years.append(y1)
years.append(y2)
else:
years.append(y2)
yearsAccuracy = 0
for y in range(1,len(years)):
if( years[y] > years[y-1]):
yearsAccuracy +=1
#print(T.edges())
#print years
#print "edge Accuracy = " + str(edgeAccuracy)
print ttl + " = " + str(yearsAccuracy)
#for d in Data:
# graph(d['title'])
#nx.draw(G)
#nx.dfs_edges(G)
#plt.show()
#test(generateDataPoints(testArticles))