2016-12-05 6 views
0

私はプロローグを使って自然言語ファイルを単語のリストに読みたいと思っています。ここでは、ファイルのサンプルです:Prolog:自然言語ファイルを単語のリストに読み込みますか?

The Architecture major occupies the office with the Ctrl+Alt+Del comic poster. 
The CSE major belongs to the RPI Flying Club. 

私は空白、句読点と大文字を処理する、すでにいくつかのコードを持って、私は、ファイルを開いて、そのコードに、このデータを供給するかどうかはわかりません。

%%% Examples: 
%%%   % read_line(L). 
%%%   The sky was blue, after the rain. 
%%%   L = [the,sky,was,blue,',',after,the,rain,'.'] 
%%%   % read_line(L). 
%%%   Which way to the beach? 
%%%   L = [which,way,to,the, beach,'?'] 
%%% 

read_line(Words) :- get0(C), 
        read_rest(C,Words). 

/* A period or question mark ends the input. */ 
read_rest(46,['.']) :- !. 
read_rest(63,['?']) :- !. 

/* Spaces and newlines between words are ignored. */ 
read_rest(C,Words) :- (C=32 ; C=10) , !, 
        get0(C1), 
        read_rest(C1,Words). 

/* Commas between words are absorbed. */ 
read_rest(44,[','|Words]) :- !, 
          get0(C1), 
          read_rest(C1,Words). 

/* Otherwise get all of the next word. */ 
read_rest(C,[Word|Words]) :- lower_case(C,LC), 
          read_word(LC,Chars,Next), 
          name(Word,Chars), 
          read_rest(Next,Words). 

/* Space, comma, newline, period or question mark separate words. */ 
read_word(C,[],C) :- (C=32 ; C=44 ; C=10 ; 
         C=46 ; C=63) , !. 

/* Otherwise, get characters, convert alpha to lower case. */ 
read_word(C,[LC|Chars],Last) :- lower_case(C,LC), 
           get0(Next), 
           read_word(Next,Chars,Last). 

/* Convert to lower case if necessary. */ 
lower_case(C,C) :- (C < 65 ; C > 90) , !. 
lower_case(C,LC) :- LC is C + 32. 


/* for reference ... 
newline(10). 
comma(44). 
space(32). 
period(46). 
question_mark(63). 
*/ 
+0

進捗状況が発生しています。 get_byte(Stream、C)の使用 私はファイルの最初の文字を取得できました。 – Wenzel745

+0

ドキュメントのPrologファイルI/O述語を検索しましたか? – lurker

+0

より良い方法は 'library(pio)'を使うことです – false

答えて

0

これは私が思いついた解決策ですが、奇妙なバグがあります。 read_fileからmaplist文を削除すると、プログラム全体がハングします。誰でも修正を知っていますか?

/* Opens file and sends to recursive read_line*/ 
read_file(Hints, File) :- 
        open(File, read, Stream, [type(binary)]), 
        read_line(Hints, Stream), 
        close(Stream), 
        writeln("File read complete"), nl, 
        maplist(writeln, Hints), nl. 

/* Reads in a single line, places in master list, continues in file*/ 
read_line([H|T], Stream) :- 
        get_byte(Stream, C), 
        read_rest(C, H, Stream), 

        %Breaks on EOF, otherwise continues 
        (at_end_of_stream(Stream) 
        -> ! 
        ; read_line(T, Stream) 
        ). 
関連する問題