Skip to content Skip to sidebar Skip to footer

How To Recognize Name (from Text File) In User Input And Then Print Name

My ideal goal is for the chat bot to recognize that you are talking about one of your siblings. So, when you mention your brother or sister (either by name or the keywords: my brot

Solution 1:

Your bigger problem is managing the state of your program. Everyloop; you are testing all of your if, and them being true will get executed all the time, which is not what you want.

I would suggest to not rush steps too quickly, for instance I don't think if not first: does what you expect.

One way to help organise and manage that state is to use functions. Use plenty of them!

Then I would suggest going piece by piece : you need to figure out under what conditions you want each question/answer to appear in your code. If you're asking about a brother you don't know, then probably the code that talks about an unknown brother shouldn't be in the place. Or should have condition to guard the code from executing.

You'll probably get to a point where you'll have conditions all over the place, when that happens (and not before, or for curiosity) you should check out "state machines".

Side notes about python 3 :

Python 2 is becoming obsolete in 2020 and should not be used anymore. Systems will not ship with them, and people are expected to use python 3, and support for python 2 will stop. This is not to say you shouldn't continue using python 2 as a learning tool, but you should think about learning python 3, you'll get more help more easily. Also there's a few cool features that python 2 doesn't have and you wouldn't want to miss out on that^^

Solution 2:

I have updated your code with nltk pos tags to extract out the names from text file. Try it:

import string
import nltk

nltk.download('maxent_treebank_pos_tagger')

brother_status = dict([
('name', ''),
('nickname', ''),
('current age', ''),
('relationship', '')])

brother_keywords = ["Brother", "brother"]
sister_keywords = ["Sister", "sister"]

defmain(): 
    whileTrue:
    user_input = raw_input("What type of sibling do you have: ").translate(string.maketrans("",""), string.punctuation)
    for keyword in brother_keywords:
        if keyword in user_input:
            withopen('file.txt', 'r') as sibling_database:
                data = sibling_database.readline()
                data = nltk.word_tokenize(data)
                tagged_data = nltk.pos_tag(data)
                for name, pos in tagged_data:
                    if pos == "NNP":
                        print(name)
                        returnbreakif user_input.split():
            withopen('file.txt') as sibling_database:
                for line in sibling_database:
                    for word in user_input.split():
                        if word in line:
                                print("Oh, so your brother's name is " + line.split(':')[1] * 1)
                                returnbreakif user_input.split():
            for keyword in brother_keywords:
                if keyword in user_input:
                    if user_input notin brother_status:
                        withopen ('file.txt') as sibling_database:
                            first = sibling_database.read(1)
                            ifnot first:
                                print ("You never mentioned a brother. What's his name?")
                                user_input = raw_input("What's his name: ")
                                brother_status['name'] = (user_input)
                                withopen('file.txt', 'w') as sibling_database:
                                    sibling_database.write("Brother's Name:" + brother_status['name'] * 1 + '\n')
                                    print ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")
                                    continueif __name__ == "__main__":
      main()

Post a Comment for "How To Recognize Name (from Text File) In User Input And Then Print Name"