-
Notifications
You must be signed in to change notification settings - Fork 6
/
wn_query.pl
107 lines (88 loc) · 2.41 KB
/
wn_query.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
/*
https://github.com/ekaf/wordnet-prolog/raw/master/wn_query.pl
(c) 2017-20 Eric Kafe, CC BY 4.0, https://creativecommons.org/licenses/by/4.0/
SWI-prolog program implementing some common WordNet use cases,
and a few formal checks, like symmetry and transitive loop detection.
*/
:-consult('db_version.pl').
:-consult('prolog/wn_s.pl').
semrels(['at','cs','ent','hyp','ins','mm','mp','ms','sim']).
loadrels:-
semrels(L),
member(R,L),
swritef(F,'prolog/wn_%w.pl',[R]),
writef('Consulting %w relation: %w\n',[R,F]),
consult(F),
false.
loadrels:-
nl.
:-loadrels.
/* ------------------------------------------------------------------
Synonyms have the same identifier: */
syn(A,A).
/* ------------------------------------------------------------------
Transitive hypernymy and hyponymy: */
thyp(A,B):-
hyp(A,B).
thyp(A,C):-
hyp(A,B),
thyp(B,C),
% Prevent transitive loops (f. ex. in original WordNet 3.0):
(A=C, !, writef('Transitive loop: %w %w %w\n', [A,B,C]); true).
/* ------------------------------------------------------------------
Word relations
------------------------- */
wordrel(R,A,B):-
% R is a relation between synsets, A and B are words
s(I,_,A,_,_,_),
apply(R,[I,J]),
s(J,_,B,_,_,_).
out2set([],_,_,[]).
out2set([H|T],W,R,S):-
sort([H|T],S),
outset(S,'',O),
writef('%w %w: [%w]\n',[W,R,O]).
outset([H],A,B):-
swritef(B,'%w%w', [A,H]).
outset([H|T],A,C):-
swritef(B,'%w%w,', [A,H]),
outset(T,B,C).
sameset([H|T],[H|T]).
irel(R,W):-
% Apply relation in both directions
% 1) Find all related words
findall(X, wordrel(R,W,X), L1),
out2set(L1,W,R,S1),
% 2) Inverse relation
findall(Y, wordrel(R,Y,W), L2),
swritef(Ri,'inverse %w',[R]),
out2set(L2,W,Ri,S2),
% 3) Check if R is symmetric
% If both sets are identical and non-empty, the relation is symmetric w.r.t. the query word:
(sameset(S1,S2) -> writef('Both sets are identical, so %w(%w,X) is symmetric\n', [R,W]); true).
/* ------------------------------------------
Word query
---------------------- */
qword(W):-
% Synonymy is symmetric
irel(syn,W),
irel(thyp,W),
semrels(L),
member(R,L),
irel(R,W),
false.
qword(_).
/* ------------------------------------------
Test some word queries
------------------------------------------ */
go:-
wn_version(WV),
atom_concat('output/wn_query.pl-Output-',WV,F),
tell(F),
member(W,['car','tree','house','check','line','London']),
qword(W),
nl,
false.
go:-
told.
:-go.