Scanning for two word phrase in Python dictionary -


i trying use python dictionary object translate input string other words or phrases. having success translating single words input, can't seem figure out how translate multi-word phrases.

example:

sentence = input("please enter sentence: ") mydict = {"hello": "hi","mean adult":"grumpy elder", ...ect} 

how can return hi grumpy elder if user enters hello mean adult input?

the same way would.

translation = mydict['fast car'] 

a solution particular problem following, maxlen maximum number of words in single phrase in dictionary.

translation = []  words = sentence.split(' ') maxlen = 3 index = 0  while index < len(words):     in range(maxlen, 0, -1):         phrase = ' '.join(words[index:index+i])         if phrase in mydict:             translation.append(mydict[phrase])             index +=             break     else:         translation.append(words[index])         index += 1  print ' '.join(translation) 

given sentence hello nice fast car, outputs hi sweet quick ride


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -