Looping through substrings of a string against a dictionary

Viewed 55

Please be gentle - I am new to this and English is not my first language. For a school project, the assignment is to create a program that allows the user to input text for a ticket and the result for who it should be routed to. Below is the code I have so far. It works fine for single word keywords, but not for keywords with two or more words. I don't necessarily need the answer as much as a push in the right direction. We are not supposed to use things we haven't learned yet so that limits me to some very basic functionality - lists, dictionaries, simple loops, etc. If multiple keywords are in the input, only the keyword found at the highest position in the table is used for routing.

The program needs to use keywords as case insensitive.

If there is no keyword present, it should be routed to "Next Available Technician"

The goal is to have output that is formatted exactly as such:

Also I think I am suppose to use the .find() function, but I am not sure how it would be implemented.

2 Answers

Loop over the techRouting dictionary. Check if the keyword is in the text, and if it is, use the corresponding routed to value.

c = ''
for keyword in techRouting:
    if keyword in ticket_text:
        c = keyword
        break

There is a couple of issues here:

  1. The way of extracting keywords from input

    ticketText = ticketText.split(" ")
    

    gives you a list, consisting of a parts, that had whitespace " " inbetween.

    In other words, ticketText breaks to a separate words, and none of words contains a space.

    In [61]: ticketText = "Password doesn't work"
        ...: ticketKeywords = ticketText.split(" ")
        ...: print(ticketKeywords) 
    

    results to

    ['Password', "doesn't", 'work'] 
    
  2. Keywords from list should also be lowercased before comparison. As well, as the input.

  3. You probably should not split the input into keywords at all. Just use keyword in inputText to check, if inputText contains the whole keyword as a substring.

Related