1 # This Python file uses the following encoding: utf-8
3 Created on May 18, 2011
8 from itertools
import product
9 from nltk
.parse
.chart
import AbstractChartRule
11 class AbstractTagChartRule(AbstractChartRule
):
12 def application_filter(self
, chart
, grammar
):
15 def applies_to(self
, chart
, grammar
, edge
):
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
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
:
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
):
35 def apply(self
, *args
):
36 super(AbstractTagChartRule
, self
).apply(*args
)