LING 388: Language and Computers: Sandiway Fong
LING 388: Language and Computers: Sandiway Fong
Computers
Sandiway Fong
Lecture 8
Adminstrivia
Review of grammar rules
One worked exercise
Homework 3
submit by next Wednesday midnight to
TA Ben Martin
[email protected]
Last Time
Grammars
what is a grammar?
informally
Sentence NP VP
VP Verb NP
Verb took
NP the man
NP the book
derivation
top-down (from the sentence symbol), bottom-up (from the
words)
-->
is the rewrite symbol
[book], [took]
terminals are enclosed in brackets
sentence, vp, np
terminals and non-terminal symbols
Last Time
example grammar
(from last time)
query format
?- sentence(S,[]).
?- listin g .
?- listing.
example
listing
example
listing.
vp(A, B) :
verb(A, C),
np(C, B).
np(A, B) :
det(A, C),
vp(C, B).
derivation
?- sentence([the,man,took,the,book],[]).
listing.
vp(A, B) :Call: (7) sentence([the, man, took, the, book], []) ? creep
Call: (8) np([the, man, took, the, book], _G353) ? creep
verb(A, C),
Call: (9) det([the, man, took, the, book], _G353) ? creep
np(C, B).
Exit: (9) det([the, man, took, the, book], [man, took, the, book]) ? creep
Call: (9) 'C'([man, took, the, book], man, _G357) ? creep
np(A, B) :Exit: (9) 'C'([man, took, the, book], man, [took, the, book]) ? creep
det(A, C),
Exit: (8) np([the, man, took, the, book], [took, the, book]) ? creep
Call: (8) vp([took, the, book], []) ? creep
det(A, C),
Call: (10) det([the, book], _G353) ? creep
np(A, C),
Call: (10) 'C'([book], book, []) ? creep
Exit: (10) 'C'([book], book, []) ? creep
vp(C, B).
Exit: (9) np([the, book], []) ? creep
Exit: (8) vp([took, the, book], []) ? creep
Exit: (7) sentence([the, man, took, the, book], []) ? creep
Yes
Worked Exercise
Let's write grammar rules to handle:
I saw the boy with a telescope
(ambiguous: 2
derivations)
the boy with a telescope saw me
(unambiguous: 1
derivation)
Worked Exercise
Step 1: decide on the parses you want your
grammar to produce
Unsure? Use a parser you already know one
Worked Exercise
Step 2: write the rules
Remember: terminal and nonterminal
symbols should begin with a lowercase
letter
1. s --> np, vp.
2. np --> dt, nn.
3. np --> prp.
4.
Worked Exercise
Step 3: test and debug your
grammar
?
?-
Prolog Terms
LISP s-exp notation:
Complex terms:
general template:
Simple terms
atoms: s, np, vp, dt
numbers: 1, 45.3
variables: List, Sentence
functor(argument1,..,argumentn
)
functor/n (n= arity)
functor must be an atom
arguments can be simple terms
or (recursive) complex terms
Examples:
nn(boy)
dt(the)
np(nn(boy),dt(the))
Exercise 1
Part 1: Construct Prolog term representations
for the two Stanford parses:
exclude the ROOT node
Exercise 2
Writing a program to analyse/construct complex terms
Exercise 2
using functor and arg
Term = np(nn(boy),dt(the))
Homework 3
Question 1: what does this do?
Question 2: what does this do?
Exercise 3
Univ predicate:
Homework 3
Question 4: Given
devise a query using =.. that builds
the complex term NP:
Question 5: devise a query using =..
that builds the complex term PP: