Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
AI - Weeks 19 Natural Language Processing PART 2 Lee McCluskey, room 2/07 Email [email protected] http://scom.hud.ac.uk/scomtlm/cha2555/ Natural Language Processing NLP: the process Text (sentence, email, news story..) -- Parsing -- Referencing UNDERSTANDING PROCESS (“Natural Language Understanding”) -- Meaning Extraction and Integration Knowledge Base: representation of meaning Summary/ Classification Translation Natural Language Generation Natural Language Processing NLP: the process Text “The cat sits on the mat” sentence UNDERSTANDING PROCESS (“Natural Language Understanding”) np The cat Knowledge Base: representation of meaning Fact(type: statement, agent: Fido, action: is_a, object: cat) Fact(type: statement, agent: Freda, action: loves, object: Fido) Natural Language Processing vp sits on the mat Fact(type: statement, agent: cat-002, action: sits_on, object: mat-001) NLP (NLU) : typical process 1. 2. 3. 4. 5. 6. Scanning Parsing Finding referents for pronouns etc Resolving Ambiguities Meaning Extraction Meaning Integration Natural Language Processing NLP (NLU) : tools to help • Parser- Generators (e.g. YACC, JavaCUP) are tools which input BNF Grammars and output Parsers for computer languages such as programming languages. • Extra information can be added to the Grammar to make the Parser output useful information. • PGs can be used for Natural Language Processing also. Grammar Sentence / program Parser Generator Parser Parse Tree or Syntax Error Report Natural Language Processing NLP (NLU) : Definite Clause Grammars Prolog has a built in Parser Generator tool called the “Definite clause grammar” See http://en.wikipedia.org/wiki/Definite_clause_grammar http://www.cs.sunysb.edu/~warren/xsbbook/node10.html This interprets grammar rules as “recursive descent” parsers (hence generating a parser immediately from the grammar). DCG - very simple example – here is a BNF grammar: <sentence> --> <noun> <verb> <noun> <noun> --> i | sentences <verb> --> parse Natural Language Processing NLP (NLU) : Definite Clause Grammars Grammar with this Prolog-looking syntax sentence --> noun, verb, noun. noun --> [i]. noun --> [sentences]. verb --> [parse]. Input this to the Prolog Interpreter gives a listing of: sentence(A, B) :noun(A, C), verb(C, D), noun(A, B) :A = [ i | B]. noun(A, B) :A = [sentences | B]. verb(A, B) :A = [ parse | B ]. noun(D, B). Try it out with sentence([i,parse,sentences],A). Natural Language Processing Larger Example – BNF grammar for a bit of English sentence --> noun_phrase --> noun_phrase --> noun_phrase --> noun_phrase --> verb_phrase --> verb_phrase --> determiner --> adjective --> noun --> noun --> verb --> preposition --> noun_phrase verb_phrase determiner adjective noun adjective noun determiner noun noun verb noun_phrase verb preposition noun_phrase a | an fruit flies | fruit | time | arrow banana like | flies like Natural Language Processing Example in Prolog DCG syntax sentence --> noun_phrase --> noun_phrase --> noun_phrase --> verb_phrase --> verb_phrase --> determiner --> adjective --> noun --> noun --> noun --> noun --> noun --> verb --> verb --> preposition --> noun_phrase, verb_phrase. determiner, adjective, noun. adjective, noun. determiner, noun. verb, noun_phrase. verb, preposition, noun_phrase. [a]. [fruit]. [flies]. This Grammar can [banana]. [fruit]. now be loaded into [time]. Prolog and ACTS [arrow]. LIKE A PARSER. [like]. [flies]. [like]. Natural Language Processing Prolog Grammar Rules sentence(A, B) :noun_phrase(A, C), verb_phrase(C, B). noun_phrase(A, B) :determiner(A, C), adjective(C, D), noun(D, B). noun_phrase(A, B) :adjective(A, C), noun(C, B). noun_phrase(A, B) :determiner(A, C), noun(C, B). verb_phrase(A, B) :verb(A, C), noun_phrase(C, B). verb_phrase(A, B) :verb(A, C), preposition(C, D), noun_phrase(D, B). determiner(A,B) :- A = [a|B]. adjective(A,B) :- A = [fruit| B]. noun(A,B) :- A = [flies|B]. noun(A,B) :- A = [banana|B]. This is the Prolog noun(A,B) :- A= [fruit|B]. Code that the noun(A,B) :- A = [time|B]. Grammar Translates noun(A,B) :- A = [arrow|B]. verb(A,B) :- A = [like|B]. to. The variables verb(A,B) :- A = [flies|B]. implement a kind of preposition(A,B) :- A = [like|B]. “stream processing” Natural Language Processing Getting More From the Parsing Stage Other functions we might need – extract information about the parse, and ensure constraints are maintained in various parts of the sentence, eg - Return parse tree - Check consistency of gender - Checking consistency of number sentence(sentence( NP, VP)) --> noun_phrase(NP, No, Gender), verb_phrase(VP, No, Gender). -- build up parse tree as “self describing” term -- when a part of the sentence commits to number or gender, then record it for consistency Natural Language Processing NLP (NLU) : Definite Clause Grammars Grammar with this Prolog-looking syntax sentence(s(subject(S),V,object(O))) --> noun(S), verb(V), noun(O). noun(noun(i)) --> [i]. noun(noun(sentence)) --> [sentences]. verb(verb(parse)) --> [parse]. Input this to the Prolog Interpreter gives a listing of: sentence(s(subject(S), V, object(O)), A, B) :noun(S, A, C), verb(V, C, D), noun(O, D, B). noun(noun(i), A, B) :A = [ i | B]. noun(noun(sentence), A, B) :A = [sentences | B]. verb(verb(parse), A, B) :A = [ parse | B ]. Try it out sentence(Tree, [i,parser,sentences],X). X = [], Tree = s(subject(noun(i)),verb(parser),object(noun(sentence))) ? yes Natural Language Processing Parsing + Returning a Parse Tree /* syntax bit + No (-singular or plural) + Gender (-Masc, Fem or Inamin.)*/ sentence(sentence( NP, VP)) --> noun_phrase(NP, No, Gender), verb_phrase(VP, No, Gender). noun_phrase(noun_phrase([D,N]), No,Gender) --> d(D, No,Gender), n(N, No,Gender). verb_phrase(verb_phrase([V,N]), No,Gender) --> v(V, No,Gender), noun_phrase(N, _,_). /* no ref to no or gen of object*/ verb_phrase(verb_phrase([V]), No,Gender) --> v(V, No,Gender). d(determiner(the), No,Gender) --> [the]. d(determiner(a), singular,Gender) --> [a]. d(determiner(some), plural,Gender) --> [some]. sentence(X,[the,man,eats,the,apples],Y). d(determiner(all), plural,Gender) --> [all]. n(noun(apple), singular, inanimate) --> [apple]. X = sentence( n(noun(apples), plural, inanimate) --> [apples]. noun_phrase([determiner(the),noun(man)]), n(noun(man), singular, animate) --> [man]. verb_phrase([verb(eat), n(noun(men), plural, animate) --> [men]. noun_phrase([determiner(the),noun(apples)])])), v(verb(eat), singular, animate) --> [eats]. Y = [] ? v(verb(eats), plural, animate) --> [eat]. v(verb(sing), singular, animate) --> [sings]. Natural Language Processing v(verb(sings), plural, animate) --> [sing].