Attempting to use a Context Free Grammar in order to produce a parser/expert system

25 views
Skip to first unread message

jwfor...@gmail.com

unread,
Dec 13, 2017, 5:49:40 PM12/13/17
to SWI-Prolog
I am presently in the midst of producing a sentence parser/expert system hybrid in Prolog that should ideally parse a sentence into three different lists, which then matches it with a specific recommendation determined via the content of those three lists.

Currently, this is how my parser looks:

sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))):-
np(Sentence, Noun_Phrase, Rem),
vp(Rem, Verb_Phrase, []), !.

np([X|Sentence], np(det(X), NP2), Rem):-
det(X),
np2(Sentence, NP2, Rem).
np(Sentence, Noun_Phrase, Rem):-
np2(Sentence, Noun_Phrase, Rem).
np(Sentence,np(Noun_Phrase, Prep_Phrase), Rem):-
%append(X, Y, Sentence),
pp(Sentence, Prep_Phrase, Rem),
np(Rem, Noun_Phrase, []).

np2([X|Rem], np2(noun(X)), Rem):-
noun(X).
np2([X|Sentence], np2(adj(X), Noun_Phrase), Rem):-
adj(X),
np2(Sentence, Noun_Phrase, Rem).

pp([X|Sentence], pp(prep(X), Noun_Phrase), Rem):-
prep(X),
np(Sentence, Noun_Phrase, Rem).

vp([X|Sentence], vp(verb(X), Prep_Phrase), Rem) :-
verb(X),
pp(Sentence, Prep_Phrase, Rem).
vp([X|Sentence], vp(verb(X), Noun_Phrase), Rem) :-
verb(X),
np(Sentence, Noun_Phrase, Rem).
vp([X|Rem], vp(verb(X)), Rem) :-
verb(X).
It is not ideal as it uses a cut to circumvent an earlier problem that went relatively undiagnosed, but the parser as a whole does it's job relatively well. I understand that not all sentences can be parsed in this manner, but for the examples given, this is what was recommended. As can be observed from what has been presented above, there are 5 types of words; adjectives, determiners, nouns, prepositions and verbs.

To obtain a sentence parse, this is the way that the input must be structured:

sentence([a,very,young,boy,loves,a,manual,problem], S).
However, following this parse what must be done afterwards is to turn it into a minimalist expert system of sorts, where it analyzes each word in a sentence structure and determines a specific outcome. This is where I am stuck and despite the best attempts from peers, have not been able to figure out a viable solution. Previously I used sublists which worked, but was then informed that I should not achieve the goal in this manner and so this idea was scrapped. The example given to us in the project brief was as follows:

present(construction_kit, subject(very,young,boy), verb(loves), object(manual,problem)).
present(golfing_sweater, subject(young,father), verb(loves), object(golf).

In simplest terms, following the parse what should then be done is to have the sentence analyzed and a present from a set list/database assigned to it depending on the words used in a given statement. This is the point that I have been unable to progress from.

Barb Knox

unread,
Dec 14, 2017, 2:15:39 AM12/14/17
to SWI-Prolog
A more Prolog-y approach would be to use a DCG, which would eliminate almost all your list bookkeeping.  I guesstimate that the resulting code would be about 1/3 the size of the above code, making it easier to debug and modify.

Also, natural language parsers generally produce a detailed parse tree rather than word lists.  For example, something like
   sentence(subject(np(det(a), …, noun(boy))), predicate(trans_vp(verb(loves), object(…))))
There are many different ways to structure a parse tree.


To obtain a sentence parse, this is the way that the input must be structured:

sentence([a,very,young,boy,loves,a,manual,problem], S).
However, following this parse what must be done afterwards is to turn it into a minimalist expert system of sorts, where it analyzes each word in a sentence structure and determines a specific outcome. This is where I am stuck and despite the best attempts from peers, have not been able to figure out a viable solution. Previously I used sublists which worked, but was then informed that I should not achieve the goal in this manner and so this idea was scrapped. The example given to us in the project brief was as follows:

present(construction_kit, subject(very,young,boy), verb(loves), object(manual,problem)).
present(golfing_sweater, subject(young,father), verb(loves), object(golf).

In simplest terms, following the parse what should then be done is to have the sentence analyzed and a present from a set list/database assigned to it depending on the words used in a given statement. This is the point that I have been unable to progress from.

It is in general not easy to map a syntactic parse tree into some semantic/pragmatic representation.  This is an area of active research.  To make your problem tractable you need to fully think through the ontology of your universe of presents, including what makes a particular kind of present suitable for a particular kind of recipient.  Once you have that then you can probably see which aspects of a parse tree determine the kind of recipient, and thus the kind of present.

Good luck.

-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------

norbert...@gmail.com

unread,
Dec 14, 2017, 4:50:11 AM12/14/17
to SWI-Prolog


On Thursday, 14 December 2017 08:15:39 UTC+1, barbara wrote:

It is in general not easy to map a syntactic parse tree into some semantic/pragmatic representation.  This is an area of active research.  

Recall that Discourse Representation Theory (DRT) was already established in the 1980s.
Reply all
Reply to author
Forward
0 new messages