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
self
impliesother
- __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
ContextRegister
as it goes down the sequence ofFactor
pairs. Perhaps(?) this simpler process can weed outFactor
s that clearly don’t satisfy a comparison before moving on to the more costlyAnalogy
process. 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
terms
ofself
andother
, such that they fit the relationshipcomparison
.
- contradicts(other, context=None)
Test whether
self
implies the absence ofother
.- Return type
- Returns
True
if 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 oneExplanation
of how an analogy between the generic terms of the two objects can make them contradictory.In this example using two
FactorGroup
s, if the threeStatement
s inbrexit
were asserted about the threeEntity
terms 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
self
does not contradictother
.This should only be called after confirming that
other
is notNone
.- Return type
- Returns
True
if self and other can’t both be true at the same time. Otherwise returnsFalse
.
- explanations_contradiction(other, context=None)
Test whether
self
implies()
the absence ofother
.This should only be called after confirming that
other
is notNone
.- Return type
- Returns
True
if self and other can’t both be true at the same time. Otherwise returnsFalse
.
- explanations_implication(other, context=None)
Generate
ContextRegister
s 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
self
andself
’s attributes forFactor
with specifiedname
.- Return type
- Returns
a
Comparable
with the specifiedname
attribute if it exists, otherwiseNone
.
- get_factor_by_str(query)
Search of
self
andself
’s attributes forFactor
with 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 betweenTerm
s in self and other
- Returns
whether other implies self.
- implies(other, context=None)
Test whether
self
impliesother
.When inherited by
FactorGroup
, this method will check whether everyStatement
inother
is 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 toself
context (
Optional
[ContextRegister
]) – pairs of terms that must remain matched when yielding new contexts
- Yields
ContextRegister
s matching allTerm
s ofself
andother
.- Return type
- make_generic()
Get a copy of
self
except ensuregeneric
isTrue
.Note
The new object created with this method will still have all the attributes of
self
exceptgeneric=False
.- Return type
- Returns
a new object changing
generic
toTrue
.
- make_same_context(other)
Make ContextRegister assuming all terms in self correspond to the same terms in other.
- means(other, context=None)
Test whether
self
andother
have identical meanings.- Return type
- Returns
whether
other
is anotherFactor
with 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 ofchanges
with values.- Parameters
changes (
ContextRegister
) – hasComparable
s to replace as keys, and has their replacements as the corresponding values.- Return type
- Returns
a new
Comparable
object with the replacements made.
- own_attributes()
Return attributes of
self
that 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
) – anotherComparable
object with genericFactor
s- Yields
all possible ContextRegisters linking the two
Comparable
s- 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
Factor
s used in comparisons with otherFactor
s.- Return type
- Returns
a tuple of attributes that are designated as the
terms
for whichever subclass ofFactor
calls this method. These can be used for comparing objects usingconsistent_with()
- update_context_register(other, context, comparison)
Find ways to update
self_mapping
to allow relationshipcomparison
.- Parameters
other (
Optional
[Comparable
]) – anotherComparable
being compared toself
register – keys representing
Comparable
s fromself
’s context and values representingComparable
s inother
’s context.comparison (
Callable
) – a function defining the comparison that must beTrue
betweenself
andother
. Could beComparable.means()
orComparable.__ge__()
.
- Yields
every way that
self_mapping
can be updated to be consistent withself
andother
having 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
Terms
terms to satisfy a comparison.- Parameters
context (
Optional
[ContextRegister
]) – keys representing terms inself
and values representing terms inother
. The keys and values have been found in corresponding positions inself
andother
.- Yields
every way that
matches
can be updated to be consistent with each element ofself.need_matches
having the relationshipself.comparison
with 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
other
is consistent withself
.
- nettlesome.terms.means(left, right)
Call
Factor.means()
as function alias.This exists because
Explanation
objects expect a function rather than a method- Return type
- Returns
whether
other
is anotherFactor
with the same meaning asself
.
- nettlesome.terms.contradicts(left, right)
Call
Factor.contradicts()
as function alias.This exists because
Explanation
objects expect a function rather than a method- Return type
- Returns
whether
other
is anotherFactor
that can contradictself
, assuming relevant context factors
- @nettlesome.terms.new_context_helper(func)
Search
Factor
for genericFactor
s to use in new context.Decorators for memoizing generic
Factor
s. Used when changing an abstractRule
from one concrete context to another.If a
list
has been passed in rather than adict
, uses the input as a series ofFactor
s to replace thegeneric_terms
from the calling object.Also, if
changes
contains a replacement for the calling object, the decorator returns the replacement and never calls the decorated function.- Parameters
factor – a
Factor
that is having its genericFactor
s replaced to change context (for instance, to change to the context of a different case involving parties represented by differentEntity
objects).changes – indicates which generic
Factor
s withinfactor
should be replaced and what they should be replaced with.
- Returns
a new
Factor
object in the new context.