Taaltheorie en Taalverwerking BSc Artificial Intelligence Raquel - - PowerPoint PPT Presentation

taaltheorie en taalverwerking
SMART_READER_LITE
LIVE PREVIEW

Taaltheorie en Taalverwerking BSc Artificial Intelligence Raquel - - PowerPoint PPT Presentation

Taaltheorie en Taalverwerking BSc Artificial Intelligence Raquel Fernndez Institute for Logic, Language, and Computation Winter 2012, lecture 2b Raquel Fernndez TtTv 2012 - lecture 2b 1 / 24 Announcements Presentations website and


slide-1
SLIDE 1

Taaltheorie en Taalverwerking

BSc Artificial Intelligence

Raquel Fernández Institute for Logic, Language, and Computation

Winter 2012, lecture 2b

Raquel Fernández TtTv 2012 - lecture 2b 1 / 24

slide-2
SLIDE 2

Announcements

  • Presentations website and work plan

https://sites.google.com/site/projectstttv2012/

  • Guest lecture on Machine Translation by Gideon Wenniger

http://staff.science.uva.nl/~gemaille/

  • Homework

Raquel Fernández TtTv 2012 - lecture 2b 2 / 24

slide-3
SLIDE 3

Overview

Last lecture:

  • Introduction to grammars
  • Chomsky hierarchy
  • grammars for natural language: not regular, mostly context-free.

Raquel Fernández TtTv 2012 - lecture 2b 3 / 24

slide-4
SLIDE 4

Overview

Last lecture:

  • Introduction to grammars
  • Chomsky hierarchy
  • grammars for natural language: not regular, mostly context-free.

Today:

  • Grammars for syntactic analysis of natural languages
  • Implementation of CFGs in Prolog as DCGs

Raquel Fernández TtTv 2012 - lecture 2b 3 / 24

slide-5
SLIDE 5

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign.

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-6
SLIDE 6

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-7
SLIDE 7

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

  • speakers have intuitions about what are well-formed sentences of

a language, even if they don’t know what a sentence means:

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-8
SLIDE 8

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

  • speakers have intuitions about what are well-formed sentences of

a language, even if they don’t know what a sentence means:

Colourless green ideas sleep furiously (Noam Chomsky)

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-9
SLIDE 9

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

  • speakers have intuitions about what are well-formed sentences of

a language, even if they don’t know what a sentence means:

Colourless green ideas sleep furiously (Noam Chomsky) The gostak distims the doshes (Andrew Ingraham)

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-10
SLIDE 10

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

  • speakers have intuitions about what are well-formed sentences of

a language, even if they don’t know what a sentence means:

Colourless green ideas sleep furiously (Noam Chomsky) The gostak distims the doshes (Andrew Ingraham)

  • the same sequence of words can convey different meanings:

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-11
SLIDE 11

Syntax

Syntax is the area of linguistics which studies the internal structure

  • f natural language sentences.
  • not all sequences of words are well-formed sentences:

US of criticisms policy Chomsky threats has because of death received his foreign. Chomsky has received death threats because of his criticisms of US foreign policy.

  • speakers have intuitions about what are well-formed sentences of

a language, even if they don’t know what a sentence means:

Colourless green ideas sleep furiously (Noam Chomsky) The gostak distims the doshes (Andrew Ingraham)

  • the same sequence of words can convey different meanings:

The tourist saw the astronomer with the telescope

Raquel Fernández TtTv 2012 - lecture 2b 4 / 24

slide-12
SLIDE 12

Syntactic Ambiguity

We can account for some of the different meanings of a sentence by assigning more than one possible internal structure to it.

Raquel Fernández TtTv 2012 - lecture 2b 5 / 24

slide-13
SLIDE 13

Syntactic Ambiguity

We can account for some of the different meanings of a sentence by assigning more than one possible internal structure to it.

S NP Det the N tourist VP VP V saw NP Det the N astronomer PP P with NP Det the N telescope

Raquel Fernández TtTv 2012 - lecture 2b 5 / 24

slide-14
SLIDE 14

Syntactic Ambiguity

S NP Det the N tourist VP V saw NP NP Det the N astronomer PP P with NP Det the N telescope

Raquel Fernández TtTv 2012 - lecture 2b 6 / 24

slide-15
SLIDE 15

Syntactic Ambiguity

S NP Det the N tourist VP V saw NP NP Det the N astronomer PP P with NP Det the N telescope

Using linguistic terminnology, in one case the prepositional phrase (PP) modifies the verb phrase (VP), while in the other case it modifies the noun phrase (NP).

Raquel Fernández TtTv 2012 - lecture 2b 6 / 24

slide-16
SLIDE 16

Formal Grammars for Syntactic Analysis

Raquel Fernández TtTv 2012 - lecture 2b 7 / 24

slide-17
SLIDE 17

Formal Grammars for Syntactic Analysis

The number of well-formed sentences of a language is potentially infinite.

  • Formal grammars allow us to generate an infinite number of

sentences with finite means (a finite set of rules).

Raquel Fernández TtTv 2012 - lecture 2b 7 / 24

slide-18
SLIDE 18

Formal Grammars for Syntactic Analysis

The number of well-formed sentences of a language is potentially infinite.

  • Formal grammars allow us to generate an infinite number of

sentences with finite means (a finite set of rules).

  • For a grammar to be able to generate an infinite languages it

must be recursive.

Raquel Fernández TtTv 2012 - lecture 2b 7 / 24

slide-19
SLIDE 19

Formal Grammars for Syntactic Analysis

The number of well-formed sentences of a language is potentially infinite.

  • Formal grammars allow us to generate an infinite number of

sentences with finite means (a finite set of rules).

  • For a grammar to be able to generate an infinite languages it

must be recursive.

∗ we can have direct recursion within a rule, or ∗ recursion across rules

NP → Det N NP → NP PP PP → P NP

Raquel Fernández TtTv 2012 - lecture 2b 7 / 24

slide-20
SLIDE 20

Formal Grammars for Syntactic Analysis

The number of well-formed sentences of a language is potentially infinite.

  • Formal grammars allow us to generate an infinite number of

sentences with finite means (a finite set of rules).

  • For a grammar to be able to generate an infinite languages it

must be recursive.

∗ we can have direct recursion within a rule, or ∗ recursion across rules

NP → Det N NP → NP PP PP → P NP

∗ there is recursion when the same non-terminal symbol appears both in the left-hand side of a rule and in the left-hand side of a (possible different) rule.

Raquel Fernández TtTv 2012 - lecture 2b 7 / 24

slide-21
SLIDE 21

Formal Grammars for Syntactic Analysis

  • A grammar can be used to decide whether a sentence is a

well-formed sentence of a given language or not.

∗ If the sentence can be derived from the grammar rules, then it is well-formed, i.e. part of the language specified by the grammar or grammatical

  • A grammar can also be used to assign structure to a sentence

∗ the computational process of assigning a syntactic structure to a sentence is called parsing and the resulting trees of a derivation parse trees. → We’ll discuss parsing next week. (Recall our discussion of FSAs as recognizers and generators)

Raquel Fernández TtTv 2012 - lecture 2b 8 / 24

slide-22
SLIDE 22

Grammar Equivalence

What grammar should we choose?

Raquel Fernández TtTv 2012 - lecture 2b 9 / 24

slide-23
SLIDE 23

Grammar Equivalence

What grammar should we choose?

  • Two grammars are strongly equivalent if they generate the same

language (the same set of strings) and assign the same parse tree to each sentence (the non-terminal symbols may be different).

Raquel Fernández TtTv 2012 - lecture 2b 9 / 24

slide-24
SLIDE 24

Grammar Equivalence

What grammar should we choose?

  • Two grammars are strongly equivalent if they generate the same

