Developer forum

Forum » Ecommerce - Standard features » Best spelling using new index

Best spelling using new index

Mario Santos
Reply

Hi everyone,

We are updating the old index to the new one.

Using the old index we have tags when there aren't products found like:

  • Ecom:Search.BestSpellingCorrection.CorrectedPhrase
  • Ecom:Search.BestSpellingCorrection.OriginalPhrase
  • example: https://www.magpul.com/search-results?PID=88&eComQuery=remingten&ProductNumber=remingten

I can't find any similar tags using the new index. Does anyone have any suggestions?

 

Best Regards,

Mário Santos


Replies

 
Nicolai Høeg Pedersen
Reply

It has not been implemented in the new index.

You can get the spelling suggestions using the API and loop it your self in a Razor template:

Dynamicweb.Extensibility.Searching.eCommerce.FilterManager.GetSpellingCorrections(phrase, 5)

 
Mario Santos
Reply

Hi Nicolai,

This worked when I've ran the old index.

We want to remove the old index, is there any way to get this functionality without the old index?

 

Best Regards,

Mário

 
Nicolai Høeg Pedersen
Reply

Hi Mario

That is my suggestion - it is in the olde index API, but it does not rely on the old index data - so you can use my suggestion together with the new index.

 
Mario Santos
Reply

Hi Nicolai,

 

I don't think that's accurate. I tried to use that method and gave me 0 results. Then I simply tried create and running the old index and I started to get results. So it looks like it does rely on the old index data.

 

Can you confirm?

 

Best Regards,

Mário

 
Nicolai Høeg Pedersen
Reply

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, "&nbsp;", " ") ' 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

 
Mario Santos
Reply

Hi Nicolai,

 

Thank you. We'll give it a try but will this be included in a future version in the new index?

 

BR, Mário

 
Nicolai Høeg Pedersen
Reply

Hi Mario

Maybe it will, but it has not been planned.

NP

 
Mario Santos
Reply

Ok... Thanks once again!

BR, Mário

 

You must be logged in to post in the forum