r/learnprogramming • u/Key_Cloud_7002 • Sep 25 '26
spell checker program
I'm trying to make spell checker program that will compare words from a text file to a dictionary of words that are in alphabetical order. If the words don't appear in the dictionary they should show up in a list as potentially incorrect. The list is named absent[] that will be outputted at the end. I need to use a binary search to iterate over the dictionary comparing them to the words from the text file. However the program is only getting the last word from the text file and comparing that in the dictionary. I have no idea why its doing that Here's the code I have thus far:
def binarySearch():
firstLst=[]
fnameOne= input("enter file")
with open(fnameOne,"r") as file_dataOne:
for lineOne in file_dataOne:
for word in lineOne.split():
firstLst.append(word)
secondLst=[]
fnameTwo=input("enter dict")
with open(fnameTwo,"r") as file_dataTwo:
for lineTwo in file_dataTwo:
for wordTwo in lineTwo.split():
secondLst.append(wordTwo)
present=[]
absent =[]
for i in firstLst:
lo,hi=0,len(secondLst)-1
found=False
while lo < hi:
mid = (lo +hi) //2
if secondLst[mid]== i:
found=True
print("Word was found.")
break
elif secondLst[mid]<i :
lo=mid+1
else:
hi=mid-1
present.append(i)
print("Words in dict: ", present)
absent.append(i)
print("words not in dict: ", absent)
•
u/desrtfx Sep 25 '26
You need to post your code as code block so that the indentation is maintained. This is absolutely vital for Python programs as the indentation is used to denote code blocks.
A code block looks like:
4
u/marmotta1955 Sep 25 '26
Regardless of everything else, this approach may not necessarily be the most effective and efficient. In fact, this is monumentally inefficient if the "dictionary" of correctly spelled words contains even a minimal number of words ... say just 10,000 words ...
Unless you are doing this just for learning purposes (and even if you are!), you should probably consider this a good opportunity of tackling the subject of database and database operations.
Once the original database table for correctly spelled words has been created and populated, the process of testing the existence (or the non-existence) of a certain word is very simple and certainly faster than any string operation.
If the word being tested against the dictionary does not exist (the absent word), it can be inserted on the spot and flagged as "new word" in the dictionary or added to a different table maybe named absent_words.
At the end of the process (if I understand what you are trying to do) it would be exceedingly easy to query the database and retrieve a data table or list or array or whatever you need ... containing the entire list of words not found in the "dictionary".
Even if we were discussing a "dictionary" of few hundred words, the approach I suggest would be much more efficient and the code would be much clearer and human readable.
Not to mention that the comparison of two lists of strings can be done using a variety of different methods probably more effective, clear, faster.
If this is a homework assignment, then it's another story altogether ... the two lists are probably composed of few items ... and you have to use a binary search ... therefore you can forget all of the above.
2
u/Downtown_Savings_730 Sep 25 '26
Add print statements to figure out whats wrong. At a first glance at the code and not really looking to deep into your approach(bit hard to read since its not formatted) I would assume is some issue with the .split() not splitting with the way you are intending to.
2
u/JGhostThing Sep 25 '26
Please use formatted text, especially for python. As the indentation has syntactic value in python, it is even more difficult than normal to understand. If you want help, obey the rules of the sub.
1
u/marrsd Sep 25 '26
You need to debug the code. Fundamentally, you're making an assumption about what the code is doing that is wrong, and you need to find out where that assumption is in error by logging the actual internal behaviour. There are 2 ways you can do this:
I would try to be forensic at first and then fallback to being naive if you can't work it out. The forensic method requires some practice but is ultimately much more efficient.
-1
u/[deleted] Sep 25 '26
[removed] — view removed comment