Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:14:06+00:00 2026-05-29T10:14:06+00:00

I am making a simple chat bot in Python. It has a text file

  • 0

I am making a simple chat bot in Python. It has a text file with regular expressions which help to generate the output. The user input and the bot output are separated by a | character.

my name is (?P<'name'>\w*) | Hi {'name'}!

This works fine for single sets of input and output responses, however I would like the bot to be able to store the regex values the user inputs and then use them again (i.e. give the bot a ‘memory’). For example, I would like to have the bot store the value input for ‘name’, so that I can have this in the rules:

my name is (?P<'word'>\w*) | You said your name is {'name'} already!
my name is (?P<'name'>\w*) | Hi {'name'}!

Having no value for ‘name’ yet, the bot will first output ‘Hi steve’, and once the bot does have this value, the ‘word’ rule will apply. I’m not sure if this is easily feasible given the way I have structured my program. I have made it so that the text file is made into a dictionary with the key and value separated by the | character, when the user inputs some text, the program compares whether the user input matches the input stored in the dictionary, and prints out the corresponding bot response (there is also an ‘else’ case if no match is found).

I must need something to happen at the comparing part of the process so that the user’s regular expression text is saved and then substituted back into the dictionary somehow. All of my regular expressions have different names associated with them (there are no two instances of ‘word’, for example…there is ‘word’, ‘word2’, etc), I did this as I thought it would make this part of the process easier. I may have structured the thing completely wrong to do this task though.

Edit: code

import re

io = {}

with open("rules.txt") as brain:
     for line in brain:
        key, value = line.split('|')
        io[key] = value

string = str(raw_input('> ')).lower()+' word'

x = 1

while x == 1:
    for regex, output in io.items():
        match = re.match(regex, string)
        if match:
            print(output.format(**match.groupdict()))
            string = str(raw_input('> ')).lower()+' word'
    else:
        print ' Sorry?'
        string = str(raw_input('> ')).lower()+' word'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:14:07+00:00Added an answer on May 29, 2026 at 10:14 am

    I had some difficulty to understand the principle of your algorithm because I’m not used to employ the named groups.
    The following code is the way I would solve your problem, I hope it will give you some ideas.

    I think that having only one dictionary isn’t a good principle, it increases the complexity of reasoning and of the algorithm. So I based the code on two dictionaries: direg and memory

    Theses two dictionaries have keys that are indexes of groups, not all the indexes, only some particular ones, the indexes of the groups being the last in each individual patterns.
    Because, for the fun, I decided that the regexes must be able to have several groups.

    What I call individual patterns in my code are the following strings:

    "[mM]y name [Ii][sS] (\w*)"
    
    "[Ii]n repertory (\w*) I [wW][aA][nN][tT] file (\w*)"
    
    "[Ii] [wW][aA][nN][tT] to ([ \w]*)"
    

    You see that the second individual pattern has 2 capturing groups: consequently there are 3 individual patterns, but a total of 4 groups in all the individual groups.

    So the creation of the dictionaries needs some additional care to take account of the fact that the index of the last matching group ( which I use with help of the attribute of name lastindex of a regex MatchObject ) may not correspond to the numbering of individual regexes present in the regex pattern: it’s harder to explain than to understand. That’s the reason why I count in the function distr() the occurences of strings {0} {1} {2} {3} {4} etc whose number MUST be the same as the number of groups defined in the corresponding individual pattern.

    I found the suggestion of Laurence D’Oliveiro to use ‘||’ instead of ‘|’ as separator interesting.

    My code simulates a session in which several inputs are done:

    import re
    
    regi = ("[mM]y name [Ii][sS] (\w*)"
            "||Hi {0}!"
            "||You said that your name was {0} !!!",
    
            "[Ii]n repertory (\w*) I [wW][aA][nN][tT] file (\w*)"
            "||OK here's your file {0}\\{1} :"
            "||I already gave you the file {0}\\{1} !",
    
            "[Ii] [wW][aA][nN][tT] to ([ \w]*)"
            "||OK, I will do {0}"
            "||You already did {0}. Do yo really want again ?")
    
    
    direg  = {}
    memory = {}
    def distr(regi,cnt = 0,di = direg,mem = memory,
              regnb = re.compile('{\d+}')):
        for i,el in enumerate(regi,start=1):
            sp = el.split('||')
            cnt += len(regnb.findall(sp[1]))
            di[cnt] = sp[1]
            mem[cnt] = sp[2]
            yield sp[0]
    
    regx = re.compile('|'.join(distr(regi)))
    print 'direg :\n',direg
    print
    print 'memory :\n',memory
    for inp in ('I say that my name is Armano the 1st',
                'In repertory ONE I want file SPACE',
                'I want to record music',
                'In repertory ONE I want file SPACE',
                'I say that my name is Armstrong',
                'But my name IS Armstrong now !!!',
                'In repertory TWO I want file EARTH',
                'Now my name is Helena'):
    
        print '\ninput  ==',inp
    
        mat = regx.search(inp)
        if direg[mat.lastindex]:
            print 'output ==',direg[mat.lastindex]\
                  .format(*(d for d in mat.groups() if d))
            direg[mat.lastindex] = None
            memory[mat.lastindex] = memory[mat.lastindex]\
                                    .format(*(d for d in mat.groups() if d))
        else:
            print 'output ==',memory[mat.lastindex]\
                  .format(*(d for d in mat.groups() if d))
            if not memory[mat.lastindex].startswith('Sorry'):
                memory[mat.lastindex] = 'Sorry, ' \
                                        + memory[mat.lastindex][0].lower()\
                                        + memory[mat.lastindex][1:]
    

    result

    direg :
    {1: 'Hi {0}!', 3: "OK here's your file {0}\\{1} :", 4: 'OK, I will do {0}'}
    
    memory :
    {1: 'You said that your name was {0} !!!', 3: 'I already gave you the file {0}\\{1} !', 4: 'You already did {0}. Do yo really want again ?'}
    
    input  == I say that my name is Armano the 1st
    output == Hi Armano!
    
    input  == In repertory ONE I want file SPACE
    output == OK here's your file ONE\SPACE :
    
    input  == I want to record music
    output == OK, I will do record music
    
    input  == In repertory ONE I want file SPACE
    output == I already gave you the file ONE\SPACE !
    
    input  == I say that my name is Armstrong
    output == You said that your name was Armano !!!
    
    input  == But my name IS Armstrong now !!!
    output == Sorry, you said that your name was Armano !!!
    
    input  == In repertory TWO I want file EARTH
    output == Sorry, i already gave you the file ONE\SPACE !
    
    input  == Now my name is Helena
    output == Sorry, you said that your name was Armano !!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam making a simple app, I display pdf file with some text and images
I'm making a simple chat program for me and my friends as a tech
I am making a TCP chat server from a simple tutorial I found and
How to change sizes of ViewController? I'm making simple app which uses deliberately two
I am making a simple chat application and I am trying to find out
I´m making a simple website to display information from XML file that is created
I'm making a simple cross platform chat program. I'm using wXWidgets for the GUI
This is similar to my last question. I'm making a simple tcp/ip chat program
I'm making a simple little utility while learning Python. It dynamically generates a list
I am making a simple chat GUI and I have written both a server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.