将数据从文本文件分割成事实列表到prolog文件

时间:2016-01-18 09:21:20

标签: file parsing io prolog sicstus-prolog

我想将数据文件分成像functor(arg1, arg2, ..., argN)这样的事实列表,其中functor的名称是大写行,参数是小写行跟随他们, 随后,新条款保存在执行时创建的prolog文件中

file.txt的

FUNCTOR1
arg1 
arg2
FUNCTOR2
arg1
arg2
arg3
FUNCTOR3
arg1
arg2
arg3
arg4

结果:

?- split_data_to_facts('file.txt',List,'file.pl').
List = ['functor1(arg1,arg2)','functor2(arg1,arg2,arg3)','functor3(arg1,arg2,arg3,arg4)'].

file.pl

“”将作为最后一个

附加
functor1(arg1,arg2).        
functor2(arg1,arg2,arg3).
functor3(arg1,arg2,arg3,arg4).

构建并编译 prolog文件file.pl后:

?- functor2(X,Y,Z).
X=arg1,
Y=arg2,
Z=arg3;
yes

1 个答案:

答案 0 :(得分:1)

我们假设内置的read_line_to_codes / 2可用:那么您可以应用一行的预测

process_file(Path) :-
  open(Path, read, In),
  read_line_to_codes(In, Line1),
  read_line_to_codes(In, Line2), % assume not empty
  process_lines(Line2, [Line1], In, Facts),
  maplist(writeln, Facts).  % just for test

process_lines(end_of_file, LastFactDef, In, [LastFact]) :-
  lines_fact(LastFactDef, LastFact),
  close(In).
process_lines([U|Us], LastFactDef, In, [LastFact|Facts]) :-
  upper_lower(U, _),
  lines_fact(LastFactDef, LastFact),
  read_line_to_codes(In, Line),
  process_lines(Line, [[U|Us]], In, Facts).
process_lines(Last, Lines, In, Facts) :-
  read_line_to_codes(In, Line),
  process_lines(Line, [Last|Lines], In, Facts).

lines_fact(Lines, Fact) :-
  reverse(Lines, [FunctorUpper|ArgCodes]),
  maplist(make_lower, FunctorUpper, FunctorCodes),
  maplist(atom_codes, [Functor|Args], [FunctorCodes|ArgCodes]),
  Fact =.. [Functor|Args].

% if uppercase get lowercase
upper_lower(U, L) :-
  between(0'A, 0'Z, U), L is 0'a + U - 0'A.

make_lower(C, L) :- upper_lower(C, L) ; L = C.

在SWI-Prolog中运行测试(我们默认使用read_line_to_codes / 2和/ 3之间):

?- process_file('/home/carlo/test/file.txt').
functor1(arg1 ,arg2)
functor2(arg1,arg2,arg3)
functor3(arg1,arg2,arg3,arg4)
true