3

Hi I'm trying to input a string and then have the string split into individual words. The unique words that are in the string and that are also in the dictionary keys of "contents" retrieve the corresponding values from the dictionary "files".

How do I split the input string to check individual words against the dictionary "concept" keys and if possible return the words in the string, not the dictionary keys?

I tried to split the string into a list and then pass the list values directly into the dictionary but I got lost real quick (those are the variables commented out at the top. Any help is appreciated. Thank you

def concept(word):

# convert var(word) to list
#my_string_list=[str(i) for i in word]

# join list(my_string_list) back to string
#mystring = ''.join(my_string_list)

# use this to list python files
files    = {1:"file0001.txt",
            2:"file0002.txt",
            3:"file0003.txt",
            4:"file0004.txt",
            5:"file0005.txt",
            6:"file0006.txt",
            7:"file0007.txt",    
            8:"file0008.txt",
            9:"file0009.txt"}

# change keys to searchable simple keyword phrases. 
concepts = {'GAMES':[1,2,4,3,3],
            'BLACKJACK':[5,3,5,3,5],
            'MACHINE':[4,9,9,9,4],
            'DATABASE':[5,3,3,3,5],
            'LEARNING':[4,9,4,9,4]}

# convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
if word.upper() not in concepts:
    print("{}: Not Found in Database" .format(word)) not in concepts
    return

# for matching keys in dict 'concept' list values in dict 'files'
for pattern in concepts[word.upper()]:
    print(files[pattern])


# return input box at end of query        
while True:
    concept(input("Enter Concept Idea: "))
    print("\n")
Agla
  • 49
  • 6

1 Answers1

3

Assuming the input is a list of words separated by spaces you could do:

def concept(phrase):

    words = phrase.split()

    # use this to list python files
    files = {1: "file0001.txt",
             2: "file0002.txt",
             3: "file0003.txt",
             4: "file0004.txt",
             5: "file0005.txt",
             6: "file0006.txt",
             7: "file0007.txt",
             8: "file0008.txt",
             9: "file0009.txt"}

    # change keys to searchable simple keyword phrases.
    concepts = {'GAMES': [1, 2, 4, 3, 3],
                'BLACKJACK': [5, 3, 5, 3, 5],
                'MACHINE': [4, 9, 9, 9, 4],
                'DATABASE': [5, 3, 3, 3, 5],
                'LEARNING': [4, 9, 4, 9, 4]}

    for word in words:
        # convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
        if word.upper() not in concepts:
            print("{}: Not Found in Database".format(word))
        else:
            # for matching keys in dict 'concept' list values in dict 'files'
            for pattern in concepts[word.upper()]:
                print(files[pattern])

concept("games blackjack foo")

Output

file0001.txt
file0002.txt
file0004.txt
file0003.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
foo: Not Found in Database

The line words = phrase.split() split the string phrase on spaces. To check if a word is in the dictionary you need to do it one at the time, hence the loop for word in words iterating over the words of phrase.

Further

  1. How can I check if a key exists in a dictionary?
  2. Split a string by a delimiter in python
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
  • Wow, you were so much faster than me and your answer is so much better too :) just one comment, might not be applicable though, if word.upper() not in concepts.keys(): is better suitable for the requirement you think? – Arka Mallick Dec 06 '18 at 01:47
  • I believe is most *pythonic* just to do if word.upper() not in concepts. – Dani Mesejo Dec 06 '18 at 01:48
  • Thank you for the clarification. You are right, i was under wrong impression. +1 – Arka Mallick Dec 06 '18 at 01:54
  • Thank you that's very very close. Is there I way to return more than one word that's not listed in the dictionary. For example, if the input was "Today games are blackjack". Can it return "today" and "are": Not Found in Database. I can't seem to make it iterate and check for all words not in the dictionary. – Agla Dec 06 '18 at 02:15
  • I just passed that string to the function concept and it printed: `Today: Not Found in Database file0001.txt file0002.txt file0004.txt file0003.txt file0003.txt are: Not Found in Database file0005.txt file0003.txt file0005.txt file0003.txt file0005.txt` – Dani Mesejo Dec 06 '18 at 02:16
  • The output above included both `Today` and `are` – Dani Mesejo Dec 06 '18 at 02:20
  • Sorry I accidentally left the "return" statement in under the "If" condition when I was editing. Why would that make a difference? – Agla Dec 06 '18 at 02:27
  • Yes it would, in fact that could cause only to print until the first word no in concepts – Dani Mesejo Dec 06 '18 at 02:29