本文介绍了在 prolog 中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在Prolog中逐行读取文件
我发现以下序言代码一次读取一个字符并打印出来.
I found the following prolog code which reads one character at a time and prints out.
process(File) :-
open('C:/Users/BHARAT/Desktop/a.txt', read, In),
get_char(In, Char1),
process_stream(Char1, In),
close(In).
process_stream(end_of_file, _) :- !.
process_stream(Char, In) :-
print(Char),
get_char(In, Char2),
process_stream(Char2, In).
但是如果文件有多行,有没有办法一次读取一整行,以便标记化.
But if the file has multiple lines is there a way to read 1 whole line at a time so that it will be easy for tokenizing.
推荐答案
你说你想对输入进行标记 - 最好的方法是明确的子句语法 (DCG).使用 SWI 中的 library(pio)
您可以直接使用语法读取文件,如下所示:
You say you want to tokenize the input - the best way to do this are definite clause grammars (DCGs). With library(pio)
in SWI you can use the grammar directly to read in a file like so:
?- use_module(library(pio)).
?- phrase_from_file(seq(Xs),f).
seq([]) --> [].
seq([E|Es]) --> [E], seq(Es).
现在用更精细的分词器替换 seq//1
.
Replace now seq//1
by some more elaborate tokenizer.
这篇关于在 prolog 中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!