是否可以在prolog中执行字符串处理

时间:2015-01-08 09:29:54

标签: python prolog swi-prolog

我想在句子上执行一些操作,如字符串搜索,迭代,拆分等。我检查了几个Prolog tuts但没有帮助 - http://kti.ms.mff.cuni.cz/~bartak/prolog/contents.htmlhttp://www.bitwisemag.com/copy/programming/prolog/intro/firststeps.html。我想写这样的程序:

for linking in post_script_link_list:
        sub_link = re.search('\((.*?)\)',linking).group(1)
        if sub_link in links_list1:         
            if ('Who').lower() in sent_split or 'Who' in sent_split:
                result = 'person'
                break
            elif (('Where').lower() in sent_split or 'Where' in sent_split) and 'What' not in read_str and 'what' not in read_str:
                result = 'location'
                break
            elif ('Why').lower() in sent_split or 'Why' in sent_split:
                result = 'reason'
                break
            elif ('How many').lower() in read_str or 'How many' in read_str:
                result = 'count'
                break
            elif (('When').lower() in sent_split or 'When' in sent_split) and 'What' not in read_str and 'what' not in read_str:
                result = 'time'
                break
            elif (('How').lower() in sent_split or 'How' in sent_split) and ('How long').lower() not in read_str and 'How long' not in read_str and ('How much').lower() not in read_str and 'How much' not in read_str:
                result = 'manner'
                break
        elif sub_link in links_list2:
            check_yn = verify_yesno(post_script_link_list)
            if check_yn == 1:
                result = 'Yes/No'
                break
            elif check_yn == 0:
                result = 'Not found'
                break
            break
        else:
            result = 'Not found'

可以执行usnig prolog吗?我正在使用pyswip和python

更新

1 ?- split_string("a.b.c.d", ".", "", L).
ERROR: toplevel: Undefined procedure: split_string/4 (DWIM could not correct goal)

1 个答案:

答案 0 :(得分:2)

要检查字符串是否包含特定子字符串,您可以使用append/3sub_atom/5,具体取决于您如何表示字符串。

如果您的字符串是原子,请使用sub_atom/5

substring(Atom, Substring):-
    sub_atom(Atom, _, _, _, Substring).

?- substring('... Where is that?', 'Where').
true ;
false.

如果您的字符串是代码列表,请使用append/3

substring(Codes, Substring):-
    append(S, _, Codes),
    append(_, Substring, S).

?- substring(".. Where is that?", "Where").
true ;
false.