Home > front end >  Looping through substrings of a string against a dictionary
Looping through substrings of a string against a dictionary

Time:09-13

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.

The routing to be used is shown below:

Keyword Routed To
urgent Pry Ority
Linux Pebble Penguin
Windows Kurt Ains
Mac Don Alds
Office Dusty Places
Zoom Don Alds
Password doesn't work Pebble Penguin
Password incorrect Pebble Penguin
Password reset Pebble Penguin
Locked out Pebble Penguin
New password Pebble Penguin
Routed to next available technician

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:

[Formatting of output][1]

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

#    List of all keywords used for routing to techs

keywordList = [
    "urgent",
    "Linux",
    "Windows",
    "Mac",
    "Office",
    "Zoom"
    "Password doesn't work",
    "Password incorrect",
    "Password reset",
    "Locked out",
    "New password"]

#    Dictionary to relate keywords (keys) to techs (values)
techRouting = {
    "urgent": "Pry Ority",
    "Linux": "Pebble Penguin",
    "Windows": "Kurt Ains",
    "Mac": "Don Alds",
    "Office": "Dusty Places",
    "Zoom": "Don Alds",
    "Password doesn't work": "Pebble Penguin",
    "Password incorrect": "Pebble Penguin",
    "Password reset": "Pebble Penguin",
    "Locked out": "Pebble Penguin",
    "New password": "Pebble Penguin"}

#    Prompt input of ticket text
ticketText = input("Enter the text for the ticket: ").lower()

#    Split text into individual words for checking - this is also causing the issue
ticketText = ticketText.split(" ")

#    Loop to check for keywords within the ticket text - only works for the single word keywords, not multiple word keywords

for t in ticketText:
    for k in keywordList:
        if t != k:
            c = ""
            pass
        elif t == k:
            c = str(k)
            break

if (c == ""):
    print("Keyword is: None\n\nRouted to: Next Available Agent")
else:
    print("Keyword is: "   c)
    print("Routed to:"   techRouting.get(c))

EDITED CODE

I'm still not able to "find" (a clue) the keywords that are phrases. Also it is case sensitive - I need to make this case insensitive. I have tried using regular expressions, but it has so far caused more problems than it has solved.

techRouting = {
    "urgent": "Pry Ority",
    "Linux": "Pebble Penguin",
    "Windows": "Kurt Ains",
    "Mac": "Don Alds",
    "Office": "Dusty Places",
    "Zoom": "Don Alds",
    "Password doesn't work": "Pebble Penguin",
    "Password incorrect": "Pebble Penguin",
    "Password reset": "Pebble Penguin",
    "Locked out": "Pebble Penguin",
    "New password": "Pebble Penguin"}

ticket_text = input("Enter the text for the ticket: ").lower()

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

if (c == ""):
    print("Keyword is: None\nRouted to: Next Available Agent")
else:
    print("Keyword is: "   c)
    print("Routed to: "   str(techRouting.get(c)))

CodePudding user response:

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

CodePudding user response:

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