简体   繁体   中英

How to replace words in a string in Python?

I am currently attempting to write a basic 'AI' that can reflect statements. For example, if a user were to type "My cat is brown." the AI would return "Your cat is brown?"

I have written a table of variable data which I intend to use for the translation:

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"]]

However, I'm having some trouble implementing the data.

My current definition for string reflection is:

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

Ignore the elicitations reference, that is a separate part of the program I have completed.

I'd really appreciate any help that someone could offer me.

Cheers!

What you really want to do is define a grammar such that you can pick out the pronoun, and then only replace the pronoun ....

basically for something like this you will need to create a grammar no matter what ... or your AI will never ammount to much of anything

take a look at Neds List Of PyParsers ... I tend to like ply

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)

Notes:

  1. This will replace parts of words. To make a proper replacement you would need to carefully craft a regular expression. Maybe something like r'\\b{}\\b'.format(oldword) would work, but I'm not sure:

     import re for old, new in reflections: message = re.sub(r'\\b{}\\b'.format(old), new, message) 
  2. A nested list is not the most logical data structure, consider using a dictionary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM