2016-05-22 23 views
0

私はPrologに詳しくはありませんが、属性値のペアを使用してCSVデータからルールを生成する手助けが必要です。プロローグでcsvファイルからルールを生成する方法は?

以下の表からルールを生成しようとしましたが、それは困難でした。

obj height hair eyes class 
o1 short blond blue c1 
o2 short blond brown c2 
o3 tall red  blue  c1 
. 
. 
.etc. 

私は、クラスごとにこれらのステップを繰り返し、C1についての最初のルールを見つけ、すべての属性値のペアのためのすべてのp(C1|av)を計算し、その後、セットから覆われたオブジェクトを削除しようとしました。

答えて

0

あなたがしようとしていることは100%確信していませんが、TSV(タブ区切り?)ファイルを読み込み、その値に基づいて句を生成しようとしていると思います。例えば

、以下のTSVファイル...

ID a1 a2 
o1 abc def 
o2 bcd efg 

が...事実を表して...

a1(o1, abc) 
a1(o2, bcd) 
a2(o1, def) 
a2(o2, efg) 

次のプログラムでは、タブ区切りのファイルを読み込み、生成されますそのような句のリスト。ここに私の入力ファイルは

% Exmaple clause to read a file and print out facts to the console. 
% Instead of printing, you could do whatever you like with them (assert them into WM for example) 
example :- 
    build_facts_from_file('c:\\tmp\\data.tsv', FACTS), 
    writeln(FACTS). 

% build_facts_from_file(FILENAME_IN, FACTS_OUT). 
% Reads the file and outputs a list of facts. 
build_facts_from_file(FILENAME, FACTS) :- 
    open(FILENAME, read,S), 
    read_tsv(S,[HEADER | RECORDS]), 
    close(S), 
    build_records(HEADER, RECORDS, FACTS). 

% read_tsv(INPUT_STREAM_IN, LINES_OUT) 
% Read tab-seperated values from the stream S into a list (one element per line) 
read_tsv(S, []) :- 
    % Stop if EOF 
    at_end_of_stream(S), !. 

read_tsv(S, [L | T]) :- 
    % Read whole line as character codes 
    read_line_to_codes(S, CODES), 
    % Translate the line into a list of atoms 
    translate_tsv(CODES, [], L), 
    % Read the other lines 
    read_tsv(S, T). 

% translate(CHARACTER_CODE_LIST_IN, ATOM_SO_FAR_IN, ATOMS_OUT). 
% Gets a line, splits it on TAB characters and turns each element into an atom 
translate_tsv([], [], []) :- !. 
    % Stopping condition when tab was at end of line, but no extra element 
translate_tsv([], ATOM_CHARS, [ATOM]) :- 
    % Stopping condition when non-tab was at end of line, 
    % turn list of codes for the atom found so far into an atom 
    atom_chars(ATOM, ATOM_CHARS). 
translate_tsv([9 | CODES], ATOM_CHARS, [ATOM | ATOMS]) :- !, 
    % Tab found, turn ATOM_CHARS into ATOM and start building the next atom 
    atom_chars(ATOM, ATOM_CHARS), 
    translate_tsv(CODES, [], ATOMS). 
translate_tsv([CODE | CODES], ATOM_CHARS, ATOMS) :- !, 
    % Non-tab character found, appent CODE to the end of ATOM_CHARS and keep going 
    append(ATOM_CHARS, [CODE], TMP_ATOM_CHARS), 
    translate_tsv(CODES, TMP_ATOM_CHARS, ATOMS). 

% build_records(HEADER_IN, RECORDS_IN, FACTS_OUT) 
% Process each element in RECORDS_IN to create FACTS_OUT 
build_records(_, [], []). 
    % Stopping case, no records left to build 
build_records(HEADER, [RECORD | RECORDS], FACTS) :- 
    % Build facts for all the other records 
    build_records(HEADER, RECORDS, FTMP1), 
    % Build facts for this record 
    build_fact(HEADER, RECORD, FTMP2), 
    % Add lists together 
    append(FTMP1, FTMP2, FACTS). 

% build_fact(HEADER_IN, ATTRIBUTES_IN, FACTS_OUT) 
% Builds clauses for each attribute - strips out the object ID (assumed to be first attribute) 
% and calls build_fact/4 to do the work 
build_fact([_ | HEADER], [ID | ATTRIBUTES], FACTS) :- 
    build_fact(ID, HEADER, ATTRIBUTES, FACTS). 

% build_fact(OBJECT_ID_IN, PROPERTY_NAMES_IN, VALUES_IN, FACTS_OUT) 
build_fact(ID, [PROP | PROPS], [VALUE | VALUES], [FACT | FACTS]) :- 
    % create term for the fact 
    FACT =.. [PROP, ID, VALUE], 
    % build the rest 
    build_fact(ID, PROPS, VALUES, FACTS). 

build_fact(_, [], [], []). 
    % Stopping condition 

...次のコードは、この形式のファイルを読み込む(オブジェクトIDは最初の列であることが予想される)と句が生成されます...

id height hair eyes class 
o1 short blond blue c1 
o2 short blond brown c2 
o3 tall red blue c1 

です出力例:

1 ?- example. 
[height(o3,tall),hair(o3,red),eyes(o3,blue),class(o3,c1),height(o2,short),hair(o2,blond),eyes(o2,brown),class(o2,c2),height(o1,short),hair(o1,blond),eyes(o1,blue),class(o1,c1)] 

が、これは

を役に立てば幸い
関連する問題