-
Notifications
You must be signed in to change notification settings - Fork 0
/
owlace_dcg.pl
462 lines (365 loc) · 13 KB
/
owlace_dcg.pl
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
% This file is part of the OWL verbalizer.
% Copyright 2008-2011, Kaarel Kaljurand <[email protected]>.
%
% The OWL verbalizer is free software: you can redistribute it and/or modify it
% under the terms of the GNU Lesser General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your option) any later version.
%
% The OWL verbalizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
% without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License along with the
% OWL verbalizer. If not, see http://www.gnu.org/licenses/.
:- module(owlace_dcg, [
owl_ace/2
]).
/** <module> Definite Clause Grammar to transform an OWL axiom into an ACE sentence
Converts an axiom in a syntactic fragment of OWL into a sentence in a
fragment of ACE.
Things to discuss:
==
* Ambiguity of RelCl and coordination.
* Comma-and vs and. Use comma-and only when disjunction is around.
==
Things to test:
==
* Roundtripping OWL->ACE->OWL (generate OWL with owl_generator.pl)
* Roundtripping ACE->OWL->ACE (generate ACE with this grammar)
* How many solutions from ACE->OWL? Is the first one correct?
* How many solutions from OWL->ACE? Is the first one correct?
* Compatibility with ACE semantics (i.e. treatment of relative clauses and coordinations)
==
@author Kaarel Kaljurand
@version 2011-06-10
*/
%% owl_ace(+OWL:term, -ACE:list) is nondet.
%
% Verbalizes an OWL axiom as an ACE sentence. Assumes that the OWL axiom comes
% from a certain syntactic fragment of OWL, e.g. where all the intersections
% are binary, which does not contain any EquivalentClasses-axioms, etc. etc.
% So, calling this module must be preceeded by the heavy axiom rewriting performed
% by table_1/2 and rewrite_subclassof/2.
%
% An example of verbalizing an OWL axiom as an ACE sentence.
%
%==
% ?- owlace_dcg:owl_ace('SubClassOf'('Class'(protein),'ObjectSomeValuesFrom'('ObjectInverseOf'('ObjectProperty'(modify)),'ObjectOneOf'(['NamedIndividual'('Met')]))),ACE).
%
%ACE = ['Every', cn_sg(protein), is, tv_vbg(modify), by, pn_sg('Met'), '.']
%==
%
% @param OWL is an OWL axiom in the OWL 2 Functional-Style Syntax (Prolog notation)
% @param ACE is an ACE sentence represented as a list of atoms
%
owl_ace(OWL, ACE) :-
phrase(ip(OWL), ACE),
!.
%
% SYNTAX
%
ip(
'SubObjectPropertyOf'('ObjectPropertyChain'(PropertyChain), 'ObjectProperty'(S)),
TokenList,
[]
) :-
propertychain_verbchain(PropertyChain, [a, thing, that | Tail]),
TokenListBeginning = ['If', 'X' | Tail],
append(TokenListBeginning, ['Y', then, 'X', tv_sg(S), 'Y', '.'], TokenList).
ip(
'SubObjectPropertyOf'('ObjectPropertyChain'(PropertyChain), 'ObjectInverseOf'('ObjectProperty'(S))),
TokenList,
[]
) :-
propertychain_verbchain(PropertyChain, [a, thing, that | Tail]),
TokenListBeginning = ['If', 'X' | Tail],
append(TokenListBeginning, ['Y', then, 'X', is, tv_vbg(S), by, 'Y', '.'], TokenList).
% BUG: use passive to be consistent with the thesis
ip('SubObjectPropertyOf'('ObjectInverseOf'('ObjectProperty'(R)), 'ObjectInverseOf'('ObjectProperty'(S)))) -->
['If', 'Y', tv_sg(R), 'X', then, 'Y', tv_sg(S), 'X', '.'].
ip('SubObjectPropertyOf'('ObjectInverseOf'('ObjectProperty'(R)), 'ObjectProperty'(S))) -->
['If', 'Y', tv_sg(R), 'X', then, 'X', tv_sg(S), 'Y', '.'].
ip('SubObjectPropertyOf'('ObjectProperty'(R), 'ObjectInverseOf'('ObjectProperty'(S)))) -->
['If', 'X', tv_sg(R), 'Y', then, 'Y', tv_sg(S), 'X', '.'].
ip('SubObjectPropertyOf'('ObjectProperty'(R), 'ObjectProperty'(S))) -->
['If', 'X', tv_sg(R), 'Y', then, 'X', tv_sg(S), 'Y', '.'].
% BUG: use passive to be consistent with the thesis
ip('DisjointObjectProperties'(['ObjectInverseOf'('ObjectProperty'(R)), 'ObjectInverseOf'('ObjectProperty'(S))])) -->
['If', 'Y', tv_sg(R), 'X', then, it, is, false, that, 'Y', tv_sg(S), 'X', '.'].
ip('DisjointObjectProperties'(['ObjectInverseOf'('ObjectProperty'(R)), 'ObjectProperty'(S)])) -->
['If', 'Y', tv_sg(R), 'X', then, it, is, false, that, 'X', tv_sg(S), 'Y', '.'].
ip('DisjointObjectProperties'(['ObjectProperty'(R), 'ObjectInverseOf'('ObjectProperty'(S))])) -->
['If', 'X', tv_sg(R), 'Y', then, it, is, false, that, 'Y', tv_sg(S), 'X', '.'].
ip('DisjointObjectProperties'(['ObjectProperty'(R), 'ObjectProperty'(S)])) -->
['If', 'X', tv_sg(R), 'Y', then, it, is, false, that, 'X', tv_sg(S), 'Y', '.'].
ip(SubClassOf) -->
np_subj(Y, SubClassOf),
ibar(num=sg, Y),
['.'].
ibar(Num, C2) -->
auxc(Num, C1, C2),
cop(C1).
ibar(Num, C2) -->
auxv(Num, Neg, Vbn, C1, C2),
vp(Num, Neg, Vbn, C1).
cop('ObjectOneOf'([Individual])) -->
pn('ObjectOneOf'([Individual])).
cop('ObjectIntersectionOf'([C1, C2])) -->
[a],
cn(num=sg, C1),
relcoord(num=sg, C2).
cop(C) -->
[a],
cn(num=sg, C).
vp(Num, Neg, Vbn, Restriction) -->
tv(Num, Neg, Vbn, R),
np_obj(Num, R, Restriction).
np_subj(C, 'SubClassOf'('ObjectOneOf'([Individual]), C)) -->
pn('ObjectOneOf'([Individual])).
np_subj(Y, 'SubClassOf'(C, D)) -->
det_subj(C, Y, 'SubClassOf'(C, D)),
cn(num=sg, C).
np_subj(Y, 'SubClassOf'(C, D)) -->
det_subj('ObjectIntersectionOf'([X, Coord]), Y, 'SubClassOf'(C, D)),
cn(num=sg, X),
relcoord(num=sg, Coord).
np_obj(_, R, 'ObjectSomeValuesFrom'(R, 'ObjectOneOf'([Individual]))) -->
pn('ObjectOneOf'([Individual])).
np_obj(_, R, 'DataHasValue'(R, '^^'(Integer, 'http://www.w3.org/2001/XMLSchema#integer'))) -->
[Integer],
{ integer(Integer) }.
np_obj(_, R, 'DataHasValue'(R, '^^'(Double, 'http://www.w3.org/2001/XMLSchema#double'))) -->
[Double],
{ float(Double) }.
np_obj(_, R, 'DataHasValue'(R, '^^'(String, 'http://www.w3.org/2001/XMLSchema#string'))) -->
[qs(String)],
{ atom(String) }.
np_obj(num=sg, R, 'ObjectHasSelf'(R)) -->
[itself].
np_obj(num=pl, R, 'ObjectHasSelf'(R)) -->
[themselves].
np_obj(_, R, Restriction) -->
det_obj(Num, R, C, Restriction),
cn(Num, C).
np_obj(_, R, Restriction) -->
det_obj(Num, R, 'ObjectIntersectionOf'([C, Coord]), Restriction),
cn(Num, C),
relcoord(Num, Coord).
% Relative clauses
%
% Note that only binary and/or are supported. The binding order
% is implemented by multiple levels of rules.
%
% We need to specify the cases where we need comma-and.
%
% [ A or B , and C ]
% [ A , and B or C ]
% [ A or B , and C or D ]
%
% What about:
%
% [ A and B , and C ]
% [ A , and B and C ]
%
% and other cases which a more complex.
% BUG: Here we restrict the use of comma-and only to cases where
% there is a UnionOf nearby.
relcoord(Num, 'ObjectIntersectionOf'(['ObjectUnionOf'(CL1), 'ObjectUnionOf'(CL2)])) -->
relcoord_1(Num, 'ObjectUnionOf'(CL1)),
[',', and],
relcoord_1(Num, 'ObjectUnionOf'(CL2)).
relcoord(Num, 'ObjectIntersectionOf'(['ObjectUnionOf'(CL1), C2])) -->
relcoord_1(Num, 'ObjectUnionOf'(CL1)),
[',', and],
relcoord_2(Num, C2).
relcoord(Num, 'ObjectIntersectionOf'([C1, 'ObjectUnionOf'(CL2)])) -->
relcoord_2(Num, C1),
[',', and],
relcoord_1(Num, 'ObjectUnionOf'(CL2)).
relcoord(Num, Coord) -->
relcoord_1(Num, Coord).
relcoord_1(Num, 'ObjectUnionOf'([C1, C2])) -->
relcoord_2(Num, C1),
[or],
relcoord_1(Num, C2).
relcoord_1(Num, Coord) -->
relcoord_2(Num, Coord).
relcoord_2(Num, 'ObjectIntersectionOf'([C1, C2])) -->
rel(Num, C1),
[and],
relcoord_2(Num, C2).
relcoord_2(Num, Coord) -->
rel(Num, Coord).
/*
relcoord(Num, 'ObjectIntersectionOf'([C1, C2])) -->
relcoord_1(Num, C1),
comma_and(C1, C2, 'ObjectIntersectionOf'([C1, C2])),
relcoord(Num, C2).
relcoord(Num, Coord) -->
relcoord_1(Num, Coord).
relcoord_1(Num, 'ObjectUnionOf'([C1, C2])) -->
relcoord_2(Num, C1),
or(C1, C2, 'ObjectUnionOf'([C1, C2])),
relcoord_1(Num, C2).
relcoord_1(Num, Coord) -->
relcoord_2(Num, Coord).
relcoord_2(Num, 'ObjectIntersectionOf'([C1, C2])) -->
rel(Num, C1),
and(C1, C2, 'ObjectIntersectionOf'([C1, C2])),
relcoord_2(Num, C2).
relcoord_2(Num, Coord) -->
rel(Num, Coord).
*/
rel(Num, X) -->
[that],
ibar(Num, X).
%
% FORMAL LEXICON
%
% Syntactic sugar 'no' could be implemented like this:
det_subj(C1, C2, 'SubClassOf'(C1, 'ObjectComplementOf'(C2))) -->
['No'].
det_subj(C1, C2, 'SubClassOf'(C1, C2)) -->
['Every'].
% General number rule. Note that we do not support the number 0.
det_obj(num=sg, R, C, 'ObjectSomeValuesFrom'(R, C)) -->
[a].
det_obj(num=pl, R, C, 'ObjectAllValuesFrom'(R, C)) -->
[nothing, but].
det_obj(num=sg, R, C, 'ObjectMinCardinality'(1, R, C)) -->
[at, least, 1].
det_obj(num=sg, R, C, 'ObjectMaxCardinality'(1, R, C)) -->
[at, most, 1].
det_obj(num=sg, R, C, 'ObjectExactCardinality'(1, R, C)) -->
[exactly, 1].
det_obj(num=pl, R, C, 'ObjectMinCardinality'(Integer, R, C)) -->
[at, least, Integer],
{ at_least_2(Integer) }.
det_obj(num=pl, R, C, 'ObjectMaxCardinality'(Integer, R, C)) -->
[at, most, Integer],
{ at_least_2(Integer) }.
det_obj(num=pl, R, C, 'ObjectExactCardinality'(Integer, R, C)) -->
[exactly, Integer],
{ at_least_2(Integer) }.
auxc(num=sg, C, C) -->
[is].
auxc(num=pl, C, C) -->
[are].
auxc(num=sg, C, 'ObjectComplementOf'(C)) -->
[is, not].
auxc(num=pl, C, 'ObjectComplementOf'(C)) -->
[are, not].
auxv(num=sg, neg=yes, vbn=no, C, 'ObjectComplementOf'(C)) -->
[does, not].
auxv(num=pl, neg=yes, vbn=no, C, 'ObjectComplementOf'(C)) -->
[do, not].
auxv(num=sg, neg=yes, vbn=yes, C, 'ObjectComplementOf'(C)) -->
[is, not].
auxv(num=pl, neg=yes, vbn=yes, C, 'ObjectComplementOf'(C)) -->
[are, not].
auxv(num=sg, neg=no, vbn=yes, C, C) -->
[is].
auxv(num=pl, neg=no, vbn=yes, C, C) -->
[are].
auxv(_, neg=no, vbn=no, C, C) -->
[].
comma_and(C1, C2, 'ObjectIntersectionOf'([C1, C2])) -->
[',', and].
and(C1, C2, 'ObjectIntersectionOf'([C1, C2])) -->
[and].
or(C1, C2, 'ObjectUnionOf'([C1, C2])) -->
[or].
%
% CONTENT LEXICON: pn//1, cn//2, tv//4
%
% Proper names
pn('ObjectOneOf'(['NamedIndividual'(Lemma)])) -->
[pn_sg(Lemma)].
/*
TODO: this way of handling Anonymous individuals does not work:
1) they must be ACE variables (of the form [A-Z][0-9]*)
2) in OWL axiom ordering does not matter, but we need to make
sure that a ClassAssertion is verbalized before SubClassOf in case both
reference the same anonymous individual
pn('ObjectOneOf'(['AnonymousIndividual'(NodeId)])) -->
[NodeId].
*/
% Nouns (including `thing')
% For nouns we have to describe 2 forms: {num=sg, num=pl}
cn(num=sg, 'Class'('http://www.w3.org/2002/07/owl#Thing')) -->
[thing].
cn(num=pl, 'Class'('http://www.w3.org/2002/07/owl#Thing')) -->
[things].
cn(num=sg, 'Class'(Lemma)) -->
[cn_sg(Lemma)].
cn(num=pl, 'Class'(Lemma)) -->
[cn_pl(Lemma)].
% Transitive verbs
%
% For verbs we have to describe 8 forms:
% {neg=yes, neg=no} * {num=sg, num=pl} * {vbn=no, vbn=yes}
% modifies
% modify
% does not modify
% do not modify
% is modified by
% are modified by
% is not modified by
% are not modified by
% Note that the (external) lexicon has to provide only 3 forms:
% finite form, infinitive, and past participle (e.g. modifies, modify, modified).
tv(num=sg, neg=no, vbn=no, 'ObjectProperty'(Lemma)) -->
[tv_sg(Lemma)].
tv(num=pl, neg=no, vbn=no, 'ObjectProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(num=sg, neg=yes, vbn=no, 'ObjectProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(num=pl, neg=yes, vbn=no, 'ObjectProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(_, _, vbn=yes, 'ObjectInverseOf'('ObjectProperty'(Lemma))) -->
[tv_vbg(Lemma), by].
% BUG: DataProperties are treated in the same was as object properties,
% i.e. with transitive verbs
% BUG: why don't we use tv_sg and tv_vbg here?
tv(num=sg, neg=no, vbn=no, 'DataProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(num=pl, neg=no, vbn=no, 'DataProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(num=sg, neg=yes, vbn=no, 'DataProperty'(Lemma)) -->
[tv_pl(Lemma)].
tv(num=pl, neg=yes, vbn=no, 'DataProperty'(Lemma)) -->
[tv_pl(Lemma)].
%% propertychain_verbchain(?PropertyChain:list, ?VerbChain:list) is det.
%
% Example:
%
%==
% propertychain_verbchain(['ObjectProperty'(know), 'ObjectInverseOf'('ObjectProperty'(eat))], VerbChain).
%
% VerbChain = [a, thing, that, tv_sg(know), a, thing, that, is, tv_vbg(eat), by].
%==
%
% @param PropertyChain is a list of OWL property expressions
% @param VerbChain is a list of ACE tokens containing `a thing', `that', and the verb
%
propertychain_verbchain([], []).
propertychain_verbchain([Property | PropertyChainTail], VerbChain) :-
property_verb(Property, VerbChainTail, VerbChain),
propertychain_verbchain(PropertyChainTail, VerbChainTail).
%% property_verb(+Property:term, ?VerbChainTail:list, -VerbChain:list) is det.
%
% @param Property is an OWL property expression
% @param VerbChainTail is a list of ACE tokens containing `a thing', `that', and the verb
% @param VerbChain is a list of ACE tokens containing `a thing', `that', and the verb
%
property_verb('ObjectProperty'(R), VerbChainTail, [a, thing, that, tv_sg(R) | VerbChainTail]).
property_verb('ObjectInverseOf'('ObjectProperty'(R)), VerbChainTail, [a, thing, that, is, tv_vbg(R), by | VerbChainTail]).
%% at_least_2(?Integer:integer)
%
% Generates integers that are larger than 1.
%
at_least_2(Integer) :-
between(2, infinite, Integer).
% length/2 can be used in Prologs which do not provide between/3 (with infinite)
%at_least_2(Integer) :-
% length([_,_|_], Integer).