Esempio n. 1
0
 def test_generic_terms_equal(self):
     generic = Statement(predicate="something happened", generic=True)
     generic_false = Statement(predicate=Predicate(
         content="something happened", truth=False),
                               generic=True)
     assert generic.means(generic_false)
     assert generic_false.means(generic)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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"))
Esempio n. 4
0
 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)
Esempio n. 5
0
    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
Esempio n. 6
0
 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)
Esempio n. 7
0
 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
Esempio n. 8
0
    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)
Esempio n. 9
0
 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)
Esempio n. 10
0
 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)
Esempio n. 11
0
 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()
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
 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))
Esempio n. 15
0
    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 == ""
Esempio n. 16
0
 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
Esempio n. 17
0
 def test_false_does_not_contradict_absent(self):
     absent_fact = Statement(
         predicate=Predicate(
             content="${rural_s_telephone_directory} was copyrightable",
             truth=True),
         terms=[Entity(name="Rural's telephone directory")],
         absent=True,
     )
     false_fact = Statement(
         predicate=Predicate(content="${the_java_api} was copyrightable",
                             truth=False),
         terms=[Entity(name="the Java API", generic=True, plural=False)],
         absent=False,
     )
     assert not false_fact.contradicts(absent_fact)
     assert not absent_fact.contradicts(false_fact)
Esempio n. 18
0
    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")],
            )
Esempio n. 19
0
    def test_implication_and_contradiction(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")])
        explanation = bob_lived.explain_implication(carl_lived)

        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(statement_long)
        right = FactorGroup(statement_short)
        new_explanation = left.explain_contradiction(right,
                                                     context=explanation)

        expected = "the statement that <bob> lived at <bob's house>"
        assert new_explanation.reasons[0].left.key.lower() == expected
        assert new_explanation.reasons[0].operation == operator.ge

        part = "from the center of <houston> to <bob's house> was at least 50 mile"
        assert part in new_explanation.reasons[1].left.key.lower()
        assert new_explanation.reasons[1].operation == contradicts
Esempio n. 20
0
 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))
Esempio n. 21
0
 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")],
             )
         )
Esempio n. 22
0
 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())
Esempio n. 23
0
 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)
Esempio n. 24
0
 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
Esempio n. 25
0
 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)
Esempio n. 26
0
 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
Esempio n. 27
0
 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)
Esempio n. 28
0
 def test_same_meaning_after_adding_implied(self):
     dave = Entity(name="Dave")
     speed_template = "${driver}'s driving speed was"
     fast_fact = Statement(
         predicate=Comparison(content=speed_template,
                              sign=">=",
                              expression="100 miles per hour"),
         terms=dave,
     )
     slow_fact = Statement(
         predicate=Comparison(
             content=speed_template,
             sign=">=",
             expression="20 miles per hour",
         ),
         terms=dave,
     )
     new = fast_fact + slow_fact
     assert new.means(fast_fact)
Esempio n. 29
0
    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
Esempio n. 30
0
    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