An instance of the Alphabet class contains the types of characters used for indexing. The alphabet of an index is available through the Alphabet property of the Index.Dictionaries object. For detailed information on the types of characters and the default content of the alphabet, see the Character types page.
Note
The alphabet defines how the text of documents is divided into terms. Therefore, the alphabet of an index can be changed only while the index contains no documents and is not performing an operation; otherwise, a method that changes the alphabet throws InvalidOperationException. A call that does not change the content of the alphabet is allowed. Configure the alphabet after creating a new index and before adding documents. See also the Index format and compatibility page.
Since version 26.11, the default alphabet of a new index also defines as letters the combining diacritical marks and the letters, marks and digits of Hebrew, Devanagari, Georgian, the extended Latin and Greek blocks and Hangul (see Character types). An index keeps the alphabet that it was created with, so an index created by an earlier version, also after it is updated with the IndexUpdater class, divides the words of these scripts as before.
The GetCharacterType method is used to get the type of a specific character. The method has three overloads: the character can be specified as a char value, as a Unicode code point (an int value), or as a string that contains a character or a surrogate pair (a character outside the Basic Multilingual Plane).
The Count property returns the number of characters of a type other than CharacterType.Separator. Enumerating an Alphabet object returns the Unicode code points of these characters.
To set the type of characters in the alphabet, use the SetRange method. The characters can be passed as an array of char values, as an array of Unicode code points, or as an array of strings, each containing a character or a surrogate pair. The last two overloads allow you to set the type of characters outside the Basic Multilingual Plane. Since version 26.11, the method throws ArgumentOutOfRangeException for a type that is not a member of the CharacterType enumeration and for a code point that is less than 0 or greater than 0x10FFFF; the alphabet is not changed.
To set the CharacterType.Separator type for all characters in the alphabet, use the Clear method. Note that after clearing, the alphabet contains no letters, so no words can be indexed. Call this method only when you are going to define all the required letters afterwards, for example, with the SetRange method or by importing an alphabet from a file.
To export types of all characters to a file, use the ExportDictionary method.
To import types of characters from a file, use the ImportDictionary method. The imported types are set for the characters listed in the file; the types of other characters are not changed. To make the alphabet exactly the same as the exported one, call the Clear method before importing. Errors of exporting and importing are reported by the ErrorOccurred event of the index instead of exceptions. Since version 26.11, a file with a type that is not a member of CharacterType or a value that is not a code point is not imported: the error is reported, and the alphabet is not changed.
The following example demonstrates the use of methods of the alphabet.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating a new index in the specified folder; the alphabet can be changed only while the index contains no documentsIndexindex=newIndex(indexFolder,true);// Getting the type of the hyphen characterCharacterTypehyphenType=index.Dictionaries.Alphabet.GetCharacterType('-');Console.WriteLine("The type of hyphen: "+hyphenType);if(hyphenType!=CharacterType.Blended){// Setting the Blended type for the hyphen characterindex.Dictionaries.Alphabet.SetRange(newchar[]{'-'},CharacterType.Blended);}// Setting the Letter type for characters outside the Basic Multilingual Plane specified as Unicode code pointsindex.Dictionaries.Alphabet.SetRange(newint[]{0x1D400,0x1D401},CharacterType.Letter);// Mathematical bold capital A and B// Setting the Letter type for a character specified as a surrogate pairindex.Dictionaries.Alphabet.SetRange(newstring[]{"\U0001D41A"},CharacterType.Letter);// Mathematical bold small A// Getting the types of characters specified as a Unicode code point and as a surrogate pairConsole.WriteLine("The type of U+1D400: "+index.Dictionaries.Alphabet.GetCharacterType(0x1D400));Console.WriteLine("The type of U+1D41A: "+index.Dictionaries.Alphabet.GetCharacterType("\U0001D41A"));// Getting the number of characters that are not separatorsConsole.WriteLine("Characters that are not separators: "+index.Dictionaries.Alphabet.Count);// Exporting the alphabet to a fileindex.Dictionaries.Alphabet.ExportDictionary(@"c:\Alphabet.dat");// Indexing documents; after that, the alphabet of the index cannot be changedindex.Add(documentsFolder);
To create a new index with the alphabet of another index, export the alphabet of the other index and, in the new index, call the Clear method and then the ImportDictionary method before adding documents. Since version 26.11, this is needed, for example, to merge a new index with an index created by an earlier version: the default alphabets and tokenization dictionaries of these indexes differ, and the Merge method merges only indexes that divide text into words in the same way (see Merge indexes). The tokenization dictionary is taken in the same way. The following example demonstrates how to create a new index with the alphabet and the tokenization dictionary of an existing index.
C#
stringindexFolder=@"c:\MyIndex\";// An existing index, for example, created by an earlier versionstringnewIndexFolder=@"c:\MyNewIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Opening the existing index and exporting its alphabet and tokenization dictionary to filesIndexindex=newIndex(indexFolder);index.Dictionaries.Alphabet.ExportDictionary(@"c:\Alphabet.dat");index.Dictionaries.TokenizationDictionary.ExportDictionary(@"c:\TokenizationDictionary.dat");// Creating a new index in the specified folderIndexnewIndex=newIndex(newIndexFolder,true);// Setting the Separator type for all characters, so that only the imported types remain in the dictionariesnewIndex.Dictionaries.Alphabet.Clear();newIndex.Dictionaries.TokenizationDictionary.Clear();// Importing the dictionaries from the filesnewIndex.Dictionaries.Alphabet.ImportDictionary(@"c:\Alphabet.dat");newIndex.Dictionaries.TokenizationDictionary.ImportDictionary(@"c:\TokenizationDictionary.dat");// Indexing documents; the new index divides text into words as the existing index doesnewIndex.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: