Artificial intelligence program..not really just simple

This is the place for queries that don't fit in any of the other categories.

Artificial intelligence program..not really just simple

Postby el_manu » Wed Sep 04, 2013 4:55 am

Code: Select all
a = []
start = input("Hello, my name is Eliza. What would you like to talk about? ")
while start != "Go away":
   if "feel" in start:
      start = input("""Do you often feel that way?
""")
    else:
      start = input("""Please go on.
""")


Um yeah.. my code is to have an input in a loop, and it ends when the user writes "Go away"..then if the line has the word feel, then yeah the response should be "Do you often feel that way?"

if "i am <something>" is in the input, then the program should respond" How long have you been <something>?."

Otherwise, if none of these rules apply, Eliza should just print Please go on.


i don't know how to print for example the word after i am???

Mekire: Merged a quintuple post :x
also:

If the input "<some words> me" appears in the line, the program should respond "<some words> ,no."

ALL THAT I DON't KNOW HOW TO DO IS TO PRINT <some words> or <something>

do i need to use lists????

Each line has to be converted to lower case

DO I NEED to use index on a list???
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby Mekire » Wed Sep 04, 2013 7:39 am

You really need to reconsider your tone. No one here is being paid to help you; we are all volunteers. Do not quintuple post. Do not even double post unless you have something really important to add. Ideally you would use the edit button but so far you have not engendered trust with that responsibility.

I am going to offer some ideas and then I am going to lock the thread for a few hours. When I unlock it, if you want more help, you need to post an updated attempt. It doesn't have to necessarily work, but it does need to show that you are trying to apply the ideas being suggested.

So... firstly let's talk about split. The method split takes a string and divides it into a list based on a separator (the separator defaults to a space if not given).
Code: Select all
my_input = input("Enter something: ")
input_in_a_list = my_input.split()
print(input_in_a_list)
Code: Select all
Enter something: This is the end of the road.
['This', 'is', 'the', 'end', 'of', 'the', 'road.']

Now that you have all the separate words in the list you can use slicing for example to get sections.
Code: Select all
>>> input_in_a_list[:4]
['This', 'is', 'the', 'end']
>>> input_in_a_list[5:]
['the', 'road.']
>>>

You could also possibly use str.startswith to find out if your string has a certain beginning:
Code: Select all
if my_input.startswith("This is the"):

Experiment with with the methods of str and list. You should make yourself intimately familiar with them.
-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

index function

Postby el_manu » Wed Sep 04, 2013 8:06 am

how can I use the index function to find the position of two words next to eachother in an input...
i know that you can do this just say:

Code: Select all
a = ['One', 'Long', 'Day', 'The', 'Python', 'ROARED']
print(words.index('One'))
print(words.index('The'))


it would print:
Code: Select all
0
4


i totally get that.. but how can you find the position of two words next to eachother..if the list is an input.

Code: Select all
a = input("Python can get annoying sometimes huh? ")
a.split()
    if "You Lost" in a:
        print(words.index('You' 'Lost')) # I cannot do this because you can't...but how can i find the position of these
    else:
        print("Thanks for the help.")


how can i find the position of these words in this input



and do you really have to lock the editing from my first posts???

i wouldn't get rid of it and you could just ban my ip and my account from the webpage if I did


