TX Spell .NET for WPF

Looking Up Synonyms in Code

Topic

A word, its meanings, and for each meaning the words that say the same: that is what TX Spell .NET returns for a word you pass to it in code. The method is the same on the TXSpellChecker of your application and on the TXSpell class for code without a user interface, and it works for every language with a synonym list. You build a console application that looks up German synonyms and prints them.

The Three Overloads

CreateSynonyms returns an array of SynonymGroup objects, one per meaning:

OverloadUses
CreateSynonyms(string text)The lists in SynonymLists
CreateSynonyms(string text, CultureInfo language)The lists of that language in SynonymLists and in the default folder
CreateSynonyms(string text, SynonymList synonymList)The one list you pass, which must be in SynonymLists

Only lists whose IsCreateSynonymsEnabled property is true are used. A word the lists don't know returns an empty array, and the lookup ignores case, so "Haus" and "haus" give the same result. The TXSpellChecker of WPF has the same method: CreateSynonyms.

Create the Console Application

  1. Create a console application with the package TXTextControl.TXSpell.Core.SDK, as in Checking Text in Code:

    dotnet new console -n Synonyms
    cd Synonyms
    dotnet add package TXTextControl.TXSpell.Core.SDK
    
  2. Download the German language archive, dictionary.de_DE.zip, copy th_de_DE_v2.dat and th_de_DE_v2.idx from its folder SynonymLists into a folder SynonymLists of the project, and let the build copy them next to the English list of the package, as Synonym Lists shows.

  3. Replace the contents of Program.cs with this code:

    using System;
    using System.Globalization;
    using TXTextControl.Proofing;
    
    TXSpell spell = new TXSpell();
    
    // Initialize the new instance before using it
    spell.Create();
    
    Console.WriteLine($"Available synonym lists: {string.Join(", ", spell.AvailableSynonymLists)}");
    
    // Synonyms from the German synonym lists only
    SynonymGroup[] groups = spell.CreateSynonyms("schnell", new CultureInfo("de-DE"));
    
    Console.WriteLine($"schnell: {groups.Length} groups");
    
    foreach (SynonymGroup group in groups)
    {
        Console.WriteLine($"- {string.Join(", ", Array.ConvertAll(group.Synonyms, synonym => synonym.Text))}");
    }
    
  4. Run the application with dotnet run. The console shows the two meanings of "schnell" and their synonyms:

    Available synonym lists: th_de_DE_v2.dat, th_en_US_v2.dat
    schnell: 2 groups
    - schnell (Hauptform), behänd(e), eilig, flink, flott, flugs, geschwind (veraltend), hurtig, munter, rapid, rapide, rasch, schleunig, speditiv (schweiz.), unerwartet schnell, Zeit sparend, zeitsparend, zügig, eilends (geh.), zeitnah (geh.), dalli (ugs.), fix (ugs.), in lebhafter Fortbewegungsweise (ugs., ironisch), mit Karacho (ugs.), mit Schmackes (ugs., regional), subito (ugs.), zackig (ugs.)
    - im Nu, in kurzer Frist, in kurzer Zeit, innert Kürze (schweiz., österr.), kurzfristig, kurzzeitig, schnell, zügig
    

What the Code Does

  • Create initializes the spell checker and loads the default synonym list, the US English one of the package. AvailableSynonymLists lists every list in the SynonymLists folder.
  • CreateSynonyms with a German CultureInfo loads the German list from the folder and uses the German lists only. English words would return no groups.
  • Each group has an array Synonyms of Synonym objects. Its first entry describes the meaning itself, here "schnell" and "im Nu".
  • Text is the synonym with the notes of the list, such as "(ugs.)" for colloquial words.

Name and Text

A Synonym has three properties for its text:

PropertyHoldsExample
NameThe synonym, in its casegeschwind
TextThe synonym with the notes of the listgeschwind (veraltend)
IDThe lowercase identifier in the listgeschwind

Use Name to replace a word in a document, and Text to show the user what the list says about a synonym. In the US English list, the notes include (similar term), (generic term) and (antonym), so antonyms are part of the groups; Text tells them apart.

A group also has a PartOfSpeech, such as (noun) or (verb) in the US English list, and null in lists that don't define one, such as the German list, and a Language, the language of the list it came from.

Use One List Only

The third overload takes a SynonymList, for example to offer the synonyms of a specialist thesaurus. Add the list to the spell checker first; a list that isn't in SynonymLists returns no groups:

SynonymList german = new SynonymList("th_de_DE_v2.dat");
spell.SynonymLists.Add(german);

SynonymGroup[] groups = spell.CreateSynonyms("schnell", german);

Synonym Lists describes the lists, their folder and how the default list is chosen.

Synonyms in TX Text Control

With a spell checker connected to TX Text Control, the Thesaurus Dialog of TX Text Control calls the same method for the selected word, and your code can add synonyms to the context menu of the editor. Synonyms in the Context Menu shows both.

Next Steps