2010-04-13 19:34:39 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2009 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-07-28 22:49:43 +04:00
|
|
|
#include <limits>
|
2010-04-13 19:34:39 +04:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
// Include Visual Leak Detector
|
2011-07-24 03:26:56 +04:00
|
|
|
//#include <vld.h>
|
2010-04-13 19:34:39 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ExtractedRule.h"
|
|
|
|
#include "Hole.h"
|
|
|
|
#include "HoleCollection.h"
|
|
|
|
#include "RuleExist.h"
|
|
|
|
#include "SentenceAlignmentWithSyntax.h"
|
|
|
|
#include "SyntaxTree.h"
|
|
|
|
#include "tables-core.h"
|
|
|
|
#include "XmlTree.h"
|
2011-10-13 22:57:23 +04:00
|
|
|
#include "InputFileStream.h"
|
2012-05-11 21:18:01 +04:00
|
|
|
#include "OutputFileStream.h"
|
2010-04-13 19:34:39 +04:00
|
|
|
|
|
|
|
using namespace std;
|
2012-06-30 18:43:47 +04:00
|
|
|
using namespace MosesTraining;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
|
|
|
typedef vector< int > LabelIndex;
|
|
|
|
typedef map< int, int > WordIndex;
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
class ExtractTask
|
2012-07-31 22:40:43 +04:00
|
|
|
{
|
2012-04-17 08:54:48 +04:00
|
|
|
private:
|
2012-07-31 22:40:43 +04:00
|
|
|
SentenceAlignmentWithSyntax &m_sentence;
|
|
|
|
const RuleExtractionOptions &m_options;
|
2012-08-01 00:49:38 +04:00
|
|
|
Moses::OutputFileStream& m_extractFile;
|
|
|
|
Moses::OutputFileStream& m_extractFileInv;
|
2013-09-08 01:04:01 +04:00
|
|
|
Moses::OutputFileStream& m_extractFileContext;
|
|
|
|
Moses::OutputFileStream& m_extractFileContextInv;
|
2012-07-31 22:40:43 +04:00
|
|
|
|
|
|
|
vector< ExtractedRule > m_extractedRules;
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-07-31 22:40:43 +04:00
|
|
|
// main functions
|
|
|
|
void extractRules();
|
|
|
|
void addRuleToCollection(ExtractedRule &rule);
|
|
|
|
void consolidateRules();
|
|
|
|
void writeRulesToFile();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-07-31 22:40:43 +04:00
|
|
|
// subs
|
2012-08-23 22:40:09 +04:00
|
|
|
void addRule( int, int, int, int, int, RuleExist &ruleExist);
|
2012-07-31 22:40:43 +04:00
|
|
|
void addHieroRule( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, RuleExist &ruleExist, HoleCollection &holeColl, int numHoles, int initStartF, int wordCountT, int wordCountS);
|
2012-11-10 03:55:01 +04:00
|
|
|
void saveHieroPhrase( int startT, int endT, int startS, int endS
|
2012-08-23 22:40:09 +04:00
|
|
|
, HoleCollection &holeColl, LabelIndex &labelIndex, int countS);
|
2012-11-10 03:55:01 +04:00
|
|
|
string saveTargetHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, WordIndex &indexT, HoleCollection &holeColl, const LabelIndex &labelIndex, double &logPCFGScore, int countS);
|
2012-11-10 03:55:01 +04:00
|
|
|
string saveSourceHieroPhrase( int startT, int endT, int startS, int endS
|
2012-07-31 22:40:43 +04:00
|
|
|
, HoleCollection &holeColl, const LabelIndex &labelIndex);
|
|
|
|
void preprocessSourceHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, WordIndex &indexS, HoleCollection &holeColl, const LabelIndex &labelIndex);
|
2012-11-10 03:55:01 +04:00
|
|
|
void saveHieroAlignment( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, const WordIndex &indexS, const WordIndex &indexT, HoleCollection &holeColl, ExtractedRule &rule);
|
2012-11-10 03:55:01 +04:00
|
|
|
void saveAllHieroPhrases( int startT, int endT, int startS, int endS, HoleCollection &holeColl, int countS);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
|
|
|
inline string IntToString( int i ) {
|
2012-07-31 22:40:43 +04:00
|
|
|
stringstream out;
|
|
|
|
out << i;
|
|
|
|
return out.str();
|
|
|
|
}
|
2012-04-17 08:54:48 +04:00
|
|
|
|
|
|
|
public:
|
2013-09-08 01:04:01 +04:00
|
|
|
ExtractTask(SentenceAlignmentWithSyntax &sentence, const RuleExtractionOptions &options, Moses::OutputFileStream &extractFile, Moses::OutputFileStream &extractFileInv, Moses::OutputFileStream &extractFileContext, Moses::OutputFileStream &extractFileContextInv):
|
2012-04-17 08:54:48 +04:00
|
|
|
m_sentence(sentence),
|
|
|
|
m_options(options),
|
2012-08-01 00:49:38 +04:00
|
|
|
m_extractFile(extractFile),
|
2013-09-08 01:04:01 +04:00
|
|
|
m_extractFileInv(extractFileInv),
|
|
|
|
m_extractFileContext(extractFileContext),
|
|
|
|
m_extractFileContextInv(extractFileContextInv) {}
|
2012-04-17 08:54:48 +04:00
|
|
|
void Run();
|
|
|
|
|
|
|
|
};
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
// stats for glue grammar and unknown word label probabilities
|
|
|
|
void collectWordLabelCounts(SentenceAlignmentWithSyntax &sentence );
|
|
|
|
void writeGlueGrammar(const string &, RuleExtractionOptions &options, set< string > &targetLabelCollection, map< string, int > &targetTopLabelCollection);
|
|
|
|
void writeUnknownWordLabel(const string &);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
cerr << "extract-rules, written by Philipp Koehn\n"
|
|
|
|
<< "rule extraction from an aligned parallel corpus\n";
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
RuleExtractionOptions options;
|
2012-08-19 02:47:05 +04:00
|
|
|
int sentenceOffset = 0;
|
2012-04-17 08:54:48 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
int thread_count = 1;
|
|
|
|
#endif
|
2011-02-24 16:57:11 +03:00
|
|
|
if (argc < 5) {
|
2012-04-17 08:54:48 +04:00
|
|
|
cerr << "syntax: extract-rules corpus.target corpus.source corpus.align extract ["
|
2012-07-31 22:40:43 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
<< " --GlueGrammar FILE"
|
2011-02-24 16:57:11 +03:00
|
|
|
<< " | --UnknownWordLabel FILE"
|
|
|
|
<< " | --OnlyDirect"
|
|
|
|
<< " | --MaxSpan[" << options.maxSpan << "]"
|
|
|
|
<< " | --MinHoleTarget[" << options.minHoleTarget << "]"
|
|
|
|
<< " | --MinHoleSource[" << options.minHoleSource << "]"
|
|
|
|
<< " | --MinWords[" << options.minWords << "]"
|
|
|
|
<< " | --MaxSymbolsTarget[" << options.maxSymbolsTarget << "]"
|
|
|
|
<< " | --MaxSymbolsSource[" << options.maxSymbolsSource << "]"
|
|
|
|
<< " | --MaxNonTerm[" << options.maxNonTerm << "]"
|
|
|
|
<< " | --MaxScope[" << options.maxScope << "]"
|
|
|
|
<< " | --SourceSyntax | --TargetSyntax"
|
2012-05-27 15:43:16 +04:00
|
|
|
<< " | --AllowOnlyUnalignedWords | --DisallowNonTermConsecTarget |--NonTermConsecSource | --NoNonTermFirstWord | --NoFractionalCounting"
|
|
|
|
<< " | --UnpairedExtractFormat"
|
2012-08-23 22:40:09 +04:00
|
|
|
<< " | --ConditionOnTargetLHS ]"
|
2013-09-08 01:04:01 +04:00
|
|
|
<< " | --BoundaryRules[" << options.boundaryRules << "]"
|
|
|
|
<< " | --FlexibilityScore\n";
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
char* &fileNameT = argv[1];
|
|
|
|
char* &fileNameS = argv[2];
|
|
|
|
char* &fileNameA = argv[3];
|
|
|
|
string fileNameGlueGrammar;
|
|
|
|
string fileNameUnknownWordLabel;
|
|
|
|
string fileNameExtract = string(argv[4]);
|
|
|
|
|
|
|
|
int optionInd = 5;
|
|
|
|
|
|
|
|
for(int i=optionInd; i<argc; i++) {
|
|
|
|
// maximum span length
|
|
|
|
if (strcmp(argv[i],"--MaxSpan") == 0) {
|
|
|
|
options.maxSpan = atoi(argv[++i]);
|
|
|
|
if (options.maxSpan < 1) {
|
|
|
|
cerr << "extract error: --maxSpan should be at least 1" << endl;
|
2010-04-13 19:34:39 +04:00
|
|
|
exit(1);
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i],"--MinHoleTarget") == 0) {
|
|
|
|
options.minHoleTarget = atoi(argv[++i]);
|
|
|
|
if (options.minHoleTarget < 1) {
|
|
|
|
cerr << "extract error: --minHoleTarget should be at least 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i],"--MinHoleSource") == 0) {
|
|
|
|
options.minHoleSource = atoi(argv[++i]);
|
|
|
|
if (options.minHoleSource < 1) {
|
|
|
|
cerr << "extract error: --minHoleSource should be at least 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
// maximum number of words in hierarchical phrase
|
|
|
|
else if (strcmp(argv[i],"--MaxSymbolsTarget") == 0) {
|
|
|
|
options.maxSymbolsTarget = atoi(argv[++i]);
|
|
|
|
if (options.maxSymbolsTarget < 1) {
|
|
|
|
cerr << "extract error: --MaxSymbolsTarget should be at least 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
// maximum number of words in hierarchical phrase
|
|
|
|
else if (strcmp(argv[i],"--MaxSymbolsSource") == 0) {
|
|
|
|
options.maxSymbolsSource = atoi(argv[++i]);
|
|
|
|
if (options.maxSymbolsSource < 1) {
|
|
|
|
cerr << "extract error: --MaxSymbolsSource should be at least 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// minimum number of words in hierarchical phrase
|
|
|
|
else if (strcmp(argv[i],"--MinWords") == 0) {
|
|
|
|
options.minWords = atoi(argv[++i]);
|
|
|
|
if (options.minWords < 0) {
|
|
|
|
cerr << "extract error: --MinWords should be at least 0" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// maximum number of non-terminals
|
|
|
|
else if (strcmp(argv[i],"--MaxNonTerm") == 0) {
|
|
|
|
options.maxNonTerm = atoi(argv[++i]);
|
|
|
|
if (options.maxNonTerm < 1) {
|
|
|
|
cerr << "extract error: --MaxNonTerm should be at least 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// maximum scope (see Hopkins and Langmead (2010))
|
|
|
|
else if (strcmp(argv[i],"--MaxScope") == 0) {
|
|
|
|
options.maxScope = atoi(argv[++i]);
|
|
|
|
if (options.maxScope < 0) {
|
|
|
|
cerr << "extract error: --MaxScope should be at least 0" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
} else if (strcmp(argv[i], "--GZOutput") == 0) {
|
|
|
|
options.gzOutput = true;
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
// allow consecutive non-terminals (X Y | X Y)
|
|
|
|
else if (strcmp(argv[i],"--TargetSyntax") == 0) {
|
|
|
|
options.targetSyntax = true;
|
|
|
|
} else if (strcmp(argv[i],"--SourceSyntax") == 0) {
|
|
|
|
options.sourceSyntax = true;
|
|
|
|
} else if (strcmp(argv[i],"--AllowOnlyUnalignedWords") == 0) {
|
|
|
|
options.requireAlignedWord = false;
|
|
|
|
} else if (strcmp(argv[i],"--DisallowNonTermConsecTarget") == 0) {
|
|
|
|
options.nonTermConsecTarget = false;
|
|
|
|
} else if (strcmp(argv[i],"--NonTermConsecSource") == 0) {
|
|
|
|
options.nonTermConsecSource = true;
|
|
|
|
} else if (strcmp(argv[i],"--NoNonTermFirstWord") == 0) {
|
|
|
|
options.nonTermFirstWord = false;
|
|
|
|
} else if (strcmp(argv[i],"--OnlyOutputSpanInfo") == 0) {
|
|
|
|
options.onlyOutputSpanInfo = true;
|
|
|
|
} else if (strcmp(argv[i],"--OnlyDirect") == 0) {
|
|
|
|
options.onlyDirectFlag = true;
|
|
|
|
} else if (strcmp(argv[i],"--GlueGrammar") == 0) {
|
|
|
|
options.glueGrammarFlag = true;
|
|
|
|
if (++i >= argc) {
|
|
|
|
cerr << "ERROR: Option --GlueGrammar requires a file name" << endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
fileNameGlueGrammar = string(argv[i]);
|
|
|
|
cerr << "creating glue grammar in '" << fileNameGlueGrammar << "'" << endl;
|
|
|
|
} else if (strcmp(argv[i],"--UnknownWordLabel") == 0) {
|
|
|
|
options.unknownWordLabelFlag = true;
|
|
|
|
if (++i >= argc) {
|
|
|
|
cerr << "ERROR: Option --UnknownWordLabel requires a file name" << endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
fileNameUnknownWordLabel = string(argv[i]);
|
|
|
|
cerr << "creating unknown word labels in '" << fileNameUnknownWordLabel << "'" << endl;
|
|
|
|
}
|
|
|
|
// TODO: this should be a useful option
|
|
|
|
//else if (strcmp(argv[i],"--ZipFiles") == 0) {
|
|
|
|
// zipFiles = true;
|
|
|
|
//}
|
|
|
|
// if an source phrase is paired with two target phrases, then count(t|s) = 0.5
|
|
|
|
else if (strcmp(argv[i],"--NoFractionalCounting") == 0) {
|
|
|
|
options.fractionalCounting = false;
|
2012-05-25 20:29:47 +04:00
|
|
|
} else if (strcmp(argv[i],"--PCFG") == 0) {
|
|
|
|
options.pcfgScore = true;
|
2012-05-27 15:43:16 +04:00
|
|
|
} else if (strcmp(argv[i],"--UnpairedExtractFormat") == 0) {
|
|
|
|
options.unpairedExtractFormat = true;
|
|
|
|
} else if (strcmp(argv[i],"--ConditionOnTargetLHS") == 0) {
|
|
|
|
options.conditionOnTargetLhs = true;
|
2013-09-08 01:04:01 +04:00
|
|
|
} else if (strcmp(argv[i],"--FlexibilityScore") == 0) {
|
|
|
|
options.flexScoreFlag = true;
|
2013-05-29 21:16:15 +04:00
|
|
|
} else if (strcmp(argv[i],"-threads") == 0 ||
|
2012-04-17 08:54:48 +04:00
|
|
|
strcmp(argv[i],"--threads") == 0 ||
|
|
|
|
strcmp(argv[i],"--Threads") == 0) {
|
2012-09-22 02:11:59 +04:00
|
|
|
#ifdef WITH_THREADS
|
2012-04-17 08:54:48 +04:00
|
|
|
thread_count = atoi(argv[++i]);
|
2012-09-22 02:11:59 +04:00
|
|
|
#else
|
|
|
|
cerr << "thread support not compiled in." << '\n';
|
|
|
|
exit(1);
|
|
|
|
#endif
|
2012-08-19 02:47:05 +04:00
|
|
|
} else if (strcmp(argv[i], "--SentenceOffset") == 0) {
|
|
|
|
if (i+1 >= argc || argv[i+1][0] < '0' || argv[i+1][0] > '9') {
|
|
|
|
cerr << "extract: syntax error, used switch --SentenceOffset without a number" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
sentenceOffset = atoi(argv[++i]);
|
2012-08-23 22:40:09 +04:00
|
|
|
} else if (strcmp(argv[i],"--BoundaryRules") == 0) {
|
|
|
|
options.boundaryRules = true;
|
2011-02-24 16:57:11 +03:00
|
|
|
} else {
|
|
|
|
cerr << "extract: syntax error, unknown option '" << string(argv[i]) << "'\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cerr << "extracting hierarchical rules" << endl;
|
|
|
|
|
|
|
|
// open input files
|
2011-10-13 22:57:23 +04:00
|
|
|
Moses::InputFileStream tFile(fileNameT);
|
|
|
|
Moses::InputFileStream sFile(fileNameS);
|
|
|
|
Moses::InputFileStream aFile(fileNameA);
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
istream *tFileP = &tFile;
|
|
|
|
istream *sFileP = &sFile;
|
|
|
|
istream *aFileP = &aFile;
|
|
|
|
|
|
|
|
// open output files
|
2012-05-12 02:41:51 +04:00
|
|
|
string fileNameExtractInv = fileNameExtract + ".inv" + (options.gzOutput?".gz":"");
|
2012-05-11 21:18:01 +04:00
|
|
|
Moses::OutputFileStream extractFile;
|
|
|
|
Moses::OutputFileStream extractFileInv;
|
2013-09-08 01:04:01 +04:00
|
|
|
Moses::OutputFileStream extractFileContext;
|
|
|
|
Moses::OutputFileStream extractFileContextInv;
|
2012-05-12 02:41:51 +04:00
|
|
|
extractFile.Open((fileNameExtract + (options.gzOutput?".gz":"")).c_str());
|
2011-02-24 16:57:11 +03:00
|
|
|
if (!options.onlyDirectFlag)
|
2012-05-11 21:18:01 +04:00
|
|
|
extractFileInv.Open(fileNameExtractInv.c_str());
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2013-09-08 01:04:01 +04:00
|
|
|
if (options.flexScoreFlag) {
|
|
|
|
string fileNameExtractContext = fileNameExtract + ".context" + (options.gzOutput?".gz":"");
|
|
|
|
extractFileContext.Open(fileNameExtractContext.c_str());
|
|
|
|
if (!options.onlyDirectFlag) {
|
2013-09-27 12:35:24 +04:00
|
|
|
string fileNameExtractContextInv = fileNameExtract + ".context.inv" + (options.gzOutput?".gz":"");
|
|
|
|
extractFileContextInv.Open(fileNameExtractContextInv.c_str());
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
|
|
|
}
|
2012-04-17 08:54:48 +04:00
|
|
|
|
|
|
|
// stats on labels for glue grammar and unknown word label probabilities
|
|
|
|
set< string > targetLabelCollection, sourceLabelCollection;
|
|
|
|
map< string, int > targetTopLabelCollection, sourceTopLabelCollection;
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// loop through all sentence pairs
|
2012-08-19 02:47:05 +04:00
|
|
|
size_t i=sentenceOffset;
|
2014-06-08 19:23:14 +04:00
|
|
|
string targetString, sourceString, alignmentString;
|
|
|
|
|
|
|
|
while(getline(*tFileP, targetString)) {
|
2011-02-24 16:57:11 +03:00
|
|
|
i++;
|
2012-07-31 22:40:43 +04:00
|
|
|
|
2014-06-08 19:23:14 +04:00
|
|
|
getline(*sFileP, sourceString);
|
|
|
|
getline(*aFileP, alignmentString);
|
|
|
|
|
|
|
|
if (i%1000 == 0) cerr << i << " " << flush;
|
2012-04-17 08:54:48 +04:00
|
|
|
|
2012-07-31 22:40:43 +04:00
|
|
|
SentenceAlignmentWithSyntax sentence
|
2013-05-29 21:16:15 +04:00
|
|
|
(targetLabelCollection, sourceLabelCollection,
|
|
|
|
targetTopLabelCollection, sourceTopLabelCollection, options);
|
2011-02-24 16:57:11 +03:00
|
|
|
//az: output src, tgt, and alingment line
|
|
|
|
if (options.onlyOutputSpanInfo) {
|
|
|
|
cout << "LOG: SRC: " << sourceString << endl;
|
|
|
|
cout << "LOG: TGT: " << targetString << endl;
|
|
|
|
cout << "LOG: ALT: " << alignmentString << endl;
|
|
|
|
cout << "LOG: PHRASES_BEGIN:" << endl;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2014-06-08 19:23:14 +04:00
|
|
|
if (sentence.create(targetString.c_str(), sourceString.c_str(), alignmentString.c_str(),"", i, options.boundaryRules)) {
|
2011-02-24 16:57:11 +03:00
|
|
|
if (options.unknownWordLabelFlag) {
|
2012-07-31 22:40:43 +04:00
|
|
|
collectWordLabelCounts(sentence);
|
2012-04-17 08:54:48 +04:00
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
ExtractTask *task = new ExtractTask(sentence, options, extractFile, extractFileInv, extractFileContext, extractFileContextInv);
|
2012-04-17 08:54:48 +04:00
|
|
|
task->Run();
|
|
|
|
delete task;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
if (options.onlyOutputSpanInfo) cout << "LOG: PHRASES_END:" << endl; //az: mark end of phrases
|
|
|
|
}
|
|
|
|
|
2011-10-13 22:57:23 +04:00
|
|
|
tFile.Close();
|
|
|
|
sFile.Close();
|
|
|
|
aFile.Close();
|
2011-02-24 16:57:11 +03:00
|
|
|
// only close if we actually opened it
|
|
|
|
if (!options.onlyOutputSpanInfo) {
|
2012-05-11 21:18:01 +04:00
|
|
|
extractFile.Close();
|
|
|
|
if (!options.onlyDirectFlag) extractFileInv.Close();
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
2013-09-08 01:04:01 +04:00
|
|
|
if (options.flexScoreFlag) {
|
|
|
|
extractFileContext.Close();
|
|
|
|
if (!options.onlyDirectFlag) extractFileContextInv.Close();
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
if (options.glueGrammarFlag)
|
2012-04-17 08:54:48 +04:00
|
|
|
writeGlueGrammar(fileNameGlueGrammar, options, targetLabelCollection, targetTopLabelCollection);
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
if (options.unknownWordLabelFlag)
|
|
|
|
writeUnknownWordLabel(fileNameUnknownWordLabel);
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void ExtractTask::Run()
|
|
|
|
{
|
2012-04-17 08:54:48 +04:00
|
|
|
extractRules();
|
|
|
|
consolidateRules();
|
|
|
|
writeRulesToFile();
|
|
|
|
m_extractedRules.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtractTask::extractRules()
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
2012-07-31 22:40:43 +04:00
|
|
|
int countT = m_sentence.target.size();
|
|
|
|
int countS = m_sentence.source.size();
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// phrase repository for creating hiero phrases
|
|
|
|
RuleExist ruleExist(countT);
|
|
|
|
|
|
|
|
// check alignments for target phrase startT...endT
|
|
|
|
for(int lengthT=1;
|
2012-04-17 08:54:48 +04:00
|
|
|
lengthT <= m_options.maxSpan && lengthT <= countT;
|
2011-02-24 16:57:11 +03:00
|
|
|
lengthT++) {
|
|
|
|
for(int startT=0; startT < countT-(lengthT-1); startT++) {
|
|
|
|
|
|
|
|
// that's nice to have
|
|
|
|
int endT = startT + lengthT - 1;
|
|
|
|
|
|
|
|
// if there is target side syntax, there has to be a node
|
2012-07-31 22:40:43 +04:00
|
|
|
if (m_options.targetSyntax && !m_sentence.targetTree.HasNode(startT,endT))
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// find find aligned source words
|
|
|
|
// first: find minimum and maximum source word
|
2014-07-28 22:49:43 +04:00
|
|
|
int minS = std::numeric_limits<int>::max();
|
2011-02-24 16:57:11 +03:00
|
|
|
int maxS = -1;
|
2012-07-31 22:40:43 +04:00
|
|
|
vector< int > usedS = m_sentence.alignedCountS;
|
2011-02-24 16:57:11 +03:00
|
|
|
for(int ti=startT; ti<=endT; ti++) {
|
2012-07-31 22:40:43 +04:00
|
|
|
for(unsigned int i=0; i<m_sentence.alignedToT[ti].size(); i++) {
|
|
|
|
int si = m_sentence.alignedToT[ti][i];
|
2011-02-24 16:57:11 +03:00
|
|
|
if (si<minS) {
|
|
|
|
minS = si;
|
|
|
|
}
|
|
|
|
if (si>maxS) {
|
|
|
|
maxS = si;
|
|
|
|
}
|
|
|
|
usedS[ si ]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unaligned phrases are not allowed
|
|
|
|
if( maxS == -1 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// source phrase has to be within limits
|
2012-04-17 08:54:48 +04:00
|
|
|
if( maxS-minS >= m_options.maxSpan )
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if source words are aligned to out of bound target words
|
|
|
|
bool out_of_bounds = false;
|
|
|
|
for(int si=minS; si<=maxS && !out_of_bounds; si++)
|
|
|
|
if (usedS[si]>0) {
|
|
|
|
out_of_bounds = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if out of bound, you gotta go
|
|
|
|
if (out_of_bounds)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// done with all the checks, lets go over all consistent phrase pairs
|
|
|
|
// start point of source phrase may retreat over unaligned
|
|
|
|
for(int startS=minS;
|
|
|
|
(startS>=0 &&
|
2012-04-17 08:54:48 +04:00
|
|
|
startS>maxS - m_options.maxSpan && // within length limit
|
2012-07-31 22:40:43 +04:00
|
|
|
(startS==minS || m_sentence.alignedCountS[startS]==0)); // unaligned
|
2011-02-24 16:57:11 +03:00
|
|
|
startS--) {
|
|
|
|
// end point of source phrase may advance over unaligned
|
|
|
|
for(int endS=maxS;
|
2012-04-17 08:54:48 +04:00
|
|
|
(endS<countS && endS<startS + m_options.maxSpan && // within length limit
|
2012-07-31 22:40:43 +04:00
|
|
|
(endS==maxS || m_sentence.alignedCountS[endS]==0)); // unaligned
|
2011-02-24 16:57:11 +03:00
|
|
|
endS++) {
|
|
|
|
// if there is source side syntax, there has to be a node
|
2012-07-31 22:40:43 +04:00
|
|
|
if (m_options.sourceSyntax && !m_sentence.sourceTree.HasNode(startS,endS))
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// TODO: loop over all source and target syntax labels
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// if within length limits, add as fully-lexical phrase pair
|
2012-04-17 08:54:48 +04:00
|
|
|
if (endT-startT < m_options.maxSymbolsTarget && endS-startS < m_options.maxSymbolsSource) {
|
2012-08-23 22:40:09 +04:00
|
|
|
addRule(startT,endT,startS,endS, countS, ruleExist);
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// take note that this is a valid phrase alignment
|
|
|
|
ruleExist.Add(startT, endT, startS, endS);
|
|
|
|
|
|
|
|
// extract hierarchical rules
|
|
|
|
|
|
|
|
// are rules not allowed to start non-terminals?
|
2012-04-17 08:54:48 +04:00
|
|
|
int initStartT = m_options.nonTermFirstWord ? startT : startT + 1;
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
HoleCollection holeColl(startS, endS); // empty hole collection
|
2012-04-17 08:54:48 +04:00
|
|
|
addHieroRule(startT, endT, startS, endS,
|
2011-02-24 16:57:11 +03:00
|
|
|
ruleExist, holeColl, 0, initStartT,
|
|
|
|
endT-startT+1, endS-startS+1);
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
void ExtractTask::preprocessSourceHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, WordIndex &indexS, HoleCollection &holeColl, const LabelIndex &labelIndex)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
vector<Hole*>::iterator iterHoleList = holeColl.GetSortedSourceHoles().begin();
|
|
|
|
assert(iterHoleList != holeColl.GetSortedSourceHoles().end());
|
|
|
|
|
|
|
|
int outPos = 0;
|
|
|
|
int holeCount = 0;
|
|
|
|
int holeTotal = holeColl.GetHoles().size();
|
|
|
|
for(int currPos = startS; currPos <= endS; currPos++) {
|
|
|
|
bool isHole = false;
|
|
|
|
if (iterHoleList != holeColl.GetSortedSourceHoles().end()) {
|
|
|
|
const Hole &hole = **iterHoleList;
|
|
|
|
isHole = hole.GetStart(0) == currPos;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
if (isHole) {
|
|
|
|
Hole &hole = **iterHoleList;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
int labelI = labelIndex[ 2+holeCount+holeTotal ];
|
2012-04-17 08:54:48 +04:00
|
|
|
string label = m_options.sourceSyntax ?
|
2012-07-31 22:40:43 +04:00
|
|
|
m_sentence.sourceTree.GetNodes(currPos,hole.GetEnd(0))[ labelI ]->GetLabel() : "X";
|
2011-02-24 16:57:11 +03:00
|
|
|
hole.SetLabel(label, 0);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
currPos = hole.GetEnd(0);
|
|
|
|
hole.SetPos(outPos, 0);
|
|
|
|
++iterHoleList;
|
|
|
|
++holeCount;
|
|
|
|
} else {
|
|
|
|
indexS[currPos] = outPos;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
outPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(iterHoleList == holeColl.GetSortedSourceHoles().end());
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:55:01 +04:00
|
|
|
string ExtractTask::saveTargetHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, WordIndex &indexT, HoleCollection &holeColl, const LabelIndex &labelIndex, double &logPCFGScore
|
|
|
|
, int countS)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
HoleList::iterator iterHoleList = holeColl.GetHoles().begin();
|
|
|
|
assert(iterHoleList != holeColl.GetHoles().end());
|
|
|
|
|
|
|
|
string out = "";
|
|
|
|
int outPos = 0;
|
|
|
|
int holeCount = 0;
|
|
|
|
for(int currPos = startT; currPos <= endT; currPos++) {
|
|
|
|
bool isHole = false;
|
|
|
|
if (iterHoleList != holeColl.GetHoles().end()) {
|
|
|
|
const Hole &hole = *iterHoleList;
|
|
|
|
isHole = hole.GetStart(1) == currPos;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
if (isHole) {
|
|
|
|
Hole &hole = *iterHoleList;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
const string &sourceLabel = hole.GetLabel(0);
|
|
|
|
assert(sourceLabel != "");
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
int labelI = labelIndex[ 2+holeCount ];
|
2012-08-23 22:40:09 +04:00
|
|
|
string targetLabel;
|
|
|
|
if (m_options.targetSyntax) {
|
|
|
|
targetLabel = m_sentence.targetTree.GetNodes(currPos,hole.GetEnd(1))[labelI]->GetLabel();
|
|
|
|
} else if (m_options.boundaryRules && (startS == 0 || endS == countS - 1)) {
|
2013-05-29 21:16:15 +04:00
|
|
|
targetLabel = "S";
|
2012-08-23 22:40:09 +04:00
|
|
|
} else {
|
|
|
|
targetLabel = "X";
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
hole.SetLabel(targetLabel, 1);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2012-05-27 15:43:16 +04:00
|
|
|
if (m_options.unpairedExtractFormat) {
|
|
|
|
out += "[" + targetLabel + "] ";
|
|
|
|
} else {
|
|
|
|
out += "[" + sourceLabel + "][" + targetLabel + "] ";
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2012-05-25 20:29:47 +04:00
|
|
|
if (m_options.pcfgScore) {
|
2012-07-31 22:40:43 +04:00
|
|
|
double score = m_sentence.targetTree.GetNodes(currPos,hole.GetEnd(1))[labelI]->GetPcfgScore();
|
2012-05-25 20:29:47 +04:00
|
|
|
logPCFGScore -= score;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
currPos = hole.GetEnd(1);
|
|
|
|
hole.SetPos(outPos, 1);
|
|
|
|
++iterHoleList;
|
|
|
|
holeCount++;
|
|
|
|
} else {
|
|
|
|
indexT[currPos] = outPos;
|
2012-07-31 22:40:43 +04:00
|
|
|
out += m_sentence.target[currPos] + " ";
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
outPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(iterHoleList == holeColl.GetHoles().end());
|
2011-04-07 15:03:23 +04:00
|
|
|
return out.erase(out.size()-1);
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:55:01 +04:00
|
|
|
string ExtractTask::saveSourceHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, HoleCollection &holeColl, const LabelIndex &labelIndex)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
vector<Hole*>::iterator iterHoleList = holeColl.GetSortedSourceHoles().begin();
|
|
|
|
assert(iterHoleList != holeColl.GetSortedSourceHoles().end());
|
|
|
|
|
|
|
|
string out = "";
|
|
|
|
int outPos = 0;
|
|
|
|
int holeCount = 0;
|
|
|
|
for(int currPos = startS; currPos <= endS; currPos++) {
|
|
|
|
bool isHole = false;
|
|
|
|
if (iterHoleList != holeColl.GetSortedSourceHoles().end()) {
|
|
|
|
const Hole &hole = **iterHoleList;
|
|
|
|
isHole = hole.GetStart(0) == currPos;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
if (isHole) {
|
|
|
|
Hole &hole = **iterHoleList;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
const string &targetLabel = hole.GetLabel(1);
|
|
|
|
assert(targetLabel != "");
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
const string &sourceLabel = hole.GetLabel(0);
|
2012-05-27 15:43:16 +04:00
|
|
|
if (m_options.unpairedExtractFormat) {
|
|
|
|
out += "[" + sourceLabel + "] ";
|
|
|
|
} else {
|
|
|
|
out += "[" + sourceLabel + "][" + targetLabel + "] ";
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
currPos = hole.GetEnd(0);
|
|
|
|
hole.SetPos(outPos, 0);
|
|
|
|
++iterHoleList;
|
|
|
|
++holeCount;
|
|
|
|
} else {
|
2012-07-31 22:40:43 +04:00
|
|
|
out += m_sentence.source[currPos] + " ";
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
outPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(iterHoleList == holeColl.GetSortedSourceHoles().end());
|
2011-04-07 15:03:23 +04:00
|
|
|
return out.erase(out.size()-1);
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:55:01 +04:00
|
|
|
void ExtractTask::saveHieroAlignment( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, const WordIndex &indexS, const WordIndex &indexT, HoleCollection &holeColl, ExtractedRule &rule)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
// print alignment of words
|
|
|
|
for(int ti=startT; ti<=endT; ti++) {
|
2011-04-07 15:03:23 +04:00
|
|
|
WordIndex::const_iterator p = indexT.find(ti);
|
|
|
|
if (p != indexT.end()) { // does word still exist?
|
2012-07-31 22:40:43 +04:00
|
|
|
for(unsigned int i=0; i<m_sentence.alignedToT[ti].size(); i++) {
|
|
|
|
int si = m_sentence.alignedToT[ti][i];
|
2011-04-07 15:03:23 +04:00
|
|
|
std::string sourceSymbolIndex = IntToString(indexS.find(si)->second);
|
|
|
|
std::string targetSymbolIndex = IntToString(p->second);
|
|
|
|
rule.alignment += sourceSymbolIndex + "-" + targetSymbolIndex + " ";
|
2012-04-17 08:54:48 +04:00
|
|
|
if (! m_options.onlyDirectFlag)
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignmentInv += targetSymbolIndex + "-" + sourceSymbolIndex + " ";
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// print alignment of non terminals
|
|
|
|
HoleList::const_iterator iterHole;
|
|
|
|
for (iterHole = holeColl.GetHoles().begin(); iterHole != holeColl.GetHoles().end(); ++iterHole) {
|
2011-09-14 11:15:36 +04:00
|
|
|
const Hole &hole = *iterHole;
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-09-14 11:15:36 +04:00
|
|
|
std::string sourceSymbolIndex = IntToString(hole.GetPos(0));
|
|
|
|
std::string targetSymbolIndex = IntToString(hole.GetPos(1));
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignment += sourceSymbolIndex + "-" + targetSymbolIndex + " ";
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.onlyDirectFlag)
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignmentInv += targetSymbolIndex + "-" + sourceSymbolIndex + " ";
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignment.erase(rule.alignment.size()-1);
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.onlyDirectFlag) {
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignmentInv.erase(rule.alignmentInv.size()-1);
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:55:01 +04:00
|
|
|
void ExtractTask::saveHieroPhrase( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, HoleCollection &holeColl, LabelIndex &labelIndex, int countS)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
WordIndex indexS, indexT; // to keep track of word positions in rule
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
ExtractedRule rule( startT, endT, startS, endS );
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// phrase labels
|
2012-08-23 22:40:09 +04:00
|
|
|
string targetLabel;
|
|
|
|
if (m_options.targetSyntax) {
|
|
|
|
targetLabel = m_sentence.targetTree.GetNodes(startT,endT)[labelIndex[0] ]->GetLabel();
|
|
|
|
} else if (m_options.boundaryRules && (startS == 0 || endS == countS - 1)) {
|
|
|
|
targetLabel = "S";
|
|
|
|
} else {
|
|
|
|
targetLabel = "X";
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
string sourceLabel = m_options.sourceSyntax ?
|
2012-07-31 22:40:43 +04:00
|
|
|
m_sentence.sourceTree.GetNodes(startS,endS)[ labelIndex[1] ]->GetLabel() : "X";
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// create non-terms on the source side
|
2012-04-17 08:54:48 +04:00
|
|
|
preprocessSourceHieroPhrase(startT, endT, startS, endS, indexS, holeColl, labelIndex);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// target
|
2012-05-25 20:29:47 +04:00
|
|
|
if (m_options.pcfgScore) {
|
2012-07-31 22:40:43 +04:00
|
|
|
double logPCFGScore = m_sentence.targetTree.GetNodes(startT,endT)[labelIndex[0]]->GetPcfgScore();
|
2012-11-10 03:55:01 +04:00
|
|
|
rule.target = saveTargetHieroPhrase(startT, endT, startS, endS, indexT, holeColl, labelIndex, logPCFGScore, countS)
|
2013-05-29 21:16:15 +04:00
|
|
|
+ " [" + targetLabel + "]";
|
2012-05-25 20:29:47 +04:00
|
|
|
rule.pcfgScore = std::exp(logPCFGScore);
|
|
|
|
} else {
|
|
|
|
double logPCFGScore = 0.0f;
|
2012-11-10 03:55:01 +04:00
|
|
|
rule.target = saveTargetHieroPhrase(startT, endT, startS, endS, indexT, holeColl, labelIndex, logPCFGScore, countS)
|
2013-05-29 21:16:15 +04:00
|
|
|
+ " [" + targetLabel + "]";
|
2012-05-25 20:29:47 +04:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// source
|
2012-11-10 03:55:01 +04:00
|
|
|
rule.source = saveSourceHieroPhrase(startT, endT, startS, endS, holeColl, labelIndex);
|
2012-05-27 15:43:16 +04:00
|
|
|
if (m_options.conditionOnTargetLhs) {
|
|
|
|
rule.source += " [" + targetLabel + "]";
|
|
|
|
} else {
|
|
|
|
rule.source += " [" + sourceLabel + "]";
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// alignment
|
2012-11-10 03:55:01 +04:00
|
|
|
saveHieroAlignment(startT, endT, startS, endS, indexS, indexT, holeColl, rule);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2013-09-08 01:04:01 +04:00
|
|
|
// context (words to left and right)
|
|
|
|
if (m_options.flexScoreFlag) {
|
2013-09-27 12:35:24 +04:00
|
|
|
rule.sourceContextLeft = startS == 0 ? "<s>" : m_sentence.source[startS-1];
|
|
|
|
rule.sourceContextRight = endS+1 == m_sentence.source.size() ? "<s>" : m_sentence.source[endS+1];
|
|
|
|
rule.targetContextLeft = startT == 0 ? "<s>" : m_sentence.target[startT-1];
|
|
|
|
rule.targetContextRight = endT+1 == m_sentence.target.size() ? "<s>" : m_sentence.target[endT+1];
|
|
|
|
rule.sourceHoleString = "";
|
|
|
|
rule.targetHoleString = "";
|
|
|
|
|
|
|
|
HoleList::const_iterator iterHole;
|
|
|
|
for (iterHole = holeColl.GetHoles().begin(); iterHole != holeColl.GetHoles().end(); ++iterHole) {
|
|
|
|
const Hole &hole = *iterHole;
|
|
|
|
rule.sourceHoleString += hole.GetLabel(0) + ": ";
|
|
|
|
|
|
|
|
// rule starts with nonterminal; end of NT is considered left context
|
|
|
|
if (hole.GetStart(0) == startS) {
|
|
|
|
rule.sourceContextLeft = m_sentence.source[hole.GetEnd(0)];
|
|
|
|
}
|
|
|
|
// rule ends with nonterminal; start of NT is considered right context
|
|
|
|
else if (hole.GetEnd(0) == endS) {
|
|
|
|
rule.sourceContextRight = m_sentence.source[hole.GetStart(0)];
|
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
|
2013-09-27 12:35:24 +04:00
|
|
|
if (hole.GetStart(1) == startT) {
|
|
|
|
rule.targetContextLeft = m_sentence.target[hole.GetEnd(1)];
|
|
|
|
} else if (hole.GetEnd(1) == endT) {
|
|
|
|
rule.targetContextRight = m_sentence.target[hole.GetStart(1)];
|
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
|
2013-09-27 12:35:24 +04:00
|
|
|
for (int i = hole.GetStart(0); i <= hole.GetEnd(0); ++i) {
|
|
|
|
rule.sourceHoleString += m_sentence.source[i] + " ";
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
2013-09-27 12:35:24 +04:00
|
|
|
rule.targetHoleString += hole.GetLabel(1) + ": ";
|
|
|
|
for (int i = hole.GetStart(1); i <= hole.GetEnd(1); ++i) {
|
|
|
|
rule.targetHoleString += m_sentence.target[i] + " ";
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
2013-09-27 12:35:24 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
addRuleToCollection( rule );
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:55:01 +04:00
|
|
|
void ExtractTask::saveAllHieroPhrases( int startT, int endT, int startS, int endS, HoleCollection &holeColl, int countS)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
LabelIndex labelIndex,labelCount;
|
|
|
|
|
|
|
|
// number of target head labels
|
2012-07-31 22:40:43 +04:00
|
|
|
int numLabels = m_options.targetSyntax ? m_sentence.targetTree.GetNodes(startT,endT).size() : 1;
|
2011-02-24 16:57:11 +03:00
|
|
|
labelCount.push_back(numLabels);
|
|
|
|
labelIndex.push_back(0);
|
|
|
|
|
|
|
|
// number of source head labels
|
2012-07-31 22:40:43 +04:00
|
|
|
numLabels = m_options.sourceSyntax ? m_sentence.sourceTree.GetNodes(startS,endS).size() : 1;
|
2011-02-24 16:57:11 +03:00
|
|
|
labelCount.push_back(numLabels);
|
|
|
|
labelIndex.push_back(0);
|
|
|
|
|
|
|
|
// number of target hole labels
|
|
|
|
for( HoleList::const_iterator hole = holeColl.GetHoles().begin();
|
|
|
|
hole != holeColl.GetHoles().end(); hole++ ) {
|
2012-07-31 22:40:43 +04:00
|
|
|
int numLabels = m_options.targetSyntax ? m_sentence.targetTree.GetNodes(hole->GetStart(1),hole->GetEnd(1)).size() : 1 ;
|
2010-04-13 19:34:39 +04:00
|
|
|
labelCount.push_back(numLabels);
|
|
|
|
labelIndex.push_back(0);
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// number of source hole labels
|
|
|
|
holeColl.SortSourceHoles();
|
|
|
|
for( vector<Hole*>::iterator i = holeColl.GetSortedSourceHoles().begin();
|
|
|
|
i != holeColl.GetSortedSourceHoles().end(); i++ ) {
|
|
|
|
const Hole &hole = **i;
|
2012-07-31 22:40:43 +04:00
|
|
|
int numLabels = m_options.sourceSyntax ? m_sentence.sourceTree.GetNodes(hole.GetStart(0),hole.GetEnd(0)).size() : 1 ;
|
2010-04-13 19:34:39 +04:00
|
|
|
labelCount.push_back(numLabels);
|
|
|
|
labelIndex.push_back(0);
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the holes
|
|
|
|
bool done = false;
|
|
|
|
while(!done) {
|
2012-11-10 03:55:01 +04:00
|
|
|
saveHieroPhrase( startT, endT, startS, endS, holeColl, labelIndex, countS );
|
2012-04-17 08:54:48 +04:00
|
|
|
for(unsigned int i=0; i<labelIndex.size(); i++) {
|
2011-02-24 16:57:11 +03:00
|
|
|
labelIndex[i]++;
|
|
|
|
if(labelIndex[i] == labelCount[i]) {
|
|
|
|
labelIndex[i] = 0;
|
|
|
|
if (i == labelIndex.size()-1)
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// this function is called recursively
|
|
|
|
// it pokes a new hole into the phrase pair, and then calls itself for more holes
|
2012-04-17 08:54:48 +04:00
|
|
|
void ExtractTask::addHieroRule( int startT, int endT, int startS, int endS
|
2013-05-29 21:16:15 +04:00
|
|
|
, RuleExist &ruleExist, HoleCollection &holeColl
|
|
|
|
, int numHoles, int initStartT, int wordCountT, int wordCountS)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
// done, if already the maximum number of non-terminals in phrase pair
|
2012-04-17 08:54:48 +04:00
|
|
|
if (numHoles >= m_options.maxNonTerm)
|
2011-02-24 16:57:11 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
// find a hole...
|
|
|
|
for (int startHoleT = initStartT; startHoleT <= endT; ++startHoleT) {
|
2012-04-17 08:54:48 +04:00
|
|
|
for (int endHoleT = startHoleT+(m_options.minHoleTarget-1); endHoleT <= endT; ++endHoleT) {
|
2011-02-24 16:57:11 +03:00
|
|
|
// if last non-terminal, enforce word count limit
|
2012-04-17 08:54:48 +04:00
|
|
|
if (numHoles == m_options.maxNonTerm-1 && wordCountT - (endHoleT-startT+1) + (numHoles+1) > m_options.maxSymbolsTarget)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// determine the number of remaining target words
|
|
|
|
const int newWordCountT = wordCountT - (endHoleT-startHoleT+1);
|
|
|
|
|
|
|
|
// always enforce min word count limit
|
2012-04-17 08:54:48 +04:00
|
|
|
if (newWordCountT < m_options.minWords)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// except the whole span
|
|
|
|
if (startHoleT == startT && endHoleT == endT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// does a phrase cover this target span?
|
|
|
|
// if it does, then there should be a list of mapped source phrases
|
|
|
|
// (multiple possible due to unaligned words)
|
|
|
|
const HoleList &sourceHoles = ruleExist.GetSourceHoles(startHoleT, endHoleT);
|
|
|
|
|
|
|
|
// loop over sub phrase pairs
|
|
|
|
HoleList::const_iterator iterSourceHoles;
|
|
|
|
for (iterSourceHoles = sourceHoles.begin(); iterSourceHoles != sourceHoles.end(); ++iterSourceHoles) {
|
|
|
|
const Hole &sourceHole = *iterSourceHoles;
|
|
|
|
|
|
|
|
const int sourceHoleSize = sourceHole.GetEnd(0)-sourceHole.GetStart(0)+1;
|
|
|
|
|
|
|
|
// enforce minimum hole size
|
2012-04-17 08:54:48 +04:00
|
|
|
if (sourceHoleSize < m_options.minHoleSource)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// determine the number of remaining source words
|
|
|
|
const int newWordCountS = wordCountS - sourceHoleSize;
|
|
|
|
|
|
|
|
// if last non-terminal, enforce word count limit
|
2012-04-17 08:54:48 +04:00
|
|
|
if (numHoles == m_options.maxNonTerm-1 && newWordCountS + (numHoles+1) > m_options.maxSymbolsSource)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// enforce min word count limit
|
2012-04-17 08:54:48 +04:00
|
|
|
if (newWordCountS < m_options.minWords)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// hole must be subphrase of the source phrase
|
|
|
|
// (may be violated if subphrase contains additional unaligned source word)
|
|
|
|
if (startS > sourceHole.GetStart(0) || endS < sourceHole.GetEnd(0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// make sure target side does not overlap with another hole
|
|
|
|
if (holeColl.OverlapSource(sourceHole))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// if consecutive non-terminals are not allowed, also check for source
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.nonTermConsecSource && holeColl.ConsecSource(sourceHole) )
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// check that rule scope would not exceed limit if sourceHole
|
|
|
|
// were added
|
2012-04-17 08:54:48 +04:00
|
|
|
if (holeColl.Scope(sourceHole) > m_options.maxScope)
|
2011-02-24 16:57:11 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// require that at least one aligned word is left (unless there are no words at all)
|
2012-04-17 08:54:48 +04:00
|
|
|
if (m_options.requireAlignedWord && (newWordCountS > 0 || newWordCountT > 0)) {
|
2011-02-24 16:57:11 +03:00
|
|
|
HoleList::const_iterator iterHoleList = holeColl.GetHoles().begin();
|
|
|
|
bool foundAlignedWord = false;
|
|
|
|
// loop through all word positions
|
|
|
|
for(int pos = startT; pos <= endT && !foundAlignedWord; pos++) {
|
|
|
|
// new hole? moving on...
|
|
|
|
if (pos == startHoleT) {
|
|
|
|
pos = endHoleT;
|
|
|
|
}
|
|
|
|
// covered by hole? moving on...
|
|
|
|
else if (iterHoleList != holeColl.GetHoles().end() && iterHoleList->GetStart(1) == pos) {
|
|
|
|
pos = iterHoleList->GetEnd(1);
|
|
|
|
++iterHoleList;
|
|
|
|
}
|
|
|
|
// covered by word? check if it is aligned
|
|
|
|
else {
|
2012-07-31 22:40:43 +04:00
|
|
|
if (m_sentence.alignedToT[pos].size() > 0)
|
2011-02-24 16:57:11 +03:00
|
|
|
foundAlignedWord = true;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
if (!foundAlignedWord)
|
|
|
|
continue;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// update list of holes in this phrase pair
|
2013-05-03 12:47:06 +04:00
|
|
|
holeColl.Add(startHoleT, endHoleT, sourceHole.GetStart(0), sourceHole.GetEnd(0));
|
2011-02-24 16:57:11 +03:00
|
|
|
// now some checks that disallow this phrase pair, but not further recursion
|
|
|
|
bool allowablePhrase = true;
|
|
|
|
|
|
|
|
// maximum words count violation?
|
2012-04-17 08:54:48 +04:00
|
|
|
if (newWordCountS + (numHoles+1) > m_options.maxSymbolsSource)
|
2011-02-24 16:57:11 +03:00
|
|
|
allowablePhrase = false;
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
if (newWordCountT + (numHoles+1) > m_options.maxSymbolsTarget)
|
2011-02-24 16:57:11 +03:00
|
|
|
allowablePhrase = false;
|
|
|
|
|
|
|
|
// passed all checks...
|
2013-05-29 21:16:15 +04:00
|
|
|
if (allowablePhrase)
|
2013-05-03 12:47:06 +04:00
|
|
|
saveAllHieroPhrases(startT, endT, startS, endS, holeColl, wordCountS);
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// recursively search for next hole
|
2012-04-17 08:54:48 +04:00
|
|
|
int nextInitStartT = m_options.nonTermConsecTarget ? endHoleT + 1 : endHoleT + 2;
|
|
|
|
addHieroRule(startT, endT, startS, endS
|
2013-05-03 12:47:06 +04:00
|
|
|
, ruleExist, holeColl, numHoles + 1, nextInitStartT
|
2011-02-24 16:57:11 +03:00
|
|
|
, newWordCountT, newWordCountS);
|
2013-05-03 12:47:06 +04:00
|
|
|
|
|
|
|
holeColl.RemoveLast();
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 22:40:09 +04:00
|
|
|
void ExtractTask::addRule( int startT, int endT, int startS, int endS, int countS, RuleExist &ruleExist)
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2012-08-23 22:40:09 +04:00
|
|
|
// contains only <s> or </s>. Don't output
|
2013-05-29 21:16:15 +04:00
|
|
|
if (m_options.boundaryRules
|
|
|
|
&& ( (startS == 0 && endS == 0)
|
|
|
|
|| (startS == countS-1 && endS == countS-1))) {
|
2012-08-23 22:40:09 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
if (m_options.onlyOutputSpanInfo) {
|
2011-02-24 16:57:11 +03:00
|
|
|
cout << startS << " " << endS << " " << startT << " " << endT << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtractedRule rule(startT, endT, startS, endS);
|
|
|
|
|
|
|
|
// phrase labels
|
|
|
|
string targetLabel,sourceLabel;
|
2012-05-27 15:43:16 +04:00
|
|
|
if (m_options.targetSyntax && m_options.conditionOnTargetLhs) {
|
2012-07-31 22:40:43 +04:00
|
|
|
sourceLabel = targetLabel = m_sentence.targetTree.GetNodes(startT,endT)[0]->GetLabel();
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2012-05-27 15:43:16 +04:00
|
|
|
sourceLabel = m_options.sourceSyntax ?
|
2012-07-31 22:40:43 +04:00
|
|
|
m_sentence.sourceTree.GetNodes(startS,endS)[0]->GetLabel() : "X";
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-08-23 22:40:09 +04:00
|
|
|
if (m_options.targetSyntax) {
|
|
|
|
targetLabel = m_sentence.targetTree.GetNodes(startT,endT)[0]->GetLabel();
|
|
|
|
} else if (m_options.boundaryRules && (startS == 0 || endS == countS - 1)) {
|
|
|
|
targetLabel = "S";
|
|
|
|
} else {
|
|
|
|
targetLabel = "X";
|
|
|
|
}
|
2012-05-27 15:43:16 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// source
|
|
|
|
rule.source = "";
|
|
|
|
for(int si=startS; si<=endS; si++)
|
2012-07-31 22:40:43 +04:00
|
|
|
rule.source += m_sentence.source[si] + " ";
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.source += "[" + sourceLabel + "]";
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// target
|
|
|
|
rule.target = "";
|
|
|
|
for(int ti=startT; ti<=endT; ti++)
|
2012-07-31 22:40:43 +04:00
|
|
|
rule.target += m_sentence.target[ti] + " ";
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.target += "[" + targetLabel + "]";
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2012-05-25 20:29:47 +04:00
|
|
|
if (m_options.pcfgScore) {
|
2012-07-31 22:40:43 +04:00
|
|
|
double logPCFGScore = m_sentence.targetTree.GetNodes(startT,endT)[0]->GetPcfgScore();
|
2012-05-25 20:29:47 +04:00
|
|
|
rule.pcfgScore = std::exp(logPCFGScore);
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// alignment
|
|
|
|
for(int ti=startT; ti<=endT; ti++) {
|
2012-07-31 22:40:43 +04:00
|
|
|
for(unsigned int i=0; i<m_sentence.alignedToT[ti].size(); i++) {
|
|
|
|
int si = m_sentence.alignedToT[ti][i];
|
2011-04-07 15:03:23 +04:00
|
|
|
std::string sourceSymbolIndex = IntToString(si-startS);
|
|
|
|
std::string targetSymbolIndex = IntToString(ti-startT);
|
|
|
|
rule.alignment += sourceSymbolIndex + "-" + targetSymbolIndex + " ";
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.onlyDirectFlag)
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignmentInv += targetSymbolIndex + "-" + sourceSymbolIndex + " ";
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2011-04-07 15:03:23 +04:00
|
|
|
|
2013-09-08 01:04:01 +04:00
|
|
|
// context (words to left and right)
|
|
|
|
if (m_options.flexScoreFlag) {
|
2013-09-27 12:35:24 +04:00
|
|
|
rule.sourceContextLeft = startS == 0 ? "<s>" : m_sentence.source[startS-1];
|
|
|
|
rule.sourceContextRight = endS+1 == m_sentence.source.size() ? "<s>" : m_sentence.source[endS+1];
|
|
|
|
rule.targetContextLeft = startT == 0 ? "<s>" : m_sentence.target[startT-1];
|
|
|
|
rule.targetContextRight = endT+1 == m_sentence.target.size() ? "<s>" : m_sentence.target[endT+1];
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
|
|
|
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignment.erase(rule.alignment.size()-1);
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.onlyDirectFlag)
|
2011-04-07 15:03:23 +04:00
|
|
|
rule.alignmentInv.erase(rule.alignmentInv.size()-1);
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
addRuleToCollection( rule );
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
void ExtractTask::addRuleToCollection( ExtractedRule &newRule )
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
// no double-counting of identical rules from overlapping spans
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.duplicateRules) {
|
2011-02-24 16:57:11 +03:00
|
|
|
vector<ExtractedRule>::const_iterator rule;
|
2012-04-17 08:54:48 +04:00
|
|
|
for(rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
2011-02-24 16:57:11 +03:00
|
|
|
if (rule->source.compare( newRule.source ) == 0 &&
|
|
|
|
rule->target.compare( newRule.target ) == 0 &&
|
|
|
|
!(rule->endT < newRule.startT || rule->startT > newRule.endT)) { // overlapping
|
|
|
|
return;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2012-04-17 08:54:48 +04:00
|
|
|
m_extractedRules.push_back( newRule );
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
void ExtractTask::consolidateRules()
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
typedef vector<ExtractedRule>::iterator R;
|
|
|
|
map<int, map<int, map<int, map<int,int> > > > spanCount;
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// compute number of rules per span
|
2012-04-17 08:54:48 +04:00
|
|
|
if (m_options.fractionalCounting) {
|
|
|
|
for(R rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
2011-02-24 16:57:11 +03:00
|
|
|
spanCount[ rule->startT ][ rule->endT ][ rule->startS ][ rule->endS ]++;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// compute fractional counts
|
2012-04-17 08:54:48 +04:00
|
|
|
for(R rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
|
|
|
rule->count = 1.0/(float) (m_options.fractionalCounting ? spanCount[ rule->startT ][ rule->endT ][ rule->startS ][ rule->endS ] : 1.0 );
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// consolidate counts
|
2013-02-06 16:10:38 +04:00
|
|
|
map<std::string, map< std::string, map< std::string, float> > > consolidatedCount;
|
2012-04-17 08:54:48 +04:00
|
|
|
for(R rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
2013-02-06 16:10:38 +04:00
|
|
|
consolidatedCount[ rule->source ][ rule->target][ rule->alignment ] += rule->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(R rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
|
|
|
float count = consolidatedCount[ rule->source ][ rule->target][ rule->alignment ];
|
|
|
|
rule->count = count;
|
|
|
|
consolidatedCount[ rule->source ][ rule->target][ rule->alignment ] = 0;
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
void ExtractTask::writeRulesToFile()
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
|
|
|
vector<ExtractedRule>::const_iterator rule;
|
2012-04-17 08:54:48 +04:00
|
|
|
ostringstream out;
|
|
|
|
ostringstream outInv;
|
2013-09-08 01:04:01 +04:00
|
|
|
ostringstream outContext;
|
|
|
|
ostringstream outContextInv;
|
2012-04-17 08:54:48 +04:00
|
|
|
for(rule = m_extractedRules.begin(); rule != m_extractedRules.end(); rule++ ) {
|
2011-02-24 16:57:11 +03:00
|
|
|
if (rule->count == 0)
|
|
|
|
continue;
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
out << rule->source << " ||| "
|
|
|
|
<< rule->target << " ||| "
|
|
|
|
<< rule->alignment << " ||| "
|
2012-05-25 20:29:47 +04:00
|
|
|
<< rule->count << " ||| ";
|
|
|
|
if (m_options.pcfgScore) {
|
|
|
|
out << " ||| " << rule->pcfgScore;
|
|
|
|
}
|
2012-04-17 08:54:48 +04:00
|
|
|
out << "\n";
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
if (!m_options.onlyDirectFlag) {
|
|
|
|
outInv << rule->target << " ||| "
|
|
|
|
<< rule->source << " ||| "
|
|
|
|
<< rule->alignmentInv << " ||| "
|
|
|
|
<< rule->count << "\n";
|
2011-09-14 11:15:36 +04:00
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
|
|
|
|
if (m_options.flexScoreFlag) {
|
2013-09-27 12:35:24 +04:00
|
|
|
for(int iContext=0; iContext<2; iContext++) {
|
|
|
|
outContext << rule->source << " ||| "
|
|
|
|
<< rule->target << " ||| "
|
|
|
|
<< rule->alignment << " ||| ";
|
|
|
|
iContext ? outContext << "< " << rule->sourceContextLeft << "\n" : outContext << "> " << rule->sourceContextRight << "\n";
|
|
|
|
|
|
|
|
if (!m_options.onlyDirectFlag) {
|
|
|
|
outContextInv << rule->target << " ||| "
|
|
|
|
<< rule->source << " ||| "
|
|
|
|
<< rule->alignmentInv << " ||| ";
|
|
|
|
iContext ? outContextInv << "< " << rule->targetContextLeft << "\n" : outContextInv << "> " << rule->targetContextRight << "\n";
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
2013-09-27 12:35:24 +04:00
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
|
2013-09-27 12:35:24 +04:00
|
|
|
if (rule->sourceHoleString != "") {
|
|
|
|
outContext << rule->source << " ||| "
|
|
|
|
<< rule->target << " ||| "
|
|
|
|
<< rule->alignment << " ||| v "
|
|
|
|
<< rule->sourceHoleString << "\n";
|
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
|
2013-09-27 12:35:24 +04:00
|
|
|
if (!m_options.onlyDirectFlag and rule->targetHoleString != "") {
|
|
|
|
outContextInv << rule->target << " ||| "
|
|
|
|
<< rule->source << " ||| "
|
|
|
|
<< rule->alignmentInv << " ||| v "
|
|
|
|
<< rule->targetHoleString << "\n";
|
|
|
|
}
|
2013-09-08 01:04:01 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2012-08-01 00:49:38 +04:00
|
|
|
m_extractFile << out.str();
|
|
|
|
m_extractFileInv << outInv.str();
|
2013-09-08 01:04:01 +04:00
|
|
|
m_extractFileContext << outContext.str();
|
|
|
|
m_extractFileContextInv << outContextInv.str();
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:54:48 +04:00
|
|
|
void writeGlueGrammar( const string & fileName, RuleExtractionOptions &options, set< string > &targetLabelCollection, map< string, int > &targetTopLabelCollection )
|
2010-04-13 19:34:39 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
ofstream grammarFile;
|
|
|
|
grammarFile.open(fileName.c_str());
|
|
|
|
if (!options.targetSyntax) {
|
2014-11-04 17:05:13 +03:00
|
|
|
grammarFile << "<s> [X] ||| <s> [S] ||| 1 ||| 0-0 ||| 0" << endl
|
|
|
|
<< "[X][S] </s> [X] ||| [X][S] </s> [S] ||| 1 ||| 0-0 1-1 ||| 0" << endl
|
2011-02-24 16:57:11 +03:00
|
|
|
<< "[X][S] [X][X] [X] ||| [X][S] [X][X] [S] ||| 2.718 ||| 0-0 1-1 ||| 0" << endl;
|
|
|
|
} else {
|
|
|
|
// chose a top label that is not already a label
|
|
|
|
string topLabel = "QQQQQQ";
|
2012-04-17 08:54:48 +04:00
|
|
|
for( unsigned int i=1; i<=topLabel.length(); i++) {
|
2011-02-24 16:57:11 +03:00
|
|
|
if(targetLabelCollection.find( topLabel.substr(0,i) ) == targetLabelCollection.end() ) {
|
|
|
|
topLabel = topLabel.substr(0,i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// basic rules
|
2014-11-04 17:05:13 +03:00
|
|
|
grammarFile << "<s> [X] ||| <s> [" << topLabel << "] ||| 1 ||| 0-0" << endl
|
|
|
|
<< "[X][" << topLabel << "] </s> [X] ||| [X][" << topLabel << "] </s> [" << topLabel << "] ||| 1 ||| 0-0 1-1" << endl;
|
2011-02-24 16:57:11 +03:00
|
|
|
|
|
|
|
// top rules
|
|
|
|
for( map<string,int>::const_iterator i = targetTopLabelCollection.begin();
|
|
|
|
i != targetTopLabelCollection.end(); i++ ) {
|
2014-11-04 17:05:13 +03:00
|
|
|
grammarFile << "<s> [X][" << i->first << "] </s> [X] ||| <s> [X][" << i->first << "] </s> [" << topLabel << "] ||| 1 ||| 0-0 1-1 2-2" << endl;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
// glue rules
|
|
|
|
for( set<string>::const_iterator i = targetLabelCollection.begin();
|
|
|
|
i != targetLabelCollection.end(); i++ ) {
|
|
|
|
grammarFile << "[X][" << topLabel << "] [X][" << *i << "] [X] ||| [X][" << topLabel << "] [X][" << *i << "] [" << topLabel << "] ||| 2.718 ||| 0-0 1-1" << endl;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
grammarFile << "[X][" << topLabel << "] [X][X] [X] ||| [X][" << topLabel << "] [X][X] [" << topLabel << "] ||| 2.718 ||| 0-0 1-1 " << endl; // glue rule for unknown word...
|
|
|
|
}
|
|
|
|
grammarFile.close();
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect counts for labels for each word
|
|
|
|
// ( labels of singleton words are used to estimate
|
|
|
|
// distribution oflabels for unknown words )
|
|
|
|
|
|
|
|
map<string,int> wordCount;
|
|
|
|
map<string,string> wordLabel;
|
|
|
|
void collectWordLabelCounts( SentenceAlignmentWithSyntax &sentence )
|
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
int countT = sentence.target.size();
|
|
|
|
for(int ti=0; ti < countT; ti++) {
|
|
|
|
string &word = sentence.target[ ti ];
|
|
|
|
const vector< SyntaxNode* >& labels = sentence.targetTree.GetNodes(ti,ti);
|
|
|
|
if (labels.size() > 0) {
|
|
|
|
wordCount[ word ]++;
|
|
|
|
wordLabel[ word ] = labels[0]->GetLabel();
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeUnknownWordLabel(const string & fileName)
|
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
ofstream outFile;
|
|
|
|
outFile.open(fileName.c_str());
|
|
|
|
typedef map<string,int>::const_iterator I;
|
|
|
|
|
|
|
|
map<string,int> count;
|
|
|
|
int total = 0;
|
|
|
|
for(I word = wordCount.begin(); word != wordCount.end(); word++) {
|
|
|
|
// only consider singletons
|
|
|
|
if (word->second == 1) {
|
|
|
|
count[ wordLabel[ word->first ] ]++;
|
|
|
|
total++;
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
for(I pos = count.begin(); pos != count.end(); pos++) {
|
|
|
|
double ratio = ((double) pos->second / (double) total);
|
|
|
|
if (ratio > 0.03)
|
|
|
|
outFile << pos->first << " " << ratio << endl;
|
|
|
|
}
|
2010-04-13 19:34:39 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
outFile.close();
|
2010-04-13 19:34:39 +04:00
|
|
|
}
|