Skip to content

Commit

Permalink
added q6 and q2
Browse files Browse the repository at this point in the history
  • Loading branch information
museystem31 committed Nov 16, 2015
1 parent ca23d0c commit b461db4
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
93 changes: 92 additions & 1 deletion 312-pess-grammar.pl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,100 @@
% lists of Prolog atoms. (For those who know the term, this is a
% cheap lexical analyzer.)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Main- Q6: Added expanding vocab funtionality %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load ProNTo_Morph module
:- ensure_loaded('Schlachter/pronto_morph_engine').

% Import the s files.
:- ensure_loaded('wn_s').

% (q6 main function- expand_vocab) if you input a sentence into the program,
(1) it will first produce a list of all the stems for each word in the sentence.
% The second step is that it will go through the list of stems and produce a list of
% stems with all their parts of speech. In the last step, the function asserts
% everything in the list, and return the list as well.
expand_vocab([]).
expand_vocab([H|T]):-
addword(H),
expand_vocab(T).


% (helper) add each word to the dictionary
%,_listofstemswithpartofspeech
addword(_word):-
allstems(_word,_stemlist),
allstemswithpartofspeech(_stemlist,_listofstemswithpartofspeech),
assert_all(_listofstemswithpartofspeech).


% (step1.main) get all the possible stems from a word without extra parts.
allstems(W,L2):-
morph_atoms_bag(W, L),
stem_helper(L,L2).

% (helper) remove extra parts from the the possible stems.
% ex. [[[testing]],[[test, -ing]],[teste, -ing]] --> [testing, test, teste]
stem_helper([],[]).
stem_helper([[[X]|[]]|T], [X|L2]):-
stem_helper(T,L2),!.
stem_helper([[[X,Y]|[]]|T], [X|L2]):-
stem_helper(T,L2),!.


% (helper) get every possible part of speech for a word as a list from wordnet
getpartofspeech(X, Y):-
findall(_partofspeech, s(_,_,X,_partofspeech,_,_), Y).

% (helper) making a list of the word with all its parts of speech in the form of partofspeech(word).
parse_list(W,[],[]).
parse_list(W,[H|T],[A|T2]):-
build_vocab(W, H, A).
parse_list(W,T,T2),!.

% (helper) convert word and its part of speech into the form partofspeech(X).
build_vocab(X, n, n(X)).
build_vocab(X, v, v(X)).
build_vocab(X, adj, adj(X)).
build_vocab(X, s, adj(X)).
build_vocab(X, adv, adv(X)).

% (helper) converts arg X,Y into a functor X(Y)
parse_xy(X,Y,A) :- functor(A,X,1), arg(1,A,Y).

% (main) build a list of same stem with different parts of speech.
build_vocab_list(W, L):-
getpartofspeech(W, Y),
parse_list(W,Y,L).

% (helper) appends lists
append([],L,L).
append([H|T],L2,[H|L3]) :- append(T,L2,L3).

% (step2.main) all the stems with all their parts of speech as a list
% ex. [testing, teste, test] --> [n(testing), n(teste), v(teste), n(test), v(test)]
allstemswithpartofspeech([],[]).
allstemswithpartofspeech([H|T],L2):-
build_vocab_list(H, L),
append(L,Acc,L2),
allstemswithpartofspeech(T,Acc),!.

% (step3. main) asserting all the elements of the list to expand the vocabulary
assert_all([]).
assert_all([H|T]):-
assert(H),
assert_all(T).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% End of Main- Q6 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% added expansion functionality to read_sentence

% Read a sentence (rule).
read_sentence(_) :- peek_char(Ch), Ch = 'end_of_file', !, fail.
read_sentence(S) :- read_sent_helper(S).
read_sentence(S) :- read_sent_helper(S), expand_vocab(S).

% Read a sentence as individual words.
read_sent_helper([]) :- peek_char(Ch), % Stop at end of file.
Expand Down
42 changes: 42 additions & 0 deletions 312-pess.pl
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,44 @@
%% 312pess-grammar.pl (which allows that file to run independently of
%% 312pess.pl).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Main- Q2: main (based on Amzi's Clam shell) %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

main :-
greeting,
repeat,
write('> '),
read(X),
do(X),
X == quit.

greeting :-
write('This is the CPSC312 Prolog Expert System Shell.'), nl,
write('Based on Amzi''s "native Prolog shell".'), nl,
write('Type help. load. solve. or quit.'), nl,
write('at the prompt. Notice the period after each command!'), nl.

do(load):-
write('Enter file name in single quotes, followed by a period'), nl,
write('(e.g ''bird.kb''.):'), nl,
read(F),
load_rules(F),!.

do(solve):- solve,!.

do(help):-
write('Type help. load. solve. or quit.'), nl,
write('at the prompt. Notice the period after each command!'),nl,!.

do(quit).

do(X):-
write(X),
write(' is not a legal command.'), nl,
fail.


%%%%%%%%%%%%%%%%% BONUS Q2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand All @@ -416,3 +454,7 @@
ls([]).
ls([Z|Zs]) :- bug([Z]), ls(Zs).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% End of Main- Q2 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

0 comments on commit b461db4

Please sign in to comment.