7 Replies - 12259 Views - Last Post: 29 September 2014 - 01:33 PM Rate Topic: -----

#1 Kolacats   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 14

Python spell checker program help

Posted 29 September 2014 - 11:47 AM

Hello, I have recently got into python and have wrote a program that checks the spelling of any English word.
file = open("words.txt", "r") # Opens the file
word = str(input("Type your word for spell checking: "))
word = word.lower() # changes the variable word to lower case
success = False
i = 1
for i in range(1,53751): # Checks if i is between 1 and 53751 (there are 53751 lines in words.txt)
    line = str(file.readline(i)) # sets line to line(i)
    i = i + 1
    if word == line: # if line is = to word it prints success and breaks out of loop
        print("Your word is spelt correctly!")
        success = True
        break
if success == False: # if word is never equal to line it prints unsuccessful 
    print("The word is spelt wrong")



For some unknown reason the program always prints that the word is spelt wrong, I have no idea why and it gives no errors so i think all the syntax is fine. I have checked to see if the program can read each line and it can by printing line in the for loop.

Thanks

The dictionary of words (words.txt) is attached Attached File  words.txt (480.93K)
Number of downloads: 1541

Is This A Good Question/Topic? 0
  • +

Replies To: Python spell checker program help

#2 andrewsw   User is offline

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,510
  • Joined: 12-December 12

Re: Python spell checker program help

Posted 29 September 2014 - 12:06 PM

Each line includes whitespace, newlines and perhaps spaces, so never matches the word.

the docs

Quote

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous;

You can firstly use for line in file rather than the magic number 53751:

Quote

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code

You could then use strip() to remove the whitespace. So,
for line in file:
    if word == line.strip():

This post has been edited by andrewsw: 29 September 2014 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 Kolacats   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 14

Re: Python spell checker program help

Posted 29 September 2014 - 12:14 PM

I have used the strip function but the program still doesn't work

This post has been edited by andrewsw: 29 September 2014 - 12:16 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,510
  • Joined: 12-December 12

Re: Python spell checker program help

Posted 29 September 2014 - 12:17 PM

There is no need to quote a large previous post, there is a Reply button further down the page.

It will work because I've used your file and run modified code successfully myself. Spend more than five minutes on it.
Was This Post Helpful? 0
  • +
  • -

#5 Kolacats   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 14

Re: Python spell checker program help

Posted 29 September 2014 - 12:28 PM

Here is the code I currently have:
file = open("words.txt", "r") # Opens the file
word = str(input("Type your word for spell checking: "))
word = word.lower() # changes the variable word to lower case
success = False
i = 1
for line in file: # Runs the loop for each line in the file
    line = str(file.readline(i)) # sets line to line(i)
    i = i + 1
    if word == line.strip(" "): # if line is = to word it prints success and breaks out of loop
        print("Your word is spelt correctly!")
        success = True
        break
if success == False: # if word is never equal to line it prints unsuccessful 
    print("The word is spelt wrong")


Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,510
  • Joined: 12-December 12

Re: Python spell checker program help

Posted 29 September 2014 - 12:43 PM

You no longer need this line:
    line = str(file.readline(i)) # sets line to line(i)


    if word == line.strip(" "):

this only removes spaces, not newlines.

Read the code-fragment I posted.
Was This Post Helpful? 0
  • +
  • -

#7 Kolacats   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 14

Re: Python spell checker program help

Posted 29 September 2014 - 01:31 PM

Thanks, it's all fixed now. This has been a big help
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,510
  • Joined: 12-December 12

Re: Python spell checker program help

Posted 29 September 2014 - 01:33 PM

You're welcome. I assume you got it down to code like this:
for line in file:
    if word == line.strip(): # if line is = to word it prints success and breaks out of loop
        print("Your word is spelt correctly!")
        success = True
        break

if success == False: # if word is never equal to line it prints unsuccessful 
    print("The word is spelt wrong")

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1