> Hi,
>
> I am practicing reading English sentences from a file, using get0/2. My
> codes are as follows:
>
> readInSentences :-
> open('sentences.txt', read, Stream, [encoding(utf8)]),
> readIn(Stream, List),
> close(Stream),
> write(List),nl,
> put_list(List).
>
> readIn(Stream, []) :-
> get0(Stream, -1).
This calls get0/2 -- these days you should probably
be using the ISO predicates get_char/1 or get_code/1 --
which reads a character, and then it tries to unify
that character with -1. If you are not at the end of
the stream, that unification will fail. At this
point, the character you just read is GONE FOREVER.
I can't think of *any* time that it makes sense to
call get0([Stream, ]Code) or get_code([Stream, ]Code)
or get_char([Stream, ]Char) with Code or Char anything
but a variable. There should probably be a style
warning for it. The thing is that when you learn that
the character *wasn't* what you were expecting, you
get *no* information about what it is. The next call
to a character input command will read the *next*
character.
You should certainly use
read_in(Stream, List) :-
get0(Stream, Code),
( Code = -1 ->
List = []
; ....
or possibly
read_in(Stream, List) :-
get0(Stream, Code),
read_in(Code, Stream, List).
(This is a style I like. Think of the predicates without
a Code argument as representing states in a deterministic
finite state automaton and the clauses of the predicate
_with_ a Code argument as representing the transitions.
Why deterministic? Because the get... predicates don't
backtrack.)
> readIn(Stream, [Word|Tail]) :-
> get0(Stream, Word),
You *write* Word, but what you have is a character (code),
not a word.
> readIn(Stream, Tail).
You will find sentence reading commands in Clocksin & Mellish,
The Art of Prolog, The Craft of Prolog, the DEC-10 Prolog
library, and several other places.
>
> Could someone tell me why some of the alphabetic letters are not read? I
> use utf-8 because eventually I want to try my codes on Chinese texts.
This has nothing to do with UTF-8.
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.