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.
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.
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.
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);
The following example demonstrates how to create another index with the same alphabet by importing the alphabet exported in the previous example.
C#
stringnewIndexFolder=@"c:\MyNewIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// 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 alphabetnewIndex.Dictionaries.Alphabet.Clear();// Importing the alphabet from a filenewIndex.Dictionaries.Alphabet.ImportDictionary(@"c:\Alphabet.dat");// Indexing documentsnewIndex.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: