Home > Enterprise >  Dictionary and Input: How to use the user input in the value
Dictionary and Input: How to use the user input in the value

Time:01-02

I am working on a kind of chatbot. I am wondering if there is a way to use the input as a value in the dictionary. For example: when the input is "my name is x" I want to get the answer "Hello x". I tried this: "my name is (.*)": ["Hello ! % 1"] and it didn't worked well. I suppose, I need to define (.*) and %1 but I don't know how to make it.

Please help me if you know a way to make this. Thank you.

words = {"good night": ["nighty night", "good night", "sleep well"],
         "good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
         "hi": ["hello", "hey", "hi"],
         "how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
                         "I am doing great thanks", "Better than some, not as good as others",
                         "I am better now, it is good to talk to you"],
         "bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
         "my name is (.*)": ["Hello ! % 1"]
         }

text_punk = input("text something: ")

greet_words = words.keys() #check if the key words is in input_text.
word_available = [word for word in greet_words if word in text_punk]

if word_available: # if words are available take the first of key.
    punk = random.choice(words[word_available[0]])
    print(punk)
    talk(punk) #this is for pyttsx3
else:
    print("problem!")

CodePudding user response:

It looks like you were going for something like this:

from random import choice
from re import match, sub

text_punk = ''

while 'bye' not in text_punk:
    words = {"good night": ["nighty night", "good night", "sleep well"],
             "good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
             "hi": ["hello", "hey", "hi"],
             "how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
                             "I am doing great thanks", "Better than some, not as good as others",
                             "I am better now, it is good to talk to you"],
             "bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
             "my name is (.*)": ["Hello ! {1}"]
             }

    text_punk = input("text something: ").lower()

    response = 'I did not get that, sorry'

    for expression in words:
        if m := match(expression, text_punk):
            response = choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups()) 1)))
            break

    print(response)

Running it with some input:

text something: hi
hello
text something: my name is grismar
Hello ! grismar
text something: bye!
catch you later

Of course, this is where the magic happens:

choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups()) 1)))

If expression matched on the input in text_punk, it selects a random phrase from that dictionary value. It then tries to format that chosen value (replacing values in {}) with the match group.

It does that by listing all the groups, 0 through however many there are using range(len(m.groups()) 1).

For example, if the input is 'my name is grismar', group 0 will be 'my name is grismar' and group 1 will be grismar. So the command comes down to:

choice(words['my name is (.*)']).format('my name is grismar', 'grismar')

And since choice(words['my name is (.*)']) can only be 'Hello ! {1}', it's really:

'Hello ! {1}'.format('my name is grismar', 'grismar')

It will also work for multiple greetings:

"my name is (.*)": ["Hello {1}!", "Hey {1}, long time no see!"]

And you can have multiple matches:

"my name is ([^\s] ) (.*)": ["Hello {1}!", "Greetings, {2}!", "Mx {2}, first name {1}, OK"]

Example:

text something: my name is jaap van der velde
Hello jaap!
text something: my name is jaap van der velde
Greetings, van der velde!
text something: my name is jaap van der velde
Mx van der velde, first name jaap, OK
text something: 
  • Related