New to Python: Dictionary Search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • galadhad
    New Member
    • Jun 2007
    • 2

    #1

    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:

    Code:
    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::

    Code:
    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by galadhad
    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:

    Code:
    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::

    Code:
    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:[code=Python]revDict = dict(zip(transD ict.values(), transDict.keys( )))

    s = 'MON'.lower()
    if s in revDict:
    print revDict[s]
    [/code]OR you can go for an exact match:[code=Python]s = 'MON'
    for key in transDict:
    if s.lower() == transDict[key]:
    print transDict[key]
    break[/code]The 'break' ends the iteration after finding a match. HTH :)

    Comment

    • galadhad
      New Member
      • Jun 2007
      • 2

      #3
      Originally posted by bvdet
      You can create a reverse dictionary like this:[code=Python]revDict = dict(zip(transD ict.values(), transDict.keys( )))

      s = 'MON'.lower()
      if s in revDict:
      print revDict[s]
      [/code]OR you can go for an exact match:[code=Python]s = 'MON'
      for key in transDict:
      if s.lower() == transDict[key]:
      print transDict[key]
      break[/code]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:

      Code:
      if s == d[key]
      ...which just returned an error.

      Thank you again very, very much!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by galadhad
        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:

        Code:
        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.

        Comment

        Working...