Pynini

2.1.7 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating basic finite-state acceptors and transducers, concatenating FSTs, and composing an input string with a mapping transducer to transform text.

import pynini as pn

# Create a simple acceptor for 'hello'
hello_fst = pn.accep("hello")

# Create an acceptor for 'world'
world_fst = pn.accep("world")

# Concatenate them with a space acceptor using the overloaded '+' operator
hello_world_fst = hello_fst + pn.accep(" ") + world_fst

print(f"Hello World FST: {hello_world_fst}")
print(f"Shortest path for Hello World: {pn.shortestpath(hello_world_fst).string()}")

# Create a simple transducer mapping 'a' to 'b'
a_to_b_map = pn.string_map([("a", "b"), ("c", "d")])

# Input string as an acceptor
input_string_fst = pn.accep("apple_cake")

# Compose the input with the transducer
output_fst = pn.compose(input_string_fst, a_to_b_map)

# Get the shortest path (result string)
if not output_fst.empty():
    print(f"'apple_cake' -> '{pn.shortestpath(output_fst).string()}'")
else:
    print("No valid output path found.")

view raw JSON →