• Welcome to Valhalla Legends Archive.
 

Automatic SpellCheck Function

Started by Dyndrilliac, September 20, 2004, 11:50 AM

Previous topic - Next topic

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

UserLoser.

#1
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

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

K

#3
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



Tuberload

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.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Dyndrilliac

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
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

K

#6
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.

Banana fanna fo fanna

You should store the changes in a dictionary. Then think about using Google's spellcheck.

MyndFyre

Also, be careful with replacing "ur" with "your."  Sometimes people use "ur" for "you're."
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Soul Taker

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

hismajesty

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.

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

DeTaiLs

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



hismajesty

The Sony Vaio comes with office, but it doesn't come with the registration key (at least, I can't find it.) :P

SNiFFeR

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 ;).