kylebgorman / pynini

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

Mutating arc_type of an FST #59

Closed david-waterworth closed 1 year ago

david-waterworth commented 1 year ago

Is it possible to take an FST constructed with the standard arc_type (semiring) and change it to say log?

The reason I ask is I've constructed an FST and now want to use the log semiring but that requires a lot of recoding, for example, the following code fails because byte.DIGIT has arc_type of 'standard' so composition throws an exception.

equipment_types = pynini.string_map(["AHU"], arc_type="log").optimize() 
identifier = byte.DIGIT

fst = equipment_types + identifier

If I have to I will redefine my own versions of DIGIT etc so I can specify the arc_type but I was hoping I can simply mutate the final FST instead?

kylebgorman commented 1 year ago

Only via making a copy (not tested):

identifer = pynini.arc_map(fst, map_type="to_log")
david-waterworth commented 1 year ago

Thanks, looks like it's arcmap rather than arc_map.

I don't need it anyway though - for some reason I had in my head that shortest path with the standard (tropical) semiring took the max rather than the sum of the weights over the path. I verified that it is in fact summing, I assume path length is calculated using the (x) rather than (+) semiring operator?

import pynini
from pynini.lib import pynutil

A = pynutil.add_weight(pynini.accep("A"), 9.0)
B = pynutil.add_weight(pynini.accep("B"), 2.0)
C = pynutil.add_weight(pynini.accep("C"), 5.0)

fst = A + (B | C)
fst

paths = pynini.shortestpath(fst, nshortest=10).paths()
for istring,ostring,weight in paths.items():
    print(ostring,weight)

I did notice that add_weight errors when the fst is log semiring.

pynutil.add_weight(pynini.accep("A", arc_type="log"), 9.0)

LEVEL(FST_FLAGS_fst_error_fatal ? base_logging::FATAL : base_logging::ERROR): Concat: Arguments with non-matching arc types standard and log

kylebgorman commented 1 year ago

Yeah, see slide 3 here re: the semirings: https://www.openfst.org/twiki/pub/FST/FstSltTutorial/part1.pdf

Thanks for bug report, that's an easy fix on my side.