(NET) NET (2019)

<< Multi Languages Spell Checker for webmaster. Part 5. Multilang asynchronous spell checker with NHunspell. <<


Sorry, no time for more details description, only code.



   1:  Imports System.Runtime.CompilerServices
   2:   
   3:  Module RichTextBoxExtSync
   4:   
   5:      <Extension>
   6:      Public Sub AppendColorText(R As RichTextBox, Txt As String, C As Color)
   7:          Dim Start1 As Integer = R.TextLength
   8:          R.AppendText(Txt)
   9:          Dim End1 As Integer = R.TextLength
  10:          R.Select(Start1, End1 - Start1)
  11:          R.SelectionColor = C
  12:      End Sub
  13:   
  14:  End Module

   1:  Imports System.Runtime.CompilerServices
   2:   
   3:  Module RichTextBoxExt
   4:   
   5:      <Extension>
   6:      Public Sub AppendTextAsync(R As RichTextBox, Txt As String)
   7:          R.Invoke(Sub() R.AppendText(Txt))
   8:      End Sub
   9:   
  10:   
  11:      <Extension>
  12:      Public Sub AppendColorTextAsync(R As RichTextBox, Txt As String, C As Color)
  13:          Dim Start1 As Integer = CInt(R.Invoke(Function() R.TextLength))
  14:          R.Invoke(Sub() R.AppendText(Txt))
  15:          Dim End1 As Integer = CInt(R.Invoke(Function() R.TextLength))
  16:          R.Invoke(Sub() R.Select(Start1, End1 - Start1))
  17:          R.Invoke(Sub() R.SelectionColor = C)
  18:      End Sub
  19:   
  20:  End Module

   1:  Imports System.ComponentModel
   2:  Imports System.Text.RegularExpressions
   3:  Imports NHunspell
   4:  'https://www.rdocumentation.org/packages/hunspell/versions/3.0/topics/hunspell
   5:   
   6:  Imports RichTextBoxExt
   7:   
   8:  Public Class SpellForm
   9:   
  10:      Dim EnChecker As Hunspell
  11:      Dim RuChecker As Hunspell
  12:      Dim UaChecker As Hunspell
  13:      Dim Words As String()
  14:      Dim WithGoodWord As Boolean = True
  15:      Dim WithEvents BGW1 As New BackgroundWorker
  16:      Dim BGW1_Prm As New Object
  17:   
  18:   
  19:      Private Sub SpellForm_Load(sender As Object, e As EventArgs) Handles Me.Load
  20:          RichTextBox1.WordWrap = False
  21:          EnChecker = New Hunspell("E:\Projects\SpellChecker\SpellChecker\dictionaries\en-US\index.aff", "E:\Projects\SpellChecker\SpellChecker\dictionaries\en-US\index.dic")
  22:          RuChecker = New Hunspell("E:\Projects\SpellChecker\SpellChecker\dictionaries\ru\ru_RU.aff", "E:\Projects\SpellChecker\SpellChecker\dictionaries\ru\ru_RU.dic")
  23:          UaChecker = New Hunspell("E:\Projects\SpellChecker\SpellChecker\dictionaries\uk\index.aff", "E:\Projects\SpellChecker\SpellChecker\dictionaries\uk\index.dic")
  24:          Me.Show()
  25:          Me.BringToFront()
  26:      End Sub
  27:   
  28:      Public Sub Check(TXT As String)
  29:          Application.DoEvents()
  30:          If TXT IsNot Nothing Then
  31:              If Not BGW1.IsBusy Then
  32:                  TextBox1.Clear()
  33:                  TextBox1.Text = TXT
  34:                  TextBox1.Refresh()
  35:                  RichTextBox1.Clear()
  36:                  RichTextBox1.Refresh()
  37:   
  38:                  BGW1_Prm = TXT
  39:                  BGW1.RunWorkerAsync(BGW1_Prm)
  40:              End If
  41:              Me.BringToFront()
  42:              'FillRichTextBlock(TXT)
  43:          End If
  44:      End Sub
  45:   
  46:      Private Sub BGW1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW1.DoWork
  47:          FillRichTextBlock(e.Argument)
  48:      End Sub
  49:   
  50:   
  51:      Sub FillRichTextBlock(TXT As String)
  52:          Dim TXT1 As String = TXT.Replace("  ", " ").Replace(vbCrLf, " ")
  53:          Words = TXT1.Split(" ")
  54:          Words = Words.Distinct.ToArray
  55:          For i As Integer = 0 To Words.Count - 1
  56:              Dim CharsToTrim As Char() = {",", ":", ";", "/", "\", """", "'", "$", "*", ".", "?", "-", "(", ")"}
  57:              Dim CurWord As String = Words(i).Trim.TrimEnd(CharsToTrim).TrimStart(CharsToTrim).Trim
  58:              If Not String.IsNullOrEmpty(CurWord) Then
  59:                  Me.BeginInvoke(Sub() Text = "SpellChecker (" & i & "/" & Words.Count - 1 & ")")
  60:                  RichTextBox1.AppendTextAsync(vbCrLf)
  61:                  Dim EnCheck As Boolean
  62:                  Dim RuCheck As Boolean
  63:                  Dim UaCheck As Boolean
  64:                  If Regex.IsMatch(CurWord, "^[a-zA-Z0-9\s]+$") Then
  65:                      EnCheck = EnChecker.Spell(CurWord)
  66:                      RuCheck = False
  67:                      UaCheck = False
  68:                  Else
  69:                      RuCheck = RuChecker.Spell(CurWord)
  70:                      UaCheck = UaSpellOk(CurWord)
  71:                      EnCheck = False
  72:                  End If
  73:   
  74:                  If EnCheck Then
  75:                      If WithGoodWord Then
  76:                          RichTextBox1.AppendColorTextAsync("(en) " & CurWord, Color.Gray)
  77:                      End If
  78:                  ElseIf RuCheck And Not UaCheck Then
  79:                      If WithGoodWord Then
  80:                          RichTextBox1.AppendColorTextAsync("(ru) " & CurWord, Color.Gray)
  81:                      End If
  82:                  ElseIf UaCheck And Not RuCheck Then
  83:                      If WithGoodWord Then
  84:                          RichTextBox1.AppendColorTextAsync("(ua) " & CurWord, Color.Gray)
  85:                      End If
  86:                  ElseIf UaCheck And RuCheck Then
  87:                      If WithGoodWord Then
  88:                          RichTextBox1.AppendColorTextAsync("(ur) " & CurWord, Color.Gray)
  89:                      End If
  90:                  ElseIf CurWord.ToLower.StartsWith("http") Then
  91:                      If WithGoodWord Then
  92:                      End If
  93:                  Else
  94:                      RichTextBox1.AppendColorTextAsync("(??) " & CurWord, Color.Red)
  95:                      If Regex.IsMatch(CurWord, "^[a-zA-Z0-9\s]+$") Then
  96:                          Dim EnProposal As List(Of String) = EnChecker.Suggest(CurWord)
  97:                          If EnProposal.Count > 0 Then
  98:                              RichTextBox1.AppendColorTextAsync(vbCrLf & "(en) " & CurWord, Color.Blue)
  99:                              For J As Integer = 0 To EnProposal.Count - 1
 100:                                  RichTextBox1.AppendColorTextAsync(" " & EnProposal(J), Color.Blue)
 101:                              Next
 102:                          End If
 103:                      Else
 104:                          Dim UaProposal As List(Of String) = UaChecker.Suggest(CurWord)
 105:                          If UaProposal.Count > 0 Then
 106:                              RichTextBox1.AppendColorTextAsync(vbCrLf & "(ua) " & CurWord, Color.Blue)
 107:                              For J As Integer = 0 To UaProposal.Count - 1
 108:                                  RichTextBox1.AppendColorTextAsync(" " & UaProposal(J), Color.Blue)
 109:                              Next
 110:                          End If
 111:                          Dim RuProposal As List(Of String) = RuChecker.Suggest(CurWord)
 112:                          If RuProposal.Count > 0 Then
 113:                              RichTextBox1.AppendColorTextAsync(vbCrLf & "(ru) " & CurWord, Color.Blue)
 114:                              For J As Integer = 0 To RuProposal.Count - 1
 115:                                  RichTextBox1.AppendColorTextAsync(" " & RuProposal(J), Color.Blue)
 116:                              Next
 117:                          End If
 118:                      End If
 119:   
 120:   
 121:                  End If
 122:              End If
 123:          Next
 124:      End Sub
 125:   
 126:      Function UaSpellOk(CurWord As String) As Boolean
 127:          Dim Sugg As List(Of String) = UaChecker.Suggest(CurWord)
 128:          If Sugg.Count > 0 Then
 129:              For Each One As String In Sugg
 130:                  If One = CurWord Then Return True
 131:              Next
 132:              Return False
 133:          Else
 134:              Return False
 135:          End If
 136:      End Function
 137:   
 138:      Private Sub FindToolStripButton_Click(sender As Object, e As EventArgs) Handles FindToolStripButton.Click
 139:          Try
 140:              If RichTextBox1.SelectionStart > 0 Then
 141:                  Dim CurLineNum As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
 142:                  Dim M As Match = Regex.Match(RichTextBox1.Lines(CurLineNum), " .+")
 143:                  Dim SearchWord As String = Replace(M.Value, " ", "")
 144:                  Dim X As MainForm = ParentForm
 145:                  X.SearchWord(SearchWord)
 146:              End If
 147:          Catch ex As Exception
 148:              AutoClosingMessageBox.Show("Please wait")
 149:          End Try
 150:      End Sub
 151:   
 152:      Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
 153:          If RichTextBox1.SelectionStart > 0 Then
 154:              Dim CurLineNum As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
 155:              Dim M As Match = Regex.Match(RichTextBox1.Lines(CurLineNum), " .+")
 156:              Dim SearchWord As String = Replace(M.Value, " ", "")
 157:              SelectToolStripLabel.Text = SearchWord
 158:          End If
 159:      End Sub
 160:   
 161:      Private Sub BGW1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
 162:          RichTextBox1.SelectionStart = 0
 163:          RichTextBox1.SelectionLength = 0
 164:          RichTextBox1.ScrollToCaret()
 165:      End Sub
 166:  End Class


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/SpellChecker/AsyncNHunspell.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>