Character replacements define how the text of documents is divided into terms. Therefore, the character replacement dictionary and the UseCharacterReplacements setting of an index can be changed only while the index contains no documents and is not performing an operation; otherwise, a change throws InvalidOperationException. A call that does not change the content of the dictionary is allowed. Configure character replacements after creating a new index and before adding documents. See also the Index format and compatibility page.
The Count property allows you to get the number of character replacements defined in the dictionary.
To add character replacements to the dictionary, use the AddRange method. The method accepts a collection of CharacterReplacementPair objects or a collection of key-value pairs. A pair can be created from two char values or from two Unicode code points (int values); the latter allows you to define replacements for characters outside the Basic Multilingual Plane. A replacement added for a character that already has one replaces the previous replacement. A pair whose replacement is the same as the character removes the replacement of the character.
To remove character replacements from the dictionary, the RemoveRange method is used. The characters can be specified as char values or as Unicode code points.
The Contains method is used to determine if the dictionary contains a replacement for the specified character.
To get a replacement for the specified character, use the GetReplacement method. The method returns the Unicode code point of the replacement, or the specified character itself if the dictionary contains no replacement for it.
To remove all replacements from the dictionary, use the Clear method.
To export all replacements to a file, use the ExportDictionary method.
To import character replacements from a file, use the ImportDictionary method. Errors of exporting and importing are reported by the ErrorOccurred event of the index instead of exceptions.
The following example demonstrates the use of the character replacement dictionary methods.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index settings instanceIndexSettingssettings=newIndexSettings();settings.UseCharacterReplacements=true;// Enabling character replacements during indexing// Creating a new index in the specified folder; the dictionary can be changed only while the index contains no documentsIndexindex=newIndex(indexFolder,settings,true);if(index.Dictionaries.CharacterReplacements.Contains('-')){// Getting the replacement for the hyphen characterintreplacement=index.Dictionaries.CharacterReplacements.GetReplacement('-');Console.WriteLine("The replacement for hyphen is "+char.ConvertFromUtf32(replacement));// Deleting the hyphen character replacement from the dictionaryindex.Dictionaries.CharacterReplacements.RemoveRange(newchar[]{'-'});}if(index.Dictionaries.CharacterReplacements.Count>0){// Deleting all character replacements from the dictionaryindex.Dictionaries.CharacterReplacements.Clear();}// Adding character replacements to the dictionaryCharacterReplacementPair[]pairs=newCharacterReplacementPair[]{newCharacterReplacementPair('Ä','a'),newCharacterReplacementPair('ä','a'),newCharacterReplacementPair(0x1D400,0x0061),// Mathematical bold capital A is replaced with the small letter a};index.Dictionaries.CharacterReplacements.AddRange(pairs);// Exporting character replacements to a fileindex.Dictionaries.CharacterReplacements.ExportDictionary(@"c:\CharacterReplacements.dat");// Importing character replacements from a fileindex.Dictionaries.CharacterReplacements.ImportDictionary(@"c:\CharacterReplacements.dat");// Indexing documents; after that, the character replacements of the index cannot be changedindex.Add(documentsFolder);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: