def test_does_not_share_all_factors(self, make_statement): left = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="Oakland"), Entity(name="San Francisco"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) right = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="San Francisco"), Entity(name="Oakland"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) assert not left.shares_all_factors_with(right)
def _add_if_universal(self, other: Procedure, explanation: Explanation) -> Optional[Procedure]: """Show how first Procedure triggers the second if both are universal.""" self_output_or_input = FactorGroup( (*self.outputs_group, *self.inputs_group)) other_input = list(other.inputs) implied_inputs = [] not_implied = [] while other_input: current = other_input.pop() implying_explanation = self_output_or_input.explain_implication( current, context=explanation) if implying_explanation is not None: explanation = implying_explanation implied_inputs.append(current) else: not_implied.append(current) if not any(implied_inputs): return None to_combine = Procedure(inputs=not_implied, outputs=other.outputs, despite=other.despite) return self.union(to_combine, context=explanation)
def test_likely_contexts_to_identical_factor(self, make_statement): first_group = FactorGroup([make_statement["shooting"]]) second_group = FactorGroup( [make_statement["crime"], make_statement["shooting"]]) gen = first_group.likely_contexts(second_group) context = next(gen) assert context.get("<Alice>").name == "Alice"
def test_context_prevents_implied_by_factor_tuples_not_lists( self, make_statement): left = FactorGroup(make_statement["more"]) assert not left.implied_by( make_statement["way_more"], context=(("<San Francisco>", ), (Entity(name="Richmond"), )), )
def test_contradiction_of_group(self): lived_at = Predicate(content="$person lived at $residence") bob_lived = Fact( predicate=lived_at, terms=[Entity(name="Bob"), Entity(name="Bob's house")] ) carl_lived = Fact( predicate=lived_at, terms=[Entity(name="Carl"), Entity(name="Carl's house")] ) distance_long = Comparison( content="the distance from the center of $city to $residence was", sign=">=", expression="50 miles", ) statement_long = Fact( predicate=distance_long, terms=[Entity(name="Houston"), Entity(name="Bob's house")], ) distance_short = Comparison( content="the distance from the center of $city to $residence was", sign="<=", expression="10 kilometers", ) statement_short = Fact( predicate=distance_short, terms=[Entity(name="El Paso"), Entity(name="Carl's house")], ) left = FactorGroup([bob_lived, statement_long]) right = FactorGroup([carl_lived, statement_short]) explanation = left.explain_contradiction(right) assert explanation.context["<Houston>"].name == "El Paso" assert contradicts(left, right)
def test_group_means_identical_group(self, make_statement): first_group = FactorGroup( [make_statement["crime"], make_statement["murder"]]) second_group = FactorGroup( [make_statement["crime"], make_statement["murder"]]) assert first_group.means(second_group) assert means(first_group, second_group)
def test_not_same_nonmatching_terms(self, make_statement): left = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="Oakland"), Entity(name="San Francisco"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) right = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="San Francisco"), Entity(name="Oakland"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) assert not left.means(right)
def test_ge_not_gt(self, make_statement): left = FactorGroup( [make_statement["shooting"], make_statement["murder"]]) right = FactorGroup( [make_statement["shooting_craig"], make_statement["murder_craig"]]) assert left >= right assert not left > right
def test_interchangeable_terms_in_factorgroup(self): """Fails whether the method is 'comparison' or '_verbose_comparison'""" left = FactorGroup([ Statement( predicate=Comparison( content="the distance between $place1 and $place2 was", truth=True, sign="<=", expression="35 foot", ), terms=( Entity(name="Scylla", generic=True, plural=False), Entity(name="Charybdis", generic=True, plural=False), ), ), Statement( predicate=Comparison( content= "the distance between ${monster} and a boat used by ${hero} was", truth=True, sign="<=", expression="5 foot", ), terms=( Entity(name="Scylla", generic=True, plural=False), Entity(name="Ulysses", generic=True, plural=False), ), ), ]) right = FactorGroup([ Statement( predicate=Comparison( content="the distance between $place1 and $place2 was", truth=True, sign="<=", expression="35 foot", ), terms=( Entity(name="Charybdis", generic=True, plural=False), Entity(name="Scylla", generic=True, plural=False), ), ), Statement( predicate=Comparison( content= "the distance between $thing and a boat used by $person was", truth=True, sign="<=", expression="5 foot", ), terms=( Entity(name="Scylla", generic=True, plural=False), Entity(name="Ulysses", generic=True, plural=False), ), ), ]) gen = left.explanations_implication(right) result = next(gen) assert result
def test_consistent_factor_groups_with_context(self): alice_like_craig = ContextRegister() alice_like_craig.insert_pair(alice, craig) assert FactorGroup([alice_rich, bob_poor]).consistent_with( FactorGroup([dan_poor, craig_rich]), context=alice_like_craig, )
def test_group_inconsistent_with_one_statement(self): group = FactorGroup( [self.slower_specific_statement, self.farm_statement]) register = ContextRegister() register.insert_pair(Entity(name="the car"), Entity(name="the pickup")) assert not group.consistent_with(self.faster_statement, context=register)
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_two_by_two(self, make_statement): left = FactorGroup( [make_statement["murder"], make_statement["large_weight"]]) right = FactorGroup((make_statement["murder_entity_order"], make_statement["small_weight_bob"])) context = next(left.likely_contexts(right)) assert context.check_match(Entity(name="Alice"), Entity(name="Bob"))
def test_consistent_and_same_meaning(self, make_statement): left_weight = FactorGroup(make_statement["small_weight_bob"]) right_weight = FactorGroup(make_statement["large_weight"]) explanation = left_weight.explain_consistent_with(right_weight) new_explanation = make_statement["crime_bob"].explain_same_meaning( make_statement["crime"], context=explanation) assert "Because <Bob> is like <Alice>" in str(new_explanation) assert "FactorGroup([" not in str(new_explanation)
def test_explanations_implication_of_factor(self, make_statement): """Explanation shows the statements in `left` narrow down the quantity more than `right` does.""" left = FactorGroup([ make_statement["absent_way_more"], make_statement["less_than_20"] ]) right = make_statement["less"] explanation = left.explain_implication(right) assert "implies" in str(explanation).lower()
def test_not_internally_consistent_with_context(self, make_statement): context = ContextRegister() context.insert_pair(Entity(name="Alice"), Entity(name="Alice")) context.insert_pair(Entity(name="Bob"), Entity(name="Bob")) group = FactorGroup( [make_statement["shooting"], make_statement["no_shooting"]]) with pytest.raises(ValueError): group.internally_consistent()
def test_means_to_implied_by(self, make_statement): explanation = make_statement["crime_bob"].explain_same_meaning( make_statement["crime"]) left = FactorGroup([make_statement["small_weight_bob"]]) right = FactorGroup([make_statement["large_weight"]]) new = left.explain_implied_by(right, explanation) assert len(new.reasons) == 2 assert new.reasons[1].operation == operator.ge
def test_interchangeable_terms_in_different_orders(self): more_than_100_yards = Comparison( content="the distance between $site1 and $site2 was", sign=">", expression="100 yards", ) less_than_1_mile = Comparison( content="the distance between $site1 and $site2 was", sign="<", expression="1 mile", ) protest_facts = FactorGroup([ Statement( predicate=more_than_100_yards, terms=[ Entity(name="the political convention"), Entity(name="the police cordon"), ], ), Statement( predicate=less_than_1_mile, terms=[ Entity(name="the police cordon"), Entity(name="the political convention"), ], ), ]) assert "between <the political convention> and " in str(protest_facts) more_than_50_meters = Comparison( content="the distance between $site1 and $site2 was", sign=">", expression="50 meters", ) less_than_2_km = Comparison( content="the distance between $site1 and $site2 was", sign="<=", expression="2 km", ) speech_zone_facts = FactorGroup([ Statement( predicate=more_than_50_meters, terms=[ Entity(name="the free speech zone"), Entity(name="the courthouse"), ], ), Statement( predicate=less_than_2_km, terms=[ Entity(name="the free speech zone"), Entity(name="the courthouse"), ], ), ]) assert protest_facts.implies(speech_zone_facts)
def _has_input_or_despite_factors_implying_all_inputs_of( self, other: Procedure, context: Explanation, ) -> Iterator[Explanation]: """Check if every input of other is implied by some input or despite factor of self.""" self_despite_or_input = FactorGroup((*self.despite, *self.inputs)) yield from self_despite_or_input._explanations_implication( other.inputs_group, explanation=context)
def test_same_meaning_factors_not_grouped(self, make_statement): left_weight = make_statement["small_weight_bob"] right_weight = make_statement["large_weight"] explanation = left_weight.explain_consistent_with(right_weight) new = FactorGroup(make_statement["crime_bob"]).explain_same_meaning( FactorGroup(make_statement["crime"]), context=explanation) assert "Because <Bob> is like <Alice>" in str(new) assert new.reasons[1].operation == means assert "was at least 100 kilogram, and the statement" in new.short_string
def test_no_contradiction_because_entities_vary(self, make_statement): """ If these Factors were about the same Term, they would contradict and no union would be possible. """ left = FactorGroup(make_statement["no_shooting_entity_order"]) right = FactorGroup(make_statement["shooting"]) combined = left | right assert len(combined) == 2
def test_abbreviated_contradiction_with_distance(self, make_opinion_with_holding, make_holding): watt = make_opinion_with_holding["watt_majority"] watt_rule = list(watt.holdings)[1].rule must_not_rule = make_holding["h2_output_false_ALL_MUST"] watt_rule.procedure.inputs = FactorGroup([watt_rule.inputs[3]]) must_not_rule.procedure.inputs = FactorGroup([must_not_rule.inputs[3]]) assert watt_rule.contradicts(must_not_rule)
def test_no_contradiction_because_entities_vary(self, watt_factor): """ If these Factors were about the same Entity, they would contradict and no union would be possible. """ left = FactorGroup(watt_factor["f3_different_entity"]) right = FactorGroup(watt_factor["f3_absent"]) combined = left | right assert len(combined) == 2
def test_consistent_factor_groups(self): """ Verifies that the factor groups are considered "consistent" even though it would be possible to make an analogy that would make the statements contradict. """ assert FactorGroup([alice_rich, bob_poor]).consistent_with( FactorGroup([dan_poor, craig_rich]) )
def test_means_to_contradicts(self, make_statement): explanation = make_statement["crime"].explain_same_meaning( make_statement["crime_bob"]) left = FactorGroup( [make_statement["shooting_craig"], make_statement["less"]]) right = FactorGroup( [make_statement["murder_craig"], make_statement["more"]]) new = left.explain_contradiction(right, context=explanation) assert len(new.reasons) == 2 assert new.reasons[1].operation == contradicts
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_not_same_meaning_more_reasons(self, make_statement): left = FactorGroup([ make_statement["shooting"], make_statement["crime"] ]).explain_same_meaning( FactorGroup([ make_statement["shooting_craig"], make_statement["crime_craig"] ])) right = make_statement["murder"].explain_same_meaning( make_statement["murder_craig"]) assert not left.means(right)
def test_cannot_apply_lt(self, make_statement): explanation = make_statement["crime"].explain_same_meaning( make_statement["crime_bob"]) left = FactorGroup( [make_statement["shooting_craig"], make_statement["less"]]) right = FactorGroup( [make_statement["murder_craig"], make_statement["more"]]) explanation.operation = operator.lt gen = explanation.operate(left, right) with pytest.raises(ValueError): next(gen)
def test_group_inconsistent_with_single_factor(self): group = FactorGroup( [self.slower_specific_statement, self.farm_statement]) register = ContextRegister() register.insert_pair(Entity(name="the car"), Entity(name="the pickup")) assert not group.consistent_with(self.faster_statement, context=register) assert not consistent_with( group, self.faster_statement, context=register) assert repr(group).startswith("FactorGroup([Statement") assert "30.0 mile / hour" in repr(group)
def test_no_comparison_with_comparison(self): comparison = Comparison( content="${person}'s speed was", sign=">", expression="36 kilometers per hour", ) other_comparison = Comparison(content="${person}'s speed was", sign=">", expression="10 meters per second") left = FactorGroup( Statement(predicate=comparison, terms=Entity(name="Ann"))) assert not left.means(other_comparison)