473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to Python: Dictionary Search

2 New Member
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 9672
bvdet
2,851 Recognized Expert Moderator Specialist
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
galadhad
2 New Member
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 Recognized Expert Moderator Specialist
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
2826
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 Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go...
23
2139
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 module with a static variable in it (looks complex, but see the example below, pretty simple): First a working example: <<<< file: aTest.py
0
1429
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 someone with lint-like eyes can tell what's wrong. Using xemacs I had hoped that python mode would do more for syntax problems, maybe I'm just not using...
10
3678
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. Andrew dalke@dalkescientific.com
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.