Package nltk :: Module featstruct :: Class FeatStructParser
[hide private]
[frames] | no frames]

Class FeatStructParser

source code

object --+
         |
        FeatStructParser

Instance Methods [hide private]
 
__init__(self, features=(*slash*, *type*), fdict_class=<class 'nltk.featstruct.FeatStruct'>, flist_class=<class 'nltk.featstruct.FeatList'>)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
parse(self, s, fstruct=None)
Convert a string representation of a feature structure (as displayed by repr) into a FeatStruct.
source code
 
partial_parse(self, s, position=0, reentrances=None, fstruct=None)
Helper function that parses a feature structure.
source code
 
_partial_parse(self, s, position, reentrances, fstruct=None) source code
 
_partial_parse_featlist(self, s, position, match, reentrances, fstruct) source code
 
_partial_parse_featdict(self, s, position, match, reentrances, fstruct) source code
 
_finalize(self, s, pos, reentrances, fstruct)
Called when we see the close brace -- checks for a slash feature, and adds in default values.
source code
 
_parse_value(self, name, s, position, reentrances) source code
 
parse_value(self, s, position, reentrances) source code
 
_error(self, s, expected, position) source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

    Value Parsers
 
parse_fstruct_value(self, s, position, reentrances, match) source code
 
parse_str_value(self, s, position, reentrances, match) source code
 
parse_int_value(self, s, position, reentrances, match) source code
 
parse_var_value(self, s, position, reentrances, match) source code
 
parse_sym_value(self, s, position, reentrances, match) source code
 
parse_app_value(self, s, position, reentrances, match)
Mainly included for backwards compat.
source code
 
parse_logic_value(self, s, position, reentrances, match) source code
 
parse_tuple_value(self, s, position, reentrances, match) source code
 
parse_set_value(self, s, position, reentrances, match) source code
 
_parse_seq_value(self, s, position, reentrances, match, close_paren, seq_class, plus_class)
Helper function used by parse_tuple_value and parse_set_value.
source code
Class Variables [hide private]
  _START_FSTRUCT_RE = re.compile(r'\s*(?:\((\d+)\)\s*)?(\??[\w-]...
  _END_FSTRUCT_RE = re.compile(r'\s*\]\s*')
  _SLASH_RE = re.compile(r'/')
  _FEATURE_NAME_RE = re.compile(r'\s*([\+-]?)([^\s\(\)<>"\'-=\[\...
  _REENTRANCE_RE = re.compile(r'\s*->\s*')
  _TARGET_RE = re.compile(r'\s*\((\d+)\)\s*')
  _ASSIGN_RE = re.compile(r'\s*=\s*')
  _COMMA_RE = re.compile(r'\s*,\s*')
  _BARE_PREFIX_RE = re.compile(r'\s*(?:\((\d+)\)\s*)?(\??[\w-]+\...
  _START_FDICT_RE = re.compile(r'(\s*(?:\((\d+)\)\s*)?(\??[\w-]+...
    Value Parsers
  VALUE_HANDLERS = [('parse_fstruct_value', re.compile(r'\s*(?:\...
A table indicating how feature values should be parsed.
  _SYM_CONSTS = {'False': False, 'None': None, 'True': True}
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, features=(*slash*, *type*), fdict_class=<class 'nltk.featstruct.FeatStruct'>, flist_class=<class 'nltk.featstruct.FeatList'>)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

parse(self, s, fstruct=None)

source code 

Convert a string representation of a feature structure (as displayed by repr) into a FeatStruct. This parse imposes the following restrictions on the string representation:

  • Feature names cannot contain any of the following: whitespace, parenthases, quote marks, equals signs, dashes, commas, and square brackets. Feature names may not begin with plus signs or minus signs.
  • Only the following basic feature value are supported: strings, integers, variables, None, and unquoted alphanumeric strings.
  • For reentrant values, the first mention must specify a reentrance identifier and a value; and any subsequent mentions must use arrows ('->') to reference the reentrance identifier.

partial_parse(self, s, position=0, reentrances=None, fstruct=None)

source code 

Helper function that parses a feature structure.

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

Class Variable Details [hide private]

_START_FSTRUCT_RE

Value:
re.compile(r'\s*(?:\((\d+)\)\s*)?(\??[\w-]+)?(\[)')

_FEATURE_NAME_RE

Value:
re.compile(r'\s*([\+-]?)([^\s\(\)<>"\'-=\[\],]+)\s*')

_BARE_PREFIX_RE

Value:
re.compile(r'\s*(?:\((\d+)\)\s*)?(\??[\w-]+\s*)()')

_START_FDICT_RE

Value:
re.compile(r'(\s*(?:\((\d+)\)\s*)?(\??[\w-]+\s*)())|(\s*(?:\((\d+)\)\s\
*)?(\??[\w-]+)?(\[)\s*(\s*([\+-]?)([^\s\(\)<>"\'-=\[\],]+)\s*\s*(=|->)\
|[\+-]\s*([\+-]?)([^\s\(\)<>"\'-=\[\],]+)\s*|\]))')

VALUE_HANDLERS

A table indicating how feature values should be parsed. Each entry in the table is a pair (handler, regexp). The first entry with a matching regexp will have its handler called. Handlers should have the following signature:

  def handler(s, position, reentrances, match): ...

and should return a tuple (value, position), where position is the string position where the value ended. (n.b.: order is important here!)

Value:
[('parse_fstruct_value',
  re.compile(r'\s*(?:\((\d+)\)\s*)?(\??[\w-]+)?(\[)')),
 ('parse_var_value', re.compile(r'\?[a-zA-Z_][a-zA-Z0-9_]*')),
 ('parse_str_value', re.compile(r'[uU]?[rR]?([\'"])')),
 ('parse_int_value', re.compile(r'-?\d+')),
 ('parse_sym_value', re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*')),
 ('parse_app_value',
  re.compile(r'<(app)\((\?[a-z][a-z]*)\s*,\s*(\?[a-z][a-z]*)\)>')),
...