// Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz // or its licensors, as applicable. // // You may not use this file except under the terms of the accompanying license. // // Licensed under the Apache License, Version 2.0 (the "License"); you // may not use this file except in compliance with the License. You may // obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Project: // File: lattice.h // Purpose: // Responsible: mezhirov // Reviewer: // Primary Repository: // Web Sites: www.iupr.org, www.dfki.de, www.ocropus.org #ifndef h_lattice_ #define h_lattice_ #include "ocrinterfaces.h" #include "beam-search.h" namespace ocropus { struct ReadOnlyFst : colib::IGenericFst { int oops() { throw "this FST is read-only"; } virtual void clear() {oops();} virtual int newState() {return oops();} virtual void addTransition(int from,int to,int output,float cost,int input) {oops();} virtual void setStart(int node) { oops(); } virtual void setAccept(int node,float cost=0.0) { oops(); } virtual int special(const char *s) { return oops(); } virtual void bestpath(colib::nustring &result) { beam_search(result, *this); } }; struct CompositionFst : ReadOnlyFst { /// Return the 1st FST, releasing the ownership. virtual colib::IGenericFst *move1() = 0; /// Return the 2nd FST, releasing the ownership. virtual colib::IGenericFst *move2() = 0; virtual void splitIndex(int &result1, int &result2, int index) = 0; virtual void splitIndices(colib::intarray &result1, colib::intarray &result2, colib::intarray &indices) = 0; /// Fail if this FST still owns either of the objects. virtual void checkOwnsNothing() = 0; }; /// Make an unoptimized composition of two FSTs. /// The ids are always taken from the first FST only. /// Additional parameters, override_start and override_finish /// are indices into the first FST. /// /// Setting override_start to nonnegative value has the same effect /// as l1->getStart() returning override_start. /// /// Setting override_finish to nonnegative value has the same effect /// as l2->getAcceptCost() returning 0 only for override_finish /// and INF otherwise. CompositionFst *make_CompositionFst(colib::IGenericFst *l1, colib::IGenericFst *l2, int override_start = -1, int override_finish = -1); /// Make a readable/writable FST (independent on OpenFST) /// with beam_search for bestpath(). colib::IGenericFst *make_StandardFst(); void fst_copy(colib::IGenericFst &dst, colib::IGenericFst &src); /// Reverse the FST's arcs, adding a new start vertex (former accept). /// @param no_accept void fst_copy_reverse(colib::IGenericFst &dst, colib::IGenericFst &src, bool no_accept = false); void fst_copy_best_arcs_only(colib::IGenericFst &dst, colib::IGenericFst &src); /// Insert one FST into another, mapping the start into a given vertex /// and connecting all accept arcs to another given vertex. void fst_insert(colib::IGenericFst &dst, colib::IGenericFst &src, int start, int accept = -1); void rescore_path(colib::IGenericFst &fst, colib::intarray &inputs, colib::intarray &vertices, colib::intarray &outputs, colib::floatarray &new_costs, int override_start = -1); void beam_search_and_rescore(colib::IGenericFst &main, colib::IGenericFst &transcript, double coef, int beam_width = DEFAULT_BEAM_WIDTH, int override_start = -1, int override_finish = -1); void a_star_and_rescore(colib::IGenericFst &main, colib::IGenericFst &transcript, double coef); }; #endif