📄 lattice.h
字号:
/* * Lattice.h -- * Word lattices * * Copyright (c) 1997-2006, SRI International. All Rights Reserved. * * @(#)$Header: /home/srilm/devel/lattice/src/RCS/Lattice.h,v 1.85 2006/01/06 11:58:17 stolcke Exp $ * */#ifndef _Lattice_h_#define _Lattice_h_/* ****************************************************************** header files ****************************************************************** */#include <math.h>#include "Prob.h"#include "Boolean.h"#include "Array.h"#include "LHash.h"#include "SArray.h"#include "Map2.h"#include "Vocab.h"#include "SubVocab.h"#include "File.h"#include "Debug.h"#include "LM.h"#include "Ngram.h"#include "VocabMultiMap.h"#include "MultiwordVocab.h"#include "WordMesh.h"#include "HTKLattice.h"class Lattice; /* forward declaration */typedef const VocabIndex *VocabContext;typedef unsigned NodeIndex;const NodeIndex NoNode = (NodeIndex)(-1);/* ****************************************************************** flags for node ****************************************************************** */const unsigned markedFlag = 8; //to indicate that this node is processed/* ****************************************************************** flags for transition ****************************************************************** */const unsigned pauseTFlag = 2; //there was a pause node on the linkconst unsigned directTFlag = 4; //this is a non-pause link between two nodesconst unsigned markedTFlag = 8; //to indicate that this trans has been //processedconst unsigned reservedTFlag = 16; //to indicate that this trans needs to be // reserved for bigram;/* ****************************************************************** other constants ****************************************************************** */const int minIntlog = -250000; // minumum intlog values used when // printing PFSGs. Value is chosen such // that PFSG probs can be safely converted // to bytelogs in the recognizer.extern const char *LATTICE_OR; // lattice disjunctionextern const char *LATTICE_CONCATE; // lattice concatenationextern const char *LATTICE_NONAME; // default internal lattice name/* ****************************************************************** structure ****************************************************************** */inline LogP combWeights(LogP weight1, LogP weight2){ return (weight1+weight2); }inline LogP unionWeights(LogP weight1, LogP weight2){ return (weight1 > weight2 ? weight1 : weight2); }inline LogP unit(){ return 0;} inline intnodeSort(NodeIndex n1, NodeIndex n2){ return (n1 - n2);}class NodeQueue;typedef struct SelfLoopDB { // initA NodeIndex preNodeIndex; NodeIndex postNodeIndex2; NodeIndex postNodeIndex3; NodeIndex nodeIndex; VocabIndex wordName; unsigned selfTransFlags; LogP loopProb; // initB NodeIndex fromNodeIndex; VocabIndex fromWordName; LogP fromPreProb; LogP prePostProb; unsigned fromSelfTransFlags; // initC NodeIndex toNodeIndex; unsigned selfToTransFlags;} SelfLoopDB;#ifdef USE_SARRAY#define TRANS_T SArray#define TRANSITER_T SArrayIter#else#define TRANS_T LHash#define TRANSITER_T LHashIter#endif/* ************************* * A transition in a lattice * ************************* */class LatticeTransition { public: LatticeTransition() : weight(0), flags(0) {}; LatticeTransition(LogP weight, unsigned flags) : weight(weight), flags(flags) {}; void markTrans(unsigned flag) { flags |= flag; }; void setWeight(LogP givenWeight) { weight = givenWeight; }; Boolean getFlag(unsigned flag) { return (flags & flag); }; LogP weight; // weight (e.g., probability) of transition unsigned flags; // miscellaneous flags;}; /* * Compare two transition lists for equality */BooleancompareTransitions(const TRANS_T<NodeIndex,LatticeTransition> &transList1, const TRANS_T<NodeIndex,LatticeTransition> &transList2);/* ************************* * A node in a lattice ************************* */class LatticeNode{ friend class LatticeTransition;public: LatticeNode(); // initializing lattice node; unsigned flags; VocabIndex word; // word associated with this node LogP2 posterior; // node posterior (unnormalized) HTKWordInfo *htkinfo; // HTK lattice info TRANS_T<NodeIndex,LatticeTransition> outTransitions;// outgoing transitions TRANS_T<NodeIndex,LatticeTransition> inTransitions; // incoming transitions void markNode(unsigned flag) { flags |= flag; }; // set to one the bits indicated by flag; void unmarkNode(unsigned flag) { flags &= ~flag; }; // set to zero the bits indicated by flag; Boolean getFlag(unsigned flag) { return (flags & flag); };};/* ************************* * Output file arguments for Lattice::computeNBest() ************************* */class NBestOptions{ public: NBestOptions(char *nbestOutDir, char *nbestOutDirNgram, char *nbestOutDirPron, char *nbestOutDirDur, char *nbestOutDirXscore1, char *nbestOutDirXscore2, char *nbestOutDirXscore3, char *nbestOutDirXscore4, char *nbestOutDirXscore5, char *nbestOutDirXscore6, char *nbestOutDirXscore7, char *nbestOutDirXscore8, char *nbestOutDirXscore9, char *nbestOutDirRttm); ~NBestOptions(); char *nbestOutDir; char *nbestOutDirNgram; char *nbestOutDirPron; char *nbestOutDirDur; char *nbestOutDirXscore1; char *nbestOutDirXscore2; char *nbestOutDirXscore3; char *nbestOutDirXscore4; char *nbestOutDirXscore5; char *nbestOutDirXscore6; char *nbestOutDirXscore7; char *nbestOutDirXscore8; char *nbestOutDirXscore9; char *nbestOutDirRttm; Boolean writingFiles; File *nbest; File *nbestNgram; File *nbestPron; File *nbestDur; File *nbestXscore1; File *nbestXscore2; File *nbestXscore3; File *nbestXscore4; File *nbestXscore5; File *nbestXscore6; File *nbestXscore7; File *nbestXscore8; File *nbestXscore9; File *nbestRttm; Boolean makeDirs(Boolean overwrite); Boolean openFiles(const char *name); Boolean closeFiles();};class PackedNodeList; /* ************************* * A lattice ************************* */class Lattice: public Debug{ friend class NodeQueue; friend class PackedNodeList; friend class LatticeTransition; friend class LatticeNode;public: /* ************************************************* within single lattice operations ************************************************* */ Lattice(Vocab &vocab, const char *name = LATTICE_NONAME); Lattice(Vocab &vocab, const char *name, SubVocab &ignoreVocab); ~Lattice(); Boolean computeNodeEntropy(); LogP detectSelfLoop(NodeIndex nodeIndex); Boolean recoverPauses(Boolean loop = true, Boolean all = false); Boolean recoverCompactPauses(Boolean loop = true, Boolean all = false); Boolean removeAllXNodes(VocabIndex xWord); Boolean replaceWeights(LM &lm); Boolean simplePackBigramLattice(unsigned iters = 0, Boolean maxAdd = false); Boolean approxRedBigramLattice(unsigned iters, int base, double ratio); Boolean expandToTrigram(LM &lm, unsigned maxNodes = 0); Boolean expandToCompactTrigram(Ngram &ngram, unsigned maxNodes = 0); Boolean expandToLM(LM &lm, unsigned maxNodes = 0, Boolean compact = false); Boolean noBackoffWeights; // hack to suppress backoff weights in expansion Boolean collapseSameWordNodes(SubVocab &exceptions); void splitMultiwordNodes(MultiwordVocab &vocab, LM &lm); Boolean scorePronunciations(VocabMultiMap &dictionary, Boolean intlogs = false); void alignLattice(WordMesh &sausage, double posteriorScale = 1.0) { alignLattice(sausage, ignoreVocab, posteriorScale); } void alignLattice(WordMesh &sausage, SubVocab &ignoreWords, double posteriorScale = 1.0, Boolean acousticInfo = false); void addWords(const VocabIndex *words, Prob prob, Boolean pauses = false); /* ************************************************* operations with two lattices ************************************************* */ Boolean implantLattice(NodeIndex nodeIndex, Lattice &lat, float addTime = 0.0); Boolean implantLatticeXCopies(Lattice &lat); Boolean latticeCat(Lattice &lat1, Lattice &lat2, float interSegmentTime = 0.0); Boolean latticeOr(Lattice &lat1, Lattice &lat2); /* ********************************************************* lattice input and output ********************************************************* */ Boolean readPFSG(File &file); Boolean readPFSGs(File &file); Boolean readPFSGFile(File &file); Boolean readRecPFSGs(File &file); Boolean readHTK(File &file, HTKHeader *header = 0, Boolean useNullNodes = false); Boolean readMesh(File &file); Boolean writePFSG(File &file); Boolean writeCompactPFSG(File &file); Boolean writePFSGFile(File &file); Boolean writeHTK(File &file, HTKScoreMapping scoreMapping = mapHTKnone, Boolean writePosteriors = false); void setHTKHeader(HTKHeader &header); Boolean useUnk; // map unknown words to <unk> Boolean limitIntlogs; // whether output probs should fit in bytelogs /* ********************************************************* nodes and transitions ********************************************************* */ Boolean insertNode(const char *word, NodeIndex nodeIndex); Boolean insertNode(const char *word) { return insertNode(word, maxIndex++); }; // duplicate a node with the same word name; NodeIndex dupNode(VocabIndex windex, unsigned markedFlag = 0, HTKWordInfo *htkinfo = 0); Boolean removeNode(NodeIndex nodeIndex); // all the edges connected with this node will be removed;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -