Skip to content

Commit

Permalink
fixed finding parts of speech bug
Browse files Browse the repository at this point in the history
  • Loading branch information
museystem31 committed Nov 16, 2015
2 parents 076c907 + 779e21a commit c825b8a
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 25 deletions.
45 changes: 38 additions & 7 deletions 312-pess-grammar.pl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
:- 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.
% (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 Down Expand Up @@ -101,14 +101,14 @@


% (helper) get every possible part of speech for a word as a list from wordnet
getpartofspeech(X, Y):-
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),!.
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)).
Expand Down Expand Up @@ -143,7 +143,6 @@
assert(H),
assert_all(T).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% End of Main- Q6 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -152,7 +151,7 @@

% Read a sentence (rule).
read_sentence(_) :- peek_char(Ch), Ch = 'end_of_file', !, fail.
read_sentence(S) :- read_sent_helper(S),expand_vocab(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 Expand Up @@ -920,6 +919,20 @@
n(throat).
n(insects).

%%%%%%%%%%%%%%%%%%%%%%%%
%%% Main q1 - Start %%%%
%%%%%%%%%%%%%%%%%%%%%%%%
n(type).
n(paraguay).
n(coastline).
n(bolivia).
n(brazil).
n(suriname).
n(canada).
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Main q1 - End %%%%
%%%%%%%%%%%%%%%%%%%%%%%%

% Adverbs.
:- dynamic(adv/1). % Ensure that the predicate can be modified dynamically

Expand Down Expand Up @@ -968,6 +981,24 @@
adj(rusty).
adj(square).

%%%%%%%%%%%%%%%%%%%%%%%%
%%% Main q1 - Start %%%%
%%%%%%%%%%%%%%%%%%%%%%%%
adj(enclosed).
adj(landlocked).
adj('not-landlocked').
adj('bordered-by-peru').
adj('south-american').
adj(big).
adj(small).
adj(independent).
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Main q1 - End %%%%
%%%%%%%%%%%%%%%%%%%%%%%%




% Doing verbs (i.e., not is/are or has/have/contains/contain).
:- dynamic(v/1). % Ensure that the predicate can be modified dynamically

Expand All @@ -978,4 +1009,4 @@
v(scavenges).
v(quacks).
v(summers).
v(winters).
v(winters).
58 changes: 40 additions & 18 deletions 312-pess.pl
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@
% Also establishes the default top goal (to find out what "it" is).
clear_db :-
abolish(rule,2),
dynamic(rule/2),
dynamic(rule/2).
%% For now, top_goal is set manually.
assertz(rule(top_goal(X), [attr(is_a, X, [])])).
%% assertz(rule(top_goal(X), [attr(is_a, X, [])])).

% Gloss a rule for debugging output.
bug(X) :- write('Understood: '),
Expand All @@ -419,8 +419,7 @@
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(help).

do(load):-
write('Enter file name in single quotes, followed by a period'), nl,
Expand All @@ -431,30 +430,53 @@
do(solve):- solve,!.

do(help):-
write('Type help. load. solve. or quit.'), nl,
write('Enter the command: help. load. solve. goal. new_rule. list. 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.
%%%%%%%%%% Q4 %%%%%%%%%%%%%%%%
do(goal) :-
write('Enter the new goal, followed by a period: '),
read_sentence(Y),
process(['goal:'|Y]),!.

%%%%%%%%%% Q5 %%%%%%%%%%%%%%%%
do(new_rule) :-
write('Enter a new rule, followed by a period: '),
read_sentence(Y),
process(['rule:'|Y]),!.

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

% Lists all the rules that are loaded in the form rule(X,Y) and use the
% predicate bug to print them out in natural language.
list :- current_predicate(rule/2), !, findall(rule(X,Y), rule(X,Y), Z), ls(Z), !.
list :- write('There are no rules loaded').
% Lists all the rules that are loaded in the form rule(X,Y) and prints it out
% in natural language. If there are no rules, print out "There are no rules
% loaded". If there is, then find and print them all
%
% It checks to see if rule/2 is defined and if there are any rules loaded.
% Trying to query rule(_,_) when the predicate is not defined will result in an
% error.
do(list) :- current_predicate(rule/2), rule(_,_), !, findall(rule(X,Y),
rule(X,Y), Z), ls(Z), !.
do(list) :- write('There are no rules loaded'), nl, !.

% Recursively calls bug on each element of the list
ls([]).
ls([Z|Zs]) :- bug([Z]), ls(Zs).
ls([Z|Zs]) :- print_rule([Z]), ls(Zs).

print_rule(X) :- write('Rule: '),
plain_gloss(X, Text),
write_sentence(Text), nl.

%%%%%%%%%%%%%%% END BONUS 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 changes: 36 additions & 0 deletions countries.kb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
rule:
if it is enclosed
then its type is landlocked.

rule:
if it has a coastline
then its type is not-landlocked.

rule:
if its type is landlocked and
it is south-american and
it is bordered-by-peru
then it is bolivia.

rule:
if its type is landlocked and
it is south-american
then it is paraguay.

rule:
if its type is not-landlocked and
it is south-american and
it is big
then it is brazil.

rule:
if its type is not-landlocked and
it is south-american and
it is independent and
it is small
then it is suriname.

rule:
if its type is not-landlocked and
it is big
then it is canada.
22 changes: 22 additions & 0 deletions q1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The domain choosen is a tiny subset of the countries of the World. The database can identify four countries, Bolivia, Paraguay, Brazil, Suriname and Canada by some very simple features.

The only problem encounterd while creating the database was properly formating the rules and identifying the word types of each word. This was a small hump to get over for someone who is not a native speaker but after that, creating the database was straight forward.

The word type definitions that we needed to add to 312-pess-grammer.pl were:

n(type).
n(paraguay).
n(coastline).
n(bolivia).
n(brazil).
n(suriname).
n(canada).

adj(enclosed).
adj(landlocked).
adj('not-landlocked').
adj('bordered-by-peru').
adj('south-american').
adj(big).
adj(small).
adj(independent).
23 changes: 23 additions & 0 deletions q4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
To allow the user to set the top level goal from the interpreter loop, assertz(rule(top_goal(X), [attr(is_a, X, [])])). was removed from the clear_db predicate and a new predicate was created:

goal :-
write('Enter the new goal, followed by a period: '),
read_sentence(Y),
process(['goal:'|Y]).

Example:
?- goal.
Enter the new goal, followed by a period: what the heck is THAT.
Understood: rule(top_goal(_G312),[attr(is_a,_G312,[])])
true.

?- solve.
Would you say that it has external tubular nostrils ?> no.
Would you say that it has webbed feet ?> |: no.
Would you say that it eats meat ?> |: no.
Would you say that it has one long backward toe ?> |: yes.
Would you say that it has flat bill ?> |: yes.
Would you say that it eats flying insects ?> |: yes.
Would you say that it has long rusty tail ?> |: yes.
The answer is great crested flycatcher
true .
27 changes: 27 additions & 0 deletions q5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
To allow the user to assert new facts and rules we simply create a new predicate:

new_rule :-
write('Enter a new rule, followed by a period: '),
read_sentence(Y),
process(['rule:'|Y]).

Example:

?- new_rule.
Enter a new rule, followed by a period: if its neck is medium then it is goose.
Understood: if it has medium neck then it is goose
true.

?- goal.
Enter the new goal, followed by a period: what is it.
Understood: rule(top_goal(_G288),[attr(is_a,_G288,[])])
true.

?- solve.
Would you say that it has external tubular nostrils ?> no.
Would you say that it has webbed feet ?> |: no.
Would you say that it eats meat ?> |: no.
Would you say that it has one long backward toe ?> |: no.
Would you say that it has medium neck ?> |: yes.
The answer is goose
true .

0 comments on commit c825b8a

Please sign in to comment.