Prolog Personal Homework Help

to modify the Prolog code for parsing simple sentences

Here's the original parsing code:


% grammar rules:

s(P1,P3,s(NP,VP)) :- np(P1,P2,NP), vp(P2,P3,VP).

vp(P1,P3,vp(v(Verb),NP)) :- v(P1,P2,Verb), np(P2,P3,NP).

np(P1,P2,np(name(Name))) :- proper(P1,P2,Name).

np(P1,P3,np(art(Art),noun(Noun))) :- art(P1,P2,Art), noun(P2,P3,Noun).

% sample lexicon entries:





% rules to build lexical constituents:

art(From, To, Word) :- word(Word, From, To), isart(Word).

noun(From, To, Word) :- word(Word, From, To), isnoun(Word).

v(From, To, Word) :- word(Word, From, To), isverb(Word).

proper(From, To, Word) :- word(Word, From, To), isname(Word).

% An example sentence to parse:

word(john, 1, 2).

word(eats, 2, 3).

word(the, 3, 4).

word(cat, 4, 5).


We will need to expand the lexicon entries as well as the grammar rules, to provide the SEM features. We will do this without using the SEM keyword - instead the second argument to the lexicon entry will be the semantic information.

isname(john, "John").

isverb(eats, eats1).

isart(the, the1).

isnoun(cat, cat1).

This will in turn necessitate changes to any code that uses the lexicon entry, for example the rules that build lexical constituents, e.g.

art(From, To, Word, Sem) :-

word(Word, From, To), isart(Word, Sem).

The other thing we need is a mechanism to create VAR features for constituents. For lexical constituents, the easy way to do this is to attach a VAR to each word, like this:

word(john, 1, 2, j1).

word(eats, 2, 3, e1).

word(the, 3, 4, t1).

word(cat, 4, 5, c1).

Now we need to modify the rules that build lexical constituents again, e.g.

art(From, To, Word, Sem, Var) :-

word(Word, From, To, Var), isart(Word, Sem).

We are coding the VAR feature positionally, as we did with SEM (and with the syntactic analysis). It's up to you to modify the rules that build phrasal constituents (like NP, VP, and S) so that they pass around the VAR features appropriately. Your other job, in fact your main job, is to construct and pass around the SEM features for the phrasal constituents. You will do this by modifying the grammar rules.

One point that you might find tricky is lambda-reduction, although in fact lambda reduction turns out to be implementable as a single simple rule, plus a "bypass" rule. To give you a start, here are the rules:


lambda_reduce(lambda(X, Predicate), Argument, Predicate) :-

X = Argument, !.

lambda_reduce(X, _, X). % in case there is no lambda expression to reduce.

Lambda reduction should be done as the last goal after the neck. (Why?)

You would use this rule as demonstrated below:

: lambda_reduce(lambda(X, eats1(e1, X, the1(c1, cat1))),

name(j1, "John"),

Result) ?

X = name(j1, "John")

Result = eats1(e1, name(j1, "John"), the1(c1, cat1))

In other words the condition on the right of the neck forces the Argument to unify with the lambda-variable X, and then the copy of the Predicate that is the final parameter of lambda_reduce allows you to retrieve the Result of the lambda-reduction.


You will see that it has been necessary to modify the format of the semantic interpretations of different items so that they become Prolog terms - thus for example (NAME j1 "John"), in the notation used in lectures, becomes name(j1, 'John').

As an example of part of the performance of your system, you should be able to handle all of the phrasal constituent types as exemplified below (assume "" contains the words for "John eats the cat"):

% prolog [url removed, login to view]

: np(1,2,Syn,Sem,Var) ?

Syn = np(name(john))

Sem = name(j1, John)

Var = j1

: np(3,5,Syn,Sem,Var) ?

Syn = np(art(the), noun(cat))

Sem = the1(c1, cat1)

Var = c1

: vp(2,5,Syn,Sem,Var) ?

Syn = vp(v(eats), np(art(the), noun(cat)))

Sem = lambda(_R62, eats1(e1, _R62, the1(c1, cat1)))

Var = e1

: s(1,5,Syn,Sem,Var) ?

Syn = s(np(name(john)), vp(v(eats), np(art(the), noun(cat))))

Sem = eats1(e1, name(j1, John), the1(c1, cat1)))

Var = e1

: control-D



There will be a slight change to the values of the Syn slots in the final version of the analyses - see below.

The identity of the anonymous variable in the Sem of the vp (_R62 in the example) may vary - all that matters is that it is a variable. This applies to all variables in lambda-expressions that appear in the Sem slots.

Required coverage:

The parts of speech to be covered are: art noun prep pro proper v

("pro" signifies pronoun, "proper" signifies proper noun, "prep" signifies preposition). You must use these names for the parts of speech.

The grammar rules to be covered are:

the ones at the top of this specification, with a slight modification:

s -> np vp

vp -> v np

np -> proper

np -> art cnp2

cnp2 -> cnp

cnp -> noun

The peculiarities in the last three rules are needed to allow the rule for NPs modified by a prepositional phrase to be fitted in later. It would be reasonable to just start with the 4 rules shown at the top, get them working, then move to the six rules shown above. With the six rules shown above, the Syn slots will change to the following:

: np(3,5,Syn,Sem,Var) ?

Syn = np(art(the), cnp(noun(cat)))

: vp(2,5,Syn,Sem,Var) ?

Syn = vp(v(eats), np(art(the), cnp(noun(cat))))

: s(1,5,Syn,Sem,Var) ?

Syn = s(np(name(john)), vp(v(eats), np(art(the), cnp(noun(cat)))))

and the analyses for the CNP and CNP2 in "John eats the cat." would be as follows:

: cnp(4,5,Syn,Sem,Var)?

Syn = noun(cat)

Sem = cat1

Var = c1

: cnp2(4,5,Syn,Sem,Var)?

Syn = cnp(noun(cat))

Sem = cat1

Var = c1

bitransitive verb phrases (vp -> v np np)

For example, with the vp give Mary food, with words and lexical entries:

word(give,2,3,g1). isverb(give, give1).

word(mary,3,4,m1). isname(mary, "Mary").

word(food,4,5,f1). isnoun(food, food1). and a rule np -> cnp2

the analysis should be like this:

: vp(2,5,Syn,Sem,Var)?

Syn = vp(v(give), dobj(cnp(noun(food))), iobj(np(name(mary))))

Sem = lambda(_R72, give1(g1, _R72, food1, recipient(name(m1, Mary))))

Var = g1

The exact identity of the variable _R72 is unimportant. As you will have to implement the rule np -> cnp2 to check this test item, I'll make it a requirement that this rule be implemented (it's pretty easy, anyway).

noun phrases with a prepositional phrase modifier (cnp2 -> cnp pP). Obviously, to do this one, it will be necessary to handle prepositional phrases (pP -> prep np). Note that prepositional phrase must be referred to as pP, not pp. (pp is a Prolog predefined "predicate" that pretty-prints a procedure: e.g. pp(word) ! to print out all your word definitions.)

For example, with "the apple of Mary" (encoded as

word(the, 32, 33, t3).

word(apple, 33, 34, a1). isnoun(apple, apple1).

word(of, 34, 35, o1). isprep(of, owned_by1).

word(mary, 35, 36, m1).

the analyses should be like this:

: cnp2(33,36,Syn,Sem,Var)?

Syn = cnp2(noun(apple), pP(prep(of), np(name(mary))))

Sem = lambda(_R79, &(apple1(_R79), owned_by1(_R79, name(m1, Mary))))

Var = a1

: pP(34,36,Syn,Sem,Var)?

Syn = pP(prep(of), np(name(mary)))

Sem = lambda(_R62, owned_by1(_R62, name(m1, Mary)))

Var = o1

: np(32,36,Syn,Sem,Var)?

Syn = np(art(the), cnp2(noun(apple), pP(prep(of), np(name(mary)))))

Sem = the1(a1, &(apple1(a1), owned_by1(a1, name(m1, Mary))))

Var = a1

noun phrases that are just a single pronoun. For example, with word(she,1,2,s1).


Syn = np(pro(she))

Sem = pro(s1, she1)

Var = s1

It is probably advisable to start work just with the four basic rules and associated parts of speech; then move to the six-rule version; later you can refine your program to add the extra parts of speech and rules for:

bitransitive verb phrases

noun phrases with a PP modifier

noun phrases that are just a single pronoun

Expect that in my test data I will use my own lexicon entries and words, and in particular, don't modify the format of these!

Format of lexicon entries: In order to get your rules for building lexical constituents right, you need to have the right format for the lexicon entries: here are example lexicon entries for each of the six required lexical categories:

isart(the, the1).

isname(jane, "Jane").

isnoun(wombat, wombat1).

isprep(on, on_loc1).

ispro(him, he1).

isverb(give, give1).

Warning: Your code may not work if you do not have at least one lexical entry for each part of speech that your grammar handles, even if the sentence you are currently working with does not use all the parts of speech.

What to submit: Submit (in a single file) the following:

the definition of lambda_reduce;

your grammar rules;

your rules for building lexical constituents


Om arbejdsgiveren:
( 2 bedømmelser )

Projekt ID: #1881

1 freelancer byder i gennemsnit $51 på dette job


Dear Sir, Well we can do all kind of work u need starting from design to print, We are basically situated at Delhi but having offices in various parts of the country, We can provide u quality work at very low cos Flere

$51 USD in 2 dage
(1 bedømmelse)