问题
I have this DCG grammar that understand and agree phrases like: [john, paints] and [john, likes, mary] managing the semantic meaning directly into the DCG grammar by the use of parameters
sentence2(VP) --> noun_phrase2(Actor),
verb_phrase2(Actor, VP).
noun_phrase2(Name) --> properName(Name).
verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP).
verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP),
noun_phrase2(Something).
properName(john) --> [john].
properName(mary) --> [mary].
intrans_verb(Actor, paints(Actor)) --> [paints].
trans_verb(Somebody, Something, likes(Somebody, Something)) --> [likes].
For example the mean of the phrase [john, paints] will be: paints(john), infact this is the result of the query:
?- sentence2(Meaning, [john,paints],[]).
Meaning = paints(john)
Ok, so this handle the meaning without build a parse tree.
I have an exercise that ask me to modify the previous grammar to obtain the mean from the syntattic tree of my sentence. So my queries have to return the parse tree, and also the meaning of the sentence
But I am finding some problem doing it...I was trying something like this, but don't work:
/** Adding the meaning into the Parse Tree: */
sentence2(sentence(Name, VerbPhrase)) --> noun_phrase2(Name),
verb_phrase2(VerbPhrase).
noun_phrase2(noun_phrase(Name)) --> properName2(Name).
verb_phrase2(verb_phrase(IntransVerb)) --> intrans_verb2(IntransVerb).
verb_phrase2(verb_phras(TransVerb, ProperName)) --> trans_verb2(TransVerb),
noun_phrase2(ProperName).
/* properName, intrans_verb ant trans_verb are LEAVES: */
properName2(properName(john)) --> [john].
properName2(properName(mary)) --> [mary].
intrans_verb2(Actor, intrans_verb2(paints(Actor))) --> [paints].
trans_verb2(Somebody, Something, trans_verb2(likes(Somebody, Something))) --> [likes].
回答1:
I think your question is ill-posed. If you say
?- sentence2(Meaning, [john,paints], []).
Meaning = paints(john).
you're clearly saying that the result of the parse is a "meaning" and not a parse tree. I would expect a parse tree to look more like this:
Parse = s(np(properNoun(john)), vp(intrans(paints))).
You can easily produce parse trees by augmenting your DCG with some structural information:
sentence(s(NounPhrase, VerbPhrase)) -->
noun_phrase(NounPhrase),
verb_phrase(VerbPhrase).
noun_phrase(proper_noun(john)) --> [john].
verb_phrase(intransitive(paints)) --> [paints].
If you want one or the other, just generate the one you want. If you want both, you should probably collect the meaning after the parse using the tree. Otherwise there's not much point to getting a parse tree, is there? Here's a sketch:
meaning(s(proper_noun(Noun), intransitive(Verb)), Meaning) :-
Meaning =.. [Verb, Noun].
This would of course have to be made more robust, but this is how it might be used:
?- phrase(sentence(S), [john, paints]), meaning(S, Meaning).
S = s(proper_noun(john), intransitive(paints)),
Meaning = paints(john).
Now explore, play! Make something that parses some of your native language (Italian, right?) into "meaningful" structures. Parse a few sentences of Turkish into "meaning" and generate Italian from it. You'll be surprised how easily it all comes together.
来源:https://stackoverflow.com/questions/16674593/write-a-prolog-dcg-grammar-that-handle-the-mean-of-a-sentence-and-also-build-its