diff python/tree.py @ 320:84d67cce67b7

Working burg
author Windel Bouwman
date Sun, 19 Jan 2014 16:09:44 +0100
parents 8d07a4254f04
children 44f336460c2a
line wrap: on
line diff
--- a/python/tree.py	Sat Jan 18 18:58:43 2014 +0100
+++ b/python/tree.py	Sun Jan 19 16:09:44 2014 +0100
@@ -14,15 +14,19 @@
 
 
 class State:
+    """ State used to label tree nodes """
     def __init__(self):
         self.labels = {}
+
     def has_goal(self, goal):
         return goal in self.labels
+
     def get_cost(self, goal):
-        if not self.has_goal(goal): return 999999
         return self.labels[goal][0]
+
     def get_rule(self, goal):
         return self.labels[goal][1]
+
     def set_cost(self, goal, cost, rule):
         if self.has_goal(goal):
             if self.get_cost(goal) > cost:
@@ -30,7 +34,9 @@
         else:
             self.labels[goal] = (cost, rule)
 
+
 class BaseMatcher:
+    """ Base class for matcher objects. """
     def kids(self, tree, rule):
         return self.kid_functions[rule](tree)
 
@@ -38,12 +44,13 @@
         return self.nts_map[rule]
 
     def burm_label(self, tree):
+        """ Label all nodes in the tree bottom up """
         for c in tree.children:
             self.burm_label(c)
         self.burm_state(tree)
 
     def apply_rules(self, tree, goal):
-        print(tree.state.get_rule(goal))
         rule = tree.state.get_rule(goal)
-        for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule)):
-            self.apply_rules(kid_tree, kid_goal)
+        results = [self.apply_rules(kid_tree, kid_goal)
+         for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule))]
+        self.pat_f[rule](*results)