class documentation

A Rule checks the current corpus position for a certain set of conditions; if they are all fulfilled, the Rule is triggered, meaning that it will change tag A to tag B. For other tags than A, nothing happens.

The conditions are parameters to the Rule instance. Each condition is a feature-value pair, with a set of positions to check for the value of the corresponding feature. Conceptually, the positions are joined by logical OR, and the feature set by logical AND.

More formally, the Rule is then applicable to the M{n}th token iff:

  • The M{n}th token is tagged with the Rule's original tag; and

  • For each (Feature(positions), M{value}) tuple: - The value of Feature of at least one token in {n+p for p in positions}

    is M{value}.

Class Method decode_json_obj Undocumented
Method __eq__ Undocumented
Method __hash__ Undocumented
Method __init__ Construct a new Rule that changes a token's tag from C{original_tag} to C{replacement_tag} if all of the properties specified in C{conditions} hold.
Method __ne__ Undocumented
Method __repr__ Undocumented
Method __str__ Undocumented
Method applies No summary
Method encode_json_obj Undocumented
Method format Return a string representation of this rule.
Class Variable json_tag Undocumented
Instance Variable templateid Undocumented
Method _verbose_format Return a wordy, human-readable string representation of the given rule.
Instance Variable __hash Undocumented
Instance Variable __repr Undocumented
Instance Variable _conditions Undocumented

Inherited from TagRule:

Method apply Apply this rule at every position in positions where it applies to the given sentence. I.e., for each position p in positions, if tokens[p] is tagged with this rule's original tag, and satisfies this rule's condition, then set its tag to be this rule's replacement tag.
Instance Variable original_tag The tag which this TagRule may cause to be replaced.
Instance Variable replacement_tag The tag with which this TagRule may replace another tag.
@classmethod
def decode_json_obj(cls, obj): (source)

Undocumented

def __eq__(self, other): (source)

Undocumented

def __hash__(self): (source)

Undocumented

def __init__(self, templateid, original_tag, replacement_tag, conditions): (source)

Construct a new Rule that changes a token's tag from C{original_tag} to C{replacement_tag} if all of the properties specified in C{conditions} hold.

@type templateid: string @param templateid: the template id (a zero-padded string, '001' etc,

so it will sort nicely)

@type conditions: C{iterable} of C{Feature} @param conditions: A list of Feature(positions),

each of which specifies that the property (computed by Feature.extract_property()) of at least one token in M{n} + p in positions is C{value}.
def __ne__(self, other): (source)

Undocumented

def __repr__(self): (source)

Undocumented

def __str__(self): (source)

Undocumented

def applies(self, tokens, index): (source)
Parameters
tokens:list(str)A tagged sentence
index:intThe index to check
Returns
boolTrue if the rule would change the tag of tokens[index], False otherwise
def encode_json_obj(self): (source)

Undocumented

def format(self, fmt): (source)

Return a string representation of this rule.

>>> from nltk.tbl.rule import Rule
>>> from nltk.tag.brill import Pos
>>> r = Rule("23", "VB", "NN", [(Pos([-2,-1]), 'DT')])

r.format("str") == str(r) True >>> r.format("str") 'VB->NN if Pos:DT@[-2,-1]'

r.format("repr") == repr(r) True >>> r.format("repr") "Rule('23', 'VB', 'NN', [(Pos([-2, -1]),'DT')])"

>>> r.format("verbose")
'VB -> NN if the Pos of words i-2...i-1 is "DT"'
>>> r.format("not_found")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "nltk/tbl/rule.py", line 256, in format
    raise ValueError("unknown rule format spec: {0}".format(fmt))
ValueError: unknown rule format spec: not_found
>>>
Parameters
fmt:strformat specification
Returns
strstring representation
json_tag: str = (source)

Undocumented

templateid = (source)

Undocumented

def _verbose_format(self): (source)

Return a wordy, human-readable string representation of the given rule.

Not sure how useful this is.

Undocumented

Undocumented

_conditions = (source)

Undocumented