-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_implication.py
More file actions
145 lines (127 loc) · 4.18 KB
/
test_implication.py
File metadata and controls
145 lines (127 loc) · 4.18 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
import sys
import random
# attrib_names = [ 'class','a1','a2','a3','a4','a5','a6' ]
attrib_names = [
'top-left-square',
'top-middle-square',
'top-right-square',
'middle-left-square',
'middle-middle-square',
'middle-right-square',
'bottom-left-square',
'bottom-middle-square',
'bottom-right-square',
'class'
]
def make_intent(example):
global attrib_names
return set([i+':'+str(k) for i, k in zip(attrib_names, example)])
cv_res = {
"positive_positive": 0,
"positive_negative": 0,
"negative_positive": 0,
"negative_negative": 0,
"contradictory": 0,
"total": 0,
}
def check_intersect(context_plus, context_minus, example, num_sub=1):
global cv_res
pos = 0
neg = 0
intent = make_intent(example)
for i in xrange(num_sub):
t = set(random.sample(example, random.randrange(len(intent))))
for j in context_plus:
if t.issubset(j):
pos += len(t)
for k in context_minus:
if t.issubset(k):
neg += len(t)
def score(pos, neg):
return pos * 1. / (neg + 1)
threshold = 1.1
if score(pos, neg) > threshold:
if example[-1] == 'positive':
cv_res['positive_positive'] += 1
else:
cv_res['negative_positive'] += 1
elif score(neg, pos) > threshold:
if example[-1] == 'positive':
cv_res['positive_negative'] += 1
else:
cv_res['negative_negative'] += 1
else:
cv_res['contradictory'] += 1
def check_hypothesis(context_plus, context_minus, example):
global cv_res
eintent = make_intent(example)
big_context = context_plus + context_minus
labels = {}
for e in big_context:
ei = make_intent(e)
candidate_intent = ei & eintent
if not candidate_intent:
continue
# print 'candidate_intent'
# print candidate_intent
closure = [make_intent(i) for i in big_context
if make_intent(i).issuperset(candidate_intent)]
# print 'closure:'
# print closure
res = reduce(lambda x, y: x & y if x & y else x | y, closure)
# print 'reduced:'
# print res
for cs in ['positive', 'negative']:
if 'class:' + cs in res:
labels[cs] = True
labels[cs + '_res'] = candidate_intent
# print 'classified as %s, reason:' % cs
# print candidate_intent
# print res
# print labels
if labels.get("positive", False) and labels.get("negative", False):
cv_res["contradictory"] += 1
return
if example[-1] == "positive" and labels.get("positive", False):
cv_res["positive_positive"] += 1
if example[-1] == "negative" and labels.get("positive", False):
cv_res["negative_positive"] += 1
if example[-1] == "positive" and labels.get("negative", False):
cv_res["positive_negative"] += 1
if example[-1] == "negative" and labels.get("negative", False):
cv_res["negative_negative"] += 1
'''closure = []
for candidate in context:
cintent = make_intent(candidate)
closure.append(cintent&eintent)
if cintent.issubset(eintent):
print "classified!"
print cintent
print eintent
'''
# sanity check:
# check_hypothesis(plus_examples, minus_examples, plus_examples[3])
max_index = sys.argv[1]
for index in xrange(1, int(max_index)):
index = str(index)
q = open("train" + index + ".csv", "r")
train = [a.strip().split(",") for a in q]
plus = [a for a in train if a[-1] == "positive"]
minus = [a for a in train if a[-1] == "negative"]
# print t
q.close()
w = open("test" + index + ".csv", "r")
unknown = [a.strip().split(",") for a in w]
w.close()
for elem in unknown:
cv_res['total'] += 1
# print elem
# print "done"
# check_hypothesis(plus, minus, elem)
# check_intersect(plus, minus, elem, len(elem) / 2)
check_intersect(plus, minus, elem, len(elem) / 2)
print "done: %s" % index
print cv_res
for k, v in cv_res.iteritems():
cv_res[k] = v * 1. / cv_res["total"]
print cv_res