-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_context.py
227 lines (196 loc) · 9.16 KB
/
test_context.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import operator
import pytest
from nettlesome.terms import ContextRegister, means
from nettlesome.entities import Entity
from nettlesome.groups import FactorGroup
from authorityspoke.facts import Fact
class TestContextRegisters:
def test_possible_context_without_empty_spaces(self, watt_factor, make_entity):
left = watt_factor["f1"]
right = watt_factor["f1"]
contexts = list(left.possible_contexts(right))
assert len(contexts) == 1
assert contexts[0].check_match(make_entity["motel"], make_entity["motel"])
def test_all_possible_contexts_identical_factor(self, watt_factor, make_entity):
left = watt_factor["f2"]
right = watt_factor["f2"]
contexts = list(left.possible_contexts(right))
assert len(contexts) == 2
assert any(
context.check_match(make_entity["watt"], make_entity["motel"])
for context in contexts
)
def test_context_not_equal_to_list(self):
changes = ContextRegister.from_lists(
[Entity(name="Alice")],
[Entity(name="Dan")],
)
assert changes != [[Entity(name="Alice")], [Entity(name="Dan")]]
def test_cannot_update_context_register_from_lists(self):
left = Fact(
predicate="$shooter shot $victim",
terms=[Entity(name="Alice"), Entity(name="Bob")],
)
right = Fact(
predicate="$shooter shot $victim",
terms=[Entity(name="Craig"), Entity(name="Dan")],
)
update = left.update_context_register(
right,
context=[[Entity(name="Alice")], [Entity(name="Craig")]],
comparison=means,
)
with pytest.raises(AttributeError):
next(update)
def test_limited_possible_contexts_identical_factor(self, watt_factor, make_entity):
left = watt_factor["f2"]
right = watt_factor["f2"]
context = ContextRegister()
context.insert_pair(make_entity["motel"], make_entity["motel"])
contexts = list(left.possible_contexts(right, context=context))
assert len(contexts) == 1
assert contexts[0].check_match(make_entity["watt"], make_entity["watt"])
def test_context_register_empty(self, make_complex_fact, watt_factor):
"""
Yields no context_register because the Entity in f1 doesn't imply
the Fact in f_relevant_murder.
"""
with pytest.raises(StopIteration):
next(
watt_factor["f1"]._context_registers(
make_complex_fact["f_relevant_murder"], operator.ge
)
)
def test_context_register_valid(self, make_entity, watt_factor):
expected = ContextRegister()
expected.insert_pair(make_entity["motel"], make_entity["watt"])
generated = next(
watt_factor["f1"]._context_registers(
watt_factor["f1_entity_order"], operator.le
)
)
assert generated == expected
def test_import_to_context_register(self, make_entity, watt_factor):
left = ContextRegister.from_lists(
to_replace=[watt_factor["f7"], make_entity["motel"]],
replacements=[watt_factor["f7_swap_entities"], make_entity["trees"]],
)
right = ContextRegister()
right.insert_pair(make_entity["trees"], make_entity["motel"])
assert len(left.merged_with(right)) == 3
def test_import_to_mapping_no_change(self, make_entity):
old_mapping = ContextRegister.from_lists(
[make_entity["motel"]], [make_entity["trees"]]
)
new_mapping = ContextRegister.from_lists(
[make_entity["motel"]], [make_entity["trees"]]
)
assert old_mapping.merged_with(new_mapping) == old_mapping
def test_import_to_mapping_conflict(self, make_entity):
old_mapping = ContextRegister.from_lists(
[make_entity["motel"]], [make_entity["trees"]]
)
new_mapping = ContextRegister.from_lists(
[make_entity["motel"]], [make_entity["motel"]]
)
merged = old_mapping.merged_with(new_mapping)
assert merged is None
def test_import_to_mapping_reciprocal(self, watt_factor):
left = ContextRegister.from_lists([watt_factor["f7"]], [watt_factor["f7"]])
right = ContextRegister.from_lists(
[watt_factor["f7_swap_entities"]], [watt_factor["f7_swap_entities"]]
)
mapping = left.merged_with(right)
assert mapping.get(str(watt_factor["f7"])).means(watt_factor["f7"])
def test_registers_for_interchangeable_context(self, make_entity, watt_factor):
"""
Test that _registers_for_interchangeable_context swaps the first two
items in the ContextRegister
"""
factor = watt_factor["f7"]
first_pattern, second_pattern = list(factor.term_permutations())
assert first_pattern[0] == second_pattern[1]
assert first_pattern[1] == second_pattern[0]
assert first_pattern[0] != first_pattern[1]
class TestLikelyContext:
def test_likely_context_one_factor(self, make_entity, watt_factor):
left = watt_factor["f2"]
right = watt_factor["f2"]
context = next(left.likely_contexts(right))
assert context.check_match(make_entity["motel"], make_entity["motel"])
def test_likely_context_implication_one_factor(self, make_entity, watt_factor):
left = watt_factor["f8"]
right = watt_factor["f8_meters"]
context = next(left.likely_contexts(right))
assert context.check_match(make_entity["motel"], make_entity["motel"])
def test_likely_context_two_factors(self, make_entity, watt_factor):
left = FactorGroup((watt_factor["f9_swap_entities"], watt_factor["f2"]))
right = watt_factor["f2"]
context = next(left.likely_contexts(right))
assert context.check_match(make_entity["motel"], make_entity["motel"])
def test_likely_context_two_by_two(self, make_entity, watt_factor):
left = FactorGroup((watt_factor["f9"], watt_factor["f2"]))
right = FactorGroup(
(watt_factor["f9_swap_entities"], watt_factor["f9_more_different_entity"])
)
context = next(left.likely_contexts(right))
assert context.check_match(make_entity["motel"], make_entity["trees"])
def test_likely_context_different_terms(self, make_opinion_with_holding):
lotus = make_opinion_with_holding["lotus_majority"]
oracle = make_opinion_with_holding["oracle_majority"]
left = [lotus.holdings[2].outputs[0], lotus.holdings[2].inputs[0].to_effect]
left = FactorGroup(left)
right = FactorGroup(oracle.holdings[2].outputs[0])
context = next(left.likely_contexts(right))
lotus_menu = lotus.holdings[2].generic_terms()[0]
java_api = oracle.generic_terms()[0]
assert context.get_factor(lotus_menu).compare_keys(java_api)
def test_likely_context_from_factor_meaning(self, make_opinion_with_holding):
lotus = make_opinion_with_holding["lotus_majority"]
oracle = make_opinion_with_holding["oracle_majority"]
left = lotus.holdings[2].outputs[0]
right = oracle.holdings[2].outputs[0]
likely = left._likely_context_from_meaning(right, context=ContextRegister())
lotus_menu = lotus.holdings[2].generic_terms()[0]
java_api = oracle.generic_terms()[0]
assert likely.get_factor(lotus_menu).compare_keys(java_api)
def test_union_one_generic_not_matched(self, make_opinion_with_holding):
"""
Here, both FactorGroups have "fact that <> was a computer program".
But they each have another generic that can't be matched:
fact that <the Java API> was a literal element of <the Java language>
and
fact that <the Lotus menu command hierarchy> provided the means by
which users controlled and operated <Lotus 1-2-3>
Tests that Factors from "left" should be keys and Factors from "right" values.
"""
lotus = make_opinion_with_holding["lotus_majority"]
oracle = make_opinion_with_holding["oracle_majority"]
left = FactorGroup(lotus.holdings[7].inputs[:2])
right = FactorGroup(
[oracle.holdings[3].outputs[0], oracle.holdings[3].inputs[0]]
)
new = left | right
text = (
"that <the Lotus menu command hierarchy> was a "
"literal element of <Lotus 1-2-3>"
)
assert text in new[1].short_string
class TestChangeRegisters:
def test_reverse_key_and_value_of_register(self):
left = Entity(name="Siskel")
right = Entity(name="Ebert")
register = ContextRegister.from_lists([left], [right])
assert len(register.keys()) == 1
assert register.get("<Siskel>").compare_keys(Entity(name="Ebert"))
new = register.reversed()
assert new.get("<Ebert>").compare_keys(left)
def test_factor_pairs(self):
register = ContextRegister.from_lists(
[Entity(name="apple"), Entity(name="lemon")],
[Entity(name="pear"), Entity(name="orange")],
)
gen = register.factor_pairs()
pair = next(gen)
assert pair[0].name == "apple"
assert pair[1].name == "pear"