Wissenschaft
Copyright © 2024 Jiri Kriz, www.nosco.ch

15    Natürliche Sprachen II

Lösung der Aufgaben

15.1    Syntax und Semantik

s(Sentence, Tree, Sem) :- 
    s(Sentence, [], Tree, Sem).

s(S0, S, s(NP, VP), Quant) :- 
    np(S0, S1, NP, Subj^Ass^Quant), 
    vp(S1, S, VP, Subj^Ass). 

np(S0, S, np(D, N), X^Ass^Quant) :- 
    d(S0, S1, D, X^Scope^Ass^Quant), 
    n(S1, S, N, X^Scope).
np(S0, S, np(Name), Sem) :- 
    name( S0, S, Name, Sem). 

vp(S0, S, vp(V, NP), Subj^Quant) :- 
    v( S0, S1, V, Subj^Obj^Pred), 
    np( S1, S, NP, Obj^Pred^Quant).
vp(S0, S, vp(V), Sem) :- 
    v( S0, S, V, Sem).
vp(S0, S, vp(V, NP), X^Scope) :-
    cop(S0, S1, V), /* is */
    np( S1, S, NP, X^Ass^exists(X, Scope, Ass)).

n([N|S], S, n(N), Sem) :- 
    n_(N, Sem).

name([N| S], S, name(N), Sem) :- 
    name_(N, Sem).

v([V|S], S, v(V), Sem) :- 
    v_(V, Sem).

d([D|S], S, d(D), Sem) :- 
    d_(D, Sem).

cop([C|S], S, cop(C)) :- 
    cop_(C).

n_(book,    X^book(X)).
n_(man,     X^man(X)).
n_(woman,   X^woman(X)).

v_(arrive,  X^arrives(X)).
v_(arrived, X^arrives(X)).
v_(arrives, X^arrives(X)).
v_(like,    X^Y^likes(X, Y)).
v_(likes,   X^Y^likes(X, Y)).
v_(liked,   X^Y^likes(X, Y)).
v_(love,    X^Y^loves(X, Y)).
v_(loves,   X^Y^loves(X, Y)).
v_(loved,   X^Y^loves(X, Y)).
v_(reads,   X^Y^reads(X, Y)).
v_(sing,    X^sings(X)).
v_(sings,   X^sings(X)).

cop_(is).

d_(a, X^Scope^Ass^exists(X, Scope, Ass)).
d_(every, X^Scope^Ass^all(X, Scope, Ass)).
d_(the, X^Scope^Ass^the(X, Scope, Ass)).

name_(jack, jack^Ass^Ass).
name_(john, john^Ass^Ass).
name_(mary, mary^Ass^Ass).

write_tree( T) :- 
    write_tree( T, true, []).   
/* 2rd arg = T is last sibling */
/* 3rd arg = indentation prefix, list of chars */
write_tree( T, Last, Indent) :-
    append( Indent, [124, 45, 32], IndentNode), /* '|- ' */
    write_indent(IndentNode),
    T =.. [ Node| Leaves], 
    write( Node), nl,
    indent_leaves( Indent, Last, IndentLeaves),
    write_leaves( Leaves, IndentLeaves).

write_leaves( [], Indent).
write_leaves( [ L], Indent) :- 
    write_tree( L, true, Indent).
write_leaves( [ L1, L2| Ls], Indent) :-
    write_tree( L1, false, Indent), 
    write_leaves( [ L2| Ls], Indent).

indent_leaves( Indent, true, IndentLeaves) :- !,
    append( Indent, [ 32, 32, 32], IndentLeaves). /* '   ' */
indent_leaves( Indent, false, IndentLeaves) :- !,
    append( Indent, [ 124, 32, 32], IndentLeaves).  /* '|  ' */

write_indent( []).
write_indent( [ C| Cs]) :- 
    put( C), 
    write_indent( Cs).

Back to example 15.1

15.2    Repräsentation der Semantik in einer Wissensbank

declare(exists(X, Scope, Ass)) :- !,
    marker(X), 
    assert(kb(Scope)), 
    declare(Ass), 
    !.
declare(all(X, ScopeX, exists(Y, ScopeY, Pred))) :- !,
    marker(X, Y), 
    assert(kb(ScopeY :- ScopeX)), 
    assert(kb(Pred :- ScopeX)), 
    !.
declare(all(X, ScopeX, all(Y, ScopeY, Pred))) :- !,
    assert(kb(Pred :- ScopeX, ScopeY)), 
    !.
declare(all(X, ScopeX, Pred)) :- !, 
    assert(kb(Pred :- ScopeX)).
declare(Pred) :- !, 
    assert(kb(Pred)), 
    !.

marker(X) :- 
    get_marker(M), 
    X = f(M).
marker(X, Y) :- 
    get_marker(M), 
    Y = f(M, X).

get_marker(X) :- 
    marker_(M), 
    !,
    retract(marker_(M)), 
    M1 is M + 1, 
    assert(marker_(M1)), 
    X = M1.
get_marker(X) :- 
    assert(marker_(0)), 
    X = M1.

Back to example 15.2

15.3    Abfragen

solve(all(X, Scope, Ass)) :- !,
    not (solve(Scope), 
    not solve(Ass)), 
    solve(Scope), 
    !.
solve(exists(X, Scope, Ass)) :- !, 
    solve(Scope), 
    solve(Ass), !.
solve(X) :- 
    kb(X :- Y), 
    solve(Y).
solve(X) :- 
    kb(X), 
    write(X), 
    nl.

Back to example 15.3

15.4    Intelligenter Dialog

chat([D|S]) :- 
    do(D), !, 
    s(S, Tree, Sem), 
    solve(Sem).
chat([who, D|S]) :- 
    do(D), !,
    np(S, S1, T1, Subj^Pred^Q), 
    v(S1, [], T2, Subj^Obj^Pred),
    solve(Pred), 
    write(Obj), nl.
chat([who|S]) :- !,
    vp(S, [], Tree, Subj^Pred),
    solve(Pred), 
    write(Subj), nl.
chat( S) :- 
    s(S, Tree, Sem), 
    declare(Sem).

do(does).
do(did).

Back to example 15.4