程序重复执行与预期不同的行

时间:2019-05-23 22:21:07

标签: recursion prolog logic

我有一些称为会话的规则,这些规则具有标题(字符串)和主题(字符串列表)。我正在创建一个查询,在其中输入一些关键字并找到最匹配的会话。每个关键字可以有一个权重(例如,如果关键字的权重为2,则关键字后跟-2,如果权重为1,则关键字旁没有任何内容)。 我的问题是递归。对于不带权重的输入,一切正常,但带权重的程序则以不同的方式重复执行。

session('Rules; Semantic Technology; and Cross-Industry Standards',
['XBRL - Extensible Business Reporting Language',
 'MISMO - Mortgage Industry Standards Maintenance Org',
 'FIXatdl - FIX Algorithmic Trading Definition Language',
 'FpML - Financial products Markup Language',
 'HL7 - Health Level 7',
 'Acord - Association for Cooperative Operations Research and Development (Insurance Industry)',
 'Rules for Governance; Risk; and Compliance (GRC); eg; rules for internal audit; SOX compliance; enterprise risk management (ERM); operational risk; etc',
 'Rules and Corporate Actions']).

session('Rule Transformation and Extraction',
['Transformation and extraction with rule standards; such as SBVR; RIF and OCL',
 'Extraction of rules from code',
 'Transformation and extraction in the context of frameworks such as KDM (Knowledge Discovery meta-model)',
 'Extraction of rules from natural language',
 'Transformation or rules from one dialect into another']).

accessList([H|T], H, T).

is_empty_string(String) :-
    String == ''.
not_empty_string(String) :-
    String \== ''.

get_weight_of_token(MyToken, WeightOfToken) :-
    split_string(MyToken,"-","",TempList),  % tokenize MyToken into a list ( [string_of_token, weight_of_token] ) 
    accessList(TempList,_,TempWeight),          %
    accessList(TempWeight,TempWeight2,_),       % accessing the tail of the list (weight_of_token)
    number_string(WeightOfToken,TempWeight2)    % convert string to number

    ;

    WeightOfToken is 1.

find_relevance([], SessionTitle, SessionTopics).
find_relevance([H | T], SessionTitle, SessionTopics) :-
    get_weight_of_token(H, WeightOfToken),
    format('Token = ~w with weight = ~d ~n', [H,WeightOfToken]),
    format('Title = ~w~nTopics = ~w ~n', [SessionTitle,SessionTopics]),
    find_relevance(T, SessionTitle, SessionTopics).

query(ListOfKeywords) :-
    is_empty_string(ListOfKeywords), write('List of keywords is empty!')

    ;

    not_empty_string(ListOfKeywords),
    split_string(ListOfKeywords," ","",KeywordsTokens),     %tokenize string with space as separator
    session(Title,Topics), find_relevance(KeywordsTokens, Title, Topics), fail. 

使用没有权重的输入(例如“ firword secword”),结果如下: https://imgur.com/a/Q2fU2IM

您可以看到该程序可以正常运行,并在下一个会话中调用find_revelance。

使用权重为“ firword-2 secword”的输入,结果如下: https://imgur.com/xKFpFvh

如您所见,该程序在下一个会话中不会重复至(9),而是由于某种原因而递归至(10)..特别是;; ..

之后的部分

为什么会这样?预先感谢。

*我将标题和主题更改为较小的内容,以使图像更清晰

0 个答案:

没有答案