将元组事实过滤到新列表

发布于 2025-01-09 11:15:20 字数 1079 浏览 0 评论 0原文

我在尝试过滤通过 CSV 导入的事实时遇到问题。我有以下 CSV 文件:

"colA","colB","colC","colD"
"what","is","the","chocolate"
"hello","my","friend","person"
"time","for","some","food"
"when","is","my","event"

这是使用 csv_read_file 函数导入到我的文件中的。例如,我可以运行 check 来查看 CSV 中导入的第三个单词是否包含带有 check('e') 的字母“e”

:- use_module(library(csv)).

import :-
    csv_read_file('./data.csv', Data, [functor(fact)]),
    maplist(assert, Data).

check(Q) :-
        fact(_,_,S,_),
        sub_string(S,_,_,_,Q).

我现在想做的是减少通过 CSV 导入的包含“e”的事实子集,并将其存储在另一种数据类型中以供稍后使用。这不起作用,但我想做的是过滤 check('e') 中的所有事实,但将结果存储在 Output 中。

filter(Q, Output) :-
        findall(
                fact(_,_,S,_),
                sub_string(S,_,_,_,Q),
                Output
        ).

我可以全部打印出来,是否可以将其存储在列表中?

forall((fact(A,B,S,D), sub_string(S,_,_,_,'e')), print(S)).

也尝试过这个

include(sub_string(S,_,_,_,'e'), fact(A,B,S,D), R).

谢谢

I'm having an issue with trying to filter facts imported through a CSV. I have the following CSV file:

"colA","colB","colC","colD"
"what","is","the","chocolate"
"hello","my","friend","person"
"time","for","some","food"
"when","is","my","event"

This is imported in my file using csv_read_file function. As an example I'm able to then run check to see if there is a fact with a third word imported in the CSV that contains the letter 'e' with check('e').

:- use_module(library(csv)).

import :-
    csv_read_file('./data.csv', Data, [functor(fact)]),
    maplist(assert, Data).

check(Q) :-
        fact(_,_,S,_),
        sub_string(S,_,_,_,Q).

What i'm trying to do now is reduce the subset of facts imported through the CSV that contain 'e', and store those in another data type to use later on. This isn't working but what I'm trying to do is filter all facts like in check('e') but store the results in Output.

filter(Q, Output) :-
        findall(
                fact(_,_,S,_),
                sub_string(S,_,_,_,Q),
                Output
        ).

I can print all out, is it possible to store this in a list?

forall((fact(A,B,S,D), sub_string(S,_,_,_,'e')), print(S)).

Also tried with this

include(sub_string(S,_,_,_,'e'), fact(A,B,S,D), R).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2025-01-16 11:15:20

您使用 findall/3 将输出收集到列表中是正确的。不过,请查看 findall 的文档

如果你想从fact/4中收集所有匹配的S,你可以使用:

filter(Q, Output) :-
    findall(S,
            ( fact(_, _, S, _),
              sub_string(S, _, _, _, Q)
            ),
            Output).

如果你想收集整个事实,它会是这样的:

filter(Q, Output) :-
    findall(fact(A, B, S, C),
            ( fact(A, B, S, C),
              sub_string(S, _, _, _, Q)
            ),
            Output).

注意sub_string/5回溯,所以它可能匹配每个字符串多次使用子字符串。

You were right using findall/3 to collect your output onto a list. Check out the documentation for findall though.

If you want to collect all matching S from fact/4 you may use:

filter(Q, Output) :-
    findall(S,
            ( fact(_, _, S, _),
              sub_string(S, _, _, _, Q)
            ),
            Output).

and if you want to gather the whole fact it would be something like this:

filter(Q, Output) :-
    findall(fact(A, B, S, C),
            ( fact(A, B, S, C),
              sub_string(S, _, _, _, Q)
            ),
            Output).

Note that sub_string/5 backtracks so it may match the substring more than once for each string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文