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_contradiction_of_group(self): lived_at = Predicate(content="$person lived at $residence") bob_lived = Statement( predicate=lived_at, terms=[Entity(name="Bob"), Entity(name="Bob's house")]) carl_lived = Statement( 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 = Statement( 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 = Statement( 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_possible_context_different_terms(self): left = Statement(predicate=self.bird, terms=Entity(name="Foghorn")) right = Statement(predicate=self.bird, terms=Entity(name="Woody")) contexts = list(left.possible_contexts(right)) assert len(contexts) == 1 assert contexts[0].check_match(Entity(name="Foghorn"), Entity(name="Woody"))
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_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 test_no_duplicate_explanations_consistent(self): large_payments = FactorGroup([ Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=10000, ), terms=[Entity(name="Alice"), Entity(name="Bob")], ), Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=1000, ), terms=[Entity(name="Dan"), Entity(name="Eve")], ), ]) small_payments = FactorGroup([ Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=100, ), terms={ "payer": Entity(name="Fred"), "payee": Entity(name="Greg") }, ), Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=10, ), terms={ "payer": Entity(name="Jim"), "payee": Entity(name="Kim") }, ), ]) assert large_payments.consistent_with(small_payments) all_explanations = list( large_payments.explanations_consistent_with(small_payments)) assert len(all_explanations) == 24 limited_explanations = list( large_payments.explanations_consistent_with( small_payments, context=([Entity(name="Alice")], [Entity(name="Jim")]))) assert len(limited_explanations) == 6
def test_entity_does_not_imply_statement(self): entity = Entity(name="Bob") statement = Statement(predicate="$person loves ice cream", terms=entity) assert not entity.implies(statement) assert not statement.implies(entity) assert not entity >= statement assert not statement >= entity assert not entity > statement assert not statement > entity
def test_all_possible_contexts_identical_factor(self): left = Statement(predicate=self.paid, terms=[Entity(name="Irene"), Entity(name="Bob")]) contexts = list(left.possible_contexts(left)) assert len(contexts) == 2 assert any( context.check_match(Entity(name="Irene"), Entity(name="Bob")) for context in contexts)
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 test_means_despite_plural(self): directory = Entity(name="Rural's telephone directory", plural=False) listings = Entity(name="Rural's telephone listings", plural=True) directory_original = Statement( predicate=Predicate(content="$thing was original"), terms=directory) listings_original = Statement( predicate=Predicate(content="$thing were original"), terms=listings) assert directory_original.means(listings_original)
def test_context_register_empty(self, make_complex_fact): """ Yields no context_register because the Term in f1 doesn't imply the Fact in f_relevant_murder. """ statement = Statement(predicate="$person was a defendant", terms=Entity(name="Alice")) complex_statement = make_complex_fact["relevant_murder"] gen = statement._context_registers(complex_statement, operator.ge) with pytest.raises(StopIteration): next(gen)
def test_limited_possible_contexts_identical_factor(self): statement = Statement(predicate=self.paid, terms=[Entity(name="Al"), Entity(name="Xu")]) context = ContextRegister() context.insert_pair(Entity(name="Al"), Entity(name="Xu")) contexts = list(statement.possible_contexts(statement, context=context)) assert len(contexts) == 1 assert contexts[0].check_match(Entity(name="Al"), Entity(name="Xu")) assert "Entity(name='Xu'" in repr(contexts)
def test_unequal_because_one_factor_is_absent(self, make_predicate): left = Statement( predicate=make_predicate["shooting"], terms=[Entity(name="Al"), Entity(name="Bo")], ) right = Statement( predicate=make_predicate["shooting"], terms=[Entity(name="Al"), Entity(name="Bob")], absent=True, ) assert not left.means(right)
def test_all_generic_terms_match_in_statement(self): predicate = Predicate(content="the telescope pointed at $object") morning = Statement(predicate=predicate, terms=Entity(name="Morning Star")) evening = Statement(predicate=predicate, terms=Entity(name="Evening Star")) left = FactorGroup(morning) right = FactorGroup(evening) context = ContextRegister() context.insert_pair(Entity(name="Morning Star"), Entity(name="Evening Star")) assert left._all_generic_terms_match(right, context=context)
def test_term_outside_of_group(self): speed = "${person}'s speed was" comparison = Comparison(content=speed, sign=">", expression="36 kilometers per hour") other = Comparison(content=speed, sign=">", expression="10 meters per second") left = FactorGroup( Statement(predicate=comparison, terms=Entity(name="Ann"))) right = Statement(predicate=other, terms=Entity(name="Bob")) assert left.means(right)
def test_brackets_around_generic_terms(self, make_predicate): statement = Statement( predicate=make_predicate["shooting"], terms=[Entity(name="Al"), Entity(name="Ed")], ) assert "<Al> shot <Ed>" in str(statement) absent = Statement( predicate=make_predicate["shooting"], terms=[Entity(name="Al"), Entity(name="Ed")], absent=True, ) assert "absence of the statement" in str(absent).lower()
def test_registers_for_interchangeable_context(self, make_statement): """ Test that _registers_for_interchangeable_context swaps the first two items in the ContextRegister """ factor = Statement( predicate="$person1 and $person2 met with each other", terms=[Entity(name="Al"), Entity(name="Ed")], ) first_pattern, second_pattern = list(factor.term_permutations()) assert first_pattern[0].name == second_pattern[1].name assert first_pattern[1].name == second_pattern[0].name assert first_pattern[0].name != first_pattern[1].name
def test_new_context_replace_fact(self): changes = ContextRegister.from_lists( [Entity(name="Death Star 3"), Entity(name="Kylo Ren")], [Entity(name="Death Star 1"), Entity(name="Darth Vader")], ) statement = Statement( predicate="$person blew up a planet with $weapon", terms=[Entity(name="Kylo Ren"), Entity(name="Death Star 3")], ) new = statement.new_context(changes) assert "<Darth Vader> blew up" in new.short_string
def test_make_new_complex_fact(self, make_predicate, make_statement): shooting = make_statement["shooting"] murder = make_statement["murder"] relevant = make_predicate["relevant"] fact = Statement(predicate=relevant, terms=(shooting, murder)) assert fact.terms[0].name == ""
def test_include_of_in_string(self): fact = Statement(predicate="$suspect stole bread", terms=Entity(name="Valjean")) accusation = Assertion(statement=fact, authority=Entity(name="Javert")) assert ( "the assertion, by <Javert>, of the statement that <Valjean> stole bread" in str(accusation))
def test_cannot_repeat_term_in_termsequence(self, make_predicate): with pytest.raises(DuplicateTermError): Statement( predicate="$person1 shot $person2", terms=[Entity(name="Al"), Entity(name="Al")], )
def test_truth_param(self): no_license = Statement( predicate="$business was licensed as a money transmitting business", truth=False, terms=Entity(name="Helix"), ) assert no_license.predicate.truth is False
def test_list_instead_of_group(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"))) right = [ Statement(predicate=other_comparison, terms=Entity(name="Bob")) ] assert left.means(right) assert "Because <Ann> is like <Bob>" in str( left.explain_same_meaning(right))
def test_error_predicate_contradict_factor(self, make_comparison): with pytest.raises(TypeError): make_comparison["exact"].contradicts( Statement( make_comparison["exact"], terms=[Entity(name="thing"), Entity(name="place")], ) )
def test_length_with_specific_term(self): statement = Statement( predicate="$person paid tax to $state", terms=[ Entity(name="Alice"), Entity(name="the State of Texas", generic=False), ], ) assert len(statement) == 1
def test_cannot_update_context_register_from_lists(self): left = Statement( predicate="$shooter shot $victim", terms=[Entity(name="Alice"), Entity(name="Bob")], ) right = Statement( 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_repeating_entity_string(self): three_entities = Predicate( content="$planner told $intermediary to hire $shooter") statement_3 = Statement( predicate=three_entities, terms=[Entity(name="Al"), Entity(name="Bob"), Entity(name="Cid")], ) two_entities = Predicate( content="$shooter told $intermediary to hire $shooter") statement_2 = Statement(predicate=two_entities, terms=[Entity(name="Al"), Entity(name="Bob")]) assert ("statement that <Al> told <Bob> to hire <Cid>".lower() in str(statement_3).lower()) assert ("statement that <Al> told <Bob> to hire <Al>".lower() in str(statement_2).lower())
def test_implied_by(self): left = FactorGroup([ Statement( predicate=Predicate( content= "${rural_s_telephone_directory} was a compilation of facts" ), terms=(Entity(name="Rural's telephone directory"), ), ) ]) right = FactorGroup([ Statement( predicate=Predicate( content="${rural_s_telephone_directory} was an idea"), terms=(Entity(name="Rural's telephone directory"), ), ) ]) assert not left.implied_by(right)
def test_union_one_generic_not_matched(self): """ Here, both FactorGroups have "fact that <> was a computer program". But they each have another generic that can't be matched: that <the Java API> was a literal element of <the Java language> and 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. """ language_program = Statement( predicate="$program was a computer program", terms=Entity(name="the Java language"), ) lotus_program = Statement( predicate="$program was a computer program", terms=Entity(name="Lotus 1-2-3"), ) controlled = Statement( predicate= "$part provided the means by which users controlled and operated $whole", terms=[ Entity(name="the Lotus menu command hierarchy"), Entity(name="Lotus 1-2-3"), ], ) part = Statement( predicate="$part was a literal element of $whole", terms=[ Entity(name="the Java API"), Entity(name="the Java language") ], ) left = FactorGroup([lotus_program, controlled]) right = FactorGroup([language_program, part]) new = left | right text = ("that <the Lotus menu command hierarchy> was a " "literal element of <Lotus 1-2-3>") assert text in new[0].short_string
def test_no_duplicate_context_interchangeable_terms(self): left = Statement( predicate=Predicate( content="$country1 signed a treaty with $country2"), terms=(Entity(name="Mexico"), Entity(name="USA")), ) right = Statement( predicate=Predicate( content="$country3 signed a treaty with $country1"), terms=(Entity(name="Germany"), Entity(name="UK")), ) context = ContextRegister.from_lists([Entity(name="USA")], [Entity(name="UK")]) gen = left._context_registers(right, operator.ge, context) results = list(gen) assert len(results) == 1