Comparables
- class nettlesome.terms.Comparable
Objects that can be compared for implication, same meaning, contradiction, and consistency.
- Attr generic
Whether the object is referred to in a generic sense. If True, substituting this object for another generic object of the same class does not change the meaning of other Comparable objects that incorporate this one as a term.
- Attr absent
Indicates the absence of the described object. The absence of two contradictory objects is not contradictory.
- Attr name
An identifier for this object. May be used as a shorthand way of referring to this object when replacing another Comparable object’s generic terms.
- Attr plural
Indicates whether the object refers to multiple things.
- __ge__(other)
Call
implies()as an alias.- Return type
- Returns
bool indicating whether
selfimpliesother
- __str__()
Return str(self).
- __weakref__
list of weak references to the object (if defined)
- compare_keys(other)
Test if self and other would be considered identical in a ContextRegister.
May return True even if Python’s == operation would return False.
May return False even if the .means() method would return True because self and other have generic terms.
- Return type
- compare_ordering_of_terms(other, relation, ordering)
Determine whether one ordering of self’s terms matches other’s terms.
Multiple term orderings exist where the terms can be rearranged without changing the Fact’s meaning.
For instance, “<Ann> and <Bob> both were members of the same family” has a second ordering “<Bob> and <Ann> both were members of the same family”.
- Return type
- compare_terms(other, relation)
Test if relation holds for corresponding context factors of self and other.
This doesn’t track a persistent
ContextRegisteras it goes down the sequence ofFactorpairs. Perhaps(?) this simpler process can weed outFactors that clearly don’t satisfy a comparison before moving on to the more costlyAnalogyprocess. Or maybe it’s useful for testing.- Return type
- consistent_with(other, context=None)
Check if self and other can be non-contradictory.
- Return type
- Returns
a bool indicating whether there’s at least one way to match the
termsofselfandother, such that they fit the relationshipcomparison.
- contradicts(other, context=None)
Test whether
selfimplies the absence ofother.- Return type
- Returns
Trueif self and other can’t both be true at the same time. Otherwise returnsFalse.
- contradicts_same_context(other)
Check if self contradicts other if Terms with the same name always match.
Used to check consistency of FactorGroups.
- Return type
- explain_consistent_with(other, context=None)
Get one explanation of why self and other need not contradict.
- Return type
- explain_contradiction(other, context=None)
Get one explanation of why self and other contradict.
Using the
explanations_contradiction()method, generates oneExplanationof how an analogy between the generic terms of the two objects can make them contradictory.In this example using two
FactorGroups, if the threeStatements inbrexitwere asserted about the threeEntityterms innafta, there would be an inconsistency as to whether one pair of Entities signed a treaty with each other.>>> from nettlesome import Statement, Entity, FactorGroup >>> nafta = FactorGroup([ ... Statement(predicate="$country1 signed a treaty with $country2", ... terms=[Entity(name="Mexico"), Entity(name="USA")]), ... Statement(predicate="$country2 signed a treaty with $country3", ... terms=[Entity(name="USA"), Entity(name="Canada")]), ... Statement(predicate="$country3 signed a treaty with $country1", ... terms=[Entity(name="USA"), Entity(name="Canada")])]) >>> brexit = FactorGroup([ ... Statement(predicate="$country1 signed a treaty with $country2", ... terms=[Entity(name="UK"), Entity(name="European Union")]), ... Statement(predicate="$country2 signed a treaty with $country3", ... terms=[Entity(name="European Union"), Entity(name="Germany")]), ... Statement(predicate="$country3 signed a treaty with $country1", ... terms=[Entity(name="Germany"), Entity(name="UK")], truth=False)]) >>> print(nafta.explain_contradiction(brexit)) Because <Mexico> is like <Germany>, and <USA> is like <UK>, the statement that <Mexico> signed a treaty with <USA> CONTRADICTS the statement it was false that <Germany> signed a treaty with <UK>
- Return type
- explain_implication(other, context=None)
Get one explanation of why self implies other.
- Return type
- explain_implied_by(other, context=None)
Get one explanation of why self implies other.
- Return type
- explain_same_meaning(other, context=None)
Get one explanation of why self and other have the same meaning.
>>> from nettlesome import Statement, Entity >>> hades_curse = Statement( ... predicate="$deity cursed $target", ... terms=[Entity(name="Hades"), Entity(name="Persephone")]) >>> aphrodite_curse = Statement( ... predicate="$deity cursed $target", ... terms=[Entity(name="Aphrodite"), Entity(name="Narcissus")]) >>> print(hades_curse.explain_same_meaning(aphrodite_curse)) Because <Hades> is like <Aphrodite>, and <Persephone> is like <Narcissus>, the statement that <Hades> cursed <Persephone> MEANS the statement that <Aphrodite> cursed <Narcissus>
- Return type
- explanations_consistent_with(other, context=None)
Test whether
selfdoes not contradictother.This should only be called after confirming that
otheris notNone.- Return type
- Returns
Trueif self and other can’t both be true at the same time. Otherwise returnsFalse.
- explanations_contradiction(other, context=None)
Test whether
selfimplies()the absence ofother.This should only be called after confirming that
otheris notNone.- Return type
- Returns
Trueif self and other can’t both be true at the same time. Otherwise returnsFalse.
- explanations_implication(other, context=None)
Generate
ContextRegisters that cause self to imply other.If self is absent, then generate a ContextRegister from other’s point of view and then swap the keys and values.
- Return type
- explanations_implied_by(other, context=None)
Generate explanations for how other may imply self.
- Return type
- explanations_same_meaning(other, context=None)
Generate ways to match contexts of self and other so they mean the same.
- Return type
- generic_terms()
Get Terms that can be replaced without changing
self’s meaning.
- generic_terms_by_str()
Index Terms that can be replaced without changing
self’s meaning.
- get_factor(query)
Search for Comparable with str or name matching query.
- get_factor_by_name(name)
Search of
selfandself’s attributes forFactorwith specifiedname.- Return type
- Returns
a
Comparablewith the specifiednameattribute if it exists, otherwiseNone.
- get_factor_by_str(query)
Search of
selfandself’s attributes forFactorwith specified string.
- implied_by(other, context=None)
Find whether other implies self.
- Parameters
other (
Optional[Comparable]) – a second Comparable to compare with selfcontext (
Optional[ContextRegister]) – correspondences betweenTerms in self and other
- Returns
whether other implies self.
- implies(other, context=None)
Test whether
selfimpliesother.When inherited by
FactorGroup, this method will check whether everyStatementinotheris implied by some Statement inself.>>> from nettlesome import Entity, Comparison, Statement, FactorGroup >>> over_100y = Comparison(content="the distance between $site1 and $site2 was", sign=">", expression="100 yards") >>> under_1mi = Comparison(content="the distance between $site1 and $site2 was", sign="<", expression="1 mile") >>> protest_facts = FactorGroup( ... [Statement(predicate=over_100y, terms=[Entity(name="the political convention"), Entity(name="the police cordon")]), ... Statement(predicate=under_1mi, terms=[Entity(name="the police cordon"), Entity(name="the political convention")])]) >>> over_50m = Comparison(content="the distance between $site1 and $site2 was", sign=">", expression="50 meters") >>> under_2km = Comparison(content="the distance between $site1 and $site2 was", sign="<=", expression="2 km") >>> speech_zone_facts = FactorGroup( ... [Statement(predicate=over_50m, terms=[Entity(name="the free speech zone"), Entity(name="the courthouse")]), ... Statement(predicate=under_2km, terms=[Entity(name="the free speech zone"), Entity(name="the courthouse")])]) >>> protest_facts.implies(speech_zone_facts) True
- Return type
- implies_same_context(other)
Check if self would imply other if their generic terms are matched in order.
- Return type
- property key: str
Return string representation of self for use as a key in a ContextRegister.
- Return type
- likely_contexts(other, context=None)
Generate contexts that match Terms from Factors with corresponding meanings.
- Parameters
other (
Comparable) – an object being compared toselfcontext (
Optional[ContextRegister]) – pairs of terms that must remain matched when yielding new contexts
- Yields
ContextRegisters matching allTerms ofselfandother.- Return type
- make_generic()
Get a copy of
selfexcept ensuregenericisTrue.Note
The new object created with this method will still have all the attributes of
selfexceptgeneric=False.- Return type
- Returns
a new object changing
generictoTrue.
- make_same_context(other)
Make ContextRegister assuming all terms in self correspond to the same terms in other.
- means(other, context=None)
Test whether
selfandotherhave identical meanings.- Return type
- Returns
whether
otheris anotherFactorwith the same meaning asself.
Not the same as an equality comparison with the
==symbol, which simply convertsself's andother's fields to tuples and compares them.>>> from nettlesome import Statement, Entity >>> hades_curse = Statement(predicate="$deity cursed $target", ... terms=[Entity(name="Hades"), Entity(name="Persephone")]) >>> aphrodite_curse = Statement(predicate="$deity cursed $target", ... terms=[Entity(name="Aphrodite"), Entity(name="Narcissus")]) >>> hades_curse.means(aphrodite_curse) True
- new_context(changes)
Create new
Comparable, replacing keys ofchangeswith values.- Parameters
changes (
ContextRegister) – hasComparables to replace as keys, and has their replacements as the corresponding values.- Return type
- Returns
a new
Comparableobject with the replacements made.
- own_attributes()
Return attributes of
selfthat aren’t inherited from another class.Used for getting parameters to pass to
__init__()when generating a new object.
- possible_contexts(other, context=None)
Get permutations of generic Factor assignments not ruled out by the known context.
- Parameters
other (
Comparable) – anotherComparableobject with genericFactors- Yields
all possible ContextRegisters linking the two
Comparables- Return type
- property recursive_terms: Dict[str, nettlesome.terms.Term]
Collect self’s
terms, and theirterms, recursively.
- term_permutations()
Generate permutations of context factors that preserve same meaning.
- Return type
- property term_sequence: nettlesome.terms.TermSequence
Get
Factors used in comparisons with otherFactors.- Return type
- Returns
a tuple of attributes that are designated as the
termsfor whichever subclass ofFactorcalls this method. These can be used for comparing objects usingconsistent_with()
- update_context_register(other, context, comparison)
Find ways to update
self_mappingto allow relationshipcomparison.- Parameters
other (
Optional[Comparable]) – anotherComparablebeing compared toselfregister – keys representing
Comparables fromself’s context and values representingComparables inother’s context.comparison (
Callable) – a function defining the comparison that must beTruebetweenselfandother. Could beComparable.means()orComparable.__ge__().
- Yields
every way that
self_mappingcan be updated to be consistent withselfandotherhaving the relationshipcomparison.- Return type
- class nettlesome.terms.TermSequence(value: Union[nettlesome.terms.Term, Sequence[Optional[nettlesome.terms.Term]]] = ())
A sequence of Terms that can be compared in order.
- static __new__(cls, value=())
Convert Sequence of Terms to a subclass of Tuple.
- ordered_comparison(other, operation, context=None)
Find ways for a series of pairs of
Termsterms to satisfy a comparison.- Parameters
context (
Optional[ContextRegister]) – keys representing terms inselfand values representing terms inother. The keys and values have been found in corresponding positions inselfandother.- Yields
every way that
matchescan be updated to be consistent with each element ofself.need_matcheshaving the relationshipself.comparisonwith the item at the corresponding index ofself.available.- Return type
- nettlesome.terms.consistent_with(left, right, context=None)
Call
Factor.consistent_with()as function alias.This exists because
Factor._context_registers()needs a function rather than a method for the comparison variable.- Return type
- Returns
whether
otheris consistent withself.
- nettlesome.terms.means(left, right)
Call
Factor.means()as function alias.This exists because
Explanationobjects expect a function rather than a method- Return type
- Returns
whether
otheris anotherFactorwith the same meaning asself.
- nettlesome.terms.contradicts(left, right)
Call
Factor.contradicts()as function alias.This exists because
Explanationobjects expect a function rather than a method- Return type
- Returns
whether
otheris anotherFactorthat can contradictself, assuming relevant context factors
- @nettlesome.terms.new_context_helper(func)
Search
Factorfor genericFactors to use in new context.Decorators for memoizing generic
Factors. Used when changing an abstractRulefrom one concrete context to another.If a
listhas been passed in rather than adict, uses the input as a series ofFactors to replace thegeneric_termsfrom the calling object.Also, if
changescontains a replacement for the calling object, the decorator returns the replacement and never calls the decorated function.- Parameters
factor – a
Factorthat is having its genericFactors replaced to change context (for instance, to change to the context of a different case involving parties represented by differentEntityobjects).changes – indicates which generic
Factors withinfactorshould be replaced and what they should be replaced with.
- Returns
a new
Factorobject in the new context.