language (the same set of strings) and assign the same parse tree to each sentence (the non-terminal symbols may be different).

  • Two grammars are weakly equivalent if they generate the same

language but assign different parse trees to some sentences.

Raquel Fernández TtTv 2012 - lecture 2b 9 / 24

slide-25
SLIDE 25

Grammar Equivalence

What grammar should we choose?

  • Two grammars are strongly equivalent if they generate the same

language (the same set of strings) and assign the same parse tree to each sentence (the non-terminal symbols may be different).

  • Two grammars are weakly equivalent if they generate the same

language but assign different parse trees to some sentences.

CFG 1: S → NP VP NP → D N VP → V NP CFG 2: S → D NP VP NP → N VP → XP NP XP → V D Rules shared by CFG 1&2: N → dog N → cat D → the V → chases

Raquel Fernández TtTv 2012 - lecture 2b 9 / 24

slide-26
SLIDE 26

Grammar Equivalence

What grammar should we choose?

  • Two grammars are strongly equivalent if they generate the same

language (the same set of strings) and assign the same parse tree to each sentence (the non-terminal symbols may be different).

  • Two grammars are weakly equivalent if they generate the same

language but assign different parse trees to some sentences.

CFG 1: S → NP VP NP → D N VP → V NP CFG 2: S → D NP VP NP → N VP → XP NP XP → V D Rules shared by CFG 1&2: N → dog N → cat D → the V → chases

⇒ We want to design formal grammars for natural languages that meet linguistic considereations about how words combine together.

Raquel Fernández TtTv 2012 - lecture 2b 9 / 24

slide-27
SLIDE 27

Phrase Structure: Constituents

Intuitively words group together to form phrases or constituents. Some of the most comon types of phrases are:

  • noun phrase (NP)
  • verb phrase (VP)
  • adjective phrase (AP)
  • prepositional phrase (PP)

Raquel Fernández TtTv 2012 - lecture 2b 10 / 24

slide-28
SLIDE 28

Phrase Structure: Constituents

Intuitively words group together to form phrases or constituents. Some of the most comon types of phrases are:

  • noun phrase (NP)
  • verb phrase (VP)
  • adjective phrase (AP)
  • prepositional phrase (PP)

The criteria for defining constituents are sometimes fuzzy. The main criterion is distributional: words that are part of a constituent appear in similar syntactic positions.

Raquel Fernández TtTv 2012 - lecture 2b 10 / 24

slide-29
SLIDE 29

Phrase Structure: Constituents

Intuitively words group together to form phrases or constituents. Some of the most comon types of phrases are:

  • noun phrase (NP)
  • verb phrase (VP)
  • adjective phrase (AP)
  • prepositional phrase (PP)

The criteria for defining constituents are sometimes fuzzy. The main criterion is distributional: words that are part of a constituent appear in similar syntactic positions.

NPs Maria three police cars they arrived the president and the chairman of the committee a dog that had been missing for days

Raquel Fernández TtTv 2012 - lecture 2b 10 / 24

slide-30
SLIDE 30

Phrase Structure: Constituents

Intuitively words group together to form phrases or constituents. Some of the most comon types of phrases are:

  • noun phrase (NP)
  • verb phrase (VP)
  • adjective phrase (AP)
  • prepositional phrase (PP)

The criteria for defining constituents are sometimes fuzzy. The main criterion is distributional: words that are part of a constituent appear in similar syntactic positions.

NPs Maria three police cars they arrived the president and the chairman of the committee a dog that had been missing for days APs fast highly stylish the most amazing red car convertible

Raquel Fernández TtTv 2012 - lecture 2b 10 / 24

slide-31
SLIDE 31

Phrases and Parts of Speech

  • The labels we give to constituents (NP, VP, PP,. . . ) depend on

the categories of the words we find in them (N, V, P,. . . ).

Raquel Fernández TtTv 2012 - lecture 2b 11 / 24

slide-32
SLIDE 32

Phrases and Parts of Speech

  • The labels we give to constituents (NP, VP, PP,. . . ) depend on

the categories of the words we find in them (N, V, P,. . . ).

  • Word classes or parts-of-speech (POS) are also defined

distributionally.

Raquel Fernández TtTv 2012 - lecture 2b 11 / 24

slide-33
SLIDE 33

Phrases and Parts of Speech

  • The labels we give to constituents (NP, VP, PP,. . . ) depend on

the categories of the words we find in them (N, V, P,. . . ).

  • Word classes or parts-of-speech (POS) are also defined
  • distributionally. Some of the most common ones:
  • pen classes (in continuous change/expansion):

N Noun table, dogs, justice, oak V Verb run, climb, love, ignore A Adjective green, fast, mild, imaginary Adv Adverb quickly, well, alone closed classes (fix number): P Preposition in, to, of, after, from D Determiner a, the, all, some Pro Pronoun I, you, she, theirs, our

Raquel Fernández TtTv 2012 - lecture 2b 11 / 24

slide-34
SLIDE 34

Phrases and Parts of Speech

  • The labels we give to constituents (NP, VP, PP,. . . ) depend on

the categories of the words we find in them (N, V, P,. . . ).

  • Word classes or parts-of-speech (POS) are also defined
  • distributionally. Some of the most common ones:
  • pen classes (in continuous change/expansion):

N Noun table, dogs, justice, oak V Verb run, climb, love, ignore A Adjective green, fast, mild, imaginary Adv Adverb quickly, well, alone closed classes (fix number): P Preposition in, to, of, after, from D Determiner a, the, all, some Pro Pronoun I, you, she, theirs, our

Note that the same word may have two POS: e.g. love can be N or V.

Raquel Fernández TtTv 2012 - lecture 2b 11 / 24

slide-35
SLIDE 35

Phrases and Parts of Speech

  • The labels we give to constituents (NP, VP, PP,. . . ) depend on

the categories of the words we find in them (N, V, P,. . . ).

  • Word classes or parts-of-speech (POS) are also defined
  • distributionally. Some of the most common ones:
  • pen classes (in continuous change/expansion):

N Noun table, dogs, justice, oak V Verb run, climb, love, ignore A Adjective green, fast, mild, imaginary Adv Adverb quickly, well, alone closed classes (fix number): P Preposition in, to, of, after, from D Determiner a, the, all, some Pro Pronoun I, you, she, theirs, our

Note that the same word may have two POS: e.g. love can be N or V. [POS tagging is the task of labelling words in context with their correct

  • POS. Will be covered in 2nd year course. See J&M chapter 5.]

Raquel Fernández TtTv 2012 - lecture 2b 11 / 24

slide-36
SLIDE 36

Syntactic Analysis

  • The grammar rules that tell us the parts-of-speech of each word are

referred to as the lexicon.

  • The rest of grammar rules are considered phrase structure rules and

provide a structural description of a sentence in terms of its constituents.

Raquel Fernández TtTv 2012 - lecture 2b 12 / 24

slide-37
SLIDE 37

Syntactic Analysis

  • The grammar rules that tell us the parts-of-speech of each word are

referred to as the lexicon.

  • The rest of grammar rules are considered phrase structure rules and

provide a structural description of a sentence in terms of its constituents.

  • In chapter 12 of J&M you can find details on grammar rules for several

English constructions, including several types of sentences, such as questions or commands.

Raquel Fernández TtTv 2012 - lecture 2b 12 / 24

slide-38
SLIDE 38

CFGs in Prolog

Raquel Fernández TtTv 2012 - lecture 2b 13 / 24

slide-39
SLIDE 39

CFGs in Prolog

We can implement context-free grammars (and other types) in

  • Prolog. Once we have a grammar in place, we can use Prolog as:
  • context-free recogniser: a program that correctly classifies strings

