1
2
3
4
5
6
7
9 '''
10 Interface for categories in combinatory grammars.
11 '''
12
13
15 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
16
17
19 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
20
21
23 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
24
25
26
28 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
29
30
31
32
34 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
35
36
38 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
39
41 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
42
44 raise AssertionError, 'AbstractCCGCategory is an abstract interface'
45
46
47 -class CCGVar(AbstractCCGCategory):
48 '''
49 Class representing a variable CCG category.
50 Used for conjunctions (and possibly type-raising, if implemented as a
51 unary rule).
52 '''
53 _maxID = 0
54
56 """Initialize a variable (selects a new identifier)
57
58 @param prim_only: a boolean that determines whether the variable is restricted to primitives
59 @type prim_only: C{boolean}
60 """
61 self._id = self.new_id()
62 self._prim_only = prim_only
63
64
68 new_id = classmethod(new_id)
69
72
75
78
80 """If there is a substitution corresponding to this variable,
81 return the substituted category.
82 """
83 for (var,cat) in substitutions:
84 if var == self:
85 return cat
86 return self
87
89 """ If the variable can be replaced with other
90 a substitution is returned.
91 """
92 if other.is_primitive() or not self._prim_only:
93 return [(self,other)]
94 return None
95
98
100 if not isinstance(other,CCGVar):
101 return -1
102 return cmp(self._id,other.id())
103
105 return hash(self._id)
107 return "_var" + str(self._id)
108
110 '''
111 Class representing the direction of a function application.
112 Also contains maintains information as to which combinators
113 may be used with the category.
114 '''
116 self._dir = dir
117 self._restrs = restrictions
118
119
121 return self._dir == '/'
123 return self._dir == '\\'
124
127
129 """A list of restrictions on the combinators.
130 '.' denotes that permuting operations are disallowed
131 ',' denotes that function composition is disallowed
132 '_' denotes that the direction has variable restrictions.
133 (This is redundant in the current implementation of type-raising)
134 """
135 return self._restrs
136
138 return self._restrs == '_'
139
140
141
142
152
161
162
164 return not ',' in self._restrs
165
167 return not '.' in self._restrs
168
170 return cmp((self._dir,self._restrs), (other.dir(),other.restrs()))
171 return res
172
174 return hash((self._dir,tuple(self._restrs)))
175
177 r_str = ""
178 for r in self._restrs:
179 r_str = r_str + str(r)
180 return str(self._dir) + r_str
181
182
184 if self._dir == '/':
185 return Direction('\\',self._restrs)
186 else:
187 return Direction('/',self._restrs)
188
189
191 '''
192 Class representing primitive categories.
193 Takes a string representation of the category, and a
194 list of strings specifying the morphological subcategories.
195 '''
196 - def __init__(self,categ,restrictions=[]):
197 self._categ = categ
198 self._restrs = restrictions
199
202
205
208
211
214
215
218
219
220
221
223 if not other.is_primitive():
224 return None
225 if other.is_var():
226 return [(other,self)]
227 if other.categ() == self.categ():
228 for restr in self._restrs:
229 if restr not in other.restrs():
230 return None
231 return []
232 return None
233
239
241 return hash((self._categ,tuple(self._restrs)))
242
244 if self._restrs == []:
245 return str(self._categ)
246 return str(self._categ) + str(self._restrs)
247
249 '''
250 Class that represents a function application category.
251 Consists of argument and result categories, together with
252 an application direction.
253 '''
258
261
264
267
268
269
275
276
277
289
290
293
296
299
306 return hash((self._arg,self._dir,self._res))
307
309 return "(" + str(self._res) + str(self._dir) + str(self._arg) + ")"
310