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: - def revSearch(d):
-
target = raw_input("Enter the target language word to translate: ")
-
for key in d:
-
if target in d[key]:
-
print "Possible translation for %s is: %s" % (target, key)
-
print "Press ENTER to continue"
-
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:: - If the dictionary contains these values {'my' : 'mon', 'mister' : 'monsieur'}
-
The program asks:
-
-
Enter the target language word to translate: mon
-
-
Output ->
-
Possible translation for mon is: my
-
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.
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: - def revSearch(d):
-
target = raw_input("Enter the target language word to translate: ")
-
for key in d:
-
if target in d[key]:
-
print "Possible translation for %s is: %s" % (target, key)
-
print "Press ENTER to continue"
-
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:: - If the dictionary contains these values {'my' : 'mon', 'mister' : 'monsieur'}
-
The program asks:
-
-
Enter the target language word to translate: mon
-
-
Output ->
-
Possible translation for mon is: my
-
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: - revDict = dict(zip(transDict.values(), transDict.keys()))
-
-
s = 'MON'.lower()
-
if s in revDict:
-
print revDict[s]
-
OR you can go for an exact match: - s = 'MON'
-
for key in transDict:
-
if s.lower() == transDict[key]:
-
print transDict[key]
-
break
The 'break' ends the iteration after finding a match. HTH :)
You can create a reverse dictionary like this: - revDict = dict(zip(transDict.values(), transDict.keys()))
-
-
s = 'MON'.lower()
-
if s in revDict:
-
print revDict[s]
-
OR you can go for an exact match: - s = 'MON'
-
for key in transDict:
-
if s.lower() == transDict[key]:
-
print transDict[key]
-
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:
...which just returned an error.
Thank you again very, very much!
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:
...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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |