0

I'm trying to make an AI. The AI knows to say 'Hello' to 'hi' and to stop the program on 'bye', and if you say something it doesn't know it will ask you to define it. For example, if you say 'Hello' it will ask what that means. You type 'hi' and from then on when you say 'Hello' it will say 'Hello' back. I store everything in a list called knowledge. It works like this:

knowledge = [[term, definition], [term, definition], [term, definition]]

I am trying to add an edit function, where you type edit foo and it will ask for you to input a string, to change the definition of foo. However, I'm stuck. First, of course, I need to test if it already has a definition for foo. But I can't do that. I need to be able to do it regardless of the definition. In other languages, there is typeOf(). However type() doesn't seem to work. Here's what I have, but it doesn't work:

if [term, type(str)] in knowledge:

Can someone help?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sam Hill
  • 136
  • 1
  • 10

3 Answers3

0

As noted by tehhowch in the comments, a dictionary would be more appropriate as these are "key: value" pairs.

Using a dictionary...

knowledge = {'foo': 'foo def', 'bar': 'bar def', 'baz': 'baz def'}
searchTerm= 'foo'
searchTerm in knowledge

Out[1]: True

Storing knowledge as a list of lists fails because each item in knowledge is itself a list. Therefore, searching those lists for a string type (your term) fails. Instead, you could pull the terms out as a separate list and then check that one list for the term you're looking for.

knowledge = [["foo", "foo definition"], ["bar", "bar definition"], ["baz", "baz 
definition"]]

terms = [item[0] for item in knowledge]

searchTerm= "foo"
searchTerm in terms

Out[1]: True
Justin Wager
  • 323
  • 1
  • 6
0

You can use dictionary to store definitions rather than list of lists and python's isinstance function will help you check if term belongs to specific class or not. see below example:

knowledge = {'Hello':'greeting','Hi':'greeting','Bye':'good bye'}

term = "Hello"

if isinstance(term, str):
    if term in knowledge:
        print("Defination exist")
    else:
        print("Defination doesn't exist") 
else:
    print("Entered term is not string")
Chandella07
  • 2,089
  • 14
  • 22
0

As others have mentioned, Python would typically use a dict for this kind of associative array. You approach is analogous to a Lisp data structure called an Association List. These are less efficient than the hashtable structures used by dicts, but they still have some useful properties.

For example, if you look up a key by scanning through the pairs and getting the first one, this means that you can insert another pair with the same key at the front and it will shadow the old value. You don't have to remove it. This makes insertions fast (at least with Lisp-style linked lists). You can also "undo" this operation by deleting the new one, and the old one will then be found by the scanner.

Your check if [term, type(str)] in knowledge: could be made to work as

if [term, str] in ([term, type(definition)] for term, definition in knowledge):

This uses a generator expression to convert your term, definition pairs into term, type(definition) pairs on the fly.

gilch
  • 10,813
  • 1
  • 23
  • 28