473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

search problem

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
19 1715
bvdet
2,851 Expert Mod 2GB
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
LolaT
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
145 100+
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
LolaT
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
145 100+
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
844 Expert 512MB
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
145 100+
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
2,851 Expert Mod 2GB
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
LolaT
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
BambiL
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
LolaT
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
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
LolaT
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
6,596 Expert 4TB
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
LolaT
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
844 Expert 512MB
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
T00l
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
844 Expert 512MB
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
145 100+
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

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

Similar topics

3
by: Andy Jacobs | last post by:
Hi all I have a search function on a site of mine. At the moment, the content is delivered using this: <?php echo $row_Recordset1; ?> The search function goes through the table and...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
2
by: Zambo via SQLMonster.com | last post by:
Hi! We have Sql Server 2000 in our server (NT 4). Our database have now about +350.000 rows with information of images. Table have lot of columns including information about image name, keywords,...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
3
by: Richard S | last post by:
CODE: ASP.NET with C# DATABASE: ACCES alright, im having a problem, probably a small thing, but i cant figure out, nor find it in any other post, or on the internet realy (probably cuz i wouldnt...
4
by: MrWelfare | last post by:
Hi Everyone, I'm currently trying to follow an example I found in a book a while ago to try to create a spotlight (mac-like) search that filters results as one types. The script that I have works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.