Download presentation
Presentation is loading. Please wait.
Published byJames Crawford Modified over 9 years ago
1
Natural Language Processing DCG and Syntax NLP DCG A “translation” example: special case A DCG recogniser
2
Natural Language Processing NLP is the art and science of getting computers to understand natural language. NLP draws on materials from other disciplines: computer science, formal philosophy and formal linguistics NLP is an “AI complete” task: all the activities which turn up elsewhere in AI, such as knowledge representation, planning, inference and so on turn up in one form or another in NLP.
3
DCG (Definite Clause Grammar) An example $120pw $200perweek $150pweek [$, 1, 2, 0, p, w] price-->dollar, number, unit. dollar-->[$]. number-->digit, number. number-->digit. unit-->[p,w]. unit-->[p, e, r, w, e, e, k]. unit-->[p, w, e, e, k]. digit-->[1]. digit-->[2]. digit-->[3].... ?- price([$, 2, 3, 4, p, w], []). Yes ?-price([8, 0, 0, p, w, e, e, k], []). No.
4
Expand DCG to standard predicates Price-->dollar, number, unit. price(List1, List2):- dollar(List1, List11), number(List11, List12), unit(List12, List2). dollar-->[$]. dollar([$|List], List). digit-->[1]. digit([1|List], List]). number-->digit, number. number-->digit.
5
Extending DCG 1.Add variables 2.Add normal predicates in { } ?-price(X, [$, 1,2,3, p, w], []). X=[1,2,3] price(X)-->dollar, number(X), unit. number([D|T])-->digit(D), number(T). number([D])-->digit(D). digit(1)-->[1]. digit(2)-->[2]. price(X)-->dollar, number(X), unit, {length(X,N), N<3}.
6
Expand extended DCG to standard predicates price(X)-->dollar, number(X), unit, {length(X, N), N<3}. price(X, List1, List2):- dollar(List1, List11), number(X, List11, List12), unit(List12, List2), length(X, N), N<3.
7
A “machine translation” example three hundred and thirty four: 334 twenty one: 21 fourteen: 14 five: 5 ?-to_number(N, [three, hundred, and, thirty, four],[]). N=334.
8
A “translation” example Vocabulary, lexicon digit(1) --> [one]. digit(2) --> [two]. ….. digit(9) --> [nine]. teen(10) --> [ten]. teen(11) --> [eleven]. ….. teen(19) --> [nineteen]. tens(20) --> [twenty]. tens(30) --> [thirty]. ….. tens(90) --> [ninety].
9
A “translation” example Numbers with one or two digits. xx(N) --> digit(N). xx(N) --> teen(N). xx(N) --> tens(T), rest_xx(N1), {N is T+N1}. rest_xx(N) --> digit(N). rest_xx(0) --> [].
10
A “translation” example % numbers with 3 or fewer digits xxx(N) --> digit(D), [hundred], rest_xxx(N1), {N is D*100+N1}. xxx(N) --> xx(N). rest_xxx(N) --> [and], xx(N). rest_xxx(0) --> []. %top level to_number(0) --> [zero]. to_number(N) --> xxx(N). Query ?-to_number(N, [two, hundred, and, twenty one], []). N=221
11
Representing Syntactic Knowledge Syntactic knowledge: – Syntactic Categories: e.g. Noun, Sentence. – Grammatical features: e.g. Singular, Plural – Grammar rules. Why bother?
12
Parts of language Regard sentences as being built out of constituents Two types of constituents: – words (simple constituents), which have lexical categories like noun, verb, etc. – phrases (compound constituents), like noun phrases, verb phrases, etc. How to store syntactic knowledge? – lexicon – grammar rules
13
Words: Lexical Categories (Parts of Speech) Noun (N): Jack, tree, house, cannon Verb (V): build, walk, kill Adjective (Adj): big, red, unpleasant Determiner (Det): the, a, which, that – Jack built {the, a, that} big, red house; – Which house did Jack build? Preposition (Prep): with, for, in, from, to, through, via, under
14
Words: Lexical Categories (ctd) Pronoun (Pro): her, him, she, itself, that, it – I saw the man in the park with the telescope – Don't do that to him Conjunction (Conj): and, or, but. Two kinds of lexical categories: 1.Open categories (“content words”): N, V, Adj 2.Closed categories (“function words”): Det, Prep, Pro, Conj
15
Compound Constituents Some compound constituents: Sentence (S): Jack built the house. Noun Phrase (NP): John; the big, red house; the house that Jack built; the destruction of the city. Verb Phrase (VP): built the house quickly; saw the man in the park. Prepositional Phrase (PP): with the telescope; on the table
16
A Simple Grammar S NP VP VP V NP NP Proper_N NP det N Proper_N John Proper_N Mary N cake V loves V ate det the Sentences in this language: “John loves Mary” “John ate the cake” “John loves the cake”
17
Definite Clause Grammars (DCGs) The above grammar can be simply implemented in DCG notation as follows: s --> np, vp. vp --> v, np. np --> proper_n. np --> det, n. proper_n --> [john]. proper_n --> [mary]. n --> [cake]. v --> [loves]. v --> [ate]. det --> [the].
18
A Second Implementation A more efficient implementation: s --> np, vp. vp --> [V],{verb(V)},np. np --> [N],{proper_n(N)}. np --> [Det],{det(Det)}, [N],{noun(N)}. proper_n(john). proper_n(mary). noun(cake). verb(loves). verb(ate). det(the). Notes: 1.The {} allow Prolog code to be embedded in DCG rules 2.The cost of processing is now less dependent on lexicon size (given good indexing) 3.So far we have implemented only a DCG recogniser, not a parser.
19
Translating DCG Consider the rule s --> np, vp. Prolog translates this as: s(Ws1,Ws2) :- np(Ws1,Ws),vp(Ws,Ws2). This says that after taking an s off the start of Ws1, Ws2 remains The rule proper_n --> [john]. is translated as proper_n([john|Ws],Ws). Query s([john, ate, the cake],[]). Yes s([ate, john, cake, the],[]). No
20
Parsing How to represent a tree in Prolog? s npvp proper_nvnp detn [the][cake] [ate][john] Consider the goal s([john,ate,the,cake],[]) It will have the following parse/proof tree
21
A DCG Parser We can include extra arguments on non- terminals in our grammar, these allow us to record the parse tree: s(s(NP,VP)) --> np(NP), vp(VP). vp(vp(verb(V), NP)) --> [V], {verb(V)}, np(NP). np(np(proper_n(N))) --> [N], {proper_n(N)}. np(np(det(Det), noun(N))) --> [Det], {det(Det)}, [N], noun(N).
22
A DCG Parser (ctd.) Arguments to DCG non-terminals are expanded by Prolog like this: s(s(NP,VP),Ws1,Ws2) :- np(NP,Ws1,Ws),vp(VP,Ws,Ws2). Here, arguments are used to build up a parse tree: ?- s(Parse, [john,ate,the,cake], []). Parse = s(np(proper_n(john)), vp(verb(ate), np(det(the), noun(cake))))
23
Eliminating Left Recursion Example with left recursion: S S, PP S NP, VP Will get into infinite loop! Example with left recursion eliminated: S S1, S-PP S-PP S-PP PP, S-PP S1 NP, VP (The difference is even clearer with 2 PPs – try drawing the 2 trees for NP VP PP PP) Here's how to get the same parse tree as in the 1st grammar, but using the 2nd grammar: s(S) --> s1(S1), s_pp(S1,S). s_pp(S,S) --> []. s_pp(S0,S) --> pp(PP), s_pp(s(S0,PP),S). s1(s(NP,VP)) --> np(NP), vp(VP). S S1 NP VP S-PP PP S-PP S S NP VP PP
24
Problems of the simple parser
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.