-
Notifications
You must be signed in to change notification settings - Fork 40
/
res.pl
457 lines (398 loc) · 11.9 KB
/
res.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
child('Anastasia Dovzhenko', 'Eugenia Dovzhenko').
child('Anastasia Dovzhenko', 'Alexander Dovzhenko').
child('Eugenia Dovzhenko', 'Nadezhda Libgot').
child('Eugenia Dovzhenko', 'Kirill Libgot').
child('Alexander Dovzhenko', 'Nadezhda Dovzhenko').
child('Alexander Dovzhenko', 'Anatoly Dovzhenko').
child('Igor Dovzhenko', 'Nadezhda Dovzhenko').
child('Igor Dovzhenko', 'Anatoly Dovzhenko').
child('Sergey Dovzhenko', 'Julia Dovzhenko').
child('Sergey Dovzhenko', 'Igor Dovzhenko').
child('Maksim Dovzhenko', 'Julia Dovzhenko').
child('Maksim Dovzhenko', 'Igor Dovzhenko').
child('Anatoly Dovzhenko', 'Сlaudia Dovzhenko').
child('Anatoly Dovzhenko', 'Fedor Dovzhenko').
child('Lyubov Dovzhenko', 'Сlaudia Dovzhenko').
child('Lyubov Dovzhenko', 'Fedor Dovzhenko').
child('Olga Sultanova', 'Lyubov Dovzhenko').
child('Olga Sultanova', 'Vladimir Sotnikov').
child('Maria Sultanova', 'Olga Sultanova').
child('Maria Sultanova', 'Eldar Sultanov').
child('Daria Sultanova', 'Olga Sultanova').
child('Daria Sultanova', 'Eldar Sultanov').
child('Tatiana Gavrova', 'Maria Nosova').
child('Tatiana Gavrova', 'Tikhon Nosov').
child('Nikolay Nosov', 'Maria Nosova').
child('Nikolay Nosov', 'Tikhon Nosov').
child('Valentina Parechina', 'Maria Nosova').
child('Valentina Parechina', 'Tikhon Nosov').
child('Gennady Nosov', 'Maria Nosova').
child('Gennady Nosov', 'Tikhon Nosov').
child('Nadezhda Libgot', 'Maria Nosova').
child('Nadezhda Libgot', 'Tikhon Nosov').
child('Sergey Gavrov', 'Tatiana Gavrova').
child('Sergey Gavrov', 'Victor Gavrov').
child('Natalia Makhnina', 'Tatiana Gavrova').
child('Natalia Makhnina', 'Victor Gavrov').
child('Ivan Gavrov', 'Ekaterina Gavrova').
child('Ivan Gavrov', 'Sergey Gavrov').
child('Alexey Gavrov', 'Ekaterina Gavrova').
child('Alexey Gavrov', 'Sergey Gavrov').
child('Lyubov Meshcheryakova', 'Valentina Nosova').
child('Lyubov Meshcheryakova', 'Gennady Nosov').
child('Eugene Nosov', 'Valentina Nosova').
child('Eugene Nosov', 'Gennady Nosov').
child('Vladimir Meshcheryakov', 'Lyubov Meshcheryakova').
child('Vladimir Meshcheryakov', 'Alexander Meshcheryakov').
child('Ksenia Meshcheryakova', 'Lyubov Meshcheryakova').
child('Ksenia Meshcheryakova', 'Alexander Meshcheryakov').
child('Kirill Libgot', 'Eugenia Libgot').
child('Kirill Libgot', 'Gregory Libgot').
child('Natalia Libgot', 'Eugenia Libgot').
child('Natalia Libgot', 'Gregory Libgot').
child('Boris Libgot', 'Eugenia Libgot').
child('Boris Libgot', 'Gregory Libgot').
child('Lev Libgot', 'Eugenia Libgot').
child('Lev Libgot', 'Gregory Libgot').
child('Nadezhda Dovzhenko', 'Agrafena Chernova').
child('Nadezhda Dovzhenko', 'Vladimir Chernov').
child('Sergey Parechin', 'Valentina Parechina').
child('Sergey Parechin', 'Victor Parechin').
child('Alexey Parechin', 'Valentina Parechina').
child('Alexey Parechin', 'Victor Parechin').
child('Ksenia Makhnina', 'Natalia Makhnina').
child('Ksenia Makhnina', 'Pavel Makhnin').
child('Pasha Makhnin', 'Natalia Makhnina').
child('Pasha Makhnin', 'Pavel Makhnin').
child('Anna Nosova', 'Ekaterina Nosova').
child('Anna Nosova', 'Nikolay Nosov').
child('Elena Nosova', 'Ekaterina Nosova').
child('Elena Nosova', 'Nikolay Nosov').
child('Ekaterina Parechina', 'Nadezhda Parechina').
child('Ekaterina Parechina', 'Sergey Parechin').
child('Svetlana Parechina', 'Nadezhda Parechina').
child('Svetlana Parechina', 'Sergey Parechin').
child('Julia Dovzhenko', 'Nina Krapivina').
child('Julia Dovzhenko', 'Petr Krapivin').
child('Andrey Krapivin', 'Nina Krapivina').
child('Andrey Krapivin', 'Petr Krapivin').
child('Natalya Minaeva', 'Natalia Libgot').
child('Natalya Minaeva', 'Vyacheslav Minaev').
child('Valeria Snetkova', 'Natalya Minaeva').
child('Valeria Snetkova', 'Sergey Snetkov').
male('Alexander Dovzhenko').
male('Kirill Libgot').
male('Anatoly Dovzhenko').
male('Igor Dovzhenko').
male('Sergey Dovzhenko').
male('Maksim Dovzhenko').
male('Fedor Dovzhenko').
male('Vladimir Sotnikov').
male('Eldar Sultanov').
male('Tikhon Nosov').
male('Victor Gavrov').
male('Sergey Gavrov').
male('Ivan Gavrov').
male('Nikolay Nosov').
male('Gennady Nosov').
male('Alexander Meshcheryakov').
male('Vladimir Meshcheryakov').
male('Gregory Libgot').
male('Boris Libgot').
male('Vladimir Chernov').
male('Victor Parechin').
male('Pavel Makhnin').
male('Eugene Nosov').
male('Alexey Gavrov').
male('Pasha Makhnin').
male('Sergey Parechin').
male('Alexey Parechin').
male('Petr Krapivin').
male('Andrey Krapivin').
male('Vyacheslav Minaev').
male('Sergey Snetkov').
male('Lev Libgot').
female('Eugenia Dovzhenko').
female('Anastasia Dovzhenko').
female('Nadezhda Libgot').
female('Nadezhda Dovzhenko').
female('Julia Dovzhenko').
female('Сlaudia Dovzhenko').
female('Lyubov Dovzhenko').
female('Olga Sultanova').
female('Maria Sultanova').
female('Daria Sultanova').
female('Maria Nosova').
female('Tatiana Gavrova').
female('Natalia Makhnina').
female('Ekaterina Gavrova').
female('Valentina Parechina').
female('Valentina Nosova').
female('Lyubov Meshcheryakova').
female('Ksenia Meshcheryakova').
female('Eugenia Libgot').
female('Natalia Libgot').
female('Agrafena Chernova').
female('Ksenia Makhnina').
female('Ekaterina Nosova').
female('Anna Nosova').
female('Elena Nosova').
female('Nadezhda Parechina').
female('Ekaterina Parechina').
female('Nina Krapivina').
female('Svetlana Parechina').
female('Natalya Minaeva').
female('Valeria Snetkova').
sibling(Person, Sibling):-
child(Person, P),
child(Sibling, P),
Person \= Sibling.
double(Person, BroOrSis):-
child(Person, P1),
child(BroOrSis, P2),
sibling(P1, P2),
Person \= BroOrSis.
triple(Person, BroOrSis):-
child(Person, P1),
child(BroOrSis, P2),
double(P1, P2),
Person \= BroOrSis.
check_link(husband, Husband, Wife):-
child(Child, Husband),
child(Child, Wife),
Husband \= Wife,
male(Husband).
check_link(wife, Wife, Husband):-
child(Child, Husband),
child(Child, Wife),
Husband \= Wife,
female(Wife).
check_link(brother, Brother, Y):-
sibling(Brother, Y),
male(Brother).
check_link(sister, Sister, Y):-
sibling(Sister, Y),
female(Sister).
check_link(father, Father, Child):-
child(Child, Father),
male(Father).
check_link(mother, Mother, Child):-
child(Child, Mother),
male(Mother).
check_link(parent, Parent, Child):-
child(Child, Parent).
check_link(son, Child, Parent):-
child(Child, Parent),
male(Child).
check_link(daughter, Child, Parent):-
child(Child, Parent),
female(Child).
check_link(child, Child, Parent):-
child(Child, Parent).
check_relation(X):-
member(X, [father, mother, sister, brother, son, daughter, husband, wife]).
/* Степень родства */
relative_thread(X, Y, Res):- % цепочка людей, через которых связаны 2 человека
search_bdth(X, Y, Res).
ask_relative(X, Y, Res):- % цепочка родства, через которую связаны 2 человека
check_relation(Res), !,
check_link(Res, X, Y).
relative(X, Y, Res):-
search_bdth(X, Y, Res1), !,
transform(Res1, Res).
transform([_],[]):-!. % переделевает цепочку родственников в цепочку родства
transform([First,Second|Tail],ResList):-
%length([First,Second|Tail],B),
%A is B - 1,
%length(ResList,A),
check_link(Relation,First,Second),
ResList = [Relation|Tmp],
transform([Second|Tail],Tmp),!.
prolong([X|T],[Y,X|T]):-
move(X,Y),
\+ member(Y,[X|T]).
move(X,Y):-
check_link(_,X,Y).
search_bdth(X,Y,P):-
bdth([[X]],Y,L),
reverse(L,P).
bdth([[X|T]|_],X,[X|T]).
bdth([P|QI],X,R):-
findall(Z,prolong(P,Z),T),
append(QI,T,Q0),
bdth(Q0,X,R),!.
bdth([_|T],Y,L):-
bdth(T,Y,L).
/* Вопросы, относительно степени родства */
question_word(X):-
member(X, [how, who, "How", "Who"]).
quantity(X):-
member(X, [much, many]).
purals(X):-
member(X, [sisters, brothers, sons, daughters]).
pural(sister, sisters).
pural(brother, brothers).
pural(son, sons).
pural(daughter, daughters).
help_word(X):-
member(X, [do, does]).
have_has(X):-
member(X, [have, has]).
is(X):-
member(X,[is]).
particle(X):-
member(X, ["'s"]).
question_mark(X):-
member(X, ['?']).
his_her(X):-
member(X, [his, her, he, she]).
%how many brothers does *name* have ?
ask_question(List):-
List = [A,B,C,D,E,F,H],
question_word(A),
quantity(B),
purals(C),
help_word(D),
(male(E);female(E)),
nb_setval(lastName,E),
have_has(F),
question_mark(H),
pural(C1,C),
setof(X,ask_relative(X,E,C1),T),
length(T,Res),!,
write(E),
write(" have "),
((Res =:= 1,write(Res),write(" "),write( C1));(\+(Res =:= 1),write(Res),write(" "),write( C))),!.
%how many brothers does he have ?
ask_question(List):-
List = [A,B,C1,D,E1,F,H],
question_word(A),
quantity(B),
purals(C1),
help_word(D),
his_her(E1),
nb_getval(lastName,E),
have_has(F),
question_mark(H),
pural(C,C1),
setof(X,ask_relative(X,E,C),T),
length(T,Res),
write(E),
write(" have "),
((Res =:= 1,write(Res),write(" "),write(C));(\+(Res =:= 1),write(Res),write(" "),write(C1))),!.
% who is *name* brother?
ask_question(List):-
List = [A,B,C,D,E,F],
question_word(A),
is(B),
(male(C);female(C)),
nb_setval(lastName,C),
particle(D),
check_relation(E),
question_mark(F), !,
check_link(E,Res,C),
write(Res),write(" is "), write(C),write("'s "),write(E).
% who is her brother
ask_question(List):-
List = [A,B,C1,D,E],
question_word(A),
is(B),
his_her(C1),
nb_getval(lastName,C),
check_relation(D),
question_mark(E),!,
check_link(D,Res,C),
write(Res),write(" is "), write(C),write("'s "),write(D).
% is *name* *name* s son?
ask_question(List):-
List = [A,B,C,D,E,F],
nb_setval(lastName,C),
is(A),
(male(B);female(B)),
(male(C);female(C)),
particle(D),
check_relation(E),
question_mark(F),
check_link(E,B,C),
!.
% is *name* his son?
ask_question(List):-
List = [A,B,C1,D,E],
is(A),
(male(B);female(B)),
his_her(C1),
check_relation(D),
question_mark(E),
nb_getval(lastName,C),
check_link(D,B,C),
!.
/* Анализ предложений */
/* обрабатываются только шаблонные вопросы, это не очень хорошо */
% who is *name* s brother?
% is *name* *name* s son ?
analysis(List,Res):-
append(L1,L2,List),
questionType(L1,Q),
append(M1,M2,L2),
name(M1,Q1),
append(N1,N2,M2),
relationship(N1,Q2),
questionsign(N2,Q3),
Res = sentence(Q,Q1,Q2,Q3).
% how many brothers does *name* have ?
analysis(List,Res):-
List = [A,B,C,D,E,F,G],
questionType([A,B],Q),
relationship([C],Q1),
helpword([D],Q2),
name([E],Q3),
have_Has([F],Q4),
questionsign([G],Q5),
Res = sentence(Q,Q1,Q2,Q3,Q4,Q5).
relationship([X],Res):-
check_relation(X),
Res = relationship_(X).
relationship([X],Res):-
purals(X),
Res = relationship_(X).
helpword([X],Res):-
help_word(X),
Res = helpWord(X).
have_Has([X],Res):-
have_has(X),
Res = haveHas(X).
questionType(L,question_type(X,Y)):-
question_word(X1),
is(Y1),
L = [X1,Y1],
X = questionWord(X1),
Y = auxiliaryVerb(Y1).
questionType(L,question_type(X,Y)):-
question_word(X1),
quantity(Y1),
L = [X1,Y1],
X = questionWord(X1),
Y = much_many(Y1).
questionType(L,question_type(X)):-
is(X1),
L = [X1],
X = auxiliaryVerb(X1).
name(L,names(X)):-
L = [X1,Y1],
((male(X1);female(X1)); (his_her(X1))),
particle(Y1),
X = person(X1,Y1).
name(L,names(X)):-
L = [X1],
((male(X1);female(X1)); (his_her(X1))),
X = person(X1).
name(L,names(X,Y)):-
L = [X1,Y1,Z],
(male(X1);female(X1)),
((male(Y1);female(Y1)); (his_her(Y1))),
particle(Z),
X = person(X1),
Y = relative_(Y1,Z).
questionsign([X],Res):-
question_mark(X),
Res = questionMark(X).