Hello everyone, we are currently studying the concept of a spell corrector. I am not content with the existing corrections, so I decided to create an app that not only rectifies my sentences but also can vocalize the corrected version.
Below I explain the code
Basic Python code for asking the user to correct the spelling
from textblob import TextBlob
def correct_spelling(text):
blob = TextBlob(text)
corrected_text = blob.correct()
return str(corrected_text)
# Ask for input from the user
text_with_mistakes = input("Enter a text with spelling mistakes: ")
# Correct spelling mistakes
corrected_text = correct_spelling(text_with_mistakes)
print("Corrected text:")
print(corrected_text)
Results1:
Create a streamlit that can speak the wrong words
install individual libraries:
pip install streamlit
pip install textblob
pip install pyttsx3
Consider using PyCharm. I am using the same; it automatically detects packages and indicates install them.
import streamlit as st from textblob import TextBlob import pyttsx3 def correct_spelling(text): blob = TextBlob(text) corrected_text = "" for word in blob.words: corrected_word = word.correct() if corrected_word != word: corrected_text += corrected_word + " " else: corrected_text += word + " " return corrected_text.strip() def speak_text(text): engine = pyttsx3.init() engine.say(text) engine.runAndWait() def main(): st.title("Spelling Correction and Text-to-Speech") # Input text with spelling mistakes text_with_mistakes = st.text_area("Enter text with spelling mistakes:") # Correct spelling button if st.button("Correct Spelling"): corrected_text = correct_spelling(text_with_mistakes) st.subheader("Corrected Text:") st.write(corrected_text) # Speak button if st.button("Speak"): speak_text(text_with_mistakes) if __name__ == "__main__": main()
Results
:
Finally the code for spelling correcting speaker
import streamlit as st from textblob import TextBlob import pyttsx3 def correct_spelling(text): blob = TextBlob(text) corrected_text = "" for word in blob.words: corrected_word = word.correct() if corrected_word != word: corrected_text += corrected_word + " " else: corrected_text += word + " " return corrected_text.strip() def speak_text(text): engine = pyttsx3.init() engine.say(text) engine.runAndWait() def main(): st.title("Spelling Correction and Text-to-Speech") # Input text with spelling mistakes text_with_mistakes = st.text_area("Enter text with spelling mistakes:") # Correct spelling button if st.button("Correct Spelling"): corrected_text = correct_spelling(text_with_mistakes) st.subheader("Corrected Text:") st.write(corrected_text) # Speak the corrected text speak_text(corrected_text) if __name__ == "__main__": main()
Results2
:
Explanation:
import streamlit as st
- This line imports the Streamlit library and aliases it as
st
, which is a common convention.
from textblob import TextBlob
import pyttsx3
- These lines import the
TextBlob
class from thetextblob
library for text processing and thepyttsx3
library for text-to-speech functionality.
def correct_spelling(text):
blob = TextBlob(text)
corrected_text = ""
for word in blob.words:
corrected_word = word.correct()
if corrected_word != word:
corrected_text += corrected_word + " "
else:
corrected_text += word + " "
return corrected_text.strip()
- This function
correct_spelling(text)
takes a string of text as input and returns the text with spelling mistakes corrected. It usesTextBlob
to tokenize the input text into words, corrects each word, and then rebuilds the corrected text. The.correct()
method ofTextBlob
is used to correct each word.
def speak_text(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
- This function
speak_text(text)
takes a string of text as input and speaks it out loud using text-to-speech. It initializes the text-to-speech engine usingpyttsx3
, says the input text, and then waits for the speech to finish.
def main():
st.title("Spelling Correction and Text-to-Speech")
This function
main()
is the main entry point of the Streamlit app. It sets the title of the app to "Spelling Correction and Text-to-Speech".# Input text with spelling mistakes text_with_mistakes = st.text_area("Enter text with spelling mistakes:")
This line creates a text area input field in the Streamlit app where the user can enter text with spelling mistakes. The entered text is stored in the variable
text_with_mistakes
.# Correct spelling button if st.button("Correct Spelling"):
This line creates a button labeled "Correct Spelling" in the Streamlit app. When clicked, it executes the code block inside the
if
statement.corrected_text = correct_spelling(text_with_mistakes) st.subheader("Corrected Text:") st.write(corrected_text)
This block of code executes when the "Correct Spelling" button is clicked. It corrects the spelling mistakes in the input text using the
correct_spelling()
function, then displays the corrected text as a subheader followed by the corrected text itself usingst.write()
.
# Speak the corrected text
speak_text(corrected_text)
- After correcting the spelling mistakes and displaying the corrected text, this line calls the
speak_text()
function to speak out loud the corrected text using text-to-speech.
if __name__ == "__main__":
main()
- This line ensures that the
main()
function is executed when the script is run directly. It's a common Python idiom used to define the entry point of a script.
Thanks for veieing this newsletter
bye guys