as grammatical or ungrammatical (relative to the grammar).

  • context-free parser: is a program which correctly decides

whether a string belongs to the language generated by the grammar and also tells us what its parse tree is.

Raquel Fernández TtTv 2012 - lecture 2b 13 / 24

slide-40
SLIDE 40

CFGs in Prolog

We can implement context-free grammars (and other types) in

  • Prolog. Once we have a grammar in place, we can use Prolog as:
  • context-free recogniser: a program that correctly classifies strings

as grammatical or ungrammatical (relative to the grammar).

  • context-free parser: is a program which correctly decides

whether a string belongs to the language generated by the grammar and also tells us what its parse tree is. Today we’ll look into how to use Prolog as recognizer.

Raquel Fernández TtTv 2012 - lecture 2b 13 / 24

slide-41
SLIDE 41

A Prolog Recognizer

Prolog has a very user-friendly notation for working with CFGs: Definite Clause Grammars or DCGs.

Raquel Fernández TtTv 2012 - lecture 2b 15 / 24

slide-42
SLIDE 42

A Prolog Recognizer

Prolog has a very user-friendly notation for working with CFGs: Definite Clause Grammars or DCGs.

CFG Rules Prolog DCG S → NP VP NP → Det N VP → V NP Det → a Det → the N → witch N → wizard V → curses s --> np,vp. np --> det,n. vp --> v,np. det --> [the]. det --> [a]. n --> [witch]. n --> [wizard]. v --> [curses].

Raquel Fernández TtTv 2012 - lecture 2b 15 / 24

slide-43
SLIDE 43

A Prolog Recognizer

Prolog has a very user-friendly notation for working with CFGs: Definite Clause Grammars or DCGs.

CFG Rules Prolog DCG S → NP VP NP → Det N VP → V NP Det → a Det → the N → witch N → wizard V → curses s --> np,vp. np --> det,n. vp --> v,np. det --> [the]. det --> [a]. n --> [witch]. n --> [wizard]. v --> [curses].

  • DCGs are a shorthand notation that Prolog translates into standard

Prolog clauses.

Raquel Fernández TtTv 2012 - lecture 2b 15 / 24

slide-44
SLIDE 44

A Prolog Recognizer

Prolog has a very user-friendly notation for working with CFGs: Definite Clause Grammars or DCGs.

CFG Rules Prolog DCG S → NP VP NP → Det N VP → V NP Det → a Det → the N → witch N → wizard V → curses s --> np,vp. np --> det,n. vp --> v,np. det --> [the]. det --> [a]. n --> [witch]. n --> [wizard]. v --> [curses].

  • DCGs are a shorthand notation that Prolog translates into standard

Prolog clauses.

  • Note that terminal symbols are written as list elements.
  • To query the grammar we also use lists.

Raquel Fernández TtTv 2012 - lecture 2b 15 / 24

slide-45
SLIDE 45

Is this a grammatical sentence? ?- s([a, witch, curses, the, wizard],[]). true

Raquel Fernández TtTv 2012 - lecture 2b 17 / 24

slide-46
SLIDE 46

Is this a grammatical sentence? ?- s([a, witch, curses, the, wizard],[]). true Is this an NP? ?- np([curses,the],[]). false.

Raquel Fernández TtTv 2012 - lecture 2b 17 / 24

slide-47
SLIDE 47

Is this a grammatical sentence? ?- s([a, witch, curses, the, wizard],[]). true Is this an NP? ?- np([curses,the],[]). false. Which sentences are grammatical? ?- s(X,[]). X = [the, witch, curses, the, witch] ; X = [the, witch, curses, the, wizard] ; X = [the, witch, curses, a, witch] ; X = [the, witch, curses, a, wizard] ; X = [the, wizard, curses, the, witch]

Raquel Fernández TtTv 2012 - lecture 2b 17 / 24

slide-48
SLIDE 48

