/// <summary> /// Copy one FST to another. /// </summary> /// <param name="dst">The destination. Will be cleared before copying.</param> /// <param name="src">The FST to copy.</param> public static void fst_copy(IGenericFst dst, IGenericFst src) { dst.Clear(); int n = src.nStates(); for (int i = 0; i < n; i++) { dst.NewState(); } dst.SetStart(src.GetStart()); for (int i = 0; i < n; i++) { dst.SetAccept(i, src.GetAcceptCost(i)); Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray(); Floatarray costs = new Floatarray(); src.Arcs(inputs, targets, outputs, costs, i); int inlen = inputs.Length(); if (inlen != targets.Length()) { throw new Exception("ASSERT: inputs.length() == targets.length()"); } if (inlen != outputs.Length()) { throw new Exception("ASSERT: inputs.length() == outputs.length()"); } if (inlen != costs.Length()) { throw new Exception("ASSERT: inputs.length() == costs.length()"); } for (int j = 0; j < inputs.Length(); j++) { dst.AddTransition(i, targets.At1d(j), outputs.At1d(j), costs.At1d(j), inputs.At1d(j)); } } }
public AStarSearch(IGenericFst fst) { this.fst = fst; this.accepted_from = -1; this.heap = new Heap(fst.nStates() + 1); this.n = fst.nStates(); this.came_from = new Intarray(n); this.came_from.Fill(-1); this.g = new Floatarray(n); // insert the start node int s = fst.GetStart(); g[s] = 0; came_from[s] = s; heap.Push(s, Convert.ToSingle(Heuristic(s))); }
public static void fst_read(IGenericFst fst, BinaryReader reader) { read_header_and_symbols(fst, reader); for (int i = 0; i < fst.nStates(); i++) { read_node(reader, fst, i); } }
public static void fst_write(BinaryWriter writer, IGenericFst fst) { write_header_and_symbols(writer, fst); for (int i = 0; i < fst.nStates(); i++) { write_node(writer, fst, i); } }
/// <summary> /// Copy one FST to another, preserving only lowest-cost arcs. /// This is useful for visualization. /// </summary> /// <param name="dst">The destination. Will be cleared before copying.</param> /// <param name="src">The FST to copy.</param> public static void fst_copy_best_arcs_only(IGenericFst dst, IGenericFst src) { dst.Clear(); int n = src.nStates(); for (int i = 0; i < n; i++) { dst.NewState(); } dst.SetStart(src.GetStart()); for (int i = 0; i < n; i++) { dst.SetAccept(i, src.GetAcceptCost(i)); Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray(); Floatarray costs = new Floatarray(); src.Arcs(inputs, targets, outputs, costs, i); int inlen = inputs.Length(); if (inlen != targets.Length()) { throw new Exception("ASSERT: inputs.length() == targets.length()"); } if (inlen != outputs.Length()) { throw new Exception("ASSERT: inputs.length() == outputs.length()"); } if (inlen != costs.Length()) { throw new Exception("ASSERT: inputs.length() == costs.length()"); } Dictionary <int, int> hash = new Dictionary <int, int>(); for (int j = 0; j < n; j++) { int t = targets[j]; int best_so_far = -1; if (hash.ContainsKey(t)) { best_so_far = hash[t]; } if (best_so_far == -1 || costs[j] < costs[best_so_far]) { hash[t] = j; } } Intarray keys = new Intarray(); //hash.keys(keys); keys.Clear(); foreach (int key in hash.Keys) { keys.Push(key); } for (int k = 0; k < keys.Length(); k++) { int j = hash[keys[k]]; dst.AddTransition(i, targets[j], outputs[j], costs[j], inputs[j]); } } }
protected static void write_header_and_symbols(BinaryWriter writer, IGenericFst fst) { write_int32_LE(writer, OPENFST_MAGIC); write_string(writer, "vector"); write_string(writer, "standard"); write_int32_LE(writer, MIN_VERSION); write_int32_LE(writer, /* flags: */ 0); write_int64_LE(writer, PROPERTIES); write_int64_LE(writer, fst.GetStart()); write_int64_LE(writer, fst.nStates()); write_int64_LE(writer, /* narcs (seems to be unused): */ 0L); }
/// <summary> /// Make an in-place Kleene closure of the FST. /// </summary> public static void fst_star(IGenericFst fst) { int s = fst.GetStart(); fst.SetAccept(s); for (int i = 0; i < fst.nStates(); i++) { double c = fst.GetAcceptCost(i); if (c < 1e37) { fst.AddTransition(i, s, 0, (float)c, 0); } } }
public CompositionFstImpl(IGenericFst l1, IGenericFst l2, int o_s, int o_f) { override_start = o_s; override_finish = o_f; if (l1.nStates() == 0) { throw new Exception("CHECK_ARG: l1->nStates() > 0"); } if (l2.nStates() == 0) { throw new Exception("CHECK_ARG: l2->nStates() > 0"); } // this should be here, not in the initializers. // (otherwise if CHECKs throw an exception, bad things happen) this.l1 = l1; this.l2 = l2; }
/// <summary> /// Reverse the FST's arcs, adding a new start vertex (former accept). /// </summary> public static void fst_copy_reverse(IGenericFst dst, IGenericFst src, bool no_accept = false) { dst.Clear(); int n = src.nStates(); for (int i = 0; i <= n; i++) { dst.NewState(); } if (!no_accept) { dst.SetAccept(src.GetStart()); } dst.SetStart(n); for (int i = 0; i < n; i++) { dst.AddTransition(n, i, 0, src.GetAcceptCost(i), 0); Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray(); Floatarray costs = new Floatarray(); src.Arcs(inputs, targets, outputs, costs, i); if (inputs.Length() != targets.Length()) { throw new Exception("ASSERT: inputs.length() == targets.length()"); } if (inputs.Length() != outputs.Length()) { throw new Exception("ASSERT: inputs.length() == outputs.length()"); } if (inputs.Length() != costs.Length()) { throw new Exception("ASSERT: inputs.length() == costs.length()"); } for (int j = 0; j < inputs.Length(); j++) { dst.AddTransition(targets.At1d(j), i, outputs.At1d(j), costs.At1d(j), inputs.At1d(j)); } } }
public override int nStates() { return(l1.nStates() * l2.nStates()); }