By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

search problem

P: 22
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> f=open("dict.txt","r")
  3. >>> word='zygote'
  4. >>> for line in f:
  5.     if word in line:
  6.         print "Your word is in the dictionary"
  7.  
  8.  
  9. Your word is in the dictionary
  10. Your word is in the dictionary
  11. Your word is in the dictionary
  12. Your word is in the dictionary

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...any suggestions on how I can fix my code?
Aug 1 '07 #1
Share this Question
Share on Google+
19 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> f=open("dict.txt","r")
  3. >>> word='zygote'
  4. >>> for line in f:
  5.     if word in line:
  6.         print "Your word is in the dictionary"
  7.  
  8.  
  9. Your word is in the dictionary
  10. Your word is in the dictionary
  11. Your word is in the dictionary
  12. Your word is in the dictionary

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...any suggestions on how I can fix my code?
Using re:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
  2. >>> s
  3. 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
  4. >>> 'zygote' in s
  5. True
  6. >>> import re
  7. >>> patt = re.compile('[a-z]+', re.IGNORECASE)
  8. >>> 'zygote' in patt.findall(s)
  9. True
  10. >>> patt.findall(s).index('zygote')
  11. 12
  12. >>> patt.findall(s)
  13. ['Dfhrzygote', 'hrhzygotekgf', 'uozygotehjjy', 'this', 'is', 'a', 'test', 'to', 'see', 'if', 'the', 'word', 'zygote', 'is', 'in', 'this', 'string']
  14. >>> s1 = 'Dfhrzygote hrhzygotekgf uozygotehjjy'
  15. >>> 'zygote' in patt.findall(s1)
  16. False
  17. >>> 
Aug 1 '07 #2

P: 22
Using re:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
  2. >>> s
  3. 'Dfhrzygote hrhzygotekgf uozygotehjjy - this is a test to see if the word "zygote" is in this string'
  4. >>> 'zygote' in s
  5. True
  6. >>> import re
  7. >>> patt = re.compile('[a-z]+', re.IGNORECASE)
  8. >>> 'zygote' in patt.findall(s)
  9. True
  10. >>> patt.findall(s).index('zygote')
  11. 12
  12. >>> patt.findall(s)
  13. ['Dfhrzygote', 'hrhzygotekgf', 'uozygotehjjy', 'this', 'is', 'a', 'test', 'to', 'see', 'if', 'the', 'word', 'zygote', 'is', 'in', 'this', 'string']
  14. >>> s1 = 'Dfhrzygote hrhzygotekgf uozygotehjjy'
  15. >>> 'zygote' in patt.findall(s1)
  16. False
  17. >>> 
Expand|Select|Wrap|Line Numbers
  1. >>> f=open("dict.txt","r")
  2. >>> text=f.read()
  3. >>> 'zygote' in text
  4. True
  5. >>> import re
  6. >>> patt=re.compile('[a-z]+',re.IGNORECASE)
  7. >>> 'zygote' in patt.findall(text)
  8. True
  9. >>> patt.findall(text).index('zygote')
  10. 113789
  11. >>> patt.findall(text)
after that last line, it just shuts down...
Aug 2 '07 #3

Thekid
100+
P: 145
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.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. def anagrams(s):
  3.     if s == "":
  4.         return [s]
  5.     else:
  6.         ans = set()
  7.         for an in anagrams(s[1:]):
  8.             for pos in range(len(an)+1):
  9.                 ans.add(an[:pos]+s[0]+an[pos:])
  10.         return ans
  11.  
  12. def dictionary(wordlist):
  13.     dict = {}
  14.     infile = open(wordlist, "r")
  15.     for line in infile:
  16.         word = line.split("\n")[0]
  17.         dict[word] = 1
  18.     infile.close()
  19.     return dict
  20.  
  21. def main():
  22.     anagram = raw_input("Please enter words: ")
  23.     wordLst = anagram.split(None)
  24.     diction = dictionary("words.txt") # Change this to "dict.txt" 
  25.     solution = ""
  26.     for word in wordLst:
  27.         anaLst = anagrams(word)
  28.         for ana in anaLst:
  29.             if diction.has_key(ana):
  30.                 diction[ana] = word
  31.                 solution += '%s' % (ana)
  32.                 print " %s is in the dictionary" % solution
  33. main()
  34.  
  35.  
Aug 2 '07 #4

P: 22
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.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. def anagrams(s):
  3.     if s == "":
  4.         return [s]
  5.     else:
  6.         ans = set()
  7.         for an in anagrams(s[1:]):
  8.             for pos in range(len(an)+1):
  9.                 ans.add(an[:pos]+s[0]+an[pos:])
  10.         return ans
  11.  
  12. def dictionary(wordlist):
  13.     dict = {}
  14.     infile = open(wordlist, "r")
  15.     for line in infile:
  16.         word = line.split("\n")[0]
  17.         dict[word] = 1
  18.     infile.close()
  19.     return dict
  20.  
  21. def main():
  22.     anagram = raw_input("Please enter words: ")
  23.     wordLst = anagram.split(None)
  24.     diction = dictionary("words.txt") # Change this to "dict.txt" 
  25.     solution = ""
  26.     for word in wordLst:
  27.         anaLst = anagrams(word)
  28.         for ana in anaLst:
  29.             if diction.has_key(ana):
  30.                 diction[ana] = word
  31.                 solution += '%s' % (ana)
  32.                 print " %s is in the dictionary" % solution
  33. main()
  34.  
  35.  
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
Aug 2 '07 #5

Thekid
100+
P: 145
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.
Aug 2 '07 #6

ilikepython
Expert 100+
P: 844
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> f=open("dict.txt","r")
  3. >>> word='zygote'
  4. >>> for line in f:
  5.     if word in line:
  6.         print "Your word is in the dictionary"
  7.  
  8.  
  9. Your word is in the dictionary
  10. Your word is in the dictionary
  11. Your word is in the dictionary
  12. Your word is in the dictionary

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...any suggestions on how I can fix my code?
All you need to do is this:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open("dict.txt")
  2. >>> word = 'zygote'
  3. >>> words = [line[:-1] for line in f]
  4. >>> if word in words:
  5. ...    print "Your word is in the dictionary"
  6.  
Close to your original but this one checks each line to EQUAL the word, yours checks wether the word is IN the line.
Aug 2 '07 #7

Thekid
100+
P: 145
All you need to do is this:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open("dict.txt")
  2. >>> word = 'zygote'
  3. >>> words = [line[:-1] for line in f]
  4. >>> if word in words:
  5. ...    print "Your word is in the dictionary"
  6.  
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 :)
Aug 2 '07 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. >>> f=open("dict.txt","r")
  2. >>> text=f.read()
  3. >>> 'zygote' in text
  4. True
  5. >>> import re
  6. >>> patt=re.compile('[a-z]+',re.IGNORECASE)
  7. >>> 'zygote' in patt.findall(text)
  8. True
  9. >>> patt.findall(text).index('zygote')
  10. 113789
  11. >>> patt.findall(text)
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(text)' 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.
Aug 2 '07 #9

P: 22
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 :)
Aug 3 '07 #10

P: 2
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.
Aug 3 '07 #11

P: 22
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.


Expand|Select|Wrap|Line Numbers
  1. word=str(raw_input("Enter a word: "))
  2.         words=[line[:-1] for line in f]
  3.  
  4.         if word in words:
  5.             print "The word '%s' is in the dictionary." %word
  6.             print
  7.  
  8.         elif word not in words:
  9.             words2=[line[:-1] for line in userDict]
  10.             if word in words2:
  11.                 print "The word '%s' is in the User dictionary." %word

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.
Aug 4 '07 #12

P: 56
I
Expand|Select|Wrap|Line Numbers
  1. word=str(raw_input("Enter a word: "))
  2.         words=[line[:-1] for line in f]
  3.  
  4.         if word in words:
  5.             print "The word '%s' is in the dictionary." %word
  6.             print
  7.  
  8.         elif word not in words:
  9.             words2=[line[:-1] for line in userDict]
  10.             if word in words2:
  11.                 print "The word '%s' is in the User dictionary." %word
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.
Aug 4 '07 #13

