% GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh % SYNOPSIS: English sentence parser. % Usage: see end of this file. % This is the i/o part -- rwl(S) reads a sentence as a list S. rwl(Ws):-get0(C),rwl(C,Ws). rwl(C,[W|Ws]):-wc(C),rw(C,W,C1),rwl(C1,Ws). rwl(C,Ws):-fc(C),get0(C1),rwl(C1,Ws). rwl(C,[]):-eow(C). rw(C,W,C1):-wcs(C,Cs,C1),name(W,Cs). wcs(C,[C|Cs],C0):-wc(C),!,get0(C1),wcs(C1,Cs,C0). wcs(C,[],C):-not(wc(C)). wc(C):-97 =< C, C =< 122. wc(C):-65 =< C, C =< 90. wc(95). fc(32). eow(46). % The grammar. sentence --> sentence(X). sentence(X) --> noun_phrase(X), verb_phrase(X). noun_phrase(X) --> determiner(X), noun(X). verb_phrase(X) --> verb(X), noun_phrase(X). determiner(_) --> [the]. determiner(_) --> [a]. noun(singular) --> [man]. noun(singular) --> [apple]. noun(plural) --> [men]. noun(plural) --> [apples]. verb(singular) --> [eats]. verb(singular) --> [sings]. verb(singular) --> [sees]. verb(plural) --> [eat]. verb(plural) --> [sing]. verb(plural) --> [see]. go :- write('please type a sentence: '), rwl(S), write(S),nl, test(S). test(S):- sentence(S,[]), !, write('Yes, it is a sentence.'), nl. test(S):- write('No, it is not a sentence.'), nl.