473,729 Members | 2,235 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 9683
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
2844
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 with the idea that a "dict" variable can be looked at as (effectively) two parallel arrays?...
23
2153
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
1433
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 python mode correctly in xemacs??
10
3689
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
8913
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.