% Read in a sentence

read_in([W|Ws]) :-
	getc(C),
	readword(C, W, C1),
	restsent(W, C1, Ws).

% Given a word and the character after it, read in the rest
% of the sentence

restsent(W, _, []) :- lastword(W), !.
restsent(W, C, [W1|Ws]) :-
	readword(C, W1, C1),
	restsent(W1, C1, Ws).

% Read a single word, given an initial character, and remember
% what character comes after the word

readword(C, W, C1) :-
	single_character(C), !,
	name(W, [C]),
	getc(C1).
readword(C, W, C2) :-
	in_word(C, NewC), !,
	getc(C1),
	restword(C1, Cs, C2),
	name(W, [NewC|Cs]).
readword(C, W, C2) :-
	getc(C1),
	readword(C1, W, C2).

restword(C, [NewC|Cs], C2) :-
	in_word(C, NewC), !,
	getc(C1),
	restword(C1, Cs, C2).
restword(C, [], C).

% These characters form words on their own

single_character(',').
single_character(';').
single_character(':').
single_character('?').
single_character('!').
single_character('.').

% These characters can appear within a word
% The second clause converts upper case to lower case

in_word(C, C) :-
	C >= a,
	C <= z.
in_word(C, L) :-
	C >= 'A',
	C <= 'Z',
	ascii(C, I1),
	I2 is I1 + 32,
	ascii(L, I2).
in_word(C, C) :-
	C >= '1',
	C <= '9'.
in_word('\'', '\'').

% These words terminate a sentence

lastword('.').
lastword('!').
lastword('?').
