Package nltk_lite :: Package contrib :: Package mit :: Package six863 :: Package parse :: Module category :: Class Category
[hide private]
[frames] | no frames]

Class Category

source code

??-158 --+
         |
??-159 --+
         |
        Category

A Category is a wrapper for feature dictionaries, intended for use in parsing. It can act as a Nonterminal.

A Category acts like a dictionary, except in the following ways:

Categories can contain any kind of object as their values, and can be recursive and even re-entrant. Categories are not necessarily "categories all the way down"; they can contain plain dictionaries as their values, and converting inner dictionaries to categories would probably lead to messier code for no gain.

Because Categories can contain any kind of object, they do not try to keep control over what their inner objects do. If you freeze a Category but mutate its inner objects, undefined behavior will occur.

Instance Methods [hide private]
 
__init__(self, features=None, **morefeatures) source code
 
__cmp__(self, other) source code
 
__div__(self, other)
Returns: A new Category based on this one, with its / feature set to other.
source code
bool
__eq__(self, other)
Compare Categories for equality.
source code
 
__ne__(self, other) source code
 
__hash__(self) source code
 
freeze(self)
Freezing a Category memoizes its hash value, to make comparisons on it faster.
source code
bool
frozen(self)
Returns whether this Category is frozen (immutable).
source code
 
get(self, key) source code
 
__getitem__(self, key) source code
 
__setitem__(self, key, value) source code
 
items(self) source code
 
keys(self) source code
 
values(self) source code
 
has_key(self, key) source code
Category
symbol(self)
Returns: The node value corresponding to this Category.
source code
str or None
head(self)
Returns: The head of this category (the value shown outside the brackets in its string representation).
source code
 
copy(self)
Returns: A deep copy of self.
source code
 
feature_names(self)
Returns: a list of all features that have values.
source code
 
has_feature(self, key) source code
 
remove_unbound_vars(self) source code
 
__repr__(self)
Returns: A string representation of this feature structure.
source code
 
__str__(self)
Returns: A string representation of this feature structure.
source code
Class Methods [hide private]
 
_str(cls, obj, reentrances, reentrance_ids) source code
 
to_yaml(cls, dumper, data) source code
 
from_yaml(cls, loader, node) source code
 
parse(cls, s) source code
 
inner_parse(cls, s, position, reentrances={}) source code
 
_parse(cls, s, position=0, reentrances=None)
Helper function that parses a Category.
source code
 
_parseval(cls, s, position, reentrances)
Helper function that parses a feature value.
source code
 
parse_rules(cls, s)
Parse a CFG line involving Categories.
source code
Static Methods [hide private]
 
_remove_unbound_vars(obj) source code
Class Variables [hide private]
  headname = 'head'
  yaml_tag = '!parse.Category'
  _PARSE_RE = {'name': re.compile(r'\s*([^\s\(\)"\'=,\[\]/\?]+)\...
Method Details [hide private]

__div__(self, other)

source code 
Returns:
A new Category based on this one, with its / feature set to other.

__eq__(self, other)
(Equality operator)

source code 

Compare Categories for equality. This relies on Python's built-in __eq__ for dictionaries, which is fairly thorough in checking for recursion and reentrance.

Returns: bool
True if self and other assign the same value to every feature. In particular, return true if self[p]==other[p] for every feature path p such that self[p] or other[p] is a base value (i.e., not a nested Category).

freeze(self)

source code 

Freezing a Category memoizes its hash value, to make comparisons on it faster. After freezing, the Category and all its values are immutable.

Returns:
self

symbol(self)

source code 
Returns: Category
The node value corresponding to this Category.

head(self)

source code 
Returns: str or None
The head of this category (the value shown outside the brackets in its string representation). If there is no head, returns None.

copy(self)

source code 
Returns:
A deep copy of self.

feature_names(self)

source code 
Returns:
a list of all features that have values.

__repr__(self)
(Representation operator)

source code 
Returns:
A string representation of this feature structure.

__str__(self)
(Informal representation operator)

source code 
Returns:
A string representation of this feature structure.

_parse(cls, s, position=0, reentrances=None)
Class Method

source code 

Helper function that parses a Category.

Parameters:
  • s - The string to parse.
  • position - The position in the string to start parsing.
  • reentrances - A dictionary from reentrance ids to values.
Returns:
A tuple (val, pos) of the feature structure created by parsing and the position where the parsed feature structure ends.

_parseval(cls, s, position, reentrances)
Class Method

source code 

Helper function that parses a feature value. Currently supports: None, bools, integers, variables, strings, nested feature structures.

Parameters:
  • s - The string to parse.
  • position - The position in the string to start parsing.
  • reentrances - A dictionary from reentrance ids to values.
Returns:
A tuple (val, pos) of the value created by parsing and the position where the parsed value ends.

parse_rules(cls, s)
Class Method

source code 

Parse a CFG line involving Categories. A line has this form:

lhs -> rhs | rhs | ...

where lhs is a Category, and each rhs is a sequence of Categories.

Returns:
a list of Productions, one for each rhs.

Class Variable Details [hide private]

_PARSE_RE

Value:
{'name': re.compile(r'\s*([^\s\(\)"\'=,\[\]/\?]+)\s*'), 'ident': re.co\
mpile(r'\s*\((\d+)\)\s*'), 'reentrance': re.compile(r'\s*->\s*'), 'ass\
ign': re.compile(r'\s*=?\s*'), 'bracket': re.compile(r'\s*]\s*'), 'com\
ma': re.compile(r'\s*,\s*'), 'none': re.compile(r'None(?=\s|\]|,)'), '\
int': re.compile(r'-?\d+(?=\s|\]|,)'), 'var': re.compile(r'\?[a-zA-Z_]\
[a-zA-Z0-9_]*'+ '|'+ r'\?<[a-zA-Z_][a-zA-Z0-9_]*'+ r'(=[a-zA-Z_][a-zA-\
Z0-9_]*)*>'), 'symbol': re.compile(r'\w+'), 'stringmarker': re.compile\
("['\"\\\\]"), 'categorystart': re.compile(r'\s*([^\s\(\)"\'\-=,\[\]/\\
...