-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.py
138 lines (114 loc) · 3.86 KB
/
solver.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
import itertools
from collections import defaultdict
import networkx as nx
import numpy as np
def validModels(world):
return {"validworlds":1}
def featureConsistentTemplates(world):
features = dict()
consistDict = dict()
for a,feat in world:
words = ','.join([f.word for f in feat])
temp = feat[0].templateNumber
if words in consistDict and consistDict[words] != temp:
features[words] = -1
elif words not in consistDict:
consistDict[words] = temp
features[words] = temp
out = {}
helper(features, out, 1)
helper(features, out, 2)
helper(features, out, 3)
return out
def featurePercentagesTemplates(world):
features = dict()
templateCounts = dict()
for a,feat in world:
words = ','.join([f.word for f in feat])
template = feat[0].templateNumber
if words in templateCounts:
templateCounts[words][template] += 1
else:
templateCounts[words] = [0,0,0]
templateCounts[words][template] += 1
for words,counts in templateCounts.iteritems():
total = sum(counts)
for i in range(len(counts)):
features["perc_%s,%s" % (words, str(i))] = float(counts[i]) / total
return features
def featureMajorityTemplates(world):
features = dict()
templateCounts = dict()
for a,feat in world:
words = ','.join([f.word for f in feat])
template = feat[0].templateNumber
if words in templateCounts:
templateCounts[words][template] += 1
else:
templateCounts[words] = [0,0,0]
templateCounts[words][template] += 1
for words,counts in templateCounts.iteritems():
max = 0
maxI = 0
for i in range(len(counts)):
if counts[i] > max:
max = counts[i]
maxI = i
elif counts[i] == max and max > 0:
return {}
features["maj_%s,%s" % (words, str(maxI))] = 1
return features
def helper(features, out, r):
comb = itertools.combinations(features.keys(), r)
for wordPairs in comb:
consistent = True
for wordPair in wordPairs:
if features[wordPair] < 0:
consistent = False
break
if consistent:
out_list = []
for wordPair in wordPairs:
out_list.append(wordPair)
out_list.append(str(features[wordPair]))
key = ','.join(out_list)
out[key] = 1
# (a,b,c) -> (a:{{b,c}}), (b:{{a,c}}), (c:{{a,b}})
# (b,c,d) -> (b:{{c,d}}), (c:{{b,d}}), (d:{{b,c}})
def solver(sent_tuples, featureset):
stats = {}
globalStats = {}
#outs = [enum(x) for x in sent_tuples]
count = 1
for combination in itertools.product(*sent_tuples):
# print combination
# if count % 1000 == 0:
# print count
# count = count+1
G = nx.DiGraph()
for c in combination:
key = c[0][0]
val = c[0][1]
for v in val:
G.add_edge(key,v)
if c not in stats:
stats[c] = 0
try:
k = nx.simple_cycles(G).next()
except StopIteration:
for f in featureset:
feature = f(combination)
for k in feature:
if k in globalStats:
globalStats[k] += feature[k]
else:
globalStats[k] = feature[k]
for c in combination:
stats[c] = stats[c] + 1
return (stats, globalStats)
def enum(sent_tuple):
out = []
for i in range(len(sent_tuple)):
out.append((sent_tuple[i], set(sent_tuple[0:i]).union(set(sent_tuple[i+1:]))))
return out
# print solver([[("a",frozenset({"b","c"})),("c",frozenset({"b","a"}))],[("c",frozenset({"d","a"}))]])