the only one that I wanted to get rid of beofore was the STEMMING (but i don't care anymore)
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby Mekire » Wed Sep 04, 2013 8:34 am

Again you seem to pick a tool and convince yourself you have to use it.

If you really want to use index you can do stuff like this:

Code: Select all
my_string = "And in the end you lost the game"

search = "you lost"
if search in my_string:
    search_index = my_string.index(search)
    after_search = search_index+len(search)

-Mek

Also: If I lock your thread and tell you to go experiment do not just start a new thread.
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Artificial intelligence program..not really just simple

Postby el_manu » Wed Sep 04, 2013 9:20 am

i don't think i need anymore help..thx
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby el_manu » Wed Sep 04, 2013 10:24 am

EDIT: I can't get this to work. CAn someone recommend using anotther way. do i use find??? how do i get words after "i am"

when i look at my code i think it should work but it just prints out a few blank spaces, i am prepared to scrap this attempt for a easier way

Code: Select all
a = []
Very = 0
gay = input("Hello, my name is Eliza. What would you like to talk about? ")
while gay != "go away":
   search = "i am"
   if "feel" in gay:
      gay = input("""Do you often feel that way?
""")
   elif "i am" in gay:
       search_index = gay.index(search)
       after_search = search_index+len(search)
       Annoying = Very + after_search
       I_HATE = gay[Annoying]
       while I_HATE != "":
           Very = Very + 1 
           break
       life = gay[after_search:Annoying]
       print("How long have you been", life)   
       gay = input("testing")
   else:
     gay = input("""Please go on.
""")
if gay == "go away":
    print("I hope I have helped you!")
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby el_manu » Wed Sep 04, 2013 12:28 pm

can you delete the above post and put this instead...

i don't wanna do it this way...any ideas on how to print the word after "i am"???
right now i have'nt written the full code ye,t I will do that later.....I wanna get the "i am" thing working


like to print the word after i am...i know it is not been converted to lower case i will do that after i figure this out... how can we find the index to the word after am...why is my code failing...doesn't it look like it should work

thx

refer to the code below:

Code: Select all
 a = []
    count = 1
    the_input = input("Hello, my name is Eliza. What would you like to talk about? ")
    while the_input != "go away":
       i_am_search = "i am"
       if "feel" in the_input:
          the_input = input("""Do you often feel that way?
    """)
       elif "i am" in the_input:
          search_index = the_input.index(i_am_search)
          after_search = search_index+len(i_am_search)
          The_index_of_after_am = after_search
          to_be_in_index = count + The_index_of_after_am
          Start_of_word_after_am = the_input[to_be_in_index]
          while Start_of_word_after_am != "":
             count = count + 1
             Start_of_word_after_am = the_input[to_be_in_index]
           
          The_actual_word_after_am = the_input[The_index_of_after_am:count]
          print("How long have you been", The_actual_word_after_am)   
          the_input = input("")
       else:
          the_input = input("""Please go on.
    """)
    if the_input == "go away":
       print("I hope I have helped you!")


why doesn't the while loop worK???
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby el_manu » Wed Sep 04, 2013 1:51 pm

Should i index in a list or not index at all
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby metulburr » Wed Sep 04, 2013 1:59 pm

1)
and do you really have to lock the editing from my first posts???

i wouldn't get rid of it and you could just ban my ip and my account from the webpage if I did

This is a decision from all of the mods/admin. It is forum etiquette to not post multiple times regarding the same situation and to be respectful to to each party. Mekire should not be having to merge multiple threads of yours because each thread you create is too similiar to the previous thread. If you cannot respect the rules, your privileges will be suspended until a time we see fit.

2) Your tone of your posts decides how many responses you get and the priviledges you maintain . You need to check out How to post good questions.

3) Stay calm. Slow down. Learning a new language takes time, even if you are already familiar with another language, let alone if not. Sometimes you just have to set something aside and say, that is too complex or complicated for me right now, its out of my field. Or sometimes you just need to take a break.

4) Your variable name choice is unacceptable. Names like:
gay
Annoying
I_HATE
Very

Aside from the fact that we can see your stress in the variable names you chose, you need to make meaningful variable names. Names that describe what the content is in each one.

5) Everyone is here to help one another. Like Mekire said, no one is being paid to help you. We all do it because we love Python and programming in general.

6) The forum is to help everyone, not just you. That is the reason we do not delete posts. This is not reddit. We do not delete all the discussion and leave the answer post. This is a forum with discussion. People want to see not only the result but how it was obtained too. Each post provides information that supports the final result. When someone with the same problem google searches similar questions, they will come here, read the thread, and may help them also.

7) Your way, may not be the best way. You need to be open minded when programming. If the assistance is unfamilar, take what is unfamilar and make it known.

If all of the above was followed, i would be spending the time i have available on answering your questions instead of re-posting the forums rules and expectations.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby metulburr » Wed Sep 04, 2013 2:46 pm

if "i am <something>" is in the input, then the program should respond" How long have you been <something>?."

Otherwise, if none of these rules apply, Eliza should just print Please go on.


i don't know how to print for example the word after i am???


assuming the user input "i am" as the start of the response:
Code: Select all
while True:
   res = input("Hello, my name is Eliza. What would you like to talk about? ")

   res_lower = res.lower()

   if res_lower.startswith('i am'):
      key = res.split()[2]
      print('How long have you been {}?'.format(key))
   else:
      print('Please go on')