Is this a grammatical sentence? ?- s([a, witch, curses, the, wizard],[]). true Is this an NP? ?- np([curses,the],[]). false. Which sentences are grammatical? ?- s(X,[]). X = [the, witch, curses, the, witch] ; X = [the, witch, curses, the, wizard] ; X = [the, witch, curses, a, witch] ; X = [the, witch, curses, a, wizard] ; X = [the, wizard, curses, the, witch] Which NPs can be generated? ?- np(X,[]). X = [the, witch] ; X = [the, wizard] ; X = [a, witch] ; X = [a, wizard].

Raquel Fernández TtTv 2012 - lecture 2b 17 / 24

slide-49
SLIDE 49

Is this a grammatical sentence? ?- s([a, witch, curses, the, wizard],[]). true Is this an NP? ?- np([curses,the],[]). false. Which sentences are grammatical? ?- s(X,[]). X = [the, witch, curses, the, witch] ; X = [the, witch, curses, the, wizard] ; X = [the, witch, curses, a, witch] ; X = [the, witch, curses, a, wizard] ; X = [the, wizard, curses, the, witch] Which NPs can be generated? ?- np(X,[]). X = [the, witch] ; X = [the, wizard] ; X = [a, witch] ; X = [a, wizard]. Which verbs are part of the vocabulary? ?- v(X,[]). X = [curses].

Raquel Fernández TtTv 2012 - lecture 2b 17 / 24

slide-50
SLIDE 50

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-51
SLIDE 51

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A).

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-52
SLIDE 52

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true .

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-53
SLIDE 53

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true . ?- det([the, witch, curses, the, wizard],X). X = [witch, curses, the, wizard].

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-54
SLIDE 54

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true . ?- det([the, witch, curses, the, wizard],X). X = [witch, curses, the, wizard]. ?- n([witch, curses, the, wizard],X). X = [curses, the, wizard].

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-55
SLIDE 55

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true . ?- det([the, witch, curses, the, wizard],X). X = [witch, curses, the, wizard]. ?- n([witch, curses, the, wizard],X). X = [curses, the, wizard]. ?- v([curses, the, wizard],X). X = [the, wizard].

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-56
SLIDE 56

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true . ?- det([the, witch, curses, the, wizard],X). X = [witch, curses, the, wizard]. ?- n([witch, curses, the, wizard],X). X = [curses, the, wizard]. ?- v([curses, the, wizard],X). X = [the, wizard]. ?- det([the, wizard],X). X = [wizard]

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-57
SLIDE 57

DCGs as Prolog Clauses

We can use the command listing to see how DCG rules are translated into Prolog clauses.

?- listing(s). s(A, C) :- np(A, B), vp(B, C). ?- listing(np). np(A, C) :- det(A, B), n(B, C). ?- listing(vp). vp(A, C) :- v(A, B), np(B, C). ?- listing(det). det([the|A], A). det([a|A], A). ?- s([the, witch, curses, the, wizard],[]). true . ?- det([the, witch, curses, the, wizard],X). X = [witch, curses, the, wizard]. ?- n([witch, curses, the, wizard],X). X = [curses, the, wizard]. ?- v([curses, the, wizard],X). X = [the, wizard]. ?- det([the, wizard],X). X = [wizard] n([wizard],X). X = [].

Raquel Fernández TtTv 2012 - lecture 2b 19 / 24

slide-58
SLIDE 58

DCGs and Recursion

Note that recursion may led to loops.

Raquel Fernández TtTv 2012 - lecture 2b 21 / 24

slide-59
SLIDE 59

DCGs and Recursion

Note that recursion may led to loops.

  • For instance, if we add a recursive version of the S rule, we will

end up with a recursive prolog clause which can lead to infinite recursion:

s

  • ->

s,conj,s. conj

  • ->

[and]. conj

  • ->

[or]. conj

  • ->

[but]. ?- listing(s). s(A, C) :- np(A, B), vp(B, C). s(A, D) :- s(A, B), conj(B, C), s(C, D).

