我有Weka arff file,其中的属性是Prolog查询。
我想从文件中读取查询,修改它,然后运行查询。
weka arff文件的开头如下:
@relation propositionalization
@attribute 'link(_, A, _), link(A, _, _)' {'+'}
@attribute 'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), link(B, D, activation), reaction(D, 0), reaction(B, 0), link(A, E, _), link(E, F, follows), reaction(F, 1)' {'+','-'}
@attribute 'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), reaction(B, 1), link(A, D, _), link(D, E, follows), reaction(E, 0)' {'+','-'}
等
我有一个dcg:
list([]) -->[].
list([L|Ls]) --> [L],list(Ls).
get_feature(Feature) --> "@attribute '",list(Feature), "'",list(_).
我用以下文件阅读了文件:
read_my_file(File,Lines):-
open(File,read,Stream,[]),
read_lines(Lines,Stream),!.
read_lines([H|T],Stream):-
read_line_to_codes(Stream,H), H\=end_of_file, read_lines(T,Stream).
read_lines([],Stream):-close(Stream).
使用以下内容调用短语/ 2将该功能提取为代码列表。
lineCodes_feature(Line,Feature):-
phrase(get_feature(Feature),Line),!.
然后我打电话给:
read_my_file('my_file.arff',Lines),
member(Line,Lines),
lineCodes_feature(Line,Feature),
atom_codes(Atom,Feature).
并回溯以将每个特征视为原子。
我陷入了修改并将其作为查询运行的下一步。 我想将反应/ 2项更改为反应/ 3,其中附加参数对于每个项都是相同的。
所以例如原子术语
'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), reaction(B, 1), link(A, D, _), link(D, E, follows), reaction(E, 0)'
我会想象成为一个清单:
[reaction(newarg,A, 0), link(A, B, _), link(B, C, _), reaction(newarg,C, 1), reaction(newarg,B, 1), link(A, D, _), link(D, E, follows), reaction(newarg,E, 0)]
哪会允许我以某种方式使用call / 1来查询内部数据库?
对我的方法有任何帮助或评论。
答案 0 :(得分:1)
我认为您可以使用read_term_from_codes,然后调用结果字词。
...
read_term_from_codes(Feature, Callable, []),
transform(Callable, Transformed),
Transformed,
...
一个例子,使用library(dcg/basics)和phrase_from_file / 2来简化解析
:- use_module(library(dcg/basics)).
sw :- phrase_from_file(arff_file, '/home/carlo/test/x.weka').
arff_file --> [] ; arff_line, arff_file.
arff_line -->
"@relation propositionalization", eol.
arff_line -->
"@attribute '", string(Codes), "'",
{read_term_from_codes(Codes, Prolog, []),
transform_conjunction(Prolog,Transf),
format('~w ->~n ~w~n', [Prolog, Transf])},
string(_), eol.
arff_line -->
eol.
eol --> "\n".
transform_conjunction((A,B),(C,D)) :- transform_term(A,C), !, transform_conjunction(B,D).
transform_conjunction(A,C) :- transform_term(A,C).
transform_term(reaction(B,C), reaction(newArg,B,C)).
transform_term(T, T).