【问题标题】:Is it possible to perform string processing in prolog是否可以在prolog中执行字符串处理
【发布时间】:2015-01-08 09:29:53
【问题描述】:

我想对句子执行一些操作,如字符串搜索、迭代、拆分等。我检查了几个 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)

【问题讨论】:

  • 为什么要使用prolog?你所拥有的有什么问题?你有使用 prolog 或其他函数式编程语言的经验吗?
  • @VincentBeltman:是的,我对 prolog 的经验很少,其中有简单的规则,这很好。在基于规则的系统上工作时,使用 prolog 进行管理变得简单。在python的情况下,每次都需要编辑代码并添加if ... else,代码变得过于嵌套,复杂
  • 我看到您的代码有很多改进。我可以稍微清理一下。或者您可以在代码审查中发布您的代码。
  • 如果您使用 SWI-Prolog,还有“字符串”:swi-prolog.org/pldoc/man?section=strings“字符串”上的谓词在语义上比原子和代码列表更接近您想要实现的目标谓词。不过,这不会让您的工作变得更轻松,您似乎在组织思想方面存在问题。
  • @VincentBeltman:如果建议清理@codereview.stackexchange.com/questions/76956/…,我将不胜感激

标签: python prolog swi-prolog


【解决方案1】:

要检查字符串是否包含特定子字符串,您可以使用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.

【讨论】:

  • 非常感谢,我无法理解两者之间的区别。有什么方法可以拆分句子并为每个单词迭代?
  • SWI Prolog 中有split_string/4
  • 这是因为split_string/4 从版本 7 开始可用。
  • 如果你需要实现这样的谓词,最好发布一个不同的问题,以保持问题和答案的简单和对其他人也有用。
猜你喜欢
  • 2010-11-03
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多