Package parser_course :: Package small_parsers :: Module very_simple_cky_parser
[hide private]
[frames] | no frames]

Module very_simple_cky_parser

source code

Classes [hide private]
  CKYParser
Parse strings in self.data using CKY algorithm (see SmallParser methods too):
  BURule
BURules are pairs of next daughters and mother cats.
  cnf_grammar
Initialized with an arbitrary context-free grammar, this turns the grammar into a cnf grammar and computes a bottum up grammar suitable for efficient use with a CKY parser.
  Ctr
Functions [hide private]
 
chart_edge_template()
This template function merely returns a triple of 'tuple_attributes':
source code
 
make_cnf_grammar(base_grammar, prefix='X')
Return a new instance of class Grammar containing a grammar equivalent to base_grammar's grammar but in Chomsky normal form.
source code
 
process_chain(root_cat, current_cat, chains, base_grammar, new_grammar, new_rules, seen) source code
 
process_noncnf_rule(expansion, expansions, new_cats, grammar)
new_cats is a dictionary whose keys are tuples and whose values are new categories expanding as those tuples, used to prevent hypothesizing two distinct new categories expanding as the same sequence.
source code
 
extend_preterminals(grammar, current_cat, root_cat) source code
 
make_new_cat(grammar) source code
 
update_list_dict(list_dict, key, element) source code
Function Details [hide private]

chart_edge_template()

source code 

This template function merely returns a triple of 'tuple_attributes':

 >>> ChartEdgeTemplate()
 ('end','start','mother_cat')

Executing the following then creates a class called ChartEdge with those 3 attributes:

 >>> ChartEdge = supertuple.superTuplePlus('ChartEdge',ChartEdgeTemplate())

The ChartEdge class that ChartEdgeTemplate spawns is documented here.

ChartEdges are tuples of end, start, and mother_cat, with gettable attributes with those names provided for each instance.

Now we create an instance of the new class:

  >>> e1 = ChartEdge(end= 1,start=0,mother_cat='s')
  >>> e1
  ChartEdge(1, 0, 's')
  >>> e1.mother_cat
  's'

This new class is a subclass of tuple and its instances really are tuple instances supporting all the usual tuple methods:

 >>> e1[2]
 's'

They just have some extras, like the keyword access illustrated above and a keyword display method:

 >>> e1.display()
 end: 1
 start: 0
 mother_cat: s

To test if e2 and e1 are equivalent: e1 == e2 (same as tuples) To test if an edge equivalent to e1 occurs in list L:

          e1 in L (same as tuples)

To find the index of an edge equivalent to e1 in list L:

   L.index(e1) (same as tuples)

Note: Raises ValueError if L contains no such edge.

make_cnf_grammar(base_grammar, prefix='X')

source code 

Return a new instance of class Grammar containing a grammar equivalent to base_grammar's grammar but in Chomsky normal form.

The base_grammar class inherited from must define a grammar.