typing
TokenType
dataclass
Base class representing the type of a token
Source code in genlm/control/typing.py
check(value)
is_iterable_of(element_type)
Check if this type can be interpreted as an iterable of element_type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element_type
|
TokenType
|
The type to check if this is an iterable of |
required |
Examples:
>>> Sequence(Atomic(int)).is_iterable_of(Atomic(int))
True
>>> Atomic(bytes).is_iterable_of(Atomic(int))
True
Source code in genlm/control/typing.py
Atomic
dataclass
Bases: TokenType
Represents a simple type like int or str
Source code in genlm/control/typing.py
Sequence
dataclass
Bases: TokenType
Represents a list/sequence of another type
Source code in genlm/control/typing.py
infer_type(value)
Infer the TokenType from a value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
A sample value to infer type from |
required |
Returns:
Type | Description |
---|---|
TokenType
|
The inferred type |
Examples:
>>> infer_type(42)
Atomic(type=int)
>>> infer_type([1, 2, 3])
Sequence(element_type=Atomic(type=int))
>>> infer_type([[1, 2], [3, 4]])
Sequence(element_type=Sequence(element_type=Atomic(type=int)))
Source code in genlm/control/typing.py
infer_vocabulary_type(vocabulary)
Infer the TokenType from a vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocabulary
|
List[Any]
|
A list of tokens to infer type from |
required |
Returns:
Type | Description |
---|---|
TokenType
|
The inferred type |
Raises:
Type | Description |
---|---|
ValueError
|
If vocabulary is empty or contains inconsistent types |
Examples:
>>> infer_vocabulary_type([1, 2, 3])
Atomic(type=int)
>>> infer_vocabulary_type([[1, 2], [3, 4]])
Sequence(element_type=Atomic(type=int))