search problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LolaT
    New Member
    • Jul 2007
    • 22

    search problem

    [code=python]
    >>>
    >>> f=open("dict.tx t","r")
    >>> word='zygote'
    >>> for line in f:
    if word in line:
    print "Your word is in the dictionary"


    Your word is in the dictionary
    Your word is in the dictionary
    Your word is in the dictionary
    Your word is in the dictionary[/code]


    Python displays that in my dictionary, there are 4 words that have 'zygote' in them, although I'm simply looking if the word 'zygote' itself is in the dictionary...an y suggestions on how I can fix my code?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by LolaT
    [code=python]
    >>>
    >>> f=open("dict.tx t","r")
    >>> word='zygote'
    >>> for line in f:
    if word in line:
    print "Your word is in the dictionary"


    Your word is in the dictionary
    Your word is in the dictionary
    Your word is in the dictionary
    Your word is in the dictionary[/code]


    Python displays that in my dictionary, there are 4 words that have 'zygote' in them, although I'm simply looking if the word 'zygote' itself is in the dictionary...an y suggestions on how I can fix my code?
    Using re:[code=Python]>>> s = 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
    >>> s
    'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
    >>> 'zygote' in s
    True
    >>> import re
    >>> patt = re.compile('[a-z]+', re.IGNORECASE)
    >>> 'zygote' in patt.findall(s)
    True
    >>> patt.findall(s) .index('zygote' )
    12
    >>> patt.findall(s)
    ['Dfhrzygote', 'hrhzygotekgf', 'uozygotehjjy', 'this', 'is', 'a', 'test', 'to', 'see', 'if', 'the', 'word', 'zygote', 'is', 'in', 'this', 'string']
    >>> s1 = 'Dfhrzygote hrhzygotekgf uozygotehjjy'
    >>> 'zygote' in patt.findall(s1 )
    False
    >>> [/code]

    Comment

    • LolaT
      New Member
      • Jul 2007
      • 22

      #3
      Originally posted by bvdet
      Using re:[code=Python]>>> s = 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
      >>> s
      'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
      >>> 'zygote' in s
      True
      >>> import re
      >>> patt = re.compile('[a-z]+', re.IGNORECASE)
      >>> 'zygote' in patt.findall(s)
      True
      >>> patt.findall(s) .index('zygote' )
      12
      >>> patt.findall(s)
      ['Dfhrzygote', 'hrhzygotekgf', 'uozygotehjjy', 'this', 'is', 'a', 'test', 'to', 'see', 'if', 'the', 'word', 'zygote', 'is', 'in', 'this', 'string']
      >>> s1 = 'Dfhrzygote hrhzygotekgf uozygotehjjy'
      >>> 'zygote' in patt.findall(s1 )
      False
      >>> [/code]
      [code=python]>>> f=open("dict.tx t","r")
      >>> text=f.read()
      >>> 'zygote' in text
      True
      >>> import re
      >>> patt=re.compile ('[a-z]+',re.IGNORECAS E)
      >>> 'zygote' in patt.findall(te xt)
      True
      >>> patt.findall(te xt).index('zygo te')
      113789
      >>> patt.findall(te xt)[/code]

      after that last line, it just shuts down...

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        I had a similar problem when I was working on a word unscrambler. A word like 'anagram' would print 3 times because the letter 'a' is in it 3 times. This was solved using 'set()'.
        To test yours I put the following words into a file and named it words.txt:
        zygote
        anagram
        myzygote
        zygoter
        yourzygote

        I ran them from the command line and got the same results as you with zygote, but just once with anagram. I made some changes to my unscrambler code and then ran the code. It prints this:
        'zygote is in the dictionary' <----only prints once

        Try the code below, it may work for you. You may have to make a few changes to accomodate your files.

        Code:
        import string
        def anagrams(s):
            if s == "":
                return [s]
            else:
                ans = set()
                for an in anagrams(s[1:]):
                    for pos in range(len(an)+1):
                        ans.add(an[:pos]+s[0]+an[pos:])
                return ans
            
        def dictionary(wordlist):
            dict = {}
            infile = open(wordlist, "r")
            for line in infile:
                word = line.split("\n")[0]
                dict[word] = 1
            infile.close()
            return dict
        
        def main():
            anagram = raw_input("Please enter words: ")
            wordLst = anagram.split(None)
            diction = dictionary("words.txt") # Change this to "dict.txt" 
            solution = ""
            for word in wordLst:
                anaLst = anagrams(word)
                for ana in anaLst:
                    if diction.has_key(ana):
                        diction[ana] = word
                        solution += '%s' % (ana)
                        print " %s is in the dictionary" % solution
        main()
        Last edited by Thekid; Aug 2 '07, 08:09 PM. Reason: Put the suggestion in for ("dict.txt") instead of my file ("words.txt")

        Comment

        • LolaT
          New Member
          • Jul 2007
          • 22

          #5
          Originally posted by Thekid
          I had a similar problem when I was working on a word unscrambler. A word like 'anagram' would print 3 times because the letter 'a' is in it 3 times. This was solved using 'set()'.
          To test yours I put the following words into a file and named it words.txt:
          zygote
          anagram
          myzygote
          zygoter
          yourzygote

          I ran them from the command line and got the same results as you with zygote, but just once with anagram. I made some changes to my unscrambler code and then ran the code. It prints this:
          'zygote is in the dictionary' <----only prints once

          Try the code below, it may work for you. You may have to make a few changes to accomodate your files.

          Code:
          import string
          def anagrams(s):
              if s == "":
                  return [s]
              else:
                  ans = set()
                  for an in anagrams(s[1:]):
                      for pos in range(len(an)+1):
                          ans.add(an[:pos]+s[0]+an[pos:])
                  return ans
              
          def dictionary(wordlist):
              dict = {}
              infile = open(wordlist, "r")
              for line in infile:
                  word = line.split("\n")[0]
                  dict[word] = 1
              infile.close()
              return dict
          
          def main():
              anagram = raw_input("Please enter words: ")
              wordLst = anagram.split(None)
              diction = dictionary("words.txt") # Change this to "dict.txt" 
              solution = ""
              for word in wordLst:
                  anaLst = anagrams(word)
                  for ana in anaLst:
                      if diction.has_key(ana):
                          diction[ana] = word
                          solution += '%s' % (ana)
                          print " %s is in the dictionary" % solution
          main()
          Thank you for your help!
          However, when I implement the code that you've suggested, I don't get a printed statement telling me the word is in my dictionary.
          I've tried to walk myself through the code and can't seem to figure out what's going on

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #6
            Are you running it with IDLE or as a saved file? If you have IDLE open, click on
            'File' then 'New Window'. Copy the code into the new window and then click 'File' , 'save as' and save it into your python folder (ex. wordSearch.py).
            Once it's saved you can hit F5 and should run.
            If I open wordSearch.py and hit F5 IDLE pops open and reads:
            Please enter words: <---here of course you type your word
            Then it prints out:
            zygote is in the dictionary

            The code compares the number of letters you enter to the number of letters in the dictionary so you won't get words that contain zygote in them, just the ones that match the exact number of letters.

            You can change :Please enter words: to whatever you'd like it to read, as well as: zygote is in the dictionary, to whatever you'd prefer the output to read. Just change the words in between the " " in the code to suit your needs.

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              Originally posted by LolaT
              [code=python]
              >>>
              >>> f=open("dict.tx t","r")
              >>> word='zygote'
              >>> for line in f:
              if word in line:
              print "Your word is in the dictionary"


              Your word is in the dictionary
              Your word is in the dictionary
              Your word is in the dictionary
              Your word is in the dictionary[/code]


              Python displays that in my dictionary, there are 4 words that have 'zygote' in them, although I'm simply looking if the word 'zygote' itself is in the dictionary...an y suggestions on how I can fix my code?
              All you need to do is this:
              [code=python]
              >>> f = open("dict.txt" )
              >>> word = 'zygote'
              >>> words = [line[:-1] for line in f]
              >>> if word in words:
              ... print "Your word is in the dictionary"
              [/code]
              Close to your original but this one checks each line to EQUAL the word, yours checks wether the word is IN the line.

              Comment

              • Thekid
                New Member
                • Feb 2007
                • 145

                #8
                Originally posted by ilikepython
                All you need to do is this:
                [code=python]
                >>> f = open("dict.txt" )
                >>> word = 'zygote'
                >>> words = [line[:-1] for line in f]
                >>> if word in words:
                ... print "Your word is in the dictionary"
                [/code]
                Close to your original but this one checks each line to EQUAL the word, yours checks wether the word is IN the line.
                Now that's impressive :)

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by LolaT
                  [code=python]>>> f=open("dict.tx t","r")
                  >>> text=f.read()
                  >>> 'zygote' in text
                  True
                  >>> import re
                  >>> patt=re.compile ('[a-z]+',re.IGNORECAS E)
                  >>> 'zygote' in patt.findall(te xt)
                  True
                  >>> patt.findall(te xt).index('zygo te')
                  113789
                  >>> patt.findall(te xt)[/code]

                  after that last line, it just shuts down...
                  If the index of 'zygote' is 113789, then you must have a lot of words in your dictionary! When you type in 'patt.findall(t ext)' at your interactive prompt, the IDE may not be able to handle that much data at once. I have had a similar problem in Pythonwin.

                  Comment

                  • LolaT
                    New Member
                    • Jul 2007
                    • 22

                    #10
                    Thank you to everyone for their suggestions and comments, the code seems to work now...
                    I'll definitely post up any other problems should I have them :)

                    Comment

                    • BambiL
                      New Member
                      • Jul 2007
                      • 2

                      #11
                      I'm having a similar problem with a program I'm creating.
                      I'm trying to spell check also, however I'm trying to spell check the words of an entire file.
                      I'm trying to use binary search to figure this out, but I'm quite unclear on how to do this.
                      Any suggestions will be appreciated.

                      Comment

                      • LolaT
                        New Member
                        • Jul 2007
                        • 22

                        #12
                        I've encountered yet another problem. I'm trying to get my program to check the dictionary, and then if it isn't in the dictionary, the user dictionary is checked for the word, however my code won't work.


                        [code=python]word=str(raw_in put("Enter a word: "))
                        words=[line[:-1] for line in f]

                        if word in words:
                        print "The word '%s' is in the dictionary." %word
                        print

                        elif word not in words:
                        words2=[line[:-1] for line in userDict]
                        if word in words2:
                        print "The word '%s' is in the User dictionary." %word[/code]


                        I figured it would work putting it this way, but instead it just jumps back to the menu, because I've looped it so that unless the user chooses option 4 (which in my code is the exit option) it'll continue the program.

                        I'm sure the answer is relatively simple, yet I have no clue what I'm doing...
                        if anyone knows of any books or websites that may help me use Python I'd be really thankful. I tried out the Python documentation as well, but I'm not getting any answers.

                        Comment

                        • William Manley
                          New Member
                          • Mar 2007
                          • 56

                          #13
                          Originally posted by LolaT
                          I
                          [code=python]word=str(raw_in put("Enter a word: "))
                          words=[line[:-1] for line in f]

                          if word in words:
                          print "The word '%s' is in the dictionary." %word
                          print

                          elif word not in words:
                          words2=[line[:-1] for line in userDict]
                          if word in words2:
                          print "The word '%s' is in the User dictionary." %word[/code]
                          i'm not sure, but you can try changing the "elif word not in words:" to "else:"

                          sorry I can't be more help, not at home, and no Python on this computer.
                          Last edited by William Manley; Aug 4 '07, 09:31 PM. Reason: fixin quote tag

                          Comment

                          • LolaT
                            New Member
                            • Jul 2007
                            • 22

                            #14
                            [QUOTE=William Manley]
                            Originally posted by LolaT
                            I
                            [code=python]word=str(raw_in put("Enter a word: "))
                            words=[line[:-1] for line in f]

                            if word in words:
                            print "The word '%s' is in the dictionary." %word
                            print

                            elif word not in words:
                            words2=[line[:-1] for line in userDict]
                            if word in words2:
                            print "The word '%s' is in the User dictionary." %word[/code]


                            i'm not sure, but you can try changing the "elif word not in words:" to "else:"

                            sorry I can't be more help, not at home, and no Python on this computer.
                            thanks for the suggestion, however i have an else statement after that elif statement...my else statement tells the user that the word isn't in either dictionary and then asks them if they would like to add it to the user dictionary.

                            Comment

                            • bartonc
                              Recognized Expert Expert
                              • Sep 2006
                              • 6478

                              #15
                              Originally posted by LolaT
                              I've encountered yet another problem. I'm trying to get my program to check the dictionary, and then if it isn't in the dictionary, the user dictionary is checked for the word, however my code won't work.


                              [code=python]word=str(raw_in put("Enter a word: "))
                              words=[line[:-1] for line in f]

                              if word in words:
                              print "The word '%s' is in the dictionary." %word
                              print

                              elif word not in words:
                              words2=[line[:-1] for line in userDict]
                              if word in words2:
                              print "The word '%s' is in the User dictionary." %word[/code]


                              I figured it would work putting it this way, but instead it just jumps back to the menu, because I've looped it so that unless the user chooses option 4 (which in my code is the exit option) it'll continue the program.

                              I'm sure the answer is relatively simple, yet I have no clue what I'm doing...
                              if anyone knows of any books or websites that may help me use Python I'd be really thankful. I tried out the Python documentation as well, but I'm not getting any answers.
                              There's really no need to make a list for a string in order to test membership of a string:[CODE=python]
                              >>> myDict = "this\nis\nsome \ntext\nwith\no ne\nword\nper\n line"
                              >>> print myDict
                              this
                              is
                              some
                              text
                              with
                              one
                              word
                              per
                              line
                              >>> "text" in myDict
                              True
                              >>> [/CODE]

                              Comment

                              Working...