Member Avatar for leegeorg07

hi again im doing the spell checker program in the projects for the beginner thing and i have this code:

dict  = open("DictionaryE.txt", "r").readlines()
test = "Hello my nmae is george"
correct = []
for line in test:
	if line in dict:
		correct.append(line)
		print line
		

print correct

but whenever i test it i get this :
[]

i know from other tests that it takes each letter, how can i solve this?

Recommended Answers

All 16 Replies

for line in test:

this is what is wrong in the code....
here line will take each character seperatly....
now try this

list_words=test.split(' ')

now list_words will contain the words in the sentence
now if you compare it, you would get the desired output....
:)

Member Avatar for leegeorg07

thanks, ive changed it to this:

dict  = open("DictionaryE.txt", "r").readlines()
test = "Hello my nmae is george"
correct = []
list_words=test.split(' ')
for line in list_words:
	if line in dict:
		correct.append(line)
		print line
		

print correct

but it doesn't change anything, i still just get []
what else should i do?

could you tell me how the DicitonaryE.txt looks like....?

Member Avatar for leegeorg07

sure this is the file

we have cute problem here...
did u by chance check the words in dict.....
each and every word in that ends with a stray '\n'
hence your problem...
so have a simple loop to remove those from all the words that would solve your problem...

for i in range(len(dict)):
       dict[i] = dict[i][0:len(dict[i])-2]

that should help you....

Member Avatar for leegeorg07

thanks that works all good now

i also added in

test = test.lower()

which helps with the capitals

super
more robust...
:)

mark this solved so that others also benfit from our knowledge.....

Member Avatar for leegeorg07

i tried to check a file (README.txt) in the python folder
with this code:

dict  = open("DictionaryE.txt", "r").readlines()
test= open("README.txt", "r").readlines()
correct = []


test = test.lower()
list_words=test.split(' ')
for i in range(len(dict)):
       dict[i] = dict[i][0:len(dict[i])-2]

for word in list_words:
	if word in dict:
		correct.append(line)
		print word, "is correct"
	else:
		print word, "is not correct or is unrecognised"
		

print correct

but it comes up with the error:

File "C:\Python25\spell checker.py", line 6, in <module>
    test = test.lower()
AttributeError: 'list' object has no attribute 'lower'

what can i do now?

Just a tip, but for trailing '\n' characters in files, you can just use line.strip() function:

f = open('file.txt')
l = []
for line in file:
    l.append(line.strip())

Hope that helps you out later.

Oh and for the other prob:

test= open("README.txt", "r").readlines()
#that makes a list
test = open("README.txt").read() 
#that makes a string
#now we can do test.lower() with the open("README.txt").read()
print test.lower() #DADA!
Member Avatar for leegeorg07

thanks i have this code

dict  = open("DictionaryE.txt", "r").readlines()
test= open("README.txt", "r").read()

correct = []


test = test.lower()
list_words = test.split(' ')
for i in range(len(dict)):
       dict[i] = dict[i][0:len(dict[i])-2]

for line in list_words:
	if line in dict:
		correct.append(line)
correct = str(correct)
result = open("cheese.txt", "w").write(correct)

that works well but how can i get it to delete any repeated words in the list correct before it writes it to cheese.txt

try to use in built function __contains__()
return type is True or False.
for example

>>>a=[1,2,3]
>>>a.__contains__(1)
True
Member Avatar for leegeorg07

how can i use that to check for more than 1

see... store the value if encountered first time...else skip
that is

unique=[]
if unique.__contains__(word) is False:
        unique.append(word)

now expect for the first line put the rest in a loop and ur this problem is solved

Member Avatar for leegeorg07

thanks it works perfectly

dict  = open("DictionaryE.txt", "r").readlines()
test= open("README.txt", "r").read()

correct = []
unique = []
test = test.lower()
list_words = test.split(' ')
for i in range(len(dict)):
       dict[i] = dict[i][0:len(dict[i])-2]

for line in list_words:
	if line in dict:
		correct.append(line)

for word in correct:
	if unique.__contains__(word) is False:
		unique.append(word)
unique = str(unique)
open("cheese.txt", "w").write(unique)

for anyone that wants to use it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.