Skip to content
Snippets Groups Projects
Commit f3f63901 authored by Pablo Riera's avatar Pablo Riera
Browse files

now both way conversions are ok

parent 2c4cbc6a
No related branches found
No related tags found
1 merge request!63Open fst convert
......@@ -12,7 +12,8 @@ _SemiringToWeightType = Dict([
(SR.LogSemiring{Float64,-1}, (OF.Log64Weight, 1)),
(SR.LogSemiring{Float32,Inf}, (OF.TropicalWeight, -1)),
(SR.LogSemiring{Float32,-Inf}, (OF.TropicalWeight, 1)),
(SR.TropicalSemiring{Float32}, (OF.TropicalWeight, 1))
(SR.TropicalSemiring{Float32}, (OF.TropicalWeight, 1)),
])
# Converts from OpenFst weight to TensorFSTs semiring
......@@ -66,22 +67,22 @@ end
numstates(A::TF.TensorFST{S}) where S <: SR.Semiring = length(states(A))
numarcs(A::TF.TensorFST{S},q) where S <: SR.Semiring = length(arcs_from_q(A,q))
# function TF.TensorFST(ofst::OF.Fst{W}) where W <: OF.Weight
# S = _WeightToSemiringType[W]
# tfst = TF.VectorFST{S}()
# for s in OF.states(ofst)
# TF.addstate!(tfst)
# final = S(OF.final(ofst, s))
# TF.setfinal!(tfst, s, final)
# for a in OF.arcs(ofst, s)
# arc = TF.Arc(Int(a.ilabel), Int(a.olabel), S(a.weight),
# Int(a.nextstate))
# TF.addarc!(tfst, s, arc)
# end
# end
# TF.setstart!(tfst, OF.start(ofst))
# return tfst
# end
function TF.TensorFST(ofst::OF.Fst{W}) where W <: OF.Weight
S = _WeightToSemiringType[W]
myarcs = []
for s in OF.states(ofst)
for a in OF.arcs(ofst, s)
push!(myarcs,
(src = convert(Int64, s), isym = convert(Int64, a.ilabel)-1, osym = convert(Int64, a.olabel)-1, weight = S(a.weight), dest = convert(Int64, a.nextstate))
)
end
end
tfst = TF.TensorFST(myarcs,
[(convert(Int64, OF.start(ofst)),) => S(1)],
[(convert(Int64,s),) => S(OF.final(ofst, s)) for s in OF.states(ofst)])
return tfst
end
function OF.VectorFst(tfst::TF.TensorFST{S}) where S <: SR.Semiring
......@@ -96,8 +97,8 @@ function OF.VectorFst(tfst::TF.TensorFST{S}) where S <: SR.Semiring
OF.setfinal!(ofst, s, final_w)
OF.reservearcs(ofst, s, numarcs(tfst, s))
for a in arcs_from_q(tfst, s)
arc = OF.Arc(ilabel = a.isym,
olabel = a.osym,
arc = OF.Arc(ilabel = a.isym+1,
olabel = a.osym+1,
weight = _semiring_to_weight(a.weight, sgn),
nextstate = a.dest[1])
OF.addarc!(ofst, s, arc)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment