我目前正在尝试编写一个可以反映陈述的基本“人工智能”。例如,如果用户输入“我的猫是棕色的”。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
忽略引出参考,这是我已完成的程序的一个单独部分。
我非常感谢有人可以为我提供的任何帮助。
干杯!