Raquel Fernández TtTv 2012 - lecture 2b 21 / 24

slide-60
SLIDE 60

DCGs and Recursion

Note that recursion may led to loops.

  • For instance, if we add a recursive version of the S rule, we will

end up with a recursive prolog clause which can lead to infinite recursion:

s

  • ->

s,conj,s. conj

  • ->

[and]. conj

  • ->

[or]. conj

  • ->

[but]. ?- listing(s). s(A, C) :- np(A, B), vp(B, C). s(A, D) :- s(A, B), conj(B, C), s(C, D). We can introduce new non-terminal symbols. Unfortunately there aren’t better solutions. s

  • ->

simple_s. s

  • ->

simple_s,conj,s. simple_s

  • ->

np,vp.

Raquel Fernández TtTv 2012 - lecture 2b 21 / 24

slide-61
SLIDE 61

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-62
SLIDE 62

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he].

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-63
SLIDE 63

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he]. ?- s([she,curses,the,wizard],[]). true

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-64
SLIDE 64

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he]. ?- s([she,curses,the,wizard],[]). true ?- s([she,curses,he],[]). true

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-65
SLIDE 65

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he]. ?- s([she,curses,the,wizard],[]). true ?- s([she,curses,he],[]). true CFG-like solution: s --> np_subject,vp. np_subject --> pro_subject. pro_subject --> [he]. pro_subject --> [she].

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-66
SLIDE 66

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he]. ?- s([she,curses,the,wizard],[]). true ?- s([she,curses,he],[]). true CFG-like solution: s --> np_subject,vp. np_subject --> pro_subject. pro_subject --> [he]. pro_subject --> [she]. Nicer solution: s --> np(subject),vp. np(X) --> pro(X). pro(subject) --> [he]. pro(subject) --> [she].

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-67
SLIDE 67

DCGs and Features

DCGs allow us to enhance CFGs with features by adding extra argument to the DCG rules.

For instance, adding pronouns: np --> pro. pro --> [she]. pro --> [he]. ?- s([she,curses,the,wizard],[]). true ?- s([she,curses,he],[]). true CFG-like solution: s --> np_subject,vp. np_subject --> pro_subject. pro_subject --> [he]. pro_subject --> [she]. Nicer solution: s --> np(subject),vp. np(X) --> pro(X). pro(subject) --> [he]. pro(subject) --> [she]. DGC arguments are Prolog arguments: s(A,B) :- np(subject,A,C), vp(C,B).

Raquel Fernández TtTv 2012 - lecture 2b 23 / 24

slide-68
SLIDE 68

Readings and Homework

All this material (and more) is covered in more detail in:

  • chapters 7 and 8 of Learn Prolog Now!
  • chapter 6 of NLP techniques in Prolog

Raquel Fernández TtTv 2012 - lecture 2b 24 / 24

slide-69
SLIDE 69

Readings and Homework

All this material (and more) is covered in more detail in:

  • chapters 7 and 8 of Learn Prolog Now!
  • chapter 6 of NLP techniques in Prolog

Homework:

  • please recall that homework is mandatory and that the

submission deadline is strict: Friday midgnight.

  • submit via Blackboard, one single file with answers for both

homework #2a and #2b in PDF format.

Raquel Fernández TtTv 2012 - lecture 2b 24 / 24

slide-70
SLIDE 70

Readings and Homework

All this material (and more) is covered in more detail in:

  • chapters 7 and 8 of Learn Prolog Now!
  • chapter 6 of NLP techniques in Prolog

Homework:

  • please recall that homework is mandatory and that the

submission deadline is strict: Friday midgnight.

  • submit via Blackboard, one single file with answers for both

homework #2a and #2b in PDF format. Presentations:

  • form a team and get registered with the website tomorrow

during the computer practicum.

Raquel Fernández TtTv 2012 - lecture 2b 24 / 24