计算字符串中字符串的出现次数 [英] Count the occurence of a string in a string

查看:62
本文介绍了计算字符串中字符串的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想给出一个关键字列表,每当在字符串中遇到一个词时,我都会增加我的计数器.所以我已经玩了一段时间,但我不能让它工作.这就是我所做的.每当我运行一个问题时,我都会收到此类错误:ERROR: score/3: Arguments are not enough instantiated.

So I want to give a list of keywords and, whenever a word is encountered in a string, I will increment my counter. So I've been playing around for a while but I can't make it work. This is what I've done. Whenever I run a question I get this type of error: ERROR: score/3: Arguments are not sufficiently instantiated.

score([],0,document(Title,_)).

score([H|T],Acc,document(Title,_)):-
    sub_string(case_insensitive,H,Title),
    Acc1 is Acc + 1,
    score(T,Acc1,document(Title,_)).

我的查询是这样写的:?- document(T,_),score([rule],Acc,document(T,_)).

My queries are written like this: ?- document(T,_),score([rule],Acc,document(T,_)).

推荐答案

当你查询score([rule],Acc,document(T,_))时,变量Acc 未实例化(没有值),因此您的 Acc1 is Acc + 1 失败,因为没有什么可添加到 1(参数未充分实例化em>).此外,您的累加器在递归中不断增加,但是当累加器变为零时您的基本情况会触发(,您的递归基本情况假设累加器正在减少到零).

When you query score([rule],Acc,document(T,_)), the variable Acc is uninstantiated (has no value) so your Acc1 is Acc + 1 fails since there's nothing to add to 1 (arguments are not sufficiently instantiated). Also, your accumulator keeps increasing in recursion, but your base case triggers when the accumulator goes to zero (i.e., your recursive base case assumes that the accumulator is decreasing to zero).

看起来您以前见过使用累加器,但是您混合了两种不同的方法来使用它们(向上计数或向下计数),并且您没有启动累加器的初始谓词,因此您正在尝试从初始查询中执行此操作.

It looks like you've seen use of accumulators before, but you're mixing two different methods for how they're used (counting up or counting down), and you don't have an initial predicate which starts the accumulator, so you're attempting to do this from the initial query.

这是一个带注释的工作版本.您需要从初始累加器为零开始,然后计数.

Here's an annotated working version. You need to start by having an initial accumulator of zero, which will then count up.

score(Tags, Count, Document) :-
    score(Tags, 0, Count, Document).      % Start accumulator at zero

% No more tags, so the current accumulator is what our resulting count is
%   and we don't care what the document argument is at this point
score([], A, A, _).

score([Tag|Tags], Acc, Count, document(Title,_)) :-
    sub_string(case_insensitive, Tag, Title),     % Check for Tag in Title
    Acc1 is Acc + 1,                              % Increment acc if found
    score(Tags, Acc1, Count, document(Title,_)).  % Score remaining tags

但还有一个问题:如果 sub_string/3 失败(找不到单个 Tag),那么 score/4 会彻底失败.因此,如果未找到 Tag 但不增加累加器,则需要继续递归.最简单的方法是在递归 score/4 谓词中使用 Prolog if-then-else 构造:

But there's one more problem: if sub_string/3 ever fails (a single Tag isn't found) then score/4 will fail completely. So there needs to be a continued recursion if Tag isn't found but without incrementing the accumulator. The easiest way to do this is using a Prolog if-then-else construct in the recursive score/4 predicate:

score([Tag|Tags], Acc, Count, document(Title,_)) :-
    (   sub_string(case_insensitive, Tag, Title)  % Check for Tag in Title
    ->  Acc1 is Acc + 1                           % Increment acc if found
    ;   Acc1 = Acc                                % Otherwise, no increment
    ),
    score(Tags, Acc1, Count, document(Title,_)).  % Score remaining tags

使用上述内容,您的查询 ?- document(T, _), score([rule], Acc, document(T,_)). 应该可以工作.由于只有一个标签,因此结果将是 Acc = 0Acc = 1.

Using the above, your query, ?- document(T, _), score([rule], Acc, document(T,_)). should work. Since there's only one tag, the result will either be Acc = 0 or Acc = 1.

这篇关于计算字符串中字符串的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