Question

I'm 14 (15 in less than an hour actually :D ) and I'm really struggling on this. I'm trying to create a very basic learning AI. At the moment I'm just trying to set up the framework of it, the part which helps it learn new words, so it's a bit messy and rough. Anyway, the idea is that when it doesn't understand a word, it asks what it is and then if the user supplied synonym is recognised, it will add it to that synonym's list of synonyms. Unfortunately, I keep hitting errors and problems and now I'm just confused. Please take a look at it for me. Oh and I apologise for its poor structure and poor just about everything else:

#standard dictionary

dictionary = {1:"hi",2:"yes",3:"no"}
wordvariations=[[],["yes,Yes"],["no,No"]]


#variables

breaker=0
response = "error"
success=0

#import

from random import choice

#word types

question_words= {"who","what","where","why","when","which","how","whom","Who","What","Where","Why","When","Which","How","Whom"}

#What does that mean?

def what_does_that_mean(new_word):
    link_word = input("Sorry, I don't know what that means. Could you tell me? It means the same as the word...\n")
    success=0
    for current_dict in wordvariations:
        if link_word in current_dict == TRUE:
            print ("Thanks")
            current_dict.append[new_word]
            success=1
            break
    if success == 0:
        tryagain = input("Sorry, I don't know what that means either. Would you like to try again? (y/n) \n")
        if input == "y":
            testword= input("Give me another word \n")
            what_does_this_mean(testword)
        else: return


#First start

possible_greeting =input("Hey. Nice to meet you.\n")
split_greeting = possible_greeting.split()    

for each_word in split_greeting:
    if each_word in question_words:
        response = "Sorry, I can't answer questions yet"
        breaker=1
        break
    elif (each_word == ("too")):
        response = "Thanks"
        breaker=1
        break
    else: pass

if breaker ==1:
    pass
else:
    yes_no = input("Is that how you usually greet people?\n")
    if yes_no == "yes":
        wordvariations[1].append(possible_greeting)
    elif yes_no=="no":
        new_greeting = input("Ok then. How do you often greet people?")
        wordvariations[1].append(new_greeting)
    else:
        what_does_that_mean(yes_no)

#print (response)


#Other stuff

current_greeting = choice(wordvariations[1])
print (current_greeting)

If any indentation looks really off, I've probably put it in the question box wrong.

I'll really appreciate any help here - I feel like I'm going round in circles. At the moment, the search doesn't seem to be working as it never finds a result. It's the word finding function area that needs fixing most, the rest is just a sort of start-up script to get things going. Try and ignore the entirely useless bits that are just there for later.

Thanks in advance :)

Was it helpful?

Solution

Congratulations Jake :D

A few things that will make it easier for you

>>> 'How'.lower()
'how'
if word.lower() in word_list: # where word_list contains lower case only

so you can remove the upper/lower-case versions of the words by all the time making it lowercase.

if link_word in current_dir == TRUE:

could simply be written

if link_word in current_dir:

You can also use boolean's (True/False) instead of integers

success = 1 could instead be success = True, and having booleans you can simply check them with if success

(and if you feel like it a there is actually a for ... else that does exactly what you intend to do without the success variable Why does python use 'else' after for and while loops?)

current_dict.append[newword] should be current_dict.append([newword]) or current_dict.append(newword)

you also did some misses with calling variables input like if input=='y'

I also are you running python 2 or 3? in python 2 input('?') is not what you want (it's raw_input('?')

I think you would want wordvariations to be it's own for the greetings part. Like call it greetings. (This so you don't have to ...[1] all the time)

You wrote what_does_this_mean instead of what_does_that_mean

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top