2to3 (compiles, not tested)
[tag_parser.git] / src / mjacob / nltk / parse / tag / AbstractTagChartRule.py
blob2ffae546f948b94da414a1f5bb5c56ac2d42470e
1 # This Python file uses the following encoding: utf-8
2 '''
3 Created on May 18, 2011
6 @author: mjacob
7 '''
8 from itertools import product
9 from nltk.parse.chart import AbstractChartRule
11 class AbstractTagChartRule(AbstractChartRule):
12 def application_filter(self, chart, grammar):
13 return {}
15 def applies_to(self, chart, grammar, edge):
16 """
17 i was going to do some sophisticated caching of this data, but
18 now i'm pretty sure it's only called once for every edge/rule combo
19 so that would be a complete waste
20 """
21 for key, value in list(self.application_filter(chart, grammar).items()):
22 if not hasattr(edge, key):
23 raise ValueError("unexpected key %s" % (key))
24 if getattr(edge, key)() != value:
25 return False
26 return True
28 # Default: loop through the given number of edges, and call
29 # self.apply() for each set of edges.
30 def apply_everywhere_iter(self, chart, grammar):
31 for edges in product(chart, repeat=self.NUM_EDGES):
32 for new_edge in self.apply_iter(chart, grammar, *edges):
33 yield new_edge
35 def apply(self, *args):
36 super(AbstractTagChartRule, self).apply(*args)