-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDoubleSets.py
More file actions
155 lines (139 loc) · 4.17 KB
/
getDoubleSets.py
File metadata and controls
155 lines (139 loc) · 4.17 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
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
#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
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 app.analytics.filterSentences as fl
import matplotlib.pyplot as plt
#import networkx as nx
G = {}
np.seterr(divide='ignore',invalid='ignore')
trainArticles= eval(open('bigSingleSets','r').readlines()[0])#=importArticles.getData('train')
testArticles= eval(open('bigSingleSets','r').readlines()[0])[2000:2050]#=importArticles.getData('train')
listOfYears = []
clf = svm.SVC(probability=True)
probs = []
titles = []
#A
#B
def generateTrainDataPoints(tpl):
X =tpl[0]
Y = tpl[1]
doubleSets = []
I = (trainArticles[X])
J = (trainArticles[Y])
if I is None or J is None:
return
try:
#sentencesI = I[#fl.filter(I['sentences'],I['title'])
#sentencesJ = #fl.filter(J['sentences'],J['title'])
#print type(sentencesI)
if(I['year'] < J['year']):
b = 1
else:
b = 0
val = ({'title1':I['title'],'sentences1':I['sentences'],'date1':I['year'],\
'title2':J['title'],'sentences2': J['sentences'],'date2':J['year'],\
'year':b, 'vocab':set(I['sentences'] + J['sentences'])})
return val
except:
return
def generateTestDataPoints(tpl):
X = tpl[0]
Y = tpl[1]
doubleSets = []
I = eval(testArticles[X])
J = eval(testArticles[Y])
#sentencesI = fl.filter(I['title'],I['title'])
#sentencesJ = fl.filter(J['title'],J['title'])
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):
if item is None:
return
yr = item['year']
vec = fe.get(item['sentences1'],item['sentences2'])
titles = ([item['title1'],item['title2']])
# print [vec,titles,yr]
return ([vec,titles,yr,item['date1'],item['date2']])
#C
p = Pool(30)
#Used to get Article Content
#2articles = (p.map(getArticle,trainData))
mapping = []
for i in range(len(trainArticles)):
for j in range(i,len(trainArticles)):
mapping.append([i,j])
doubleSets = p.map(generateTrainDataPoints,mapping)
#print "making train features"
#print len(doubleSets)
#print "finished Doubles"
#for item in doubleSets:
# if item is not None:
# getFeature(item)
trainFeatures = p.map(getFeature,doubleSets)
print (trainFeatures)
#print trainFeatures
#train(trainFeatures)
#print datetime.datetime.now()
#train(generateDataPoints(trainArticles))
#print "Training Complere. Now For Testing"
"""
mapping = []
for i in range(0,len(testArticles)):
for j in range(i+1,len(testArticles)):
mapping.append([i,j])
doubleSets = p.map(generateTestDataPoints,mapping)
testFeatures = p.map(getFeature,doubleSets)
#test(testFeatures)
#print datetime.datetime.now()
"""
"""
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not graph.has_key(start):
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
keys = G.keys()
start = [None,0]
for k in keys:
before = len(G[k])
if before > start[1]:
start = [k,before]
print datetime.datetime.now()
maxPath = []
for k in range(len(keys)):
newPaths = find_all_paths(G,start[0],keys[k])
for path in newPaths:
if( len(path) >= len(maxPath)):
maxPath = path
print maxPath
print len(maxPath)
print datetime.datetime.now()
print G
#nx.draw_networkx(G,with_lables=True)
#plt.show()"""