473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 9660
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

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

Similar topics

12
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
23
by: Yannick Patois | last post by:
Hi, Under some naming conditions of module files, it seems that python lost class static variables values. It seems only to append when importing a "badly" named module that itself import a...
0
by: richard.hubbell | last post by:
I am sure this is old news, the syntax of python is crazy to me. There I said it, I'm sure I'll get over it or around it. I was trying to make a whois script work and was unable. May be...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.