471,330 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

New to Python: Dictionary Search

Okay, here goes. I'm new to Python (and relatively new to programming/scripting in general - I understand basics, but complex concepts are still beyond my limited knowledge).

I am working on a project that is essentially a translation dictionary for conlangs , or constructed languages. It allows the user to enter English words and their translation in the conlang, to search for translations by English, and delete words from the dictionary.

Right now, I am working with Python 2.5 on Win32.

I am, naturally, using a Python dictionary {} as the method for doing this, as it makes the most sense. I am using the English word as the key, and the translated word as the value.

I want to include a reverse translation - that is, to go from the constructed language back to English. I have been using this function I created to do it:

Expand|Select|Wrap|Line Numbers
  1. def revSearch(d):
  2.     target = raw_input("Enter the target language word to translate: ")
  3.         for key in d:
  4.             if target in d[key]:
  5.                 print "Possible translation for %s is: %s" % (target, key)
  6.             print "Press ENTER to continue"
  7.             raw_input()
There is a problem, however. If there exists a word in the conlang that is only a few letters long, it will pull up EVERY key whose value contains that sequence of letters, resulting in useless output.

I will use French as an example::

Expand|Select|Wrap|Line Numbers
  1. If the dictionary contains these values {'my' : 'mon', 'mister' : 'monsieur'}
  2. The program asks:
  3.  
  4. Enter the target language word to translate: mon
  5.  
  6. Output ->
  7. Possible translation for mon is: my
  8. Possible translation for mon is: mister
Which is not such a big deal with a small dictionary - the problem comes as the dictionary grows.

What would be ideal is for the search to respond only if it finds the *exact* value that I enter, and ignore the others. Or, if I'm going about it all wrong, and there's another method that would get me more precise output, I'd be happy to hear about that too. After 2 hours of fruitless web searching, I decided to come here and ask, hoping that a community might be able to come up with something. After all, isn't that the beauty of open-source? :D

Thanks in advance for any help you might be able to provide.
Jun 20 '07 #1
3 9536
bvdet
2,851 Expert Mod 2GB
Okay, here goes. I'm new to Python (and relatively new to programming/scripting in general - I understand basics, but complex concepts are still beyond my limited knowledge).

I am working on a project that is essentially a translation dictionary for conlangs , or constructed languages. It allows the user to enter English words and their translation in the conlang, to search for translations by English, and delete words from the dictionary.

Right now, I am working with Python 2.5 on Win32.

I am, naturally, using a Python dictionary {} as the method for doing this, as it makes the most sense. I am using the English word as the key, and the translated word as the value.

I want to include a reverse translation - that is, to go from the constructed language back to English. I have been using this function I created to do it:

Expand|Select|Wrap|Line Numbers
  1. def revSearch(d):
  2.     target = raw_input("Enter the target language word to translate: ")
  3.         for key in d:
  4.             if target in d[key]:
  5.                 print "Possible translation for %s is: %s" % (target, key)
  6.             print "Press ENTER to continue"
  7.             raw_input()
There is a problem, however. If there exists a word in the conlang that is only a few letters long, it will pull up EVERY key whose value contains that sequence of letters, resulting in useless output.

I will use French as an example::

Expand|Select|Wrap|Line Numbers
  1. If the dictionary contains these values {'my' : 'mon', 'mister' : 'monsieur'}
  2. The program asks:
  3.  
  4. Enter the target language word to translate: mon
  5.  
  6. Output ->
  7. Possible translation for mon is: my
  8. Possible translation for mon is: mister
Which is not such a big deal with a small dictionary - the problem comes as the dictionary grows.

What would be ideal is for the search to respond only if it finds the *exact* value that I enter, and ignore the others. Or, if I'm going about it all wrong, and there's another method that would get me more precise output, I'd be happy to hear about that too. After 2 hours of fruitless web searching, I decided to come here and ask, hoping that a community might be able to come up with something. After all, isn't that the beauty of open-source? :D

Thanks in advance for any help you might be able to provide.
You can create a reverse dictionary like this:
Expand|Select|Wrap|Line Numbers
  1. revDict = dict(zip(transDict.values(), transDict.keys()))
  2.  
  3. s = 'MON'.lower()
  4. if s in revDict:
  5.     print revDict[s]
  6.  
OR you can go for an exact match:
Expand|Select|Wrap|Line Numbers
  1. s = 'MON'
  2. for key in transDict:
  3.     if s.lower() == transDict[key]:
  4.         print transDict[key]
  5.         break
The 'break' ends the iteration after finding a match. HTH :)
Jun 20 '07 #2
You can create a reverse dictionary like this:
Expand|Select|Wrap|Line Numbers
  1. revDict = dict(zip(transDict.values(), transDict.keys()))
  2.  
  3. s = 'MON'.lower()
  4. if s in revDict:
  5.     print revDict[s]
  6.  
OR you can go for an exact match:
Expand|Select|Wrap|Line Numbers
  1. s = 'MON'
  2. for key in transDict:
  3.     if s.lower() == transDict[key]:
  4.         print transDict[key]
  5.         break
The 'break' ends the iteration after finding a match. HTH :)
You, sir, are my hero. It now functions excellently.

I chose to go with option 2 as sometimes, in a conlang, a single word might translate to 2 english words, so creating the reverse dictionary would destroy some of the necessary keys.

If I'm right, in this case all the lower() function is doing is giving us something to compare to the key, is that correct? I had already tried:

Expand|Select|Wrap|Line Numbers
  1. if s == d[key] 
...which just returned an error.

Thank you again very, very much!
Jun 20 '07 #3
bvdet
2,851 Expert Mod 2GB
You, sir, are my hero. It now functions excellently.

I chose to go with option 2 as sometimes, in a conlang, a single word might translate to 2 english words, so creating the reverse dictionary would destroy some of the necessary keys.

If I'm right, in this case all the lower() function is doing is giving us something to compare to the key, is that correct? I had already tried:

Expand|Select|Wrap|Line Numbers
  1. if s == d[key] 
...which just returned an error.

Thank you again very, very much!
You are welcome. The string.lower() method is used in case the user capitalizes the word or types the word in upper case.
Jun 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Yannick Patois | last post: by
reply views Thread by richard.hubbell | last post: by
10 posts views Thread by Andrew Dalke | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.