Pynini is a Python library providing efficient Python bindings for the OpenFst C++ library, enabling the construction, manipulation, and compilation of finite-state transducers (FSTs) and finite-state acceptors (FSAs). It's widely used for tasks in natural language processing (NLP) such as text normalization, phonology, and speech recognition. The current version is 2.1.7, with active development and regular releases.
pip install pyniniVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating basic finite-state acceptors and transducers, concatenating FSTs, and composing an input string with a mapping transducer to transform text.
Ensure all FSTs involved in an operation use the same weight semiring. You can explicitly set the semiring when creating FSTs (e.g., `pn.Fst(weight_type="log")`) or convert them using `pn.cast()`.
Always assign the result of FST operations to a new variable or reassign to the original variable to capture the modified FST (e.g., `my_fst = pn.compose(my_fst, another_fst)`).
Ensure your system has necessary build tools (g++, make, etc.). For persistent issues, consider installing OpenFst manually according to its documentation and then compiling Pynini from source, ensuring it links to your specific OpenFst installation.
Optimize grammar design by minimizing states/arcs where possible. Consider breaking down complex operations into smaller, manageable steps. Profile your code to identify bottlenecks and explore Pynini's various optimization flags or algorithms (e.g., `pn.optimize()`)
Convert the Python string into a finite-state acceptor using `pynini.accep()` before concatenation. Example: `my_fst + pynini.accep("literal_string")`.Carefully examine the input string, text file, or arguments passed to functions like `pynini.string_map()` or `pynini.string_file()`. Ensure correct OpenFst FST text format, proper character escaping, and valid UTF-8 encoding. Test with smaller, simpler inputs to isolate the issue.
Verify that the file path is absolutely correct and that the file exists at that location. Ensure the Python process has the necessary read permissions for the file. Use an absolute path or ensure the file is in the current working directory.