Spelling custom dictionary

I'm new to Syncfusion and trying to get some tests of the spell checker up and running to integrate it in my app but I can't get the custom dictionary to work and I'm not completely sure I'm doing the other part correctly either.  I did post this question on StackOverflow but it hasn't gotten much response there and I saw that SF has their own forums so thought I would post here directly.  Here is the issue:

I'm looking at changing my spell checking implementation to use Syncfusion spell checking. I've got it mostly working but have one question and one issue. First my code for the test app. I followed the sample code here and got that working, then I wanted to apply it to multiple textboxes on the same window as that is how it will be in my actual application. Last I tried to activate a custom dictionary.

Here is my XAML code for the app window:

<Window x:Class="SyncfusionWpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Icon="App.ico">
<Grid>
    <StackPanel>
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
    </StackPanel>
</Grid>

Here is the code behind:

using System.Windows;
using System.Windows.Controls;
using Syncfusion.Windows.Controls;

namespace SyncfusionWpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void tb_Loaded(object sender, RoutedEventArgs e)
        {
            var spellChecker = new SfSpellChecker
            {
                CustomDictionaryPath =
                    @"C:\Users\sfaust\Source\Repos\SFTest1\SyncfusionWpfApp1\SyncfusionWpfApp1\bin\Debug\testdictionary.txt"
            };
            var textBoxSpellEditor = new TextBoxSpellEditor((TextBox)sender);
            spellChecker.PerformSpellCheckUsingContextMenu(textBoxSpellEditor);
        }
    }
}

Ok so first question. Once I moved to multiple controls it seemed like it didn't work right unless I create a new spell checker and new IEditorProperties (the TextBoxSpellEditor class) for each textbox and applied it (hence initializing it in loaded above instead of in the window like the example). Well this seems to work it seems pretty inefficient as well. My application will potentially have quite a few text boxes in it as they are inside a tree view of items so I'm a little concerned about the efficiency of having potentially hundreds of spell checkers instantiated (though I haven't tried a stress test so maybe I'm concerned unnecessarily). Is this the correct way to do this?

Second question is more of an issue. I don't see anything indicating the custom dictionary is working. I created the file at the path shown and put a few random "words" in it that it identified (correctly) as misspelled but even after putting them in that file and setting the custom dictionary path property it still identifies them as misspelled. I also don't have the 'AddToDictionary' option on the context menu. I also tried to just set the property without actually creating the file in case it wanted to create the file itself but no change. Last I tried both relative and absolute paths but no change there either. Is there something I am missing on how to activate the custom dictionary?


12 Replies

SK Senthil Kumaran Rajan Syncfusion Team February 17, 2020 12:47 PM UTC

Hi Stephen, 
 
Thank you for contacting Syncfusion support. 
 
Query 1 : Once I moved to multiple controls it seemed like it didn't work right unless I create a new spell checker and new IEditorProperties 
 
Instead of creating new instances every time for the SfSpellChecker you can pass the current text box control in the ControlToSpellCheck which is a control property. After that you can call the PerformSpellCheckUsingContextMenu. So, you can obtain the spell checker results with a single instance. 
 
private void OnGotFocus(object sender, RoutedEventArgs e) 
        {​ 
            TextBox textBox = sender as TextBox;​ 
            if (this.SpellEditor == null)​ 
            {​ 
                SpellEditor = new TextSpellEditor(textBox);​ 
            }​ 
            else​ 
                SpellEditor.ControlToSpellCheck = textBox;​ 
            SpellChecker.PerformSpellCheckUsingContextMenu(SpellEditor);​ 
        } 
 
Query 2 : I don't see anything indicating the custom dictionary is working 
 
We are analyzing the reported behavior in spell checker, we will update the details before the end of Feb 19, 2020. 
 
Regards, 
Senthil 



SK Senthil Kumaran Rajan Syncfusion Team February 19, 2020 06:45 AM UTC

Hi Stephen, 
 
Thank you for your patience. 
 
Query 2 : I don't see anything indicating the custom dictionary is working  
 
