Skip to content

symbol_table

Classes

SymbolTable

SymbolTable class. This class wraps the SymbolTable struct.

Source code in rustfst/symbol_table.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
class SymbolTable:
    """
    `SymbolTable` class. This class wraps the `SymbolTable` struct.
    """

    def __init__(self, ptr=None):
        """
        Creates an empty `SymbolTable`.
        """
        if ptr:
            self.ptr = ptr
        else:
            symt_ptr = ctypes.pointer(ctypes.c_void_p())
            ret_code = lib.symt_new(ctypes.byref(symt_ptr))
            err_msg = "__init__ failed"
            check_ffi_error(ret_code, err_msg)

            self.ptr = symt_ptr

    def add_symbol(self, symbol: str) -> int:
        """
        Adds a symbol to the table and returns the index.

        Args:
          symbol: A symbol unicode string.
        Returns:
          The integer key of the new symbol.
        """
        try:
            symbol = symbol.encode("utf-8")
        except UnicodeDecodeError:
            symbol = ctypes.c_char_p(symbol)

        integer_key = ctypes.c_size_t()
        ret_code = lib.symt_add_symbol(self.ptr, symbol, ctypes.byref(integer_key))
        err_msg = "`add_symbol` failed"
        check_ffi_error(ret_code, err_msg)

        return int(integer_key.value)

    def add_table(self, syms: SymbolTable):
        """
        This method merges another symbol table into the current table. All key
        values will be offset by the current available key.
        Args:
          syms: A `SymbolTable` to be merged with the current table.
        """
        ret_code = lib.symt_add_table(self.ptr, syms.ptr)
        err_msg = "`add_table` failed"
        check_ffi_error(ret_code, err_msg)

    def copy(self) -> SymbolTable:
        """
        Returns:
            A mutable copy of the `SymbolTable`.
        """
        clone = ctypes.pointer(ctypes.c_void_p())

        ret_code = lib.symt_copy(self.ptr, ctypes.byref(clone))
        err_msg = "`copy` failed."
        check_ffi_error(ret_code, err_msg)

        return SymbolTable(ptr=clone)

    def find(self, key: Union[int, str]) -> Union[int, str]:
        """
        Given a symbol or index, finds the other one.
        This method returns the index associated with a symbol key, or the symbol
        associated with a index key.
        Args:
          key: Either a string or an index.
        Returns:
          If key is a string, the associated index; if key is an integer, the
              associated symbol.
        Raises:
          KeyError: Key not found.
        """
        if isinstance(key, int):
            return self._find_index(key)
        if isinstance(key, str):
            return self._find_symbol(key)
        raise f"key can only be a string or integer. Not {type(key)}"

    def _find_index(self, key: int) -> str:
        key = ctypes.c_size_t(key)
        symbol = ctypes.c_void_p()
        ret_code = lib.symt_find_index(self.ptr, key, ctypes.byref(symbol))
        err_msg = "`find` failed"
        check_ffi_error(ret_code, err_msg)

        return ctypes.string_at(symbol).decode("utf8")

    def _find_symbol(self, symbol: str) -> int:
        symbol = symbol.encode("utf-8")
        index = ctypes.c_size_t()
        ret_code = lib.symt_find_symbol(self.ptr, symbol, ctypes.byref(index))
        err_msg = "`find` failed"
        check_ffi_error(ret_code, err_msg)

        return int(index.value)

    def member(self, key: Union[int, str]) -> bool:
        """
        Given a symbol or index, returns whether it is found in the table.
        This method returns a boolean indicating whether the given symbol or index
        is present in the table. If one intends to perform subsequent lookup, it is
        better to simply call the find method, catching the KeyError.
        Args:
          key: Either a string or an index.
        Returns:
          Whether or not the key is present (as a string or a index) in the table.
        """
        is_present = ctypes.c_size_t()

        ret_code = None

        if isinstance(key, int):
            index = ctypes.c_size_t(key)
            ret_code = lib.symt_member_index(self.ptr, index, ctypes.byref(is_present))
        elif isinstance(key, str):
            symbol = key.encode("utf-8")
            ret_code = lib.symt_member_symbol(
                self.ptr, symbol, ctypes.byref(is_present)
            )
        else:
            raise f"key can only be a string or integer. Not {type(key)}"

        err_msg = "`member` failed"
        check_ffi_error(ret_code, err_msg)

        return bool(is_present.value)

    def num_symbols(self) -> int:
        """
        Returns:
            The number of symbols in the symbol table.
        """
        num_symbols = ctypes.c_size_t()
        ret_code = lib.symt_num_symbols(self.ptr, ctypes.byref(num_symbols))
        err_msg = "`num_symbols` failed"
        check_ffi_error(ret_code, err_msg)

        return int(num_symbols.value)

    @classmethod
    def read(cls, filename: Union[str, Path]) -> SymbolTable:
        """
        Reads symbol table from binary file.
        This class method creates a new SymbolTable from a symbol table binary file.
        Args:
          filename: The string location of the input binary file.
        Returns:
          A new SymbolTable instance.
        See also: `SymbolTable.read_fst`, `SymbolTable.read_text`.
        """
        symt = ctypes.pointer(ctypes.c_void_p())
        ret_code = lib.symt_from_path(
            ctypes.byref(symt), str(filename).encode("utf-8"), ctypes.c_size_t(1)
        )

        err_msg = f"Read failed for bin file : {filename}"
        check_ffi_error(ret_code, err_msg)

        return cls(ptr=symt)

    @classmethod
    def read_text(cls, filename: Union[str, Path]) -> SymbolTable:
        """
        Reads symbol table from text file.
        This class method creates a new SymbolTable from a symbol table text file.
        Args:
          filename: The string location of the input text file.

        Returns:
          A new SymbolTable instance.
        See also: `SymbolTable.read`, `SymbolTable.read_fst`.
        """
        symt = ctypes.pointer(ctypes.c_void_p())
        ret_code = lib.symt_from_path(
            ctypes.byref(symt), str(filename).encode("utf-8"), ctypes.c_size_t(0)
        )

        err_msg = f"Read failed for text file : {filename}"
        check_ffi_error(ret_code, err_msg)

        return cls(ptr=symt)

    def write(self, filename: Union[str, Path]):
        """
        Serializes symbol table to a file.
        This methods writes the SymbolTable to a file in binary format.
        Args:
          filename: The string location of the output file.
        Raises:
          FstIOError: Write failed.
        """
        ret_code = lib.symt_write_file(
            self.ptr, str(filename).encode("utf-8"), ctypes.c_size_t(1)
        )

        err_msg = f"Write failed for bin file : {filename}"
        check_ffi_error(ret_code, err_msg)

    def write_text(self, filename: Union[str, Path]):
        """
        Writes symbol table to text file.
        This method writes the SymbolTable to a file in human-readable format.
        Args:
          filename: The string location of the output file.
        Raises:
          FstIOError: Write failed.
        """
        ret_code = lib.symt_write_file(
            self.ptr, str(filename).encode("utf-8"), ctypes.c_size_t(0)
        )

        err_msg = f"Write failed for text file : {filename}"
        check_ffi_error(ret_code, err_msg)

    def equals(self, other: SymbolTable) -> bool:
        """
        Check if this SymbolTable is equal to the other

        Params:
            other: SymbolTable instance
        Returns:
             bool
        """
        is_equal = ctypes.c_size_t()

        ret_code = lib.symt_equals(self.ptr, other.ptr, ctypes.byref(is_equal))
        err_msg = "Error checking equality"
        check_ffi_error(ret_code, err_msg)

        return bool(is_equal.value)

    def __eq__(self, other: SymbolTable) -> bool:
        """
        Check if this `SymbolTable` is equal to the other

        Params:
            other: SymbolTable instance
        Returns:
             bool
        """
        return self.equals(other)

    def __iter__(self) -> SymbolTableIterator:
        """
        Returns an Iterator over the SymbolTable.
        Returns:
            An iterator over the SymbolTable.
        """
        return SymbolTableIterator(self)

    @classmethod
    def from_symbols(cls, symbols: List[str]) -> SymbolTable:
        """
        Constructs a SymbolTable from list of strings.

        Args:
            symbols: List of symbols

        Returns:
            A new `SymbolTable`.
        """
        symt = cls()

        for symbol in symbols:
            symt.add_symbol(symbol)

        return symt

    def __del__(self):
        lib.symt_destroy(self.ptr)
Functions
__init__(ptr = None)

Creates an empty SymbolTable.

Source code in rustfst/symbol_table.py
15
16
17
18
19
20
21
22
23
24
25
26
27
def __init__(self, ptr=None):
    """
    Creates an empty `SymbolTable`.
    """
    if ptr:
        self.ptr = ptr
    else:
        symt_ptr = ctypes.pointer(ctypes.c_void_p())
        ret_code = lib.symt_new(ctypes.byref(symt_ptr))
        err_msg = "__init__ failed"
        check_ffi_error(ret_code, err_msg)

        self.ptr = symt_ptr
add_symbol(symbol: str) -> int

Adds a symbol to the table and returns the index.

Parameters:

Name Type Description Default
symbol str

A symbol unicode string.

required

Returns:

Type Description
int

The integer key of the new symbol.

Source code in rustfst/symbol_table.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def add_symbol(self, symbol: str) -> int:
    """
    Adds a symbol to the table and returns the index.

    Args:
      symbol: A symbol unicode string.
    Returns:
      The integer key of the new symbol.
    """
    try:
        symbol = symbol.encode("utf-8")
    except UnicodeDecodeError:
        symbol = ctypes.c_char_p(symbol)

    integer_key = ctypes.c_size_t()
    ret_code = lib.symt_add_symbol(self.ptr, symbol, ctypes.byref(integer_key))
    err_msg = "`add_symbol` failed"
    check_ffi_error(ret_code, err_msg)

    return int(integer_key.value)
add_table(syms: SymbolTable)

This method merges another symbol table into the current table. All key values will be offset by the current available key.

Parameters:

Name Type Description Default
syms SymbolTable

A SymbolTable to be merged with the current table.

required
Source code in rustfst/symbol_table.py
50
51
52
53
54
55
56
57
58
59
def add_table(self, syms: SymbolTable):
    """
    This method merges another symbol table into the current table. All key
    values will be offset by the current available key.
    Args:
      syms: A `SymbolTable` to be merged with the current table.
    """
    ret_code = lib.symt_add_table(self.ptr, syms.ptr)
    err_msg = "`add_table` failed"
    check_ffi_error(ret_code, err_msg)
copy() -> SymbolTable

Returns:

Type Description
SymbolTable

A mutable copy of the SymbolTable.

Source code in rustfst/symbol_table.py
61
62
63
64
65
66
67
68
69
70
71
72
def copy(self) -> SymbolTable:
    """
    Returns:
        A mutable copy of the `SymbolTable`.
    """
    clone = ctypes.pointer(ctypes.c_void_p())

    ret_code = lib.symt_copy(self.ptr, ctypes.byref(clone))
    err_msg = "`copy` failed."
    check_ffi_error(ret_code, err_msg)

    return SymbolTable(ptr=clone)
find(key: Union[int, str]) -> Union[int, str]

Given a symbol or index, finds the other one. This method returns the index associated with a symbol key, or the symbol associated with a index key.

Parameters:

Name Type Description Default
key Union[int, str]

Either a string or an index.

required

Returns:

Type Description
Union[int, str]

If key is a string, the associated index; if key is an integer, the associated symbol.

Raises:

Type Description
KeyError

Key not found.

Source code in rustfst/symbol_table.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def find(self, key: Union[int, str]) -> Union[int, str]:
    """
    Given a symbol or index, finds the other one.
    This method returns the index associated with a symbol key, or the symbol
    associated with a index key.
    Args:
      key: Either a string or an index.
    Returns:
      If key is a string, the associated index; if key is an integer, the
          associated symbol.
    Raises:
      KeyError: Key not found.
    """
    if isinstance(key, int):
        return self._find_index(key)
    if isinstance(key, str):
        return self._find_symbol(key)
    raise f"key can only be a string or integer. Not {type(key)}"
member(key: Union[int, str]) -> bool

Given a symbol or index, returns whether it is found in the table. This method returns a boolean indicating whether the given symbol or index is present in the table. If one intends to perform subsequent lookup, it is better to simply call the find method, catching the KeyError.

Parameters:

Name Type Description Default
key Union[int, str]

Either a string or an index.

required

Returns:

Type Description
bool

Whether or not the key is present (as a string or a index) in the table.

Source code in rustfst/symbol_table.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def member(self, key: Union[int, str]) -> bool:
    """
    Given a symbol or index, returns whether it is found in the table.
    This method returns a boolean indicating whether the given symbol or index
    is present in the table. If one intends to perform subsequent lookup, it is
    better to simply call the find method, catching the KeyError.
    Args:
      key: Either a string or an index.
    Returns:
      Whether or not the key is present (as a string or a index) in the table.
    """
    is_present = ctypes.c_size_t()

    ret_code = None

    if isinstance(key, int):
        index = ctypes.c_size_t(key)
        ret_code = lib.symt_member_index(self.ptr, index, ctypes.byref(is_present))
    elif isinstance(key, str):
        symbol = key.encode("utf-8")
        ret_code = lib.symt_member_symbol(
            self.ptr, symbol, ctypes.byref(is_present)
        )
    else:
        raise f"key can only be a string or integer. Not {type(key)}"

    err_msg = "`member` failed"
    check_ffi_error(ret_code, err_msg)

    return bool(is_present.value)
num_symbols() -> int

Returns:

Type Description
int

The number of symbols in the symbol table.

Source code in rustfst/symbol_table.py
142
143
144
145
146
147
148
149
150
151
152
def num_symbols(self) -> int:
    """
    Returns:
        The number of symbols in the symbol table.
    """
    num_symbols = ctypes.c_size_t()
    ret_code = lib.symt_num_symbols(self.ptr, ctypes.byref(num_symbols))
    err_msg = "`num_symbols` failed"
    check_ffi_error(ret_code, err_msg)

    return int(num_symbols.value)
read(filename: Union[str, Path]) -> SymbolTable classmethod

Reads symbol table from binary file. This class method creates a new SymbolTable from a symbol table binary file.

Parameters:

Name Type Description Default
filename Union[str, Path]

The string location of the input binary file.

required

Returns:

Type Description
SymbolTable

A new SymbolTable instance.

See also: SymbolTable.read_fst, SymbolTable.read_text.

Source code in rustfst/symbol_table.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@classmethod
def read(cls, filename: Union[str, Path]) -> SymbolTable:
    """
    Reads symbol table from binary file.
    This class method creates a new SymbolTable from a symbol table binary file.
    Args:
      filename: The string location of the input binary file.
    Returns:
      A new SymbolTable instance.
    See also: `SymbolTable.read_fst`, `SymbolTable.read_text`.
    """
    symt = ctypes.pointer(ctypes.c_void_p())
    ret_code = lib.symt_from_path(
        ctypes.byref(symt), str(filename).encode("utf-8"), ctypes.c_size_t(1)
    )

    err_msg = f"Read failed for bin file : {filename}"
    check_ffi_error(ret_code, err_msg)

    return cls(ptr=symt)
read_text(filename: Union[str, Path]) -> SymbolTable classmethod

Reads symbol table from text file. This class method creates a new SymbolTable from a symbol table text file.

Parameters:

Name Type Description Default
filename Union[str, Path]

The string location of the input text file.

required

Returns:

Type Description
SymbolTable

A new SymbolTable instance.

See also: SymbolTable.read, SymbolTable.read_fst.

Source code in rustfst/symbol_table.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@classmethod
def read_text(cls, filename: Union[str, Path]) -> SymbolTable:
    """
    Reads symbol table from text file.
    This class method creates a new SymbolTable from a symbol table text file.
    Args:
      filename: The string location of the input text file.

    Returns:
      A new SymbolTable instance.
    See also: `SymbolTable.read`, `SymbolTable.read_fst`.
    """
    symt = ctypes.pointer(ctypes.c_void_p())
    ret_code = lib.symt_from_path(
        ctypes.byref(symt), str(filename).encode("utf-8"), ctypes.c_size_t(0)
    )

    err_msg = f"Read failed for text file : {filename}"
    check_ffi_error(ret_code, err_msg)

    return cls(ptr=symt)
write(filename: Union[str, Path])

Serializes symbol table to a file. This methods writes the SymbolTable to a file in binary format.

Parameters:

Name Type Description Default
filename Union[str, Path]

The string location of the output file.

required

Raises:

Type Description
FstIOError

Write failed.

Source code in rustfst/symbol_table.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def write(self, filename: Union[str, Path]):
    """
    Serializes symbol table to a file.
    This methods writes the SymbolTable to a file in binary format.
    Args:
      filename: The string location of the output file.
    Raises:
      FstIOError: Write failed.
    """
    ret_code = lib.symt_write_file(
        self.ptr, str(filename).encode("utf-8"), ctypes.c_size_t(1)
    )

    err_msg = f"Write failed for bin file : {filename}"
    check_ffi_error(ret_code, err_msg)
write_text(filename: Union[str, Path])

Writes symbol table to text file. This method writes the SymbolTable to a file in human-readable format.

Parameters:

Name Type Description Default
filename Union[str, Path]

The string location of the output file.

required

Raises:

Type Description
FstIOError

Write failed.

Source code in rustfst/symbol_table.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def write_text(self, filename: Union[str, Path]):
    """
    Writes symbol table to text file.
    This method writes the SymbolTable to a file in human-readable format.
    Args:
      filename: The string location of the output file.
    Raises:
      FstIOError: Write failed.
    """
    ret_code = lib.symt_write_file(
        self.ptr, str(filename).encode("utf-8"), ctypes.c_size_t(0)
    )

    err_msg = f"Write failed for text file : {filename}"
    check_ffi_error(ret_code, err_msg)
