-
>>>
-
>>> f=open("dict.txt","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
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?
19 1715 bvdet 2,851
Expert Mod 2GB -
>>>
-
>>> f=open("dict.txt","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
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: - >>> 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
-
>>>
Using re: - >>> 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
-
>>>
- >>> f=open("dict.txt","r")
-
>>> text=f.read()
-
>>> 'zygote' in text
-
True
-
>>> import re
-
>>> patt=re.compile('[a-z]+',re.IGNORECASE)
-
>>> 'zygote' in patt.findall(text)
-
True
-
>>> patt.findall(text).index('zygote')
-
113789
-
>>> patt.findall(text)
after that last line, it just shuts down...
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. -
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()
-
-
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. -
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
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.
-
>>>
-
>>> f=open("dict.txt","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
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: -
>>> f = open("dict.txt")
-
>>> word = 'zygote'
-
>>> words = [line[:-1] for line in f]
-
>>> if word in words:
-
... print "Your word is in the dictionary"
-
Close to your original but this one checks each line to EQUAL the word, yours checks wether the word is IN the line.
All you need to do is this: -
>>> f = open("dict.txt")
-
>>> word = 'zygote'
-
>>> words = [line[:-1] for line in f]
-
>>> if word in words:
-
... print "Your word is in the dictionary"
-
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 :)
bvdet 2,851
Expert Mod 2GB - >>> f=open("dict.txt","r")
-
>>> text=f.read()
-
>>> 'zygote' in text
-
True
-
>>> import re
-
>>> patt=re.compile('[a-z]+',re.IGNORECASE)
-
>>> 'zygote' in patt.findall(text)
-
True
-
>>> patt.findall(text).index('zygote')
-
113789
-
>>> 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.
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 :)
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.
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. - word=str(raw_input("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
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.
I - word=str(raw_input("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
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.
[quote=William Manley]
I - word=str(raw_input("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
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.
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. - word=str(raw_input("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
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: -
>>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
-
>>> print myDict
-
this
-
is
-
some
-
text
-
with
-
one
-
word
-
per
-
line
-
>>> "text" in myDict
-
True
-
>>>
There's really no need to make a list for a string in order to test membership of a string: -
>>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
-
>>> print myDict
-
this
-
is
-
some
-
text
-
with
-
one
-
word
-
per
-
line
-
>>> "text" in myDict
-
True
-
>>>
I'm trying to check if the words are in a file though...
does it still not matter then?
There's really no need to make a list for a string in order to test membership of a string: -
>>> myDict = "this\nis\nsome\ntext\nwith\none\nword\nper\nline"
-
>>> print myDict
-
this
-
is
-
some
-
text
-
with
-
one
-
word
-
per
-
line
-
>>> "text" in myDict
-
True
-
>>>
That was the O/P's orignal problem, that the word can match other words: -
>>> myDict = "this\nis\nsome\ntextile\nwith\none\nword\nper\nline"
-
>>> print myDict
-
this
-
is
-
some
-
textile # not "text"
-
with
-
one
-
word
-
per
-
line
-
>>> "text" in myDict # wrong
-
True
-
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"
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. - word=str(raw_input("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
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?
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: -
f = open ("words.txt")
-
g = open ("dictionary.txt")
-
word = raw_input("Please enter your word:")
-
words = [line[:-1] for line in f]
-
words2 = [line[:-1] for line in g]
-
if word in words:
-
print " %s can be found in the dictionary " % word
-
if word in words2:
-
print " %s can be found in the user dictionary" % word
-
if word not in words + words2:
-
print "Your word was not found. Please try again"
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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.,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |