Alphabet

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.

from groupdocs.search import Index
from groupdocs.search.dictionaries import CharacterType

# Creating a new index in the specified folder; the alphabet can be changed only while the index contains no documents
index = Index("./index", True)

alphabet = index.dictionaries.alphabet

# Getting the type of the hyphen character
hyphen_type = alphabet.get_character_type("-")
print("The type of hyphen:", hyphen_type.name)

if hyphen_type != CharacterType.BLENDED:
    # Setting the Blended type for the hyphen character
    alphabet.set_range(["-"], CharacterType.BLENDED)

# Setting the Letter type for characters outside the Basic Multilingual Plane specified as Unicode code points
alphabet.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 string
alphabet.set_range(["\U0001D41A"], CharacterType.LETTER)  # Mathematical bold small A

# Getting the types of characters specified as a Unicode code point and as a string
print("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 separators
print("Characters that are not separators:", alphabet.count)

# Exporting the alphabet to a file
alphabet.export_dictionary("./Alphabet.dat")

# Indexing documents; after that, the alphabet of the index cannot be changed
index.add("./documents")

The following sample files are used in this example: documents/einstein-notes.txt, documents/newton.txt, documents/curie.txt

The type of hyphen: SEPARATOR
The type of U+1D400: LETTER
The type of U+1D41A: LETTER
Characters that are not separators: 2337

Download full output

The following example demonstrates how to create another index with the same alphabet by importing the alphabet exported in the previous example.

from groupdocs.search import Index

# Creating a new index in the specified folder
new_index = Index("./new-index", True)

# Setting the Separator type for all characters, so that only the imported types remain in the alphabet
new_index.dictionaries.alphabet.clear()

# Importing the alphabet from a file
new_index.dictionaries.alphabet.import_dictionary("./Alphabet.dat")
print("Characters that are not separators:", new_index.dictionaries.alphabet.count)

# Indexing documents
new_index.add("./documents")

The following sample files are used in this example: documents/einstein-notes.txt, documents/newton.txt, documents/curie.txt

Characters that are not separators: 2337

Download full output

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.