Posted on 16/02/2016 16:33:39
Hi Mario
Yes, I can see it checks the spelling against the index as well. It first looks up in the spell engine and then the index.
Search weighted also has a spelling engine - you can use the code from that maybe:
Private Const DICTIONARY_PATH As String = "/Admin/Content/Dictionary"
Private Sub SpellSuggest(ByVal Searchv1Form As Rendering.Template, ByVal q As String)
Dim Suggest As String = q
Dim SuggestHtml As String = q
Dim tmp As String = q
tmp = Regex.Replace(tmp, "<[^>]+>", " ") ' remove tags
tmp = Regex.Replace(tmp, " ", " ") ' spaces
tmp = Regex.Replace(tmp, "[\W-[']]+", " ") ' and non-word characters
tmp = Regex.Replace(tmp, "\s+", " ") ' replace two or more spaces with one
Using speller As Hunspell = SpellCreate()
For Each word As String In tmp.Split((" " & vbTab & vbLf).ToCharArray())
If Not String.IsNullOrEmpty(word) AndAlso word.Length > 1 AndAlso Not speller.Spell(word) Then
Dim suggestions As System.Collections.Generic.List(Of String) = speller.Suggest(word)
If Not suggestions Is Nothing AndAlso suggestions.Count > 0 Then
SuggestHtml = SuggestHtml.Replace(word, "<span class=""spellingchanged"">" & suggestions(0) & "</span>")
Suggest = Suggest.Replace(word, suggestions(0))
End If
End If
Next
End Using
If Not Suggest.Equals(q, StringComparison.InvariantCultureIgnoreCase) Then
Searchv1Form.SetTemplateValue("DwSpellSuggest", HttpContext.Current.Server.HtmlEncode(Suggest))
Searchv1Form.SetTemplateValue("DwSpellSuggest.Html", SuggestHtml)
Searchv1Form.SetTemplateValue("DwSpellSuggest.UrlEncoded", HttpContext.Current.Server.UrlEncode(Suggest))
End If
End Sub
Private Function SpellCreate() As NHunspell.Hunspell
Return New Hunspell( _
HttpContext.Current.Server.MapPath(String.Format("{0}/{1}/{1}.aff", DICTIONARY_PATH, SpellLanguage)), _
HttpContext.Current.Server.MapPath(String.Format("{0}/{1}/{1}.dic", DICTIONARY_PATH, SpellLanguage)))
End Function
Public Shared Function SpellLanguage() As String
Dim currentLanguage As String = String.Empty
If String.IsNullOrEmpty(currentLanguage) Then
'Try to use current backend culture
Dim currentCulture As CultureInfo = Base.GetCulture(forFrontend:=True)
If SpellAvailableLanguagesFromFileSystem().ContainsKey(currentCulture.ToString()) Then
currentLanguage = currentCulture.ToString()
End If
End If
If String.IsNullOrEmpty(currentLanguage) Then ' fallback to default language
currentLanguage = "en-US"
End If
Return currentLanguage
End Function
Private Shared Function SpellAvailableLanguagesFromFileSystem() As Dictionary(Of String, CultureInfo)
Dim languages As New Dictionary(Of String, CultureInfo)()
Try
For Each folder As String In IO.Directory.GetDirectories(HttpContext.Current.Server.MapPath(DICTIONARY_PATH))
Dim di As New IO.DirectoryInfo(folder)
Dim culture As CultureInfo
Try
culture = CultureInfo.GetCultureInfo(di.Name)
Catch ex As Exception
'Directory is not a culture
Continue For
End Try
languages.Add(di.Name, culture)
Next
Catch ex As Exception
'An error happened - return
End Try
Return languages
End Function