I'm learning Python with "The python workbook" it is all going well but every now and again there will be something weird that I can't understand. For example take the following case:
I'm suppose to do code that will take file name as command line argument, read the text in the file, and spell check it by comparing it to another file that contain a lots of words.
I did that and it was quite easy, but I decided to upgrade the code a bit and to exclude from the wrong list all the plurals.
Here is the code :
import sys
wrong=[]
text=[]
myDict={}
path="C:\\Users\\White PC\\Desktop\\words.txt"
try:
x=sys.argv[1]
except IndexError:
print("No command line argument provided. Now quitting....")
exit()
def spellCheck(x):
try:
with open("qqq.txt","r" ) as file:
for line in file:
word = ""
line=line.strip()
for ch in line:
if line.index(ch) < len(line):
if ch.isalpha() :
word=word+ch
elif word!="" or line.index(ch)==len(line):
word=word.lower()
text.append(word)
word=""
with open(path, "r") as allWords:
j=1
for line in allWords:
line=line.strip()
myDict[j]=line
j+=1
for word in text:
if word in myDict.values():
pass
else:
wrong.append(word)
for word in wrong:
if word[:-1] in myDict.values():
wrong.remove(word)
except FileNotFoundError:
print("Error!File not found")
def main():
spellCheck(x)
print("The misspelled words are:",wrong)
if __name__ == '__main__':
main()
So the thing kind of works, but there will always be one word which will remain in my "wrong" list no matter what I do. In my case the word is "words". I have it in the myDict
as singe "word", but it would not remove it. The funny thing is when I change the word in myDict
to "words" instead of "word" it disappears from my "Wrong" list , but another one with "s" at the end appears. Basically it is always one plural word that won't be removed and I really can't figure out why.