Exemplo n.º 1
0
 def apply_annotators(self, chart, tokens, i, j):
     """Add parses to chart cell (i, j) by applying annotators."""
     if self.annotators:
         words = [t['word'] for t in tokens]
         for annotator in self.annotators:
             for category, semantics in annotator.annotate(tokens[i:j]):
                 rule = Rule(category, tuple(words[i:j]), semantics)
                 chart[(i, j)].append(Parse(rule, words[i:j]))
Exemplo n.º 2
0
 def apply_aliases(self, chart, words, i, j):
     """Add parses to chart cell (i, j) by applying user lists."""
     if self.aliases:
         key = ' '.join(words[i:j])
         if key in self.aliases:
             lhs = '$UserList'
             rhs = tuple(key.split())
             semantics = ('.alias', ('.string', key))
             rule = Rule(lhs, rhs, semantics)
             chart[(i, j)].append(Parse(rule, words[i:j]))
Exemplo n.º 3
0
 def apply_absorb_rules(self, chart, i, j):
     """Add parses to chart cell (i, j) that require absorbing."""
     if j - i > 2: # Otherwise, there's no chance for absorption
         for m in range(i + 1, j - 1):
             for n in range(m + 1, j):
                 for parse_1, parse_2 in product(chart[(i, m)], chart[(n, j)]):
                     # Don't absorb unmatched quote marks
                     if sum(parse.rule.lhs=='$Quote' for p in range(m, n) for parse in chart[(p, p+1)]) % 2 != 0:
                         break
                     for rule in self.binary_rules[(parse_1.rule.lhs, parse_2.rule.lhs)]:
                         # Don't allow $StringStub to absorb (to control growth)
                         if rule.lhs=='$StringStub':
                             continue
                         absorbed = n - m
                         chart[(i, j)].append(Parse(rule, [parse_1, parse_2], absorbed))
Exemplo n.º 4
0
 def apply_unary_rules(self, chart, i, j):
     """Add parses to chart cell (i, j) by applying unary rules."""
     for parse in chart[(i, j)]:
         for rule in self.unary_rules[(parse.rule.lhs,)]:
             chart[(i, j)].append(Parse(rule, [parse]))
Exemplo n.º 5
0
 def apply_binary_rules(self, chart, i, j):
     """Add parses to chart cell (i, j) by applying binary rules."""
     for k in range(i + 1, j):
         for parse_1, parse_2 in product(chart[(i, k)], chart[(k, j)]):
             for rule in self.binary_rules[(parse_1.rule.lhs, parse_2.rule.lhs)]:
                 chart[(i, j)].append(Parse(rule, [parse_1, parse_2]))
Exemplo n.º 6
0
 def apply_lexical_rules(self, chart, words, i, j):
     """Add parses to chart cell (i, j) by applying lexical rules."""
     for rule in self.lexical_rules[tuple(words[i:j])]:
         chart[(i, j)].append(Parse(rule, words[i:j]))