P: 22
[quote=William Manley]
I
Expand|Select|Wrap|Line Numbers
  1. word=str(raw_input("Enter a word: "))
  2.         words=[line[:-1] for line in f]
  3.  
  4.         if word in words:
  5.             print "The word '%s' is in the dictionary." %word
  6.             print
  7.  
  8.         elif word not in words:
  9.             words2=[line[:-1] for line in userDict]
  10.             if word in words2:
  11.                 print "The word '%s' is in the User dictionary." %word

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.
Aug 4 '07 #14

bartonc
Expert 5K+
P: 6,596
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.


Expand|Select|Wrap|Line Numbers
  1. word=str(raw_input("Enter a word: "))
  2.         words=[line[:-1] for line in f]
  3.  
  4.         if word in words:
  5.             print "The word '%s' is in the dictionary." %word
  6.             print
  7.  
  8.         elif word not in words:
  9.             words2=[line[:-1] for line in userDict]
  10.             if word in words2:
  11.                 print "The word '%s' is in the User dictionary." %word

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:
Expand|Select|Wrap|Line Numbers
  1. >>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
  2. >>> print myDict
  3. this
  4. is
  5. some
  6. text
  7. with
  8. one
  9. word
  10. per
  11. line
  12. >>> "text" in myDict
  13. True
  14. >>> 
Aug 4 '07 #15

P: 22
There's really no need to make a list for a string in order to test membership of a string:
Expand|Select|Wrap|Line Numbers
  1. >>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
  2. >>> print myDict
  3. this
  4. is
  5. some
  6. text
  7. with
  8. one
  9. word
  10. per
  11. line
  12. >>> "text" in myDict
  13. True
  14. >>> 
I'm trying to check if the words are in a file though...
does it still not matter then?
Aug 4 '07 #16

ilikepython
Expert 100+
P: 844
There's really no need to make a list for a string in order to test membership of a string:
Expand|Select|Wrap|Line Numbers
  1. >>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
  2. >>> print myDict
  3. this
  4. is
  5. some
  6. text
  7. with
  8. one
  9. word
  10. per
  11. line
  12. >>> "text" in myDict
  13. True
  14. >>> 
That was the O/P's orignal problem, that the word can match other words:
Expand|Select|Wrap|Line Numbers
  1. >>> myDict = "this\nis\nsome\ntextile\nwith\none\nword\nper\nline"
  2. >>> print myDict
  3. this
  4. is
  5. some
  6. textile # not "text"
  7. with
  8. one
  9. word
  10. per
  11. line
  12. >>> "text" in myDict     # wrong
  13. True
  14.  
Aug 5 '07 #17

P: 16
Hi

Can anyone tell me how to use the first bit of code (below) in this post to not print but to count the number of a words in the dict file and then use that number to define a true or false statement ie..
>>> if word in dict.txt >50 = True


>>> f=open("dict.txt","r")
>>> word='zygote'
>>> for line in f:
if word in line:
print "Your word is in the dictionary"
Aug 5 '07 #18

ilikepython
Expert 100+
P: 844
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.


Expand|Select|Wrap|Line Numbers
  1. word=str(raw_input("Enter a word: "))
  2.         words=[line[:-1] for line in f]
  3.  
  4.         if word in words:
  5.             print "The word '%s' is in the dictionary." %word
  6.             print
  7.  
  8.         elif word not in words:
  9.             words2=[line[:-1] for line in userDict]
  10.             if word in words2:
  11.                 print "The word '%s' is in the User dictionary." %word

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.
How is the userDict formatted, is it a file?
Aug 5 '07 #19

Thekid
100+
P: 145
I tried this using 2 text files with different words in each file. If the word can be found in one or the other, it will print out which one the word was found in. If the word isn't found, it will print that out as well:

Expand|Select|Wrap|Line Numbers
  1. f = open ("words.txt")
  2. g = open ("dictionary.txt")
  3. word = raw_input("Please enter your word:")
  4. words = [line[:-1] for line in f]
  5. words2 = [line[:-1] for line in g]
  6. if word in words:
  7.             print " %s can be found in the dictionary " % word
  8. if word in words2:
  9.             print " %s can be found in the user dictionary" % word
  10. if word not in words + words2:
  11.            print "Your word was not found. Please try again"
  12.  
  13.  
Aug 6 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.