lango.matcher module

lango.matcher.get_object(tree)

Get the object in the tree object.

Method should remove unnecessary letters and words:

the
a/an
's
Parameters:tree (Tree) – Parsed tree structure
Returns:Resulting string of tree (Ex: "red car")
lango.matcher.get_proper_word(tree)

Get unmodified words in the tree object

Parameters:tree (Tree) – Parsed tree structure
Returns:Resulting string of tree (Ex: "The red car")
lango.matcher.get_tokens(tokens)

Recursively gets tokens from a match list

Parameters:tokens – List of tokens [‘(‘, ‘S’, ‘(‘, ‘NP’, ‘)’, ‘)’]
Returns:Stack of tokens
lango.matcher.get_word(tree)

Get the exact words in lowercase in the tree object.

Parameters:tree (Tree) – Parsed tree structure
Returns:Resulting string of tree (Ex: "the red car")
lango.matcher.match_rules(tree, rules, fun)

Matches a Tree structure with the given query rules.

Query rules are represented as a dictionary of template to action. Action is either a function, or a dictionary of subtemplate parameter to rules:

rules = { 'template' : { 'key': rules } }
      | { 'template' : {} }
Parameters:
  • tree (Tree) – Parsed tree structure
  • rules (dict) – A dictionary of query rules
  • fun – Function to call
Returns:

Result of function call with context or None if nothing matched

lango.matcher.match_rules_context(tree, rules, parent_context={})

Recursively matches a Tree structure with rules and returns context

Parameters:
  • tree (Tree) – Parsed tree structure
  • rules (dict) – See match_rules
  • parent_context (dict) – Context of parent call
Returns:

Context matched dictionary of matched rules or None if no match

Return type:

dict

lango.matcher.match_template(tree, template, args=None)

Check if match string matches Tree structure

Parameters:
  • tree (Tree) – Parsed Tree structure of a sentence
  • template (str) – String template to match. Example: “( S ( NP ) )”
Returns:

If they match or not

Return type:

bool

lango.matcher.match_tokens(tree, tokens, args)

Check if stack of tokens matches the Tree structure

Special matching rules that can be specified in the template:

':label': Label a token, the token will be returned as part of the context with key 'label'.
'-@': Additional single letter argument determining return format of labeled token. Valid options are:
    '-w': Return token as word
    '-o': Return token as object
'=word|word2|....|wordn': Force match the token words
'$': Force match the number of tokens
Parameters:
  • tree – Parsed tree structure
  • tokens – Stack of tokens
Returns:

Boolean if they match or not