kylebgorman / pynini

Read-only mirror of Pynini
http://pynini.opengrm.org
Apache License 2.0
118 stars 27 forks source link

weights in pynini #43

Closed kalvinchang closed 3 years ago

kalvinchang commented 3 years ago

I'm having trouble finding pynini-level functions that set weights of transitions.

For example, suppose I had the following union of transductions: pynini.union(pynini.cross('cat', 'dog'), pynini.cross('horse', 'cow')) How would I assign weights to each transduction (e.g. 0.6 to pynini.cross('cat', 'dog') and 0.4 to pynini.cross('horse', 'cow')). What I am currently thinking of doing is to manually add a state, then add an epsilon transition with weight 0.6 to pynini.cross('cat', 'dog') and another epsilon transitoin with weight 0.4 to pynini.cross('horse', 'cow')).

Do I have to go down to the pywrapfst level?

kylebgorman commented 3 years ago

There are so many ways to do this. You could manually add a state, or you could concatenate a weighted machine with an empty string to the end. We have a built-in function for this though:

from pynini.lib import pynutil

weighted = pynutil.add_weight(pynini.cross("cat", "dog"), .6)

I realize things like pynutil are not covered in any detail by the summary documentation online. However, now that you know they exist, you can take advantage of their extensive in-module documentation (e.g., help(pynutil) or help(pynutil.add_weight)). Also the appendix to the forthcoming book on Pynini documents all the functions in the "Pynini extended library" in appendices.

kalvinchang commented 3 years ago

Cool. I will try that. Thank you!!