How to Create a Spell Checker in Visual Basic
Submitted by GeePee on Friday, April 24, 2015 - 23:31.
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".
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.
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).
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.
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.
Project Complete!
Below is the full source code and download to the project files.
- a word
- Imports System.IO
- Dim dictionary As String = getFile()
- Dim words As New List(Of String)
- Using sr As New StreamReader(dictionary)
- While sr.Peek <> -1
- Dim line As String = sr.ReadLine()
- If (Not line = Nothing And Not line = " ") Then
- If (line.Contains(" ")) Then
- Dim wordss As String() = line.Split(" ")
- For Each w As String In wordss
- words.Add(w)
- Next
- Else : words.Add(line)
- End If
- End If
- End While
- End Using
- Private Function getFile()
- Dim fo As New OpenFileDialog()
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- Return fo.FileName
- End Function
- Dim falseWords As New List(Of String)
- Dim falseNumbers As New List(Of Integer)
- Dim checkingWords As String() = TextBox1.Text.Split(" ")
- For i As Integer = 0 To checkingWords.Count() - 1
- Dim isrealWord As Boolean = False
- For Each w As String In words
- If (checkingWords(i).ToLower() = w.ToLower()) Then isrealWord = True
- Next
- If Not (isrealWord) Then
- falseWords.Add(checkingWords(i))
- falseNumbers.Add(i)
- End If
- Next
- For i As Integer = 0 To falseWords.Count() - 1
- MsgBox("False Word at position " & falseNumbers(i) + 1 & ". Word: " & falseWords(i))
- Next
- Imports System.IO
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim falseWords As New List(Of String)
- Dim falseNumbers As New List(Of Integer)
- Dim dictionary As String = getFile()
- Dim words As New List(Of String)
- Using sr As New StreamReader(dictionary)
- While sr.Peek <> -1
- Dim line As String = sr.ReadLine()
- If (Not line = Nothing And Not line = " ") Then
- If (line.Contains(" ")) Then
- Dim wordss As String() = line.Split(" ")
- For Each w As String In wordss
- words.Add(w)
- Next
- Else : words.Add(line)
- End If
- End If
- End While
- End Using
- Dim checkingWords As String() = TextBox1.Text.Split(" ")
- For i As Integer = 0 To checkingWords.Count() - 1
- Dim isrealWord As Boolean = False
- For Each w As String In words
- If (checkingWords(i).ToLower() = w.ToLower()) Then isrealWord = True
- Next
- If Not (isrealWord) Then
- falseWords.Add(checkingWords(i))
- falseNumbers.Add(i)
- End If
- Next
- For i As Integer = 0 To falseWords.Count() - 1
- MsgBox("False Word at position " & falseNumbers(i) + 1 & ". Word: " & falseWords(i))
- Next
- End Sub
- Private Function getFile()
- Dim fo As New OpenFileDialog()
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- Return fo.FileName
- End Function
- End Class
Add new comment
- 214 views