📄 lattice.cc
字号:
while (LatticeTransition *trans = transIter.next(toNodeIndex)) {
insertTrans(subFinal, toNodeIndex, *trans);
removeTrans(nodeIndex, toNodeIndex);
}
// end of processing outgoing nodes of node
}
k++;
if (k > numCopies) {
break;
}
} // end of processing k-th copy of implanted node
} // end of going through the existing lattices
}
maxIndex += numCopies * lat.getMaxIndex();
return true;
}
Boolean
Lattice::readPFSGs(File &file)
{
if (debug(DebugPrintFunctionality)) {
dout() << "Lattice::readPFSGs: reading in nested PFSGs...\n";
}
removeAll();
char buffer[1024];
char *subName = 0;
// usually, it is the main PFSG
if (!readPFSG(file)) {
return false;
}
while (fscanf(file, " name %1024s", buffer) == 1) {
subName = strdup(buffer);
assert(subName != 0);
// get the number of copies needed for each subPFSG.
unsigned copies = 0;
{
VocabIndex windex = vocab.addWord(subName);
LHashIter<NodeIndex, LatticeNode> nodeIter(nodes);
NodeIndex nodeIndex;
while (LatticeNode *node = nodeIter.next(nodeIndex)) {
if (node->word == windex) {
copies++;
}
}
}
unsigned numNodes;
if (fscanf(file, " nodes %u", &numNodes) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing nodes in sub-PFSG of the PFSG file\n";
}
return false;
}
// Parse node names and create nodes
for (NodeIndex n = 0; n < numNodes; n ++) {
if (fscanf(file, "%1024s", buffer) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing node " << n << " name\n";
}
return false;
}
/*
* Map word string to VocabIndex
*/
VocabIndex windex;
if (strcmp(buffer, NullNodeName) == 0) {
windex = Vocab_None;
} else if (useUnk) {
windex = vocab.getIndex(buffer, vocab.unkIndex());
} else {
windex = vocab.addWord(buffer);
}
// make "copies" of the subPFSGs' nodes
for (unsigned k = 0; k < copies; k++) {
LatticeNode *node = nodes.insert(maxIndex+k*numNodes+n);
node->word = windex;
node->flags = 0;
}
}
unsigned initialNodeIndex;
if (fscanf(file, " initial %u", &initialNodeIndex) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing initial node Index in PFSG file\n";
}
return false;
}
LatticeNode * initialNode;
if (!(initialNode =
nodes.find(initialNodeIndex+maxIndex+(copies-1)*numNodes))) {
// only check the last initial node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "undefined initial node Index\n";
}
return false;
}
unsigned finalNodeIndex;
if (fscanf(file, " final %u", &finalNodeIndex) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing final node Index in PFSG file\n";
}
return false;
}
LatticeNode *finalNode;
if (!(finalNode =
nodes.find(finalNodeIndex+maxIndex+(copies-1)*numNodes))) {
// only check the last final node
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "undefined final node Index\n";
}
return false;
}
// reading in all the transitions for the current subPFSG
unsigned numTransitions;
if (fscanf(file, " transitions %u", &numTransitions) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing transitions in PFSG file\n";
}
return false;
}
for (unsigned i = 0; i < numTransitions; i ++) {
unsigned from, to;
double prob;
if (fscanf(file, " %u %u %lf", &from, &to, &prob) != 3) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSGs: "
<< "missing transition " << i << " in PFSG file\n";
}
return false;
}
for (unsigned k = 0; k < copies; k++) {
LatticeTransition t(IntlogToLogP(prob), 0);
insertTrans(maxIndex+k*numNodes+from, maxIndex+k*numNodes+to, t);
}
}
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::readPFSGs: done with reading "
<< copies << " copies of sub pfsg (" << subName << ")\n";
}
// going through all the nodes to flatten the current PFSG
VocabIndex windex = vocab.addWord(subName);
unsigned k = 0; // number of copies consumed
initialNodeIndex += maxIndex;
finalNodeIndex += maxIndex;
LHashIter<NodeIndex, LatticeNode> nodeIter(nodes, nodeSort);
NodeIndex nodeIndex;
LatticeNode *node;
while ((node = nodeIter.next(nodeIndex)) && (k < copies)) {
// do a substitution
if (node->word == windex) {
LatticeNode * initSubPFSGNode = nodes.find(initialNodeIndex);
NodeIndex fromNodeIndex, toNodeIndex;
// connecting incoming nodes to initialNodeIndex
{
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->inTransitions);
while (LatticeTransition *trans = transIter.next(fromNodeIndex)) {
*(initSubPFSGNode->inTransitions.insert(fromNodeIndex)) = *trans;
}
}
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with removing orig connection\n";
}
{
// add initSubPFSG to the incoming nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(initSubPFSGNode->inTransitions);
while (LatticeTransition *trans = transIter.next(fromNodeIndex)) {
LatticeNode * fromNode = nodes.find(fromNodeIndex);
*(fromNode->outTransitions.insert(initialNodeIndex)) = *trans;
}
}
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with adding new connection\n";
}
// connecting subPfsg->final to outgoing nodes
LatticeNode * finalSubPFSGNode = nodes.find(finalNodeIndex);
{
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(node->outTransitions);
while (LatticeTransition *trans = transIter.next(toNodeIndex)) {
*(finalSubPFSGNode->outTransitions.insert(toNodeIndex)) = *trans;
}
}
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with "
<< "removing its old transitions for outgoing\n";
}
{
// add finalSubPFSG to the outgoing nodes
TRANSITER_T<NodeIndex,LatticeTransition>
transIter(finalSubPFSGNode->outTransitions);
while (LatticeTransition *trans = transIter.next(toNodeIndex)) {
LatticeNode * toNode = nodes.find(toNodeIndex);
*(toNode->inTransitions.insert(finalNodeIndex)) = *trans;
}
}
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with "
<< "adding new connection for outgoing\n";
}
// removing this pseudo word, finally
removeNode(nodeIndex);
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with "
<< "removing node (" << nodeIndex << ")\n";
}
k++;
initialNodeIndex += numNodes;
finalNodeIndex += numNodes;
if (debug(DebugPrintInnerLoop)) {
dout() << "Lattice::readPFSGs: done with "
<< "single flattening (" << subName << ")\n";
}
} // end of single flatening
} // end of flatening loop
maxIndex += copies*numNodes;
if (subName) {
free(subName);
}
}
return true;
}
Boolean
Lattice::readPFSG(File &file)
{
removeAll();
if (debug(DebugPrintOutLoop)) {
dout() << "Lattice::readPFSG: reading in PFSG....\n";
}
char buffer[1024];
if (fscanf(file, " name %1024s", buffer) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing name in PFSG file\n";
}
return false;
}
if (name) {
free((void *)name);
}
/*
* try to parse duration from name string,
* and strip it from name if present
*/
{
char *pos = strchr(buffer, '(');
double value;
if (pos != 0 && sscanf(pos, "(duration=%lf)", &value) == 1) {
duration = value;
*pos = '\0';
} else {
duration = 0.0;
}
}
name = strdup(buffer);
assert(name != 0);
unsigned numNodes;
if (fscanf(file, " nodes %u", &numNodes) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing nodes in PFSG file\n";
}
return false;
}
maxIndex = numNodes;
/*
* Parse node names and create nodes
*/
for (NodeIndex n = 0; n < numNodes; n ++) {
if (fscanf(file, "%1024s", buffer) != 1) {
if (debug(DebugPrintInnerLoop)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing node " << n << " name\n";
}
return false;
}
/*
* Map word string to VocabIndex
*/
VocabIndex windex;
if (strcmp(buffer, NullNodeName) == 0) {
windex = Vocab_None;
} else if (useUnk) {
windex = vocab.getIndex(buffer, vocab.unkIndex());
} else {
windex = vocab.addWord(buffer);
}
LatticeNode *node = nodes.insert(n);
node->word = windex;
node->flags = 0;
}
unsigned initialNodeIndex;
if (fscanf(file, " initial %u", &initialNodeIndex) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing initial node Index in PFSG file\n";
}
return false;
}
LatticeNode * initialNode;
if (! (initialNode = nodes.find(initialNodeIndex))) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "undefined initial node Index\n";
}
return false;
}
initial = initialNodeIndex;
unsigned finalNodeIndex;
if (fscanf(file, " final %u", &finalNodeIndex) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing final node Index in PFSG file\n";
}
return false;
}
LatticeNode *finalNode;
if (! (finalNode = nodes.find(finalNodeIndex))) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "undefined final node Index\n";
}
return false;
}
final = finalNodeIndex;
if (top) {
initialNode->word = vocab.ssIndex();
finalNode->word = vocab.seIndex();
top = false;
}
unsigned numTransitions;
if (fscanf(file, " transitions %u", &numTransitions) != 1) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing transitions in PFSG file\n";
}
return false;
}
for (unsigned i = 0; i < numTransitions; i ++) {
unsigned from, to;
double prob;
if (fscanf(file, " %u %u %lf", &from, &to, &prob) != 3) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "missing transition " << i << " in PFSG file\n";
}
return false;
}
LatticeNode *fromNode = nodes.find(from);
if (!fromNode) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "undefined source node in transition " << i << "\n";
}
return false;
}
LatticeNode *toNode = nodes.find(to);
if (!toNode) {
if (debug(DebugPrintFatalMessages)) {
dout() << "Fatal Error in Lattice::readPFSG: "
<< "undefined target node in transition " << i << "\n";
}
return false;
}
LogP weight = IntlogToLogP(prob);
LatticeTransition *trans = fromNode->outTransitions.insert(to);
trans->weight = weight;
trans->flags = 0;
trans = toNode->inTransitions.insert(from);
trans->weight = weight;
trans->flags = 0;
}
return true;
}
Boolean
Lattice::readMesh(File &file) {
WordMesh inputMesh(vocab);
Boolean status = inputMesh.read(file);
if (!status) {
return status;
}
// set the weight of the score we insert to 1
htkheader.x1scale = 1.0;
NodeIndex fromNodeIndex = getInitial();
Boolean haveSentStart = false;
Boolean haveSentEnd = false;
for (unsigned i = 0; i < inputMesh.length(); i ++) {
// insert NULL node as destination for the following words
// in this alignment column
NodeIndex toNodeIndex = getMaxIndex();
insertNode(NullNodeName, toNodeIndex);
LHash<VocabIndex,Prob> *words = inputMesh.wordColumn(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -