Given a predefined mapping, encode the string to a sequence of numbers Args: input_string: string to encode vocab: vocabulary (string), the encoding is given by the indexing of the character sequence Returns: A list encoding the input_string
(
input_string: str,
vocab: str,
)
| 67 | |
| 68 | |
| 69 | def encode_string( |
| 70 | input_string: str, |
| 71 | vocab: str, |
| 72 | ) -> list[int]: |
| 73 | """Given a predefined mapping, encode the string to a sequence of numbers |
| 74 | |
| 75 | Args: |
| 76 | input_string: string to encode |
| 77 | vocab: vocabulary (string), the encoding is given by the indexing of the character sequence |
| 78 | |
| 79 | Returns: |
| 80 | A list encoding the input_string |
| 81 | """ |
| 82 | try: |
| 83 | return list(map(vocab.index, input_string)) |
| 84 | except ValueError as e: |
| 85 | missing_chars = [char for char in input_string if char not in vocab] |
| 86 | raise ValueError( |
| 87 | f"Some characters cannot be found in 'vocab': {set(missing_chars)}.\n" |
| 88 | f"Please check the input string `{input_string}` and the vocabulary `{vocab}`" |
| 89 | ) from e |
| 90 | |
| 91 | |
| 92 | def decode_sequence( |
nothing calls this directly
no outgoing calls
no test coverage detected