2016-12-05 2 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

여기에 나와있는 해결책이 있지만 이상한 버그가 있습니다. maplist 문을 read_file에서 제거하면 전체 프로그램이 중단됩니다. 누구든지 수정 사항을 알고 있습니까?

/* 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) 
        ). 
관련 문제