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 raises RuntimeError. 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 get_character_type method is used to get the type of a specific character. The character can be specified as a string of one character, also a character outside the Basic Multilingual Plane, or as a Unicode code point (an int value). The method returns a value of the CharacterType enumeration; its name attribute gives the name, as the example below shows.
The count property returns the number of characters of a type other than CharacterType.SEPARATOR. Iterating over an Alphabet object returns the Unicode code points of these characters.
To set the type of characters in the alphabet, use the set_range method. The characters can be passed as a list of one-character strings or as a list of Unicode code points. Characters outside the Basic Multilingual Plane can be passed either way.
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 set_range method or by importing an alphabet from a file.
To export types of all characters to a file, use the export_dictionary method.
To import types of characters from a file, use the import_dictionary 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 error_occurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the alphabet.
fromgroupdocs.searchimportIndexfromgroupdocs.search.dictionariesimportCharacterType# Creating a new index in the specified folder; the alphabet can be changed only while the index contains no documentsindex=Index("./index",True)alphabet=index.dictionaries.alphabet# Getting the type of the hyphen characterhyphen_type=alphabet.get_character_type("-")print("The type of hyphen:",hyphen_type.name)ifhyphen_type!=CharacterType.BLENDED:# Setting the Blended type for the hyphen characteralphabet.set_range(["-"],CharacterType.BLENDED)# Setting the Letter type for characters outside the Basic Multilingual Plane specified as Unicode code pointsalphabet.set_range([0x1D400,0x1D401],CharacterType.LETTER)# Mathematical bold capital A and B# Setting the Letter type for a character outside the Basic Multilingual Plane specified as a stringalphabet.set_range(["\U0001D41A"],CharacterType.LETTER)# Mathematical bold small A# Getting the types of characters specified as a Unicode code point and as a stringprint("The type of U+1D400:",alphabet.get_character_type(0x1D400).name)print("The type of U+1D41A:",alphabet.get_character_type("\U0001D41A").name)# Getting the number of characters that are not separatorsprint("Characters that are not separators:",alphabet.count)# Exporting the alphabet to a filealphabet.export_dictionary("./Alphabet.dat")# Indexing documents; after that, the alphabet of the index cannot be changedindex.add("./documents")
The following example demonstrates how to create another index with the same alphabet by importing the alphabet exported in the previous example.
fromgroupdocs.searchimportIndex# Creating a new index in the specified foldernew_index=Index("./new-index",True)# Setting the Separator type for all characters, so that only the imported types remain in the alphabetnew_index.dictionaries.alphabet.clear()# Importing the alphabet from a filenew_index.dictionaries.alphabet.import_dictionary("./Alphabet.dat")print("Characters that are not separators:",new_index.dictionaries.alphabet.count)# Indexing documentsnew_index.add("./documents")