【问题标题】:String and list processing in swi-prologswi-prolog 中的字符串和列表处理
【发布时间】:2015-02-11 09:02:38
【问题描述】:

我将句子作为输入,然后将其转换为小写。为每个单词创建这个句子的列表,然后遍历这个列表并搜索特定的关键字。

我尝试了不同的方式,但仍然无法实现:

转为小写:

tolower([], []).
tolower([Upper|UpperTail], [Lower|LowerTail]) :-
    char_type(Lower, to_lower(Upper)),tolower(UpperTail, LowerTail).

问题是,它以列表格式给出结果:

1 ?- tolower("Try This STRING", Lower).
Lower = [t, r, y, ' ', t, h, i, s, ' '|...].

在句子中搜索字符串:

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

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

但是将句子转换为小写,转换为tokens和迭代列表仍然不可用。这是我想转换成python的简单代码。

def process(read):
    in_notin_result = (
        ('who',      [],       'person'),
        ('where',    ['what'], 'location'),
        ('why',      [],       'reason'),
        ('how many', [],       'count'),
        ('when',     ['what'], 'time'),
        ('how',      [],       'manner'),
    )
    words = read.split()
    for in_sent, notin_read, result in in_notin_result:
        if in_sent in words and all(disallowed not in read for disallowed in notin_read):
            return result
    return None

def process_link(post_script_link_list, read_str):
    read_str = read_str.lower()

    for item in ('how long', 'how much'):
        if item in read_str:
            return

    for linking in post_script_link_list:
        sub_link = re.search('\((.*?)\)', linking).group(1)

        if sub_link in ['Wq','Ws','Wi','Wd']:
            process_result = process(read_str)
            if process_result is not None:
                return process_result

        elif sub_link in ['Qd']:
            return 'Yes/No' if verify_yesno(post_script_link_list) else 'noresult'

    return 'noresult'

【问题讨论】:

    标签: prolog artificial-intelligence swi-prolog


    【解决方案1】:

    SWI-Prolog 有一些惯用的 NL processing 功能,例如

    ?- tokenize_atom('Try This STRING',L), maplist(downcase_atom,L,D).
    L = ['Try', 'This', 'STRING'],
    D = [try, this, string].
    

    可以从this pack 安装正则表达式支持(在普通 Prolog 中),但是 - 我认为 - 编写 DCG 更容易。 如果你急需正则表达式,比打包更高效,有旧的XPCE regex 接口...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      相关资源
      最近更新 更多