+ All Categories
Home > Documents > Natural Language Processing

Natural Language Processing

Date post: 23-Feb-2016
Category:
Upload: armina
View: 25 times
Download: 0 times
Share this document with a friend
Description:
Natural Language Processing. A few problems from your work ‘append’ program - two lists to one or split one list into two How to write a parser in Prolog , how it works (ref. Paul’s book, page 78-85) Construct parse trees Use of parse trees. Common Problems/Mistakes. - PowerPoint PPT Presentation
21
Natural Language Processing 1. A few problems from your work 2. ‘append’ program - two lists to one or split one list into two 3. How to write a parser in Prolog, how it works (ref. Paul’s book, page 78-85) 4. Construct parse trees 5. Use of parse trees
Transcript
Page 1: Natural Language Processing

Natural Language Processing1. A few problems from your work2. ‘append’ program - two lists to one or split

one list into two3. How to write a parser in Prolog, how it

works (ref. Paul’s book, page 78-85)4. Construct parse trees5. Use of parse trees

Page 2: Natural Language Processing

Common Problems/Mistakes

• % an empty set is a subset of any set Lsubset([], [L]). % what is wrong?

• When call ‘not_u_turn’, must make sure that both parameters are not variables.not_u_turn(X,Y):- \+(u_turn(X,Y).

Page 3: Natural Language Processing

3

(re-used slide) Manipulating Lists Ex. 5 – Appending 2 lists

Write a program append(L1,L2,L3) where L3 is the concatenation of L1 and L2.e.g. append([1,2], [a,b,c],L) returns L = [1,2,a,b,c]Q: how about append(L1, L2, [1,2,3]) ?append([],L,L).append([X|T1],T2,[X|T3]):-

append(T1,T2,T3).(in SICStus Prolog, append is a builtin)

Page 4: Natural Language Processing

How many different way ‘append’ program can be called?

• append([a], [student], [a,student]). % in/in/in• append([you,are],[in,’2q49’], L). % in/in/out

L = [you,are,in,’2q49’]• append([you], L, [you,are,a,student]). % in/out/in

L = [are,a,student]• append(L, [student], [i,am,a,student]). % out/in/in

L = [i,am,a]• append([you],L1,L2). % in/out/out

• append(L1,[you],L2). % out/in/out

• append(L1,L2,[i,am,a,student]). % out/out/in

Page 5: Natural Language Processing

One port can be either Input or output

append

Page 6: Natural Language Processing

Parsing – work out the structure of a sentence The first step toward to processing

language

• I like uwe very much

subject verb object_phrase

noun_phrase adverb

I like uwe very much

Page 7: Natural Language Processing

Why do we need parser?

Traditional Parser’s Role:• Check if a sentence syntactically correct• If yes, construct a parse tree for the

sentence

Non-traditional Role:• Generate possible sentences

Page 8: Natural Language Processing

Why do we need parse tree?• Help us to understand the meaning of a

sentence• Allow us to transform a sentence to other

forms: Machine translation.

e.g. ‘I like uwe’ in Japanese, the order is ‘I uwe like’. We need to know both structures in order to translate

Reconstructing a new sentence from an old one.e.g. ‘I like uwe’ can be changed to ‘why do you like uwe’. This is useful for creating a conversation for chatbot.

Page 9: Natural Language Processing

A Small ExampleLet’s use this small set

of vocabulary { i, you, me, us, uwe,

cs_course, robolics_course, like, love, very_much }

Next, we define a verybasic English grammarshown in the left box.

sentence --> subject_phrase, verb, object_phrase.

subject_phrase --> subject_pronoun.

object_phrase --> object_pronoun, adverb.

object_phrase --> noun_phrase, adverb.

noun_phrase --> determiner, noun.

‘-->’ means ‘defined as’ and ‘consists of’

Page 10: Natural Language Processing

A Small Example (cont)

subject_pronoun --> [i].subject_pronoun --> [we].subject_pronoun --> [you].

object_pronoun --> [you].object_pronoun --> [me].object_pronoun --> [us].

determiner --> [].% determiner --> [a].% determiner --> [the].

noun --> [uwe].noun --> [cs_course].noun --> [robotics_course].

adverb --> [very, much].adverb --> [].

verb --> [like].verb --> [love].

Page 11: Natural Language Processing

How to write it in Prolog?• Those (in slide 9-10) are already Prolog code !• ‘-->’ is specially added for writing a parser

sentence --> subject_phrase, verb, object_phrase.

is equivalent to sentence(L1,Rest):- subject_phrase(L1,L2),

verb(L2,L3), object_phrase(L3,Rest).

So, we can call ‘sentence([we,love,uwe],[]).’ to check if it it a legal sentence

Page 12: Natural Language Processing

Having those defined, we can do lots of things:

| ?- sentence([we, love, uwe],[]).yes

| ?- sentence([love,you],[]).no

| ?- sentence([we, like, you, because], R).R = [because] ? yes

| ?- sentence(L,[]).L = [i,like,uwe,very,much] ? ;L = [i,like,uwe] ? ;L = [i,like,cs_course,very,much] ? ;L = [i,like,cs_course] ? ;

L = [i,like,robotics_course,very,much] ? ;L = [i,like,robotics_course] ? ;L = [i,like,you,very,much] ? ;L = [i,like,you] ? ;L = [i,like,me,very,much] ? ;L = [i,like,me] ? ;L = [i,like,us,very,much] ? yes

| ?- findall(L, sentence(L,[]), All).All = [[i,like,uwe,very,much],[i,like,uwe],

[i,like,cs_course,very,much],[i,like,cs_course],[i,like,robotics_course,very,much],[i,like,robotics_course],[i,like,you,very|...],[i,like,you],[i,like|...],[...|...]|...] ?

What is a legal sentence?

Generate all legal sentences!

Check if it a legal sentence

Page 13: Natural Language Processing

In order to generate parse tree we need to change the code (version 2)

New Oldsentence(s(X, Y, Z)) -->

subject_phrase(X), verb(Y), object_phrase(Z).

subject_phrase(sp(X)) --> subject_pronoun(X).

object_phrase(op(X,Y)) --> noun_phrase(X), adverb(Y).

object_phrase(op(X, Y)) --> object_pronoun(X), adverb(Y).

noun_phrase(np(Y)) --> noun(Y).

prepositional_phrase(pp(X, Y)) --> preposition(X), place_name(Y).

sentence --> subject_phrase, verb, object_phrase.

subject_phrase --> subject_pronoun.

object_phrase --> noun_phrase, adverb.

object_phrase --> object_pronoun, adverb.

noun_phrase --> noun.

prepositional_phrase --> preposition, place_name.

Page 14: Natural Language Processing

In order to generate parse tree we need to change the code (version 2)

New Oldpreposition(prep(in)) --> [in].preposition(prep(at)) --> [at].preposition(prep(from)) --> [from].

place_name(pname(reception)) --> [reception].

place_name(pname(london)) --> [london].place_name(pname(bristol)) --> [bristol].place_name(pname(X)) -->

[X], { next(X,_,_,_,_) }.

subject_pronoun(spn(i)) --> [i].subject_pronoun(spn(we)) --> [we].subject_pronoun(spn(you)) --> [you].

object_pronoun(opn(you))--> [you].object_pronoun(opn(me))--> [me].object_pronoun(opn(us))--> [us]. %...

preposition --> [in].preposition --> [at].preposition --> [from].

place_name --> [reception].place_name --> [london].place_name --> [bristol].place_name -->

[X], { next(X,_,_,_,_) }.

subject_pronoun --> [i].subject_pronoun --> [we].subject_pronoun --> [you].

object_pronoun --> [you].object_pronoun --> [me].object_pronoun --> [us]. % ………

Page 15: Natural Language Processing

Test on version 2 – a list of words to a parse tree| ?- sentence(Tree, [i,love,you],[]).Tree = s(sp(spn(i)),vd(love),op(opn(you),ad([]))) ? ;

| ?- sentence(Tree, [i,love,you,very,much],[]).Tree = s(sp(spn(i)),vd(love),op(opn(you),ad([very,much]))) ? yes

| ?- sentence(T,[i,am,in,bristol],[]).T = s(s_2b([i,am]),pp(prep(in),pname(bristol))) ?

| ?- sentence(s(sp(spn(i)),vd(love),op(opn(you),ad([very,much]))), L,K).L = [i,love,you,very,much|K] ? yes% 6| ?- sentence(s(s_2b([i,am]),pp(prep(in),pname(bristol))), L,[]).L = [i,am,in,bristol] ?

Test on version 2 – a parse tree to a list of word

Page 16: Natural Language Processing

Add a few more grammar rules for questions

question(q(why,do,S)) --> [why, do], sentence(S).question(q(do,S)) --> [do], sentence(S).

/* after added the above line, we can handle questions like: */| ?- question(Tree, [why,do ,you, love,me],[]).Tree =

q(why,do,s(sp(spn(you)),vd(love),op(opn(me),ad([])))) ?| ?- question(Tree, [do ,you, like,uwe],[]).Tree = q(do,s(sp(spn(you)),vd(like),op(np(uwe),ad([])))) ?

Page 17: Natural Language Processing

How to change a sentence to a question beginning with ‘why’?

I like uwe Why do you like uwe

mapping(s2why, %

s(sp(spn(N1)), vd(V), op(np(noun(N2)), ad(X))),

q(why,do,s(sp(spn(P1)),vd(V),op(np(noun(N2)),ad(X)))) ) :- mapping_spn(N1, P1).

Page 18: Natural Language Processing

How to change a sentence to a question beginning with ‘why’?

I like you Why do you like me

mapping(s2why, % type of mapping

s(sp(spn(N1)),vd(V),op(opn(N2),ad(X))),

q(why,do,s(sp(spn(P1)),vd(V),op(opn(P2),ad(X)))) ) :- mapping_spn(N1, P1), mapping_opn(N2, P2).

Page 19: Natural Language Processing

Parser Parser converging parse trees

| ?- question(Tree, [why,do,you,love,me],[]), mapping(s2why, T, Tree), sentence(T, L, []).L = [i,love,you],

T = s(sp(spn(i)),vd(love),op(opn(you),ad([]))),

Tree = q(why,do,s(sp(spn(you)),vd(love),op(opn(me),ad([])))) ?

| ?- sentence(T1, [i,like,uwe],[]), mapping(s2why,T1,T2), question(T2, L,[]).L = [why,do,you,like,uwe],T1 = s(sp(spn(i)),vd(like),op(np(noun(uwe)),ad([]))),T2 = q(why,do,s(sp(spn(you)),vd(like),op(np(noun(uwe)),ad([])))) ? yes% 7

Page 20: Natural Language Processing

Put them together (file english.pl)

test:- repeat, readin(S), gen_reply(S,Ans), write_list(Ans), nl, S = [bye|_].

test:- write(bye),nl.

gen_reply(S,Reply):- sentence(Tree1, S, _Rest),!, mapping(s2why,Tree1, Tree2),question(Tree2, Rep,[]),append(Rep, ['?'], Reply).

gen_reply(S,Reply):- question(Tree2, S, _Rest),!, mapping(s2q,Tree1, Tree2),sentence(Tree1, Rep,[]),append([yes, ','|Rep], ['!'], Reply).

gen_reply([bye|_],[bye ,for, now, '.']).

gen_reply(_,[what,'?']). % default case

% ‘!’ will be explained in next week

Page 21: Natural Language Processing

A test example from this simple chatbot

| ?- test.|: hi!|: what ?

Hi!|: what ?

i love you!|: why do you love me ?

don't know.|: what ?

do you love me?|: yes , i love you !

bye.bye for now .


Recommended