280 likes | 455 Views
Изкуствен интелект Упражнение № 9. спец. Информатика, ФМИ 200 4 /200 5. Формална граматика. букви правила лексикон - списък от думи групирани в категории (части на речта) : отворени класове съществителн o ( noun,pronoun) глагол (verb) прилагателно (adjective) затворени класове
E N D
Изкуствен интелектУпражнение № 9 спец. Информатика, ФМИ 2004/2005
Формална граматика • букви • правила • лексикон - списък от думи групирани в категории (части на речта): • отворени класове • съществителнo (noun,pronoun) • глагол (verb) • прилагателно (adjective) • затворени класове • определителeн член (a definite article, determiner) • предлог (preposition) • съюз (conjunction) • наречие (adverb)
Формална граматика • нетерминални символи • S (sentence) • NP (noun phrase) • VP (verb phrase) • PP (prepositional phrase) • RelClause (relative clause)
Формална граматика S NP VP S S Conjunction S NP Pronoun NP Noun NP Det Noun NP NP PP NP NP RelClause
Формална граматика VP Verb VP VP NP VP VP Adjective VP VP PP VP VP Adverb PP Preposition NP RelClause that VP
Обработка на естествен език • Синтактичен анализ(syntactic analysis, parsing) • Семантичен анализ(semantic analysis)
Основни алгоритмиза синтактичен анализ • Отгоре надолу (Top-down parsing) • Отдолу нагоре (Bottom-upparsing)
Отдолу нагоре (Bottom-up parsing) the cat is dead Det cat is dead (Det->the) Det Noun is dead (Noun->cat) NP is dead (NP -> Det Noun) NP Verb dead (Verb->is) NP Verb Adj (Adj->dead) NP VP (VP-> Verb Adj) S (S-> NP VP)
Отгоре надолу (Top-down parsing) S NP VP (S-> NP VP) Det Noun VP (NP -> Det Noun) the Noun VP (Det->the) the cat VP (Noun->cat) the cat Verb Adj (VP-> Verb Adj) the cat is Adj (Verb->is) the cat is dead (Adj->dead)
S NP VP Det Noun Verb Adj The cat is dead
S NP VP Det Noun Verb Adj The dog is big
Отгоре надолу det([the]). det([a]). noun([cat]). noun([dog]). verb([is]). adj([big]). adj([dead]).
Недетерминиран append append([X|Y],Z,[X|L]):- append(Y,Z,L). append([],X,X):-!.
sentence(S):- append(NP,VP,S), np(NP), vp(VP).
np(NP):- append(Det,Noun,NP), det(Det),noun(Noun), write(det=Det),nl,write(noun=Noun),nl.
vp(VP):- append(Verb,Adj,VP), verb(Verb),adj(Adj), write(verb=Verb),nl,write(adj=Adj),nl.
Пример | ?- sentence([the,cat,is,dead]). det=[the] noun=[cat] verb=[is] adj=[dead] yes
S ProN VP Verb NP PP Prep NP Det Noun PP NP Det Noun Prep Det Noun I saw on the the telescope the man hill with
S ProN VP Verb NP PP PP Prep NP Det Noun NP Prep Det Noun Det Noun I saw on the the telescope the man hill with
sentence --> noun_phrase verb_phrase noun_phrase --> determiner noun rel_clause noun_phrase --> proper_noun verb_phrase --> trans_verb noun_phrase verb_phrase --> intrans_verb rel_clause --> that verb_phrase rel_clause --> or verb_phrase rel_clause -->
determiner --> every determiner --> a noun --> man noun --> woman noun --> student proper_noun --> john proper_noun --> mary trans_verb --> loves trans_verb --> is trans_verb --> lives
Отдолу нагоре % noun(Sentence,Rest,Var,LF) noun([man|Rest],Rest,X,man(X)). noun([woman|Rest],Rest,X,woman(X)). noun([student|Rest],Rest,X,student(X)). % proper_noun(Sentence,Rest,LF) proper_noun([john|Rest],Rest,john). proper_noun([mary|Rest],Rest,mary).
% trans_verb(Sentence,Rest,Var1,Var2,LF) trans_verb([loves|Rest],Rest,X,Y,loves(X,Y)). trans_verb([is|Rest],Rest,X,Y,is_a(X,Y)). % intrans_verb(Sentence,Rest,Var,LF) intrans_verb([lives|Rest],Rest,X,lives(X)).
:-op(800,xfy,#). :-op(700,xfy,&). rel_clause([that|Rest1],Rest,X,N_LF,(N_LF & VP_LF)):- verb_phrase(Rest1,Rest,X,VP_LF). rel_clause([or|Rest1],Rest,X,N_LF,(N_LF # VP_LF)):- verb_phrase(Rest1,Rest,X,VP_LF). determiner([every|Rest],Rest,X,R_LF,V_LF,all(X,(R_LF ->V_LF))). determiner([a|Rest],Rest,X, R_LF,V_LF,exists(X,(R_LF & V_LF))).
sentence(S,Rest,LF) :- noun_phrase(S,NPRest,X,VP_LF,LF), verb_phrase(NPRest,Rest,X,VP_LF). noun_phrase(S,Rest,X,VP_LF,VP_LF) :- proper_noun(S,Rest,X). noun_phrase(S,Rest,X,VP_LF,LF) :- determiner(S,DRest,X,R_LF,VP_LF,LF), noun(DRest,NRest,X,N_LF), rel_clause(NRest,Rest,X,N_LF,R_LF).
verb_phrase(S,Rest,X,LF) :- trans_verb(S,VRest,X,Y,V_LF), noun_phrase(VRest,Rest,Y,V_LF,LF). verb_phrase(S,Rest,X,LF) :- intrans_verb(S,Rest,X,LF). rel_clause(S,S,_,LF,LF).
Пример | ?- sentence([a,man,loves,a,woman],Rest,LF). Rest = [], LF = exists(_A, man(_A)& exists(_B, woman(_B)&loves(_A,_B))) ? yes | ?- sentence([john,loves,mary],Rest,LF). Rest = [], LF = loves(john,mary) ?
Пример |?-sentence([every,man,that,lives,loves,a,woman], Rest,LF). Rest = [], LF = all(_A,(man(_A)&lives(_A)-> exists(_B,woman(_B)&loves(_A,_B))))