equals(other: SymbolTable) -> bool

Check if this SymbolTable is equal to the other

Parameters:

Name Type Description Default
other SymbolTable

SymbolTable instance

required

Returns:

Type Description
bool

bool

Source code in rustfst/symbol_table.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def equals(self, other: SymbolTable) -> bool:
    """
    Check if this SymbolTable is equal to the other

    Params:
        other: SymbolTable instance
    Returns:
         bool
    """
    is_equal = ctypes.c_size_t()

    ret_code = lib.symt_equals(self.ptr, other.ptr, ctypes.byref(is_equal))
    err_msg = "Error checking equality"
    check_ffi_error(ret_code, err_msg)

    return bool(is_equal.value)
__eq__(other: SymbolTable) -> bool

Check if this SymbolTable is equal to the other

Parameters:

Name Type Description Default
other SymbolTable

SymbolTable instance

required

Returns:

Type Description
bool

bool

Source code in rustfst/symbol_table.py
246
247
248
249
250
251
252
253
254
255
def __eq__(self, other: SymbolTable) -> bool:
    """
    Check if this `SymbolTable` is equal to the other

    Params:
        other: SymbolTable instance
    Returns:
         bool
    """
    return self.equals(other)
__iter__() -> SymbolTableIterator

Returns an Iterator over the SymbolTable.

Returns:

Type Description
SymbolTableIterator

An iterator over the SymbolTable.

Source code in rustfst/symbol_table.py
257
258
259
260
261
262
263
def __iter__(self) -> SymbolTableIterator:
    """
    Returns an Iterator over the SymbolTable.
    Returns:
        An iterator over the SymbolTable.
    """
    return SymbolTableIterator(self)
from_symbols(symbols: List[str]) -> SymbolTable classmethod

Constructs a SymbolTable from list of strings.

Parameters:

Name Type Description Default
symbols List[str]

List of symbols

required

Returns:

Type Description
SymbolTable

A new SymbolTable.

Source code in rustfst/symbol_table.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@classmethod
def from_symbols(cls, symbols: List[str]) -> SymbolTable:
    """
    Constructs a SymbolTable from list of strings.

    Args:
        symbols: List of symbols

    Returns:
        A new `SymbolTable`.
    """
    symt = cls()

    for symbol in symbols:
        symt.add_symbol(symbol)

    return symt

SymbolTableIterator

Iterator on a SymbolTable. Allows retrieving all the symbols along with their corresponding labels.

Source code in rustfst/symbol_table.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
class SymbolTableIterator:
    """
    Iterator on a SymbolTable. Allows retrieving all the symbols along with their corresponding labels.
    """

    def __init__(self, symbol_table: SymbolTable):
        """
        Constructs an iterator from the `Symboltable`.
        Args:
            symbol_table:
        """
        self._symt = symbol_table
        self._idx = 0
        self._len = self._symt.num_symbols()

    def __next__(self) -> Tuple[int, str]:
        """
        Iterator over the symbols in the `SymbolTable`.
        Returns:
            A pair label (int) and symbol (str).
        """
        if self._idx < self._len:
            output = (self._idx, self._symt.find(self._idx))
            self._idx += 1
            return output
        raise StopIteration
Functions
__init__(symbol_table: SymbolTable)

Constructs an iterator from the Symboltable.

Parameters:

Name Type Description Default
symbol_table SymbolTable required
Source code in rustfst/symbol_table.py
292
293
294
295
296
297
298
299
300
def __init__(self, symbol_table: SymbolTable):
    """
    Constructs an iterator from the `Symboltable`.
    Args:
        symbol_table:
    """
    self._symt = symbol_table
    self._idx = 0
    self._len = self._symt.num_symbols()
__next__() -> Tuple[int, str]

Iterator over the symbols in the SymbolTable.

Returns:

Type Description
Tuple[int, str]

A pair label (int) and symbol (str).

Source code in rustfst/symbol_table.py
302
303
304
305
306
307
308
309
310
311
312
def __next__(self) -> Tuple[int, str]:
    """
    Iterator over the symbols in the `SymbolTable`.
    Returns:
        A pair label (int) and symbol (str).
    """
    if self._idx < self._len:
        output = (self._idx, self._symt.find(self._idx))
        self._idx += 1
        return output
    raise StopIteration