Factor Groups

class nettlesome.groups.FactorGroup(factors=())

Terms to be used together in a comparison.

add(other)

Combine all Factors into a single FactorGroup.

Return type

Optional[FactorGroup]

add_or_raise_error(other)

Combine all Factors into a single FactorGroup.

Return type

FactorGroup

consistent_with(other, context=None)

Find whether two sets of Factors can be consistent.

Works by first determining whether one Factor potentially contradicts() another, and then determining whether it’s possible to make context assignments match between the contradictory Factors.

Parameters
Return type

bool

Returns

whether unassigned context factors can be assigned in such a way that there’s no contradiction between any factor in self_factors and other_factors, given that some Factors have already been assigned as described by matches.

contradicts(other, context=None)

Find whether two sets of Factors can be contradictory.

Parameters
  • other (Optional[Comparable]) – a second set of Factors with context factors that are internally consistent, but may not be consistent with self_factors.

  • context (Optional[ContextRegister]) – correspondences between Factors in self and other that can’t be changed in seeking a contradiction

Return type

bool

Returns

whether any Factor assignment can be found that makes a Factor in the output of other contradict a Factor in the output of self.

>>> from nettlesome import Statement, Entity
>>> 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)])
>>> nafta.contradicts(brexit)
True
drop_implied_factors()

Reduce group by removing redundant members implied by other members.

Return type

FactorGroup

Returns

new group with any redundant items remomved

explanations_contradiction(other, context=None)

Find contexts that would cause self to contradict other.

In this example, by adding a context parameter to this method’s comparison of two FactorGroups for contradiction, we can narrow down how nettlesome discovers analogies between the Entity objects. The result is that nettlesome finds only two Explanations for how a contradiction can exist.

>>> from nettlesome import Statement, Entity
>>> 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)])
>>> explanations_usa_like_uk = nafta.explanations_contradiction(
...     brexit,
...     context=([Entity(name="USA")], [Entity(name="UK")]))
>>> len(list(explanations_usa_like_uk))
2
Return type

Iterator[Explanation]

explanations_implication(other, context=None)

Find contexts that would cause self to imply other.

Return type

Iterator[Explanation]

explanations_implied_by(other, context=None)

Generate explanations for how other may imply self.

Return type

Iterator[Explanation]

explanations_same_meaning(other, context=None)

Yield explanations for how self can have the same meaning as other.

Return type

Iterator[Explanation]

explanations_union(other, context=None)

Yield contexts that allow self and other to be combined with the union operation.

Return type

Iterator[ContextRegister]

from_comparable(value)

Create a FactorGroup from a Factor or sequence of Factors.

Return type

Optional[FactorGroup]

generic_terms_by_str()

Index Terms that can be replaced without changing self’s meaning.

Return type

Dict[str, Term]

has_all_factors_of(other, context=None)

Check if self has all Factors of other.

Return type

bool

internally_consistent()

Check for contradictions among the Factors in self.

Return type

None

Returns

bool indicating whether self is internally consistent

likely_contexts(other, context=None)

Yield likely contexts based on similar Factor meanings.

Return type

Iterator[ContextRegister]

new_context(changes)

Use ContextRegister to choose changes to self’s context.

Return type

FactorGroup

property recursive_terms: Dict[str, nettlesome.terms.Term]

Collect self’s terms, and their terms, recursively.

Return type

Dict[str, Term]

Returns

a dict (instead of a set, to preserve order) of Factors.

shares_all_factors_with(other, context=None)

Find whether all of self’s Factors are in other.

Return type

bool

term_class

alias of nettlesome.factors.Factor

union(other, context=None)

Make new FactorGroup with the set of unique Factors from both self and other.

Return type

Optional[FactorGroup]

@nettlesome.groups.unique_explanations(func)

Filter out any duplicate Explanations before yielding them from func.