📄 lattice.cc
字号:
{
makeArray(char, pause, strlen(phoneSeparator)*2 + 12);
sprintf(pause,"%s-,%.2f%s",
phoneSeparator, interSegmentTime, phoneSeparator);
linkinfo->div = strdup(pause);
}
newNode->flags = 0;
newNode = nodes.insert(4);
newNode->word = Vocab_None;
newNode->flags = 0;
maxIndex = 5;
LatticeTransition t(0, 0);;
insertTrans(1, 2, t);
insertTrans(2, 3, t);
insertTrans(3, 4, t);
insertTrans(4, 0, t);
if (!(implantLattice(2, lat1))) return false;
if (!(implantLattice(4, lat2, finalTime1+interSegmentTime)))
return false;
} else {
newNode = nodes.insert(2);
newNode->word = Vocab_None;
newNode->flags = 0;
newNode = nodes.insert(3);
newNode->word = Vocab_None;
newNode->flags = 0;
maxIndex = 4;
LatticeTransition t(0, 0);;
insertTrans(1, 2, t);
insertTrans(2, 3, t);
insertTrans(3, 0, t);
if (!(implantLattice(2, lat1, finalTime1))) return false;
if (!(implantLattice(3, lat2, finalTime1))) return false;
}
limitIntlogs = lat1.limitIntlogs || lat2.limitIntlogs;
return true;
}
// add a string of words to the lattice
// (similar to MultiAlign::addWords())
void
Lattice::addWords(const VocabIndex *words, Prob prob, Boolean addPauses)
{
NodeIndex lastNode = initial;
LatticeTransition trans(ProbToLogP(prob), 0);
for (unsigned i = 0; words[i] != Vocab_None; i ++) {
NodeIndex wordNode = dupNode(words[i], 0);
insertTrans(lastNode, wordNode, trans);
if (addPauses) {
NodeIndex pauseNode = dupNode(vocab.pauseIndex(), 0);
insertTrans(lastNode, pauseNode, trans);
trans.weight = LogP_One;
insertTrans(pauseNode, wordNode, trans);
}
trans.weight = LogP_One;
lastNode = wordNode;
}
insertTrans(lastNode, final, trans);
if (addPauses) {
NodeIndex pauseNode = dupNode(vocab.pauseIndex(), 0);
insertTrans(lastNode, pauseNode, trans);
trans.weight = LogP_One;
insertTrans(pauseNode, final, trans);
}
}
// reading in recursively defined PFSGs
//
Boolean
Lattice::readRecPFSGs(File &file)
{
if (debug(DebugPrintFunctionality)) {
dout() << "Lattice::readRecPFSGs: "
<< "reading in dag PFSG......\n";
}
NodeQueue nodeQueue;
if (!readPFSG(file)) {
return false;
}
while (fgetc(file) != EOF) {
fseek(file, -1, SEEK_CUR);
Lattice *lat = new Lattice(vocab);
if (!lat->readPFSG(file)) {
if (debug(DebugPrintFunctionality)) {
dout() << "Lattice::readRecPFSGs: "
<< "failed in reading in dag PFSGs.\n";
}
dout() << "Test: 0\n";
delete lat;
return false;
}
VocabIndex word = vocab.addWord(lat->getName());
Lattice **pt = subPFSGs.insert(word);
*pt = lat;
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::readRecPFSGs: got "
<< "sub-PFSG (" << (*pt)->getName() << ").\n";
}
}
LHashIter<NodeIndex, LatticeNode> nodeIter(nodes, nodeSort);
NodeIndex nodeIndex;
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
// for non-terminal node, i.e., sub-PFSGs, get its non
// recursive equivalent PFSG.
dout() << "Processing node (" << getWord(node->word) << ")\n";
Lattice ** pt = subPFSGs.find(node->word);
if (!pt) {
// terminal node
dout() << "It's terminal node!\n";
continue;
}
Lattice * lat = *pt;
dout() << "It's NON-terminal node!\n";
nodeQueue.addToNodeQueue(nodeIndex);
}
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::readRecPFSGs: done with "
<< "preparing vtmNodes for implants\n";
}
while (nodeQueue.is_empty() == false) {
nodeIndex = nodeQueue.popNodeQueue();
// duplicate lat within this current lattice.
LatticeNode * node = nodes.find(nodeIndex);
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::readRecPFSGs: processing subPFSG ("
<< nodeIndex << ", " << getWord(node->word) << ")\n";
}
Lattice *lat = getNonRecPFSG(node->word);
if (!lat) {
continue;
}
implantLattice(nodeIndex, *lat);
dout() << "Lattice::readRecPFSGs: maxIndex ("
<< maxIndex << ")\n";
}
// release all the memory after all the subPFSGs have implanted.
LHashIter<VocabIndex, Lattice *> subPFSGsIter(subPFSGs);
VocabIndex word;
while (Lattice **pt = subPFSGsIter.next(word)) {
delete (Lattice *) (*pt);
}
return true;
}
// return a flattened PFSG with nodeVocab as its PFSG name.
Lattice *
Lattice::getNonRecPFSG(VocabIndex nodeVocab)
{
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::getNonRecPFSG: processing subPFSG ("
<< getWord(nodeVocab) << ")\n";
}
NodeQueue nodeQueue;
Lattice **pt = subPFSGs.find(nodeVocab);
if (!pt) {
// this is a terminal node
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::getNonRecPFSG: terminal node ("
<< nodeVocab << ")\n";
}
return 0;
}
Lattice * subPFSG = *pt;
LHashIter<NodeIndex, LatticeNode> nodeIter(subPFSG->nodes, nodeSort);
NodeIndex nodeIndex;
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
Lattice **pt = subPFSGs.find(node->word);
if (!(pt)) {
// terminal node
continue;
}
nodeQueue.addToNodeQueue(nodeIndex);
}
while (nodeQueue.is_empty() == false) {
nodeIndex = nodeQueue.popNodeQueue();
// duplicate lat within this current lattice.
LatticeNode * node = nodes.find(nodeIndex);
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::getNonRecPFSG: processing subPFSG ("
<< nodeIndex << ", " << getWord(node->word) << ")\n";
}
Lattice *lat = getNonRecPFSG(node->word);
if (!lat) {
continue;
}
subPFSG->implantLattice(nodeIndex, *lat);
}
return subPFSG;
}
// substitute the current occurence of lat.word in subPFSG with
// lat
Boolean
Lattice::implantLattice(NodeIndex vtmNodeIndex, Lattice &lat, float addTime)
{
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::implantLattice: processing subPFSG ("
<< lat.getName() << ", " << vtmNodeIndex
<< ", times offset by " << addTime <<" seconds)\n";
}
// going through lat to duplicate all its nodes in the current lattice
LHashIter<NodeIndex, LatticeNode> nodeIter(lat.nodes, nodeSort);
NodeIndex nodeIndex;
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
// make one copy of the subPFSG' nodes in this current PFSG
LatticeNode *newNode = nodes.insert(maxIndex+nodeIndex);
newNode->flags = node->flags;
if (node->word == Vocab_None) {
newNode->word = Vocab_None;
} else {
// this is necessary because the two lattices might use
// different vocabularies
VocabString wn = lat.vocab.getWord(node->word);
newNode->word = vocab.addWord(wn);
}
// copy HTKWordInfo information
if (node->htkinfo) {
HTKWordInfo *linkinfo = new HTKWordInfo;
assert(linkinfo != 0);
newNode->htkinfo = htkinfos[htkinfos.size()] = linkinfo;
*linkinfo = *node->htkinfo;
linkinfo->word = newNode->word; // see above
linkinfo->time = linkinfo->time + addTime;
}
// clone transition
{ // for incoming nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->inTransitions);
NodeIndex fromNodeIndex;
while (LatticeTransition *trans = transIter.next(fromNodeIndex)) {
*(newNode->inTransitions.insert(maxIndex+fromNodeIndex)) = *trans;
}
}
{ // for outgoing nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->outTransitions);
NodeIndex toNodeIndex;
while (LatticeTransition *trans = transIter.next(toNodeIndex)) {
*(newNode->outTransitions.insert(maxIndex+toNodeIndex)) = *trans;
}
}
}
NodeIndex subInitial = lat.getInitial();
LatticeNode * initialNode;
if (!(initialNode =
nodes.find(maxIndex+subInitial))) {
// check initial node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::implantLattice: ("
<< lat.getName()
<< ") undefined initial node Index ("
<< subInitial << ")\n";
}
exit(-1);
} else {
initialNode->word = Vocab_None;
}
NodeIndex subFinal = lat.getFinal();
LatticeNode *finalNode;
if (!(finalNode =
nodes.find(maxIndex+subFinal))) {
// only check the last final node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::implantLattice: ("
<< lat.getName()
<< ") undefined initial node Index ("
<< subFinal << ")\n";
}
exit(-1);
} else {
finalNode->word = Vocab_None;
}
// done with checking the nodes
// connecting initial and final subPFSG nodes with the existing PFSG
{
// processing incoming and outgoing nodes of node vtmNodeIndex;
LatticeNode * node = nodes.find(vtmNodeIndex);
NodeIndex subInitial = maxIndex+lat.getInitial();
NodeIndex subFinal = maxIndex+lat.getFinal();
{
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->inTransitions);
NodeIndex fromNodeIndex;
while (LatticeTransition *trans = transIter.next(fromNodeIndex)) {
insertTrans(fromNodeIndex, subInitial, *trans);
}
// end of processing incoming nodes of node
}
{
// processing outgoing nodes of node
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->outTransitions);
NodeIndex toNodeIndex;
while (LatticeTransition *trans = transIter.next(toNodeIndex)) {
insertTrans(subFinal, toNodeIndex, *trans);
}
// end of processing outgoing nodes of node
}
}
removeNode(vtmNodeIndex);
maxIndex += lat.getMaxIndex();
return true;
}
// substitute all the occurences of lat.word in subPFSG with
// lat
Boolean
Lattice::implantLatticeXCopies(Lattice &lat)
{
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::implantLatticeXCopies: processing subPFSG ("
<< lat.getName() << ")\n";
}
unsigned numCopies = 0;
{
LHashIter<NodeIndex, LatticeNode> nodeIter(nodes, nodeSort);
NodeIndex nodeIndex;
VocabIndex subPFSGWord = vocab.getIndex(lat.getName());
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
if (node->word == subPFSGWord) {
numCopies++;
}
}
}
// need to check whether numNodes is the same as lat.maxIndex
unsigned numNodes = lat.getNumNodes();
LHashIter<NodeIndex, LatticeNode> nodeIter(lat.nodes, nodeSort);
NodeIndex nodeIndex;
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
// need to make numCopies
// make "copies" of the subPFSG' nodes in this current PFSG
for (unsigned k = 0; k < numCopies; k++) {
LatticeNode *newNode = nodes.insert(maxIndex+k*numNodes+nodeIndex);
newNode->word = node->word;
newNode->flags = node->flags;
// clone transition
{ // for incoming nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->inTransitions);
NodeIndex fromNodeIndex;
while (transIter.next(fromNodeIndex)) {
newNode->inTransitions.insert(maxIndex+k*numNodes+fromNodeIndex);
}
}
{ // for outgoing nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->outTransitions);
NodeIndex toNodeIndex;
while (transIter.next(toNodeIndex)) {
newNode->outTransitions.insert(maxIndex+k*numNodes+toNodeIndex);
}
}
}
}
NodeIndex subInitial = lat.getInitial();
LatticeNode * initialNode;
if (!(initialNode =
nodes.find(subInitial+maxIndex+(numCopies-1)*numNodes))) {
// only check the last initial node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::implantLatticeXCopies: ("
<< lat.getName()
<< ") undefined initial node Index\n";
}
exit(-1);
}
NodeIndex subFinal = lat.getFinal();
LatticeNode *finalNode;
if (!(finalNode =
nodes.find(subFinal+maxIndex+(numCopies-1)*numNodes))) {
// only check the last final node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::implantLatticeXCopies: "
<< lat.getName()
<< "undefined final node Index\n";
}
exit(-1);
}
// done with checking the nodes
// connecting initial and final subPFSG nodes with the existing PFSG
{
unsigned k = 0;
LHashIter<NodeIndex, LatticeNode> nodeIter(nodes, nodeSort);
NodeIndex nodeIndex, fromNodeIndex, toNodeIndex;
VocabIndex subPFSGWord = vocab.getIndex(lat.getName());
for (nodeIndex = 0; nodeIndex< maxIndex; nodeIndex++) {
LatticeNode * node = nodes.find(nodeIndex);
if (node->word == subPFSGWord) {
// processing incoming and outgoing nodes of node
NodeIndex subInitial =
lat.getInitial()+maxIndex+(numCopies-1)*numNodes;
NodeIndex subFinal =
lat.getFinal()+maxIndex+(numCopies-1)*numNodes;
{
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->inTransitions);
while (LatticeTransition *trans = transIter.next(fromNodeIndex)) {
insertTrans(fromNodeIndex, subInitial, *trans);
removeTrans(fromNodeIndex, nodeIndex);
}
// end of processing incoming nodes of node
}
{
// processing outgoing nodes of node
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->outTransitions);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -