Package nltk_lite :: Package contrib :: Package mit :: Package six863 :: Package kimmo :: Module pairs
[hide private]
[frames] | no frames]

Source Code for Module nltk_lite.contrib.mit.six863.kimmo.pairs

1 -def sort_subsets(pairs, subsets):
2 def subset_size(pair): 3 if pair.input() in subsets: size1 = len(subsets[pair.input()]) 4 else: size1 = 1 5 if pair.output() in subsets: size2 = len(subsets[pair.output()]) 6 else: size2 = 1 7 return (min(size1, size2), max(size1, size2))
8 9 sort_order = [(subset_size(pair), pair) for pair in pairs] 10 sort_order.sort() 11 return [item[1] for item in sort_order] 12
13 -class KimmoPair(object):
14 """ 15 Input/Output character pair 16 """
17 - def __init__(self, input_subset, output_subset):
18 self._input = input_subset 19 self._output = output_subset
20 21
22 - def input(self): return self._input
23 - def output(self): return self._output
24 25
26 - def __repr__(self):
27 sI = self.input() 28 sO = self.output() 29 s = sI + ':' + sO 30 return s
31
32 - def __cmp__(self, other):
33 if type(self) != type(other): return -1 34 return cmp( (self._input, self._output), (other.input(), 35 other.output()))
36
37 - def __hash__(self):
38 return hash( (self._input, self._output) )
39
40 - def includes(self, pair, subsets):
41 return (self._matches(self.input(), pair.input(), subsets) and 42 self._matches(self.output(), pair.output(), subsets))
43
44 - def matches(self, input, output, subsets, negatedOutputMatch=False):
45 if not(self._matches(self.input(), input, subsets)): return False 46 m = self._matches(self.output(), output, subsets) 47 if negatedOutputMatch: return not(m) 48 return m
49 50
51 - def _matches(self, me, terminal, subsets):
52 if (me == terminal): return True 53 if (me[0] == '~'): 54 m = me[1:] 55 if (m in subsets): 56 return not(terminal in subsets[m]) 57 else: 58 return False 59 if (me in subsets): 60 return terminal in subsets[me] 61 else: 62 return False
63 64 @staticmethod
65 - def make(text):
66 parts = text.split(':') 67 if len(parts) == 1: return KimmoPair(text, text) 68 elif len(parts) == 2: return KimmoPair(parts[0], parts[1]) 69 else: raise ValueError, "Bad format for pair: %s" % text
70