Skip to content

Instantly share code, notes, and snippets.

@Joelbyte
Created February 28, 2011 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joelbyte/847541 to your computer and use it in GitHub Desktop.
Save Joelbyte/847541 to your computer and use it in GitHub Desktop.
Bacchus-Bosch - part 1
:- object(game).
:- info([
version is 1.0,
author is 'Victor Lagerkvist',
date is 2011/02/28,
comment is 'The core functionality of Bacchus-Bosch.']).
:- public(init/0).
init :-
write('Welcome to Bacchus-Bosch!'), nl,
current_input(S),
nlp::parse_line(S, As),
write('The input is: '),
meta::map([X] >> (write(X), write(' ')), As), nl,
nlp::tag_atoms(As, Ts),
write('The tagged input is: '),
meta::map([X] >> (write(X), write(' ')), Ts), nl.
:- end_object.
:- object(game_logic).
:- info([
version is 1.0,
author is 'Victor Lagerkvist',
date is 2011/02/28,
comment is 'Contains the vocabulary of Bacchus-Bosch.']).
:- public(word/2).
:- public(command/2).
:- public(variants/2).
word(entity, door).
word(entity, key).
word(entity, banana).
word(direction, north).
word(direction, east).
word(direction, south).
word(direction, west).
word(preposition, above).
word(preposition, across).
word(preposition, against).
word(preposition, along).
word(preposition, around).
word(preposition, at).
word(preposition, before).
word(preposition, behind).
word(preposition, below).
word(preposition, beneath).
word(preposition, beside).
word(preposition, besides).
word(preposition, between).
word(preposition, beyond).
word(preposition, by).
word(preposition, down).
word(preposition, following).
word(preposition, from).
word(preposition, in).
word(preposition, inside).
word(preposition, near).
word(preposition, of).
word(preposition, off).
word(preposition, on).
word(preposition, onto).
word(preposition, opposite).
word(preposition, outside).
word(preposition, over).
word(preposition, past).
word(preposition, through).
word(preposition, to).
word(preposition, toward).
word(preposition, towards).
word(preposition, under).
word(preposition, underneath).
word(preposition, up).
word(preposition, upon).
word(preposition, with).
word(preposition, within).
word(pronoun, anyone).
word(pronoun, anything).
word(pronoun, each).
word(pronoun, either).
word(pronoun, everybody).
word(pronoun, everyone).
word(pronoun, everything).
word(pronoun, he).
word(pronoun, her).
word(pronoun, herself).
word(pronoun, him).
word(pronoun, himself).
word(pronoun, his).
word(pronoun, 'I').
word(pronoun, it).
word(pronoun, its).
word(pronoun, itself).
word(pronoun, little).
word(pronoun, me).
word(pronoun, mine).
word(pronoun, more).
word(pronoun, most).
word(pronoun, much).
word(pronoun, myself).
word(pronoun, neither).
word(pronoun, nobody).
word(pronoun, none).
word(pronoun, one).
word(pronoun, one).
word(pronoun, another).
word(pronoun, other).
word(pronoun, she).
word(pronoun, some).
word(pronoun, somebody).
word(pronoun, someone).
word(pronoun, something).
word(pronoun, that).
word(pronoun, theirs).
word(pronoun, them).
word(pronoun, themselves).
word(pronoun, these).
word(pronoun, they).
word(pronoun, this).
word(pronoun, those).
word(pronoun, us).
word(pronoun, we).
word(pronoun, what).
word(pronoun, whatever).
word(pronoun, which).
word(pronoun, whichever).
word(pronoun, who).
word(pronoun, whoever).
word(pronoun, whom).
word(pronoun, whomever).
word(pronoun, whose).
word(pronoun, you).
word(pronoun, yours).
word(pronoun, yourself).
word(pronoun, yourselves).
word(verb, go).
word(verb, take).
word(verb, open).
word(verb, eat).
word(werb, use).
word(article, a).
word(article, an).
word(article, the).
word(conjunction, and).
command(take, [entity]).
command(go, [direction]).
command(go, [entity]).
command(open, [entity]).
command(open, [entity, entity]).
command(eat, [entity]).
variants(take, [take, grab, snap]).
variants(go, [go, goto, walk, run]).
:- end_object.
:- initialization((
logtalk_load(library(metapredicates_loader)),
logtalk_load(library(types_loader)),
logtalk_load(game_logic),
logtalk_load(nlp),
logtalk_load(game),
game::init)).
:- object(nlp).
:- info([
version is 1.0,
author is 'Victor Lagerkvist',
date is 2011/02/28,
comment is 'The natural language processing primitives of Bacchus-Bosch.']).
:- public(parse_line/2).
:- mode(parse_line(+Stream, Atoms), zero_or_more).
:- info(parse_line/2, [
comment is 'Reads a single line from the stream and tokenizes the characters into atoms. tokenize_input would probably be a better name!',
argnames is ['Stream', 'Atoms']]).
:- public(tag_atoms/2).
:- mode(tag_atoms(+atoms, -taggedatoms), one_or_more).
:- info(tag_atoms/2, [
comment is 'True if AtomTags is a list of pairs of the form Atom-Tag, where Tag is the tag corresponding to Atom.',
argnames is ['Atoms', 'TaggedAtoms']]).
tag_atoms(As, Ts) :-
tag_atoms(As, [], Ts).
tag_atoms([], _, []).
tag_atoms([A|As], Pre, [A-T|Ts]) :-
tag(Pre, A, T),
tag_atoms(As, [A-T|Pre],Ts).
tag_atoms([A|As], Pre, [A-unknown|Ts]) :-
%We don't use a cut since we want the ability to try several
%different tags if necessary.
\+ tag(Pre, A, _),
tag_atoms(As, [A-unknown|Pre], Ts).
%% NAME:
%% tag(+Preceding, -Word, -Tag).
%% DESCRIPTION:
%% True if Tag is the tag corresponding to Word. Preceding is used
%% whenever a rule needs to look at the history in order to tag the
%% word.
tag([_-article|_], _, entity).
tag(_, A, T) :-
game_logic::word(T, A).
parse_line(S, As) :-
read_line(S, Cs),
phrase(chars_to_list(As), Cs).
read_line(S, Chars) :-
get_char(S, C),
read_line(C, S, Chars).
read_line('\n', _, []) :- !.
read_line(C, S, [C|Chars]) :- read_line(S, Chars).
chars_to_list([A|As]) -->
chars_to_atom(A),
whitespace,
chars_to_list(As).
chars_to_list([A]) -->
chars_to_atom(A),
opt_whitespace.
blank --> [' '].
opt_whitespace --> [].
opt_whitespace --> whitespace.
whitespace --> blank, whitespace.
whitespace --> blank.
chars_to_atom(A) -->
chars(Cs),
{atom_chars(A, Cs)}.
chars([C|Cs]) -->
char(C),
chars(Cs).
chars([C]) --> char(C).
%This might be a bit too permissive, but who cares!
char(C) --> [C], {C \= ' ', C \= '\n'}.
:- end_object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment