Изкуствен интелект Упражнение № 9
DESCRIPTION
Изкуствен интелект Упражнение № 9. спец. Информатика, ФМИ 200 4 /200 5. Формална граматика. букви правила лексикон - списък от думи групирани в категории (части на речта) : отворени класове съществителн o ( noun,pronoun) глагол (verb) прилагателно (adjective) затворени класове - PowerPoint PPT PresentationTRANSCRIPT
Изкуствен интелектИзкуствен интелект
Упражнение № 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 parsingTop-down parsing)Отдолу нагоре (Bottom-upBottom-up parsingparsing)
Отдолу нагоре Отдолу нагоре ((Bottom-up parsing)Bottom-up parsing)
the cat is dead
DetDet cat is dead (Det->the)
Det NounDet Noun is dead (Noun->cat)
NPNP is dead (NP -> Det Noun)
NP VerbNP Verb dead (Verb->is)
NP Verb AdjNP Verb Adj (Adj->dead)
NP VPNP VP (VP-> Verb Adj)
SS (S-> NP VP)
Отгоре надолу Отгоре надолу ((Top-down parsing)Top-down parsing)
SS
NP VPNP VP (S-> NP VP)
Det Noun VPDet Noun VP (NP -> Det Noun)
the Noun VPNoun VP (Det->the)
the cat VP VP (Noun->cat)
the cat Verb Adj Verb Adj (VP-> Verb Adj)
the cat is Adj Adj (Verb->is)
the cat is dead (Adj->dead)
S
NP VP
Det Noun Verb Adj
The deadiscat
S
NP VP
Det Noun Verb Adj
The bigisdog
Отгоре надолуОтгоре надолу
det([the]).det([the]).
det([a]).det([a]).
noun([cat]).noun([cat]).
noun([dog]).noun([dog]).
verb([is]).verb([is]).
adj([big]).adj([big]).
adj([dead]).adj([dead]).
Недетерминиран Недетерминиран appendappend
append([X|Y],Z,[X|L]):-append([X|Y],Z,[X|L]):-
append(Y,Z,L).append(Y,Z,L).
append([],X,X):-!.append([],X,X):-!.
sentence(S):-sentence(S):-
append(NP,VP,S),append(NP,VP,S),
np(NP),np(NP),
vp(VP).vp(VP).
np(NP):-np(NP):-
append(Det,Noun,NP),append(Det,Noun,NP),
det(Det),noun(Noun),det(Det),noun(Noun),
write(det=Det),nl,write(noun=Noun),nl.write(det=Det),nl,write(noun=Noun),nl.
vp(VP):-vp(VP):-
append(Verb,Adj,VP),append(Verb,Adj,VP),
verb(Verb),adj(Adj),verb(Verb),adj(Adj),
write(verb=Verb),nl,write(adj=Adj),nl.write(verb=Verb),nl,write(adj=Adj),nl.
ПримерПример
| ?- sentence([the,cat,is,dead]).| ?- sentence([the,cat,is,dead]).
det=[the]det=[the]
noun=[cat]noun=[cat]
verb=[is]verb=[is]
adj=[dead]adj=[dead]
yesyes
S
ProN VP
VerbNP PP
saw
PPNounDet NP
NounDet
Prep
NP
NounDet
Prep
I theman onthe telescopethewithhill
S
ProN VP
VerbNP PP
saw
PP
NounDet NP
NounDet
PrepNP
NounDet
Prep
I theman onthe telescopethewithhill
sentence --> noun_phrase verb_phrasesentence --> noun_phrase verb_phrase
noun_phrase --> determiner noun rel_clausenoun_phrase --> determiner noun rel_clause
noun_phrase --> proper_nounnoun_phrase --> proper_noun
verb_phrase --> trans_verb noun_phraseverb_phrase --> trans_verb noun_phrase
verb_phrase --> intrans_verbverb_phrase --> intrans_verb
rel_clause --> that verb_phraserel_clause --> that verb_phrase
rel_clause --> or verb_phraserel_clause --> or verb_phrase
rel_clause -->rel_clause -->
determiner --> everydeterminer --> everydeterminer --> adeterminer --> anoun --> mannoun --> mannoun --> womannoun --> womannoun --> studentnoun --> studentproper_noun --> johnproper_noun --> johnproper_noun --> maryproper_noun --> marytrans_verb --> lovestrans_verb --> lovestrans_verb --> istrans_verb --> istrans_verb --> livestrans_verb --> lives
Отдолу нагореОтдолу нагоре
% noun(Sentence,Rest,Var,LF)% noun(Sentence,Rest,Var,LF)
noun([man|Rnoun([man|Restest],],RestRest,X,man(X)).,X,man(X)).
noun([woman|Rnoun([woman|Restest],],RestRest,X,woman(X)).,X,woman(X)).
noun([student|Rnoun([student|Restest],],RestRest,X,student(X)).,X,student(X)).
% proper_noun(Sentence,Rest,LF)% proper_noun(Sentence,Rest,LF)
proper_noun([john|Rproper_noun([john|Restest],],RestRest,john).,john).
proper_noun([mary|Rproper_noun([mary|Restest],],RestRest,mary).,mary).
% % trans_verbtrans_verb(Sentence,Rest,Var1,Var2,LF)(Sentence,Rest,Var1,Var2,LF)
trans_verb([loves|Rtrans_verb([loves|Restest],],RestRest,X,Y,loves(X,Y)).,X,Y,loves(X,Y)).
trans_verb([is|Rtrans_verb([is|Restest],],RestRest,X,Y,is_a(X,Y)).,X,Y,is_a(X,Y)).
% in% intrans_verbtrans_verb(Sentence,Rest,Var,LF)(Sentence,Rest,Var,LF)
intrans_verb([lives|Rintrans_verb([lives|Restest],],RestRest,X,lives(X)).,X,lives(X)).
:-op(800,xfy,#).:-op(800,xfy,#).
:-op(700,xfy,&).:-op(700,xfy,&).
rel_clause([that|Rrel_clause([that|Rest1est1],],RestRest,X,N,X,N_LF_LF,(,(N_LF N_LF && VP_LF VP_LF)):-)):-
verb_phrase(verb_phrase(Rest1Rest1,,RestRest,X,,X,VP_LFVP_LF).).
rel_clause([or|rel_clause([or|Rest1Rest1],],RestRest,X,N,X,N_LF_LF,(,(N_LF N_LF ## VP_LF VP_LF)):-)):-
verb_phrase(Rverb_phrase(Rest1,Restest1,Rest,X,V,X,VP_LFP_LF).).
determiner([every|Rdeterminer([every|Restest],],RestRest,X,,X,RR_LF,V_LF_LF,V_LF,all(X,(R,all(X,(R_LF _LF ->-> VV_LF_LF))).))).
determiner([a|Rdeterminer([a|Restest],],RestRest,X,,X,
RR_LF_LF,,V_LFV_LF,exists(X,(,exists(X,(R_LF R_LF && V_LF V_LF))).))).
sentence(S,sentence(S,RestRest,,LFLF) :-) :-
noun_phrase(noun_phrase(SS,,NPRestNPRest,X,,X,VVPP_LF_LF,,LFLF),),
verb_phrase(verb_phrase(NPRestNPRest,,RestRest,X,,X,VP_LFVP_LF).).
noun_phrase(S,noun_phrase(S,RestRest,X,,X,VP_LFVP_LF,,VP_LFVP_LF) :-) :-
proper_noun(proper_noun(SS,,RestRest,X).,X).
noun_phrase(S,noun_phrase(S,RestRest,X,V,X,VP_LFP_LF,,LFLF) :-) :-
determiner(S,Ddeterminer(S,DRestRest,X,,X,R_LFR_LF,V,VP_LFP_LF,L,LFF),),
noun(noun(DRestDRest,,NRestNRest,X,N,X,N_LF_LF),),
rel_clause(rel_clause(NRestNRest,,RestRest,X,,X,N_LFN_LF,,R_LFR_LF).).
verb_phrase(S,verb_phrase(S,RestRest,X,L,X,LFF) :-) :-
trans_verb(S,trans_verb(S,VRestVRest,X,Y,V,X,Y,V_LF_LF),),
noun_phrase(noun_phrase(VRestVRest,,RestRest,Y,V,Y,V_LF_LF,L,LFF).).
verb_phrase(verb_phrase(SS,,RestRest,X,,X,LFLF) :-) :-
intrans_verb(intrans_verb(SS,,RestRest,X,,X,LFLF).).
rel_clause(S,rel_clause(S,SS,_,,_,LFLF,,LFLF).).
ПримерПример| ?- sentence([a,man,loves,a,woman],Rest,LF).| ?- sentence([a,man,loves,a,woman],Rest,LF).
Rest = [],Rest = [],
LF = exists(_A, man(_A)&LF = exists(_A, man(_A)& exists(_B, exists(_B,
woman(_B)&loves(_A,_B))) ?woman(_B)&loves(_A,_B))) ?
yesyes
| ?- sentence([john,loves,mary],Rest,LF). | ?- sentence([john,loves,mary],Rest,LF).
Rest = [],Rest = [],
LF = loves(john,mary) ?LF = loves(john,mary) ?
ПримерПример|?-sentence([every,man,that,lives,loves,a,woman],|?-sentence([every,man,that,lives,loves,a,woman],
Rest,LF).Rest,LF).
Rest = [],Rest = [],
LF = all(_A,(man(_A)&lives(_A)-> LF = all(_A,(man(_A)&lives(_A)-> exists(_B,woman(_B)&loves(_A,_B)))) exists(_B,woman(_B)&loves(_A,_B))))