The ToWord class

Class for converting strings to list[int] with specified alphabet.

An instance of this class is callable and used to convert from str to list[int]. The characters in the string are converted to integers according to their position in the alphabet used to construct a ToWord instance if one is provided, or using words.human_readable_index otherwise.

>>> from libsemigroups_pybind11 import ToWord
>>> to_word = ToWord("bac")
>>> to_word("bac")
[0, 1, 2]
>>> to_word("bababbbcbc")
[0, 1, 0, 1, 0, 0, 0, 2, 0, 2]

>>> to_word.init()    # clear the alphabet
<ToWord object with alphabet "">
>>> to_word("bac")
[1, 0, 2]

Contents

ToWord

Class for converting strings to list[int] with specified alphabet.

ToWord.alphabet(…)

Return the alphabet used for conversion.

ToWord.can_convert_letter(…)

Check if the current ToWord instance can convert a specified letter.

ToWord.copy(…)

Copy a ToWord object.

ToWord.empty(…)

Check if the alphabet is defined.

ToWord.init(…)

Overloaded function.

Full API

class ToWord
__init__(*args, **kwargs)

Overloaded function.

__init__(self: ToWord) None

Default constructor.

Constructs an empty object with no alphabet set.

__init__(self: ToWord, alphabet: str) None

Construct with given alphabet.

Construct a ToWord object with the given alphabet.

Parameters:

alphabet (str) – the alphabet.

Raises:

LibsemigroupsError – if there are repeated letters in alphabet.

__call__(self: ToWord, input: str) list[int]

Convert a string to a list[int].

This function converts its argument input into a list[int]. The characters of input are converted using the alphabet used to construct the object or set via ToWord.init() , or with words.human_readable_index if ToWord.empty() returns True.

Parameters:

input (str) – the string to convert.

Returns:

the converted list.

Return type:

list[int]

Raises:

LibsemigroupsError – if the alphabet used to define an instance of ToWord is not empty and input contains letters that do not correspond to letters of the alphabet.

alphabet(self: ToWord) str

Return the alphabet used for conversion.

This function returns a str corresponding to the ordered-set alphabet \(\{a_0, a_1, \dots, a_{n-1}\}\) that the initialised ToWord object will use to convert from str to list[int]. Specifically, \(a_i \mapsto i\) where \(a_i\) will correspond to a letter in a str, and \(i\) is an int.

If this function returns the empty string, then conversion will be performed using words.human_readable_index.

Returns:

The alphabet.

Return type:

str

can_convert_letter(self: ToWord, c: str) bool

Check if the current ToWord instance can convert a specified letter.

This function returns True if c can be converted to an int using this ToWord instance, and False otherwise.

Parameters:

c (str) – the letter to check the convertibility of.

Returns:

Whether the letter can be converted.

Return type:

bool

Raises:

LibsemigroupsError – if c is a multi-character string.

copy(self: ToWord) ToWord

Copy a ToWord object.

Returns:

A copy.

Return type:

ToWord

empty(self: ToWord) bool

Check if the alphabet is defined.

This function returns True if no alphabet has been defined, and False otherwise.

Returns:

Whether the alphabet is empty.

Return type:

bool

init(*args, **kwargs)

Overloaded function.

init(self: ToWord) ToWord

Initialize an existing ToWord object.

This function puts a ToWord object back into the same state as if it had been newly default constructed.

Returns:

self.

Return type:

ToWord

See also

ToWord()

init(self: ToWord, alphabet: str) ToWord

Initialize an existing ToWord object.

This function puts a ToWord object back into the same state as if it had been newly constructed from alphabet.

Parameters:

alphabet (str) – the alphabet.

Returns:

self.

Return type:

ToWord

Raises:

LibsemigroupsError – if there are repeated letters in alphabet.

See also

ToWord(str)