0

我目前正在尝试编写一个可以反映陈述的基本“人工智能”。例如,如果用户输入“我的猫是棕色的”。AI 会返回“你的猫是棕色的?”

我写了一个变量数据表,打算用于翻译:

reflections = \
[["I",          "you"],
 ["i",          "you"],
 ["We",         "you"],
 ["we",         "you"],
 ["We're",      "you're"],
 ["we're",      "you're"],
 ["I'm",        "you're"],
 ["i'm",        "you're"],
 ["im",         "you're"],
 ["this",       "that"],
 ["This",       "that"],
 ["am",         "are"],
 ["Am",         "are"],
 ["My",         "your"],
 ["my",         "your"],
 ["you",        "I"], # Grammar: Sometimes "me" is better
 ["You",        "I"],
 ["u",          "me"],
 ["I'd",        "you'd"],
 ["I'll",       "you'll"],
 ["We'd",       "you'd"],
 ["we'd",       "you'd"],
 ["We'll",      "you'll"],
 ["we'll",      "you'll"],
 ["You're",     "I'm"],
 ["you're",     "I'm"],
 ["ur",         "I'm"],
 ["c",          "see"],
 ["I've",       "you've"],
 ["We've",      "you've"],
 ["we've",      "you've"],
 ["Our",        "your"],
 ["our",        "your"],
 ["was",        "were"],
 ["Was",        "were"],
 ["were",       "was"],
 ["Were",       "was"],
 ["me",         "you"],
 ["your",       "my"],
 ["Your",       "my"]]

但是,我在实现数据时遇到了一些麻烦。

我目前对字符串反射的定义是:

from string import maketrans

intab = ".!"
outtab = "??"
translate_message = maketrans(intab, outtab) #used to replace punctuation

def reflect_statement(message):
    if ' ' not in message:
        if len(message) == 0:
            return elicitations[0]
        if len(message) == 1:
            return elicitations[1]
        if len(message) == 2:
            return elicitations[2]
        if len(message) == 3:
            return elicitations[3]
        if len(message) == 4:
            return elicitations[4]
        if len(message) == 5:
            return elicitations[5]
        if len(message) == 6:
            return elicitations[6]
        if len(message) == 7:
            return elicitations[7]
        if len(message) == 8:
            return elicitations[8]
        if len(message) == 9:
            return elicitations[9]
        if len(message) == 10:
            return elicitations[10]
        if len(message) > 10:
            return elicitations[11]
    if ' ' in message:
        message = message.translate(translate_message)
        return message

忽略引出参考,这是我已完成的程序的一个单独部分。

我非常感谢有人可以为我提供的任何帮助。

干杯!

4

2 回答 2

1

你真正想做的是定义一个语法,这样你就可以挑选出代词,然后只替换代词....

基本上对于这样的事情,无论如何你都需要创建一个语法......否则你的人工智能将永远不会有任何意义

看看Neds List Of PyParsers ...我倾向于喜欢 ply

于 2013-04-04T21:27:37.427 回答
1
if ' ' not in message:
    if len(message) < 11:
        return elicitations[len(message)]
    else:
        return elicitations[11]
else:
    for pair in reflections:
        message = message.replace(*pair)

笔记:

  1. 这将替换部分单词。要进行适当的替换,您需要仔细制作正则表达式。也许类似的东西r'\b{}\b'.format(oldword)会起作用,但我不确定:

    import re
    for old, new in reflections:
        message = re.sub(r'\b{}\b'.format(old), new, message)
    
  2. 嵌套列表不是最合乎逻辑的数据结构,请考虑使用字典。

于 2013-04-04T21:29:19.873 回答