however, a method to grab if "i am" is anywhere in the string:
Code: Select all
while True:
   res = input("Hello, my name is Eliza. What would you like to talk about? ")
   #res = 'hello, i am tired now'
   res_lower = res.lower()
   search = 'i am'
   if search in res_lower:
      ind = res.find(search) + len(search) + 1 #get index of end of "i am "
      section = res[ind:] #splice input
      key = section.split()[0] #split input into list, get first index
      print('How long have you been {}?'.format(key))
   else:
     print('Please go on')


You should familarize yourself in splicing, string methods, list methods. This is what you should do before attepting a project. Just play around with it in the interpreter with gibberish strings and lists. See what you can and cannot get away with. Google search unkown methods. Read the forum tutorials.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby el_manu » Fri Sep 06, 2013 9:31 am

Code: Select all
print("Hello, my name is Eliza. What would you like to talk about?")
the_input = input('')
input_lower = the_input.lower()
i_am_search = 'i am'
input_lower = input_lower.replace("?", '')
while input_lower != 'go away':
   if 'feel' in input_lower:
      print("Do you often feel that way?")
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
   elif i_am_search in input_lower:
      index = the_input.find(i_am_search) + len(i_am_search) + 1
      section = the_input[index:]
      key = section.split()[0]
      print('How long have you been', key + '?')
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
   elif 'you' in input_lower and 'me' in input_lower:
      the_index = input_lower.find('you')
      the_second_index = input_lower.find('me')
      the_section = the_input[the_index + 4:the_second_index]
      print('What makes you think I', the_section+'you?')
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
   else:
      print("Please go on")
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
if input_lower == "go away":
   print("I hope I have helped you!")


how can I get it so that if the input is something like:

why is pi am david a good book
or
i ampii david

it prints out please go on
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby metulburr » Fri Sep 06, 2013 10:38 am

why is pi am david a good book
or
i ampii david


for the first one i would check if it starts with "i am" and if not, that there is a space before i am, " i am", which would cancal that out. And the second one i would put a space in the search "i am ", which would cancel that out.

However it looks like your copy and pasting each if statement with a small variance in the if statement, which is a sign of bad coding. Start putting the code into classes, or at least functions to avoid such behavior.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby el_manu » Fri Sep 06, 2013 11:05 am

metulburr wrote:
for the first one i would check if it starts with "i am" and if not, that there is a space before i am, " i am", which would cancal that out. And the second one i would put a space in the search "i am ", which would cancel that out.

However it looks like your copy and pasting each if statement with a small variance in the if statement, which is a sign of bad coding. Start putting the code into classes, or at least functions to avoid such behavior.


what does classes mean..
And the second one i would put a space in the search "i am ", which would cancel that out.

what does this mean
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby metulburr » Fri Sep 06, 2013 11:23 am

classes

Code: Select all
def default():
    return 'Please go on'
   
while True:
    res = input("Hello, my name is Eliza. What would you like to talk about? ")
    res_lower = res.lower()
    res_split = res_lower.split()
    search = 'i am '

    if search in res_lower:
        if 'i' in res_split and 'am' in res_split:
            ind = res.find(search) + len(search)
            section = res[ind:]
            key = section.split()[0]
            print('How long have you been {}?'.format(key))
        else:
            print(default())
    else:
        print(default())


You can break this code if you do it right. But you can also fix it by adding more if conditions to counter the break. You can also check out the re module using regular expressions if you are going to delve deeper into it, to make it more precise. But i suggest you do not as, if you cannot do it with if statments and builtins, you shouldn't be doing it with regexp. You can also easily read if conditions whereas complex regular expressions look like greek

but ill show an example re test:
Code: Select all
import re

while True:
    res = input("Hello, my name is Eliza. What would you like to talk about? ")
    res_lower = res.lower()
    m = re.search(r'\bi\b \bam\b', res_lower)
    if m:
        search = 'i am'
        ind = res_lower.find(search) + len(search)
        section = res[ind:]
        key = section.split()[0]
        print('How long have you been {}?'.format(key))
    else:
        print('Please go on')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby el_manu » Fri Sep 06, 2013 1:04 pm

