Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Dyndrilliac on September 20, 2004, 11:50 AM

Title: Automatic SpellCheck Function
Post by: Dyndrilliac on September 20, 2004, 11:50 AM
Ok, For a few months now I have been using a home-made spellcheck function to quickly check outgoing messages on my bot for common errors in spelling that I make, because either I'm typing too fast or lazy. It looks like this:

Public Function SpellCheck(strMessage As String) As String
   If InStr(strMessage, "liek") <> 0 Then strMessage = Replace(strMessage, "liek", "like")
   If InStr(strMessage, "soem") <> 0 Then strMessage = Replace(strMessage, "soem", "some")
   If InStr(strMessage, "alot") <> 0 Then strMessage = Replace(strMessage, "alot", "a lot")
   If InStr(strMessage, "amke") <> 0 Then strMessage = Replace(strMessage, "amke", "make")
   If InStr(strMessage, "amde") <> 0 Then strMessage = Replace(strMessage, "amde", "made")
   If InStr(strMessage, ";t") <> 0 Then strMessage = Replace(strMessage, ";t", "'t")
   If InStr(strMessage, ";s") <> 0 Then strMessage = Replace(strMessage, ";s", "'s")
   If InStr(strMessage, "bakc") <> 0 Then strMessage = Replace(strMessage, "bakc", "back")
   If InStr(strMessage, "ahnd") <> 0 Then strMessage = Replace(strMessage, "ahnd", "hand")
   If InStr(strMessage, " u ") <> 0 Then strMessage = Replace(strMessage, " u ", " you ")
   If InStr(strMessage, " ur ") <> 0 Then strMessage = Replace(strMessage, " ur ", " your ")
   SpellCheck = strMessage
End Function

I want to take this far beyond my original quick fix intentions and make it so it can check for thousands of errors quickly, and without adding tons of size to the program, as memory efficient as possible in VB6.

If anyone could provide some tips, suggestions, resources, or any other general assistance it would be much appreciated.
Title: Re:Automatic SpellCheck Function
Post by: UserLoser. on September 20, 2004, 01:49 PM
Small optimization:  You don't have to verify if the string you're checking is in the string you're replacing or not.  Ex: Replace("test", "1", vbNullString) is going to check the string anyways to see if "1" exists, then it'll replace it, if it does. So there really isn't a point of doing InStr() first.  If "1" doesn't exist, then the string will remain as "test".  Also, "ur" could be debatable, whether the user is saying "ur dog" (your dog), or "ur cool" (you're cool).  That would be a tough one to decide on what to replace "ur" with, and would probably require work to see the words before and/or after it
Title: Re:Automatic SpellCheck Function
Post by: Dyndrilliac on September 20, 2004, 03:35 PM
So, this would be fine?

Public Function SpellCheck(strMessage As String) As String
   Replace strMessage, "liek", "like"
   Replace strMessage, "soem", "some"
   Replace strMessage, "alot", "a lot"
   Replace strMessage, "amke", "make"
   Replace strMessage, "amde", "made"
   Replace strMessage, ";t", "'t"
   Replace strMessage, ";s", "'s"
   Replace strMessage, "bakc", "back"
   Replace strMessage, "ahnd", "hand"
   Replace strMessage, " u ", " you "
   Replace strMessage, " ur ", " your "
   SpellCheck = strMessage
End Function


I still need some advice on how to go about creating in a buil-in spellcheck function. I was kind of hoping to try and make it as extensive as Microsoft Word's SpellChecking thingy but without adding tons of size and resource usage by the program in VB.
Title: Re:Automatic SpellCheck Function
Post by: K on September 20, 2004, 03:59 PM
Quote from: Dyndrilliac on September 20, 2004, 03:35 PM
So, this would be fine?

Public Function SpellCheck(strMessage As String) As String
   Replace strMessage, "liek", "like"
   Replace strMessage, "soem", "some"
   Replace strMessage, "alot", "a lot"
   Replace strMessage, "amke", "make"
   Replace strMessage, "amde", "made"
   Replace strMessage, ";t", "'t"
   Replace strMessage, ";s", "'s"
   Replace strMessage, "bakc", "back"
   Replace strMessage, "ahnd", "hand"
   Replace strMessage, " u ", " you "
   Replace strMessage, " ur ", " your "
   SpellCheck = strMessage
End Function


close, but you're replacing a value, discarding the result, and then replacing another value, etc.

You need to save the result of the replace function.

Public Function SpellCheck(strMessage As String) As String
   strMessage = Replace(strMessage, "liek", "like")
   strMessage = Replace(strMessage, "soem", "some")
   strMessage = Replace(strMessage, "alot", "a lot")
   strMessage = Replace(strMessage, "amke", "make")
   strMessage = Replace(strMessage, "amde", "made")
   strMessage = Replace(strMessage, ";t", "'t")
   strMessage = Replace(strMessage, ";s", "'s")
   strMessage = Replace(strMessage, "bakc", "back")
   strMessage = Replace(strMessage, "ahnd", "hand")
   strMessage = Replace(strMessage, " u ", " you ")
   strMessage = Replace(strMessage, " ur ", " your ")
   SpellCheck = strMessage
End Function


Title: Re:Automatic SpellCheck Function
Post by: Tuberload on September 20, 2004, 04:18 PM
One idea would be to have an English dictionary saved somewhere and compare words to it. If a word is not found, assume it is spelled wrong. Then compare the misspelled word to other similar words and depending on their likeness provide it as an option.

This is the first idea that comes to mind, and I do not know how good of one it is.
Title: Re:Automatic SpellCheck Function
Post by: Dyndrilliac on September 20, 2004, 06:49 PM
I looked it up on MSDN and found out I could use MS Word's Spellchecker using OLE.

Public Function SpellCheck(strText As String) As String

   On Error Resume Next
   Dim oWDBasic As Object
   Dim sTmpString As String

   If strText = vbNullString Then
       Exit Function
   End If

   Screen.MousePointer = vbHourglass
   Set oWDBasic = CreateObject("Word.Basic")

   With oWDBasic
       .FileNew
       .Insert strText
       .ToolsSpelling oWDBasic.EditSelectAll
       .SetDocumentVar "MyVar", oWDBasic.Selection
   End With

   sTmpString = oWDBasic.GetDocumentVar("MyVar")
   sTmpString = Left(sTmpString, Len(sTmpString) - 1)

   If sTmpString = vbNullString Then
       SpellCheck = strText
   Else
       SpellCheck = sTmpString
   End If

   oWDBasic.FileCloseAll 2
   oWDBasic.AppClose
   Set oWDBasic = Nothing
   Screen.MousePointer = vbNormal

End Function
Title: Re:Automatic SpellCheck Function
Post by: K on September 20, 2004, 07:18 PM
Quote from: Dyndrilliac on September 20, 2004, 06:49 PM
I looked it up on MSDN and found out I could use MS Word's Spellchecker using OLE.

(edit: snipped code)

It would probably be a good idea if you're going to be using this function repeatedly to just create the word object once instead of creating/deleting it over and over again.
Title: Re:Automatic SpellCheck Function
Post by: Banana fanna fo fanna on September 20, 2004, 08:40 PM
You should store the changes in a dictionary. Then think about using Google's spellcheck.
Title: Re:Automatic SpellCheck Function
Post by: MyndFyre on September 21, 2004, 09:19 AM
Also, be careful with replacing "ur" with "your."  Sometimes people use "ur" for "you're."
Title: Re:Automatic SpellCheck Function
Post by: Soul Taker on September 21, 2004, 10:04 AM
Quote from: MyndFyre on September 21, 2004, 09:19 AM
Also, be careful with replacing "ur" with "your."  Sometimes people use "ur" for "you're."
Read the thread =P
Title: Re:Automatic SpellCheck Function
Post by: hismajesty on September 21, 2004, 03:17 PM
I once saw a way to do this by interacting with Word's dictionary database. One downfall would be the necessity of the user having to have Word installed, but if that sounds like something you'd like I'm sure a simple google search would bring something like that up.
Title: Re:Automatic SpellCheck Function
Post by: Dyndrilliac on September 21, 2004, 04:24 PM
Quote from: hismajesty[yL] on September 21, 2004, 03:17 PM
I once saw a way to do this by interacting with Word's dictionary database. One downfall would be the necessity of the user having to have Word installed, but if that sounds like something you'd like I'm sure a simple google search would bring something like that up.

Well the fact that I'm using VB requires the user to be on Windows, therefore there is a large chance Word is also installed.
Title: Re:Automatic SpellCheck Function
Post by: DeTaiLs on September 21, 2004, 05:49 PM
Quote from: Dyndrilliac on September 21, 2004, 04:24 PM
Quote from: hismajesty[yL] on September 21, 2004, 03:17 PM
I once saw a way to do this by interacting with Word's dictionary database. One downfall would be the necessity of the user having to have Word installed, but if that sounds like something you'd like I'm sure a simple google search would bring something like that up.

Well the fact that I'm using VB requires the user to be on Windows, therefore there is a large chance Word is also installed.

None of the new dells come with Word
Title: Re:Automatic SpellCheck Function
Post by: hismajesty on September 21, 2004, 07:20 PM
The Sony Vaio comes with office, but it doesn't come with the registration key (at least, I can't find it.) :P
Title: Re:Automatic SpellCheck Function
Post by: SNiFFeR on September 21, 2004, 08:59 PM
Quote from: hismajesty[yL] on September 21, 2004, 07:20 PM
The Sony Vaio comes with office, but it doesn't come with the registration key (at least, I can't find it.) :P

I'm sure if you looked somewhere where you aren't suppose to you could find it ;).