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 |
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 |
Undocumented |
Method | format |
Return a string representation of this rule. |
Class Variable | json |
Undocumented |
Instance Variable | templateid |
Undocumented |
Method | _verbose |
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 |
The tag which this TagRule may cause to be replaced. |
Instance Variable | replacement |
The tag with which this TagRule may replace another tag. |
nltk.tbl.rule.TagRule.__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.
@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}.
nltk.tbl.rule.TagRule.applies
Parameters | |
tokens:list(str) | A tagged sentence |
index:int | The index to check |
Returns | |
bool | True if the rule would change the tag of tokens[index], False otherwise |
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:str | format specification |
Returns | |
str | string representation |