We don't need to give entire path of a file in “CustomDictionaryPath” property. We can simply give the file name(For example: if the file name is "CustomDict.text", you can give like CustomDictionaryPath = @"customDict.text"). If the custom dictionary file is present inside subfolder of a project file you have to give only the folder path as well as the file name(For example: The custom dictionary file is present inside the Resource/Dictionary folder, you can give like CustomDictionaryPath =@"Resource/Dictionary/CustomDict.txt"). Please refer the below kb for further reference.
 
 
Regards, 
Senthil 



SF Stephen Faust February 19, 2020 06:50 AM UTC

Ok that seems to work mostly.  However I had to put the code in both the 'GotFocus' event and the 'Loaded' event otherwise it wouldn't spell check until you actually gave the TB focus, it wouldn't do it when you first started the program.  However once I put it in both events it worked.

As far as the custom dictionary I still don't see anything that shows that working so will be interested in what you find.

I also am noticing another oddity here as I'm testing things out.  If the misspelled word is the LAST word in the textbox and I right click it it doesn't give me the context menu, it just puts the cursor at the beginning of the word and does nothing.  Any other word I right click gives me the context menu with suggestions (though seems to be slow coming up sometimes).  I can also highlight the end word in full and then double click and it does give me the context menu.  Here is a quick screen video of what I'm seeing:



SF Stephen Faust replied to Senthil Kumaran Rajan February 19, 2020 06:54 AM UTC

Hi Stephen, 
 
Thank you for your patience. 
 
Query 2 : I don't see anything indicating the custom dictionary is working  
 
We don't need to give entire path of a file in “CustomDictionaryPath” property. We can simply give the file name(For example: if the file name is "CustomDict.text", you can give like CustomDictionaryPath = @"customDict.text"). If the custom dictionary file is present inside subfolder of a project file you have to give only the folder path as well as the file name(For example: The custom dictionary file is present inside the Resource/Dictionary folder, you can give like CustomDictionaryPath =@"Resource/Dictionary/CustomDict.txt"). Please refer the below kb for further reference.
 
 
Regards, 
Senthil 


I saw that but I tried relative path as well as absolute and it didn't make a difference.  I assume this needs to be a file on the file system correct and not an embedded resource built into the project right?


SK Senthil Kumaran Rajan Syncfusion Team February 20, 2020 11:48 AM UTC

Hi Stephen, 
 
In SfSpellChecker, we should add the file as a resource file in project to make the customer dictionary work. The file should be added as like below screenshot in our project.  
 
   
 
Regards, 
Senthil 



SF Stephen Faust February 20, 2020 04:16 PM UTC

Hm, so you can't use an external file?  I have quite a few users with industry specific words that have dictionaries developed that they would like to use, is there a way to do that?  I can't build it in in that case because they can be different per customer.  Is there a way to manually add words to the dictionary?


SK Senthil Kumaran Rajan Syncfusion Team February 21, 2020 11:05 AM UTC

Hi Stephen, 
 
In SfSpellChecker, we currently don’t have direct support to load the dictionary file from the outside of application. If you want to load resource file different location, we can achieve this by using the reflection and assign the dictionary to the spell checker as per the given code snippet.  
  
For example, if the custom dictionary is present in  this location.(D:\CustomDictionary\CustomDict.txt) 
 
Code Example[C#] 
Stream fileStream = new FileStream(@"D:\CustomDictionary\CustomDict.txt", FileMode.Open); 
spellChecker.GetType().GetField("checker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(spellChecker, new SpellCheckerBase(fileStream  ) ); 
 
Regards, 
Senthil 



SF Stephen Faust February 25, 2020 06:16 PM UTC

Sorry for the delay in response.  Unfortunately this seems to be only partly working.  Here is my current testing code:

    public partial class MainWindow
    {
        readonly SfSpellChecker _spellChecker;
        IEditorProperties _textSpellEditor;

        public MainWindow()
        {
            InitializeComponent();

            _spellChecker = new SfSpellChecker();
            LoadCustomDictionary();
        }

        void tb_Loaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is TextBox tb)) return;

            if (_textSpellEditor == null) _textSpellEditor = new TextBoxSpellEditor(tb);
            else _textSpellEditor.ControlToSpellCheck = tb;

            _spellChecker.PerformSpellCheckUsingContextMenu(_textSpellEditor);
        }

        void tb_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!(sender is TextBox tb)) return;

            if (_textSpellEditor == null) _textSpellEditor = new TextBoxSpellEditor(tb);
            else _textSpellEditor.ControlToSpellCheck = tb;

            _spellChecker.PerformSpellCheckUsingContextMenu(_textSpellEditor);
        }

        void LoadCustomDictionary()
        {
            var filePath = Path.Combine(Path.GetDirectoryName(typeof(MainWindow).Assembly.Location),
                                        "testdictionary.txt");
            Stream fileStream = new FileStream(filePath, FileMode.Open);
            _spellChecker
                .GetType()
                .GetField("checker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?
                .SetValue(_spellChecker, new SpellCheckerBase(fileStream));
        }
    }

When I step trough the last method it does successfully load the file stream and I can see that the non public 'checker' property is set.  I can also verify that when it gets to the step to perform the spell check that it has the updated checker and if I drill down to the words you will see that there is only the one word I am using for testing:



This all seems like it would be correct.  However, when the UI opens it still shows a misspell on that word and doesn't have any custom dictionary options (add to dictionary, etc.).  I even tried to put in the text file as a resource and set the custom dictionary path to that as well but it still doesn't show me dictionary options.  I would also think this would now show every other word as misspelled since the dictionary only contains 1 word now but it's not...  

Upon further investigation of this it seems that the dictionary IS being picked up for suggestions but not the 'red squigglies' that tell me there is a spelling error.  I can right click my test word that shows the misspell indicator and it suggests the same word.  I take the suggestion and it temporarily removes the squiggle but it comes back as soon as the textbox loses focus.  Other items shown as misspelled show no suggestions.  So this seems like the suggestions are now using the custom dictionary (and only the custom dictionary) but whatever makes the squigglies is using the built in dictionary and only the built in dictionary...

Ideally I would like to have it use the custom dictionary from a file path on disk and ADD those words to the built in dictionary...

Edit: unrelated but I don't see a way to edit this post to properly format the code... obviously I did it before but I can't find a control for it now... what am I missing?


UN Unknown Syncfusion Team February 26, 2020 10:47 AM UTC

Hi Stephen, 

Thanks for your update. 

We have checked your queries  physical file patch not restricted to embedded resources”, “Words added to dictionary remains in file (Custom dictionary) when application is restarted” and “Need an option in context menu to add words in dictionary”. And we have logged these as defects. The fix for these defects will be included in Volume 1 release which will roll out by end of March 2020. Please refer the below feedback link for the same. 


Regards, 
Niranjan Kumar 



SF Stephen Faust February 26, 2020 05:45 PM UTC

Ok great thank you!  Feel free to reach out here or via email if you would like any additional feedback or testing, etc. regarding this from me.


UN Unknown Syncfusion Team February 27, 2020 05:05 AM UTC

Hi Stephen, 

Thanks for your update. 

As you have mentioned in your last update, we will contact you if we require further information regarding this query. 

Regards, 
Niranjan Kumar 



UN Unknown Syncfusion Team April 2, 2020 10:53 AM UTC

Hi Stephen, 
 
Thanks for your patience. 
 
We are glad to announce that our Essential Studio Volume 1, 2020 release (v18-1-0-42) is rolled out and is available for download under the following link. 
 
 
We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance. 
 
Note :   By default, the MS controls have the Spell check functionalities. It will highlight the error words with the red wavy underline on its own based on built-in dictionary and custom dictionaries added to it. As this custom dictionary should be embedded within a project, we cannot change it at runtime. Hence providing ‘Add to Dictionary’ in context menu for a TextBox control is not possible. So “Need an option in context menu to add words in dictionary ” is declined. 
 
Regards, 
Niranjan Kumar 
 


Loader.
Up arrow icon