Search code examples
prolog

Why is Prolog being a jerk


sorted.csv

01Vbe,2r5/3Qnk1p/8/4B2b/Pp2p3/1P2P3/5PPP/3R2K1 w - - 3 32,d1d6 c8c1 d6d1 c1d1 d7d1 h5d1,600,80,100,63
3,advantage endgame fork long,https://lichess.org/b334W1Ga#63
01GC2,3b4/pp2kprp/8/1Bp5/4R3/1P6/P4PPP/1K6 b - - 0 22,e7f8 e4e8,600,82,89,374,endgame mate mateIn1 on
eMove,https://lichess.org/6c75Zk3T/black#44

file_line reads the code line by line as posted in this answer: Read a file line by line in Prolog

stuff(Lines) :- findall(Tags,
  (
    file_line("data/athousand_sorted.csv", Line),
    csv_fen(Line, Fen, Moves, Id, Tags),
    maplist(atom_string, TagsAtom, Tags),
    tags_with(TagsAtom, mateIn1)
  ),
  Lines).

csv_fen(Line, Fen, Moves, Id, Tags) :- split_string(Line, ",", "", [Id, Fen, Moves, _, _, _, _, Tags|
_]).

tags_with(Tags, Tag) :- split_string(Tags, " ", "", Ls), member(Tag, Ls).

maplist(atom_string, XYZ), doesn't work for string read from a file, though this works on the repl.

?- maplist(atom_string, Tags, ["mateIn1"]).

I want to filter lines of this file that has mateIn1 tags.


Solution

  • Does this give you a hint? (Is Tags a list of strings?)

    ?- csv_fen("id,fen,move,b1,b2,b3,b4,tags", Fen, Moves, Id, Tags).
    Fen = "fen",
    Moves = "move",
    Id = "id",
    Tags = "tags".