MCPcopy Create free account
hub / github.com/mindee/doctr / encode_string

Function encode_string

doctr/datasets/utils.py:69–89  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

67
68
69def 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
92def decode_sequence(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected