PROLOG
Spanish Language Parser
by Gabriella Barrantes
% University of New Mexico % CS 528 Advanced Topics in Artificial Intelligence % Programming Assignment % Natural Language Processing Fundamentals % Author: Elena Gabriela Barrantes-Sliesarieva % Date:10/28/02 % Design: Very basic Spanish Grammar % Considered: Sentences that: % - Start with an implicit or explicit subject. % - If subject is explicit it may have a modifying adjective % - Continue with a verb in present time % - The verb is an action executed by the subject in the first part % - The sentence may finish with the verb or continue % - If the sentence continues, it must have a preposition. In % Spanish it is not gramatical to say "the man likes the dog", % instead it must have a preposition indicating the presentation % of the object "el hombre quiere (a) el perro" (actually, "a el" is % usually comprised to "al".) % - After the preposition, there must be another noun phrase, either % indicating a subject ("el perro"), a place or a time, depending % on the verb and the preposition ("the woman swims in the lake", % "fido eats in the afternoon") == ("la mujer nada en el lago", % "fido come en la tarde"). Again this part can contain modifying % adjectives ("the beautiful woman swims in the dark lake" == % "la hermosa mujer nada en el lago oscuro"). % First Part % Grammar checker, based on Section 14.9.2 % Grammar rules checked: % -Number concordance between article, noun, adjective and verb % -Gender concordance between article, noun and adjective % Additional actions: % The original sentence is modified so a normalized sentence % (all in singular, without articles) is passed to the % The result (T) for the second (semantic) pass is % stripped of articles, and contains only generic (normalized) % nouns, verbs and adjectives. Prepositions are conserved % as they are. The adjectives are moved to the right of the % noun if they were not originally situated so concordant(X,T):-csentence(X,[],T). % Basic sentence: a noun preamble describing the subject % and an action (that might involve an object) % The preposition is considered in the continuation to % verbphrase csentence(S,E,T):- nounp(S,Rest,N,NO),!, verbp(Rest,End,N,V),!, append(NO,V,T). % Noun phrase % =========== % Cases 3,4 % --------- % article-noun-adjective or article-adjective-noun (generic/plural nouns only) nounp([Article,Noun,Adjective|E],E,N,[NNoun,NAdjective]):- article(Article,Basea),baseC(Basea,N,G), cnoun(Noun,Basec,NNoun),baseC(Basec,N,G,T),T=generic, cadjec(Adjective,Basej,NAdjective),baseC(Basej,N,G). nounp([Article,Adjective,Noun|E],E,N,[NNoun,NAdjective]):- article(Article,Basea),baseC(Basea,N,G), cadjec(Adjective,Basej,NAdjective),baseC(Basej,N,G), cnoun(Noun,Basec,NNoun),baseC(Basec,N,G,T),T=generic. % Case 1 % ------ % Noun only (proper names only) % proper names (Ana, Costa Rica, Fido) must be used without articles. % All other nouns must always be preceded by an article. nounp([Noun|E],E,N,[NNoun]):- cnoun(Noun,Base,NNoun),baseC(Base,N,G,T),T=proper. % Case 2 % ------ % article-noun (generic/plural nouns only) nounp([Article,Noun|E],E,N,[NNoun]):- article(Article,Basea),baseC(Basea,N,G), cnoun(Noun,Basec,NNoun),baseC(Basec,N,G,T),T=generic. % Rules for verbs % =============== % Case 2 % ------ % A verb and a preposition verbp([Verb|R],E,N,T):- cverb(Verb,N,V), prepp(R,E,P), append([V],P,T). prepp([],X,[]). prepp([Prep|Rest],E,T):- cprep(Prep), nounp(Rest,Next,_,P), append([Prep],P,S), prepp(Next,End,Q), append(S,Q,T). % Case 1 % ------ % A singleton verb and the sentence ends verbp([Verb|E],E,N,[V]):- cverb(Verb,N,V),E=[]. %============================================================================= % Actual instantiation of articles, adjectives, nouns, verbs and prepositions %============================================================================= % Generic concordants baseC(baseSF,singular,femenine). baseC(baseSM,singular,masculine). baseC(basePF,plural,femenine). baseC(basePM,plural,masculine). baseC(baseSFG,singular,femenine,generic). baseC(baseSFP,singular,femenine,proper). baseC(baseSMG,singular,masculine,generic). baseC(baseSMP,singular,masculine,proper). baseC(basePF,plural,femenine,generic). % proper plurals can only baseC(basePM,plural,masculine,generic). % be formed by composites % Articles article(la,baseSF). article(el,baseSM). article(un,baseSF). article(una,baseSF). article(las,basePF). article(los,basePM). article(unos,basePF). article(unas,basePM). % Nouns cnoun(mujer,baseSFG,mujer). cnoun(mujeres,basePF,mujer). cnoun(hombre,baseSMG,hombre). cnoun(hombres,basePM,hombre). cnoun(ana,baseSFP,ana). cnoun(fido,baseSMP,fido). cnoun(perro,baseSMG,perro). cnoun(perros,basePMG,perro). cnoun(lago,baseSMG,lago). % lake cnoun(lagos,basePM,lago). % lake % Adjectives cadjec(alto,baseSM,alto). % tall cadjec(alta,baseSF,alto). cadjec(altos,basePM,alto). cadjec(altos,basePF,alto). cadjec(hondo,baseSM,hondo).% deep cadjec(honda,baseSF,hondo). cadjec(hondos,basePM,hondo). cadjec(hondas,basePF,hondo). cadjec(oscuro,baseSM,oscuro).% dark cadjec(oscura,baseSF,oscuro). cadjec(oscuras,basePM,oscuro). cadjec(oscuras,basePF,oscuro). cadjec(furioso,baseSM,furioso).% furious cadjec(furiosa,baseSF,furioso). cadjec(furiosos,basePM,furioso). cadjec(furiosas,basePF,furioso). cadjec(inteligente,baseSF,inteligente). cadjec(inteligente,baseSM,inteligente). cadjec(inteligentes,basePM,inteligente). cadjec(inteligentes,basePF,inteligente). cadjec(peligroso,baseSM,peligroso). cadjec(peligrosa,baseSF,peligroso). cadjec(peligrosos,basePM,peligroso). cadjec(peligrosas,basePF,peligroso). cadjec(feliz,baseSM,feliz). cadjec(feliz,baseSM,feliz). cadjec(felices,basePM,feliz). cadjec(felices,basePF,feliz). % Some verbs cverb(nada,singular,nadar). cverb(nadan,plural,nadar). cverb(arresta,singular,arrestar). cverb(arrestan,plural,arrestar). cverb(cura,singular,curar). cverb(curan,plural,curar). % Some prepositions cprep(a). % Subject specifier cprep(con). % with cprep(en).% in (time/place) %========================================================================= %========================================================================= % Part II. Some semantics between the defined verbs, nouns, adjectives and % prepositions. Based on Sections 14.9.3, 14.67.4, and 13.3.2 % Apart from the base verb frame/noun frame operations, % I am keeping a true hierarchy and checking properties and % inheritance using it % Notice that there is no need to check for articles, number and % gender concordance, and placement of adjectives, as it was all % solved in the first pass, and now it is working with a normalized % representation. sentence(Start,End,Sentence_graph):- nounphrase(Start,Rest,Subject_graph), verbphrase(Rest,End,Predicate_graph), join([agent(Subject_graph)],Predicate_graph,Sentence_graph). % Checks that the generic property of the adjective % could be used with the given noun nounphrase([Noun,Adjective|End],End,Noun_phrase_graph):- noun(Noun,Noun_phrase_graph),cadjec(Adjective,_,_), property(P,Adjective), hasProperty(P,Noun). % For the case of noun without adjective nounphrase([Noun|End],End,Noun_phrase_graph):- noun(Noun,Noun_phrase_graph). % For the case of a sentence that ends in a lonely verb verbphrase([Verb|End],End,Verb_phrase_graph):- verb(Verb,Verb_phrase_graph),End=[]. % Verb and preposition processing verbphrase([Verb|Rest],End,Verb_phrase_graph):- verboin_slots(A,B,D). join_slot_to_frame(A,[B|C],[B|D]):- join_slot_to_frame(A,C,D). join_slots(A,B,D):- functor(A,FA,_),functor(B,FB,_), match_with_inheritance(FA,FB,FN), arg(1,A,Value_a),arg(1,B,Value_b), join(Value_a,Value_b,New_value), D=..[FN|[New_value]]. isframe([_|_]). isframe([]). is_slot(A):-functor(A,_,1). % Rules for automatic inheritance match (for a hierarchy % stored in frames) match_with_inheritance(X,X,X). match_with_inheritance(X,Y,X):- frame(name(X),isa(Y),_). match_with_inheritance(X,Y,Y):- frame(name(Y),isa(X),_). match_with_inheritance(X,Y,X):- frame(name(X),isa(Z),_), followChain(Z,Y). match_with_inheritance(X,Y,Y):- frame(name(Y),isa(Z),_), followChain(Z,X). followChain(A,B):- frame(name(A),isa(B),_). followChain(A,B):- frame(name(A),isa(Z),_), followChain(Z,B). % Rules for finding a property with inheritance hasProperty(P,N):- frame(name(N),_,L), member(P,L). hasProperty(P,N):- frame(name(N),isa(Z),_), hasProperty(P,Z). % Semantic net % ============= % Part 1. % ------ % Inheritance hierarchy (without properties for the time being) frame(name(entity),isa(root),[color]). frame(name(animado),isa(entity),[height,danger]). frame(name(animal),isa(animado),[intelligence,emotion]). frame(name(humano),isa(animal),[]). frame(name(perro),isa(animal),[]). frame(name(fido),isa(perro),[]). frame(name(hombre),isa(humano),[]). frame(name(mujer),isa(humano),[]). frame(name(ana),isa(mujer),[]). frame(name(lago),isa(cuerpo_de_agua),[]). frame(name(cuerpo_de_agua),isa(inanimado),[depth]). % Adjectives by categories property(height,alto)