Code: Select all
print("Hello, my name is Eliza. What would you like to talk about?")
the_input = input('')
input_lower = the_input.lower()
i_am_search = 'i am'
input_lower = input_lower.replace("?", '')
while input_lower != 'go away':
   if 'feel' in input_lower:
      print("Do you often feel that way?")
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
   elif i_am_search in input_lower or 'I am' in input_lower or 'I Am' in input_lower or 'I AM' in input_lower or 'I aM' in input_lower or 'i AM' in input_lower or 'i Am' in input_lower or 'i aM' :
      index = the_input.find(i_am_search) + len(i_am_search) + 1
      if input_lower[index - 6] == ' ' and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ':
         section = input_lower[index:]
         key = section.split()[0]
         print('How long have you been', key + '?')
         the_input = input('')
         input_lower = the_input.lower()
         input_lower = input_lower.replace("?", '')
      elif input_lower.startswith('i am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I Am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I AM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I aM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('i AM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('i Am') or input_lower.startswith('i aM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ': #all of this is meant to be on 1 line
         section = input_lower[index:]
         key = section.split()[0]
         print('How long have you been', key + '?')
         the_input = input('')
         input_lower = the_input.lower()
         input_lower = input_lower.replace("?", '')
      else:
         print("Please go on")
         the_input = input('')
         input_lower = the_input.lower()
         input_lower = input_lower.replace("?", '')
       
   elif 'you' in input_lower and 'me' in input_lower:
      the_index = input_lower.find('you')
      the_second_index = input_lower.find('me')
      the_section = the_input[the_index + 4:the_second_index]
      print('What makes you think I', the_section+'you?')
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
   else:
      print("Please go on")
      the_input = input('')
      input_lower = the_input.lower()
      input_lower = input_lower.replace("?", '')
if input_lower == "go away":
   print("I hope I have helped you!")


my code mostly works but I think I should take your advice and use classes.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby Mekire » Fri Sep 06, 2013 2:09 pm

Any time you have a line of code that looks even remotely like this:
Code: Select all
elif input_lower.startswith('i am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I Am') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I AM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('I aM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('i AM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ' or input_lower.startswith('i Am') or input_lower.startswith('i aM') and input_lower[index - 4] == ' ' and input_lower[index - 3] == 'a' and input_lower[index - 2] == 'm' and input_lower[index - 1] == ' ':
you are doing it wrong.

Honestly I'm not even sure what you are trying to accomplish. All you should care about is if your input (already turned to lowercase) starts with "i am ". That's it.
Code: Select all
elif my_input.startswith('i am '):
    print('How long have you been {}'.format(my_input[len("i am "):]))

You need to learn the basics of fundamental data types and the methods associated with them. After you have done that, learn to write basic functions. You are not ready to get into classes yet.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Artificial intelligence program..not really just simple

Postby metulburr » Fri Sep 06, 2013 2:33 pm

i dont think you understand the concept of str.lower(). By converting the string to lowercase, it allows to check the characters against lowercase, and not have ot worry about uppercase. for example:
Code: Select all
input_lower.startswith('I Am')

this is not going to ever execute as input_lower is already lower, so whats the point of checking it against something thats uppercase.

I actually understand that long line. But everything after the first "or" is not going to execute for the same reason, input_lower is already lowered. Regardless, a line should never be that long. Code needs to be readable. Even if you had to check that many conditions, which you dont, there are better ways of doing that than making a huge line of a condition. See lists and the "in" operator.

Better understanding of the Python language will suit you. Understand the basics first, which will allow you options into better ways to program.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby el_manu » Sat Sep 07, 2013 12:51 am

for some reason when i just had

Code: Select all
if input_lower.startswith('i am')


it didn't work??

that is why i did that, but I don't want that long line. Is thre something ive missed
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Artificial intelligence program..not really just simple

Postby metulburr » Sat Sep 07, 2013 2:31 am

Code: Select all
if input_lower.startswith('i am')

this will execute if input_lower starts with "i am", anything else and it will not execute.

it didn't work??

your going to have to define what you mean better.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Artificial intelligence program..not really just simple

Postby el_manu » Sat Sep 07, 2013 6:39 am

like if i write

Code: Select all
why is I aM good


it says

Code: Select all
please go on
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Next

Return to General Coding Help

Who is online

Users browsing this forum: alex55555, Yoriz and 2 guests