nltk.tbl.rule module

class nltk.tbl.rule.TagRule[source]

Bases: object

An interface for tag transformations on a tagged corpus, as performed by tbl taggers. Each transformation finds all tokens in the corpus that are tagged with a specific original tag and satisfy a specific condition, and replaces their tags with a replacement tag. For any given transformation, the original tag, replacement tag, and condition are fixed. Conditions may depend on the token under consideration, as well as any other tokens in the corpus.

Tag rules must be comparable and hashable.

__init__(original_tag, replacement_tag)[source]
original_tag

The tag which this TagRule may cause to be replaced.

replacement_tag

The tag with which this TagRule may replace another tag.

apply(tokens, positions=None)[source]

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.

Parameters
  • tokens (list(tuple(str, str))) – The tagged sentence

  • positions (list(int)) – The positions where the transformation is to be tried. If not specified, try it at all positions.

Returns

The indices of tokens whose tags were changed by this rule.

Return type

int

abstract applies(tokens, index)[source]
Returns

True if the rule would change the tag of tokens[index], False otherwise

Return type

bool

Parameters
  • tokens (list(str)) – A tagged sentence

  • index (int) – The index to check

class nltk.tbl.rule.Rule[source]

Bases: nltk.tbl.rule.TagRule

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}.

json_tag = 'nltk.tbl.Rule'
__init__(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.

Parameters
  • templateid (string) – the template id (a zero-padded string, ‘001’ etc, so it will sort nicely)

  • conditions (C{iterable} of C{Feature}) – 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}.

encode_json_obj()[source]
classmethod decode_json_obj(obj)[source]
applies(tokens, index)[source]
Returns

True if the rule would change the tag of tokens[index], False otherwise

Return type

bool

Parameters
  • tokens (list(str)) – A tagged sentence

  • index (int) – The index to check

format(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 (str) – format specification

Returns

string representation

Return type

str