How to Create a Spell Checker in Visual Basic

Pre-Note: As seen in the image for this post, this program outputs the word "false" as incorrectly spelt. This is because I created a test dictionary (as below) which only recognized the words "a" and "word".
  1. a word
Introduction: Welcome to my tutorial on how to create a Spell Checker in Visual Basic. For this you will need a dictionary which has the words separated by a space. If your dictionary is separated by lines etc you can simply edit the source to adapt it to the dictionary splitting technique. Steps of Creation: Step 1: First we want to create a form which contains two controls; Button1 - to begin the checking process Textbox1 - Contain the words to be spell checked. We also need to import IO to allow us to read the dictionary.
  1. Imports System.IO
Step 2: Now we will allow the user to specify a dictionary and read all the words to a new Word List. (On button1 click).
  1. Dim dictionary As String = getFile()
  2. Dim words As New List(Of String)
  3. Using sr As New StreamReader(dictionary)
  4. While sr.Peek <> -1
  5. Dim line As String = sr.ReadLine()
  6. If (Not line = Nothing And Not line = " ") Then
  7. If (line.Contains(" ")) Then
  8. Dim wordss As String() = line.Split(" ")
  9. For Each w As String In wordss
  10. words.Add(w)
  11. Next
  12. Else : words.Add(line)
  13. End If
  14. End If
  15. End While
  16. End Using
We are using a custom function called getFile to open a browser for the user, we will create this in a second. Step 3: Now we want to create our custom function called getFile. It simply allows the user to select a dictionary file as .txt.
  1. Private Function getFile()
  2. Dim fo As New OpenFileDialog()
  3. fo.Filter = "Text Files|*.txt"
  4. fo.FilterIndex = 1
  5. fo.ShowDialog()
  6. Return fo.FileName
  7. End Function
Step 4: Next we want to simply run through all the words in textbox1.text and see if they exist in the dictionary word list. We then output the position and word for each in-correct spelt word.
  1. Dim falseWords As New List(Of String)
  2. Dim falseNumbers As New List(Of Integer)
  3. Dim checkingWords As String() = TextBox1.Text.Split(" ")
  4. For i As Integer = 0 To checkingWords.Count() - 1
  5. Dim isrealWord As Boolean = False
  6. For Each w As String In words
  7. If (checkingWords(i).ToLower() = w.ToLower()) Then isrealWord = True
  8. Next
  9. If Not (isrealWord) Then
  10. falseWords.Add(checkingWords(i))
  11. falseNumbers.Add(i)
  12. End If
  13. Next
  14. For i As Integer = 0 To falseWords.Count() - 1
  15. MsgBox("False Word at position " & falseNumbers(i) + 1 & ". Word: " & falseWords(i))
  16. Next
Project Complete! Below is the full source code and download to the project files.
  1. Imports System.IO
  2. Public Class Form1
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. Dim falseWords As New List(Of String)
  5. Dim falseNumbers As New List(Of Integer)
  6. Dim dictionary As String = getFile()
  7. Dim words As New List(Of String)
  8. Using sr As New StreamReader(dictionary)
  9. While sr.Peek <> -1
  10. Dim line As String = sr.ReadLine()
  11. If (Not line = Nothing And Not line = " ") Then
  12. If (line.Contains(" ")) Then
  13. Dim wordss As String() = line.Split(" ")
  14. For Each w As String In wordss
  15. words.Add(w)
  16. Next
  17. Else : words.Add(line)
  18. End If
  19. End If
  20. End While
  21. End Using
  22. Dim checkingWords As String() = TextBox1.Text.Split(" ")
  23. For i As Integer = 0 To checkingWords.Count() - 1
  24. Dim isrealWord As Boolean = False
  25. For Each w As String In words
  26. If (checkingWords(i).ToLower() = w.ToLower()) Then isrealWord = True
  27. Next
  28. If Not (isrealWord) Then
  29. falseWords.Add(checkingWords(i))
  30. falseNumbers.Add(i)
  31. End If
  32. Next
  33. For i As Integer = 0 To falseWords.Count() - 1
  34. MsgBox("False Word at position " & falseNumbers(i) + 1 & ". Word: " & falseWords(i))
  35. Next
  36. End Sub
  37.  
  38. Private Function getFile()
  39. Dim fo As New OpenFileDialog()
  40. fo.Filter = "Text Files|*.txt"
  41. fo.FilterIndex = 1
  42. fo.ShowDialog()
  43. Return fo.FileName
  44. End Function
  45. End Class

Add new comment