-
Notifications
You must be signed in to change notification settings - Fork 0
/
TermUtils.hx
333 lines (304 loc) · 11.4 KB
/
TermUtils.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Copyright 2019 Robert Wünsche
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class TermUtils {
// rewrites <-> to <-> and both versions of --> which are "views"
public static function rewriteSimToSimInhView(term:Term): Array<Term> {
switch term {
case Cop("<->", subj,pred):
return [term, Cop("<->",pred,subj), Cop("-->",subj,pred), Cop("-->",pred,subj)];
case _:
return [term];
}
}
// clones only the first "level" of a term, used to "break" references to stay under AIKR
public static function cloneShallow(term:Term):Term {
return switch (term) {
case Name(name,label0): Name(name,label0);
case Compound(type, content): Compound(type, content);
case Cop(copula, subj, pred): Cop(copula, subj, pred);
case Prod(content): Prod(content);
case Img(base, content): Img(base, content);
case ImgWild: ImgWild;
case Var(type,name): Var(type,name);
case Str(content): Str(content);
case Set(type, content): Set(type, content);
}
}
// enumerate all concept name terms recursivly
public static function enumTerms(term:Term):Array<Term> {
return [term].concat(switch (term) {
case Name(name,_): [];
case Compound(_, content):
var res = [];
for (iContent in content) {
res = res.concat(enumTerms(iContent));
}
res;
case Cop(_, subj, pred): enumTerms(subj).concat(enumTerms(pred));
case Prod(content):
var res = [];
for (iContent in content) {
res = res.concat(enumTerms(iContent));
}
res;
case Img(base, content):
var res = [];
for (iContent in content) {
res = res.concat(enumTerms(iContent));
}
res.push(base);
res;
case ImgWild: [];
case Var(_,_): [];
case Str(_): [];
case Set(_, _):
[term]; // we treat a set as it's own name
});
}
// convert to string
public static function convToStr(term:Term) {
return switch (term) {
case ImgWild: "_";
case Name(name,label0): label0 ? name+":" : name;
case Compound(type,content):
var narseseContent = content.map(function(i) {return convToStr(i);}).join(' $type ');
'( $narseseContent )';
case Cop(copula, subj, pred): '< ${convToStr(subj)} $copula ${convToStr(pred)} >';
case Img(base, content):
var narseseContent = content.map(function(i) {return convToStr(i);}).join(" ");
'(/ ${convToStr(base)} $narseseContent)';
case Prod(content):
var narseseContent = content.map(function(i) {return convToStr(i);}).join(" * ");
'( $narseseContent )';
case Var(type,name): '$type$name';
case Str(content): '"$content"'; // TODO< escape " and \ >
case Set(type, content):
var narseseContent = content.map(function(i) {return convToStr(i);}).join(" ");
var typeEnd = type == "{" ? "}" : "]";
'$type$narseseContent$typeEnd';
}
}
public static function equal(a:Term, b:Term):Bool {
switch (a) {
case ImgWild:
switch(b) {
case ImgWild:
return true;
case _:
return false;
}
case Name(nameA,label0A):
switch(b) {
case Name(nameB,label0B):
return nameA == nameB && label0A == label0B;
case _:
return false;
}
case Compound(typeA, contentA):
switch(b) {
case Compound(typeB, contentB):
if (typeA != typeB) {
return false;
}
if (contentA.length != contentB.length) { return false; }
for (idx in 0...contentA.length) {
if (!equal(contentA[idx], contentB[idx])) {
return false;
}
}
return true;
case _:
return false;
}
case Cop(copulaA, subjA, predA):
switch(b) {
case Cop(copulaB, subjB, predB):
if (copulaA != copulaB) { return false; }
return equal(subjA, subjB) && equal(predA, predB);
case _:
return false;
}
case Img(baseA, contentA):
switch(b) {
case Img(baseB, contentB):
if (!equal(baseA, baseB)) {
return false;
}
if (contentA.length != contentB.length) { return false; }
for (idx in 0...contentA.length) {
if (!equal(contentA[idx], contentB[idx])) {
return false;
}
}
return true;
case _:
return false;
}
case Prod(contentA):
switch(b) {
case Prod(contentB):
if (contentA.length != contentB.length) { return false; }
for (idx in 0...contentA.length) {
if (!equal(contentA[idx], contentB[idx])) {
return false;
}
}
return true;
case _:
return false;
}
case Var(typeA,nameA):
switch(b) {
case Var(typeB,nameB):
return typeA==typeB && nameA==nameB;
case _:
return false;
}
case Str(contentA):
switch(b) {
case Str(contentB):
return contentA==contentB;
case _:
return false;
}
case Set(typeA,contentA):
switch(b) {
case Set(typeB,contentB):
if (typeA != typeB) {
return false;
}
if (contentA.length != contentB.length) { return false; }
for (idx in 0...contentA.length) {
if (!equal(contentA[idx], contentB[idx])) {
return false;
}
}
return true;
case _:
return false;
}
}
}
public static function isVar(term:Term): Bool {
return switch (term) {
case Var(_,_): true;
case _ : false;
}
}
public static function isSet(term:Term): Bool {
return switch (term) {
case Set(_,_): true;
case _ : false;
}
}
public static function isSetExt(term:Term): Bool {
return switch (term) {
case Set("{",_): true;
case _ : false;
}
}
public static function isSetInt(term:Term): Bool {
return switch (term) {
case Set("[",_): true;
case _ : false;
}
}
public static function isSameSet(a:Term, b:Term): Bool {
return switch (a) {
case Set(aType, _):
switch (b) {
case Set(bType,_): aType == bType;
case _: false;
}
case _ : false;
}
}
// merge set
public static function setMerge(a:Array<Term>, b:Array<Term>): Array<Term> {
var merged = a;
for(iB in b) {
var hasB = merged.filter(iMerged -> TermUtils.equal(iMerged, iB)).length > 0;
if (!hasB) {
merged.push(iB);
}
}
return merged;
}
public static function setMerge2(a:Term, b:Term): Term {
switch (a) {
case Set("[", sa):
switch (b) {
case Set("[", sb):
return Set("[", setMerge(sa, sb));
case _:
throw "Invalid set merge!";
}
case Set("{", sa):
switch (b) {
case Set("{", sb):
return Set("{", setMerge(sa, sb));
case _:
throw "Invalid set merge!";
}
case _:
throw "Invalid set merge!";
}
}
// reduces/foldes term
// ex: ( a & b & (a & c) ) ====> ( a & b & c )
public static function fold(foldedType:String, extInt:Term):Term {
var terms = [];
function processRec(term) {
switch (term) {
case Term.Compound(foldedType,content):
for (iContent in content) {
processRec(iContent);
}
case _:
if (!Utils.contains(terms, term)) {
terms.push(term);
}
}
}
processRec(extInt);
return Compound(foldedType, terms);
}
// computes structural complexity
// I define structural complexity as a complexity where compositions & , &&, |, - etc get extra complexity to bias system
public static function calcStructComplexity(term:Term): Float {
// helper to compute complexity of content of compound, etc
function calcComplexityOfArr(arr:Array<Term>):Float {
var c = 0.0;
for(iContent in arr) {
c += calcStructComplexity(iContent);
}
return c;
}
return switch term {
case Name(n,_): 1;
case ImgWild: 0;
case Compound(type,content):
var c = calcComplexityOfArr(content);
switch type {
case x if (x == "&" || x == "&&" || x == "-" || x == "|"):
c+=3.0;
case _:
c+=1.0;
}
c;
case Cop(_, subj, pred): calcStructComplexity(subj)+calcStructComplexity(pred)+1.0;
case Img(base, content):
calcComplexityOfArr(content)+1.0;
case Prod(content):
calcComplexityOfArr(content)+1.0;
case Var(type,name): 0;
case Str(content): 1;
case Set(type, content):
calcComplexityOfArr(content)+1.0;
}
}
}