2011-09-27 04:34:46 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - statistical machine translation system
|
|
|
|
Copyright (C) 2006-2011 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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2012-01-23 19:54:38 +04:00
|
|
|
#include "RuleTable/LoaderStandard.h"
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <sys/stat.h>
|
2012-01-24 00:41:49 +04:00
|
|
|
#include "RuleTable/Trie.h"
|
2011-09-27 04:34:46 +04:00
|
|
|
#include "FactorCollection.h"
|
|
|
|
#include "Word.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "InputFileStream.h"
|
|
|
|
#include "StaticData.h"
|
|
|
|
#include "WordsRange.h"
|
|
|
|
#include "UserMessage.h"
|
|
|
|
#include "ChartTranslationOptionList.h"
|
|
|
|
#include "FactorCollection.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
bool RuleTableLoaderStandard::Load(const std::vector<FactorType> &input
|
2011-11-04 21:20:12 +04:00
|
|
|
, const std::vector<FactorType> &output
|
|
|
|
, std::istream &inStream
|
|
|
|
, const std::vector<float> &weight
|
|
|
|
, size_t tableLimit
|
|
|
|
, const LMList &languageModels
|
|
|
|
, const WordPenaltyProducer* wpProducer
|
2012-01-24 00:41:49 +04:00
|
|
|
, RuleTableTrie &ruleTable)
|
2011-11-04 21:20:12 +04:00
|
|
|
{
|
|
|
|
bool ret = Load(MosesFormat
|
|
|
|
,input, output
|
|
|
|
,inStream, weight
|
|
|
|
,tableLimit, languageModels
|
|
|
|
,wpProducer, ruleTable);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
void ReformatHieroRule(int sourceTarget, string &phrase, map<size_t, pair<size_t, size_t> > &ntAlign)
|
|
|
|
{
|
|
|
|
vector<string> toks;
|
|
|
|
Tokenize(toks, phrase, " ");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < toks.size(); ++i)
|
|
|
|
{
|
|
|
|
string &tok = toks[i];
|
|
|
|
size_t tokLen = tok.size();
|
|
|
|
if (tok.substr(0, 1) == "[" && tok.substr(tokLen - 1, 1) == "]")
|
|
|
|
{ // no-term
|
|
|
|
vector<string> split = Tokenize(tok, ",");
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(split.size() == 2);
|
2011-11-06 17:46:46 +04:00
|
|
|
|
|
|
|
tok = "[X]" + split[0] + "]";
|
|
|
|
size_t coIndex = Scan<size_t>(split[1]);
|
|
|
|
|
|
|
|
pair<size_t, size_t> &alignPoint = ntAlign[coIndex];
|
|
|
|
if (sourceTarget == 0)
|
|
|
|
{
|
|
|
|
alignPoint.first = i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alignPoint.second = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
phrase = Join(" ", toks) + " [X]";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReformateHieroScore(string &scoreString)
|
|
|
|
{
|
|
|
|
vector<string> toks;
|
|
|
|
Tokenize(toks, scoreString, " ");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < toks.size(); ++i)
|
|
|
|
{
|
|
|
|
string &tok = toks[i];
|
|
|
|
|
|
|
|
float score = Scan<float>(tok);
|
|
|
|
score = exp(-score);
|
|
|
|
tok = SPrint(score);
|
|
|
|
}
|
|
|
|
|
|
|
|
scoreString = Join(" ", toks);
|
|
|
|
}
|
|
|
|
|
|
|
|
string *ReformatHieroRule(const string &lineOrig)
|
|
|
|
{
|
|
|
|
vector<string> tokens;
|
|
|
|
vector<float> scoreVector;
|
|
|
|
|
|
|
|
TokenizeMultiCharSeparator(tokens, lineOrig, "|||" );
|
|
|
|
|
|
|
|
string &sourcePhraseString = tokens[1]
|
|
|
|
, &targetPhraseString = tokens[2]
|
|
|
|
, &scoreString = tokens[3];
|
|
|
|
|
|
|
|
map<size_t, pair<size_t, size_t> > ntAlign;
|
|
|
|
ReformatHieroRule(0, sourcePhraseString, ntAlign);
|
|
|
|
ReformatHieroRule(1, targetPhraseString, ntAlign);
|
|
|
|
ReformateHieroScore(scoreString);
|
|
|
|
|
|
|
|
stringstream align;
|
|
|
|
map<size_t, pair<size_t, size_t> >::const_iterator iterAlign;
|
|
|
|
for (iterAlign = ntAlign.begin(); iterAlign != ntAlign.end(); ++iterAlign)
|
|
|
|
{
|
|
|
|
const pair<size_t, size_t> &alignPoint = iterAlign->second;
|
|
|
|
align << alignPoint.first << "-" << alignPoint.second << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
stringstream ret;
|
|
|
|
ret << sourcePhraseString << " ||| "
|
|
|
|
<< targetPhraseString << " ||| "
|
|
|
|
<< scoreString << " ||| "
|
|
|
|
<< align.str();
|
|
|
|
|
|
|
|
return new string(ret.str());
|
|
|
|
}
|
|
|
|
|
2011-11-04 21:20:12 +04:00
|
|
|
bool RuleTableLoaderStandard::Load(FormatType format
|
|
|
|
, const std::vector<FactorType> &input
|
2011-09-27 04:34:46 +04:00
|
|
|
, const std::vector<FactorType> &output
|
|
|
|
, std::istream &inStream
|
|
|
|
, const std::vector<float> &weight
|
|
|
|
, size_t /* tableLimit */
|
|
|
|
, const LMList &languageModels
|
|
|
|
, const WordPenaltyProducer* wpProducer
|
2012-01-24 00:41:49 +04:00
|
|
|
, RuleTableTrie &ruleTable)
|
2011-09-27 04:34:46 +04:00
|
|
|
{
|
|
|
|
PrintUserTime("Start loading new format pt model");
|
|
|
|
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
const std::string& factorDelimiter = staticData.GetFactorDelimiter();
|
|
|
|
|
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
string lineOrig;
|
2011-09-27 04:34:46 +04:00
|
|
|
size_t count = 0;
|
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
while(getline(inStream, lineOrig)) {
|
|
|
|
const string *line;
|
|
|
|
if (format == HieroFormat) { // reformat line
|
|
|
|
line = ReformatHieroRule(lineOrig);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // do nothing to format of line
|
|
|
|
line = &lineOrig;
|
|
|
|
}
|
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
vector<string> tokens;
|
|
|
|
vector<float> scoreVector;
|
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
TokenizeMultiCharSeparator(tokens, *line , "|||" );
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
if (tokens.size() != 4 && tokens.size() != 5) {
|
|
|
|
stringstream strme;
|
|
|
|
strme << "Syntax error at " << ruleTable.GetFilePath() << ":" << count;
|
|
|
|
UserMessage::Add(strme.str());
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
const string &sourcePhraseString = tokens[0]
|
|
|
|
, &targetPhraseString = tokens[1]
|
|
|
|
, &scoreString = tokens[2]
|
|
|
|
, &alignString = tokens[3];
|
|
|
|
|
|
|
|
bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == string::npos);
|
|
|
|
if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
|
|
|
|
TRACE_ERR( ruleTable.GetFilePath() << ":" << count << ": pt entry contains empty target, skipping\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tokenize<float>(scoreVector, scoreString);
|
|
|
|
const size_t numScoreComponents = ruleTable.GetFeature()->GetNumScoreComponents();
|
|
|
|
if (scoreVector.size() != numScoreComponents) {
|
|
|
|
stringstream strme;
|
|
|
|
strme << "Size of scoreVector != number (" << scoreVector.size() << "!="
|
|
|
|
<< numScoreComponents << ") of score components on line " << count;
|
|
|
|
UserMessage::Add(strme.str());
|
|
|
|
abort();
|
|
|
|
}
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(scoreVector.size() == numScoreComponents);
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
// parse source & find pt node
|
|
|
|
|
|
|
|
// constituent labels
|
|
|
|
Word sourceLHS, targetLHS;
|
|
|
|
|
|
|
|
// source
|
2011-11-21 14:49:26 +04:00
|
|
|
Phrase sourcePhrase( 0);
|
2011-09-27 04:34:46 +04:00
|
|
|
sourcePhrase.CreateFromStringNewFormat(Input, input, sourcePhraseString, factorDelimiter, sourceLHS);
|
|
|
|
|
|
|
|
// create target phrase obj
|
|
|
|
TargetPhrase *targetPhrase = new TargetPhrase(Output);
|
|
|
|
targetPhrase->CreateFromStringNewFormat(Output, output, targetPhraseString, factorDelimiter, targetLHS);
|
|
|
|
|
|
|
|
// rest of target phrase
|
|
|
|
targetPhrase->SetAlignmentInfo(alignString);
|
|
|
|
targetPhrase->SetTargetLHS(targetLHS);
|
|
|
|
//targetPhrase->SetDebugOutput(string("New Format pt ") + line);
|
|
|
|
|
|
|
|
// component score, for n-best output
|
|
|
|
std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),TransformScore);
|
|
|
|
std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),FloorScore);
|
|
|
|
|
|
|
|
targetPhrase->SetScoreChart(ruleTable.GetFeature(), scoreVector, weight, languageModels,wpProducer);
|
|
|
|
|
2011-10-24 15:54:42 +04:00
|
|
|
TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection(ruleTable, sourcePhrase, *targetPhrase, sourceLHS);
|
2011-09-27 04:34:46 +04:00
|
|
|
phraseColl.Add(targetPhrase);
|
|
|
|
|
|
|
|
count++;
|
2011-11-06 17:46:46 +04:00
|
|
|
|
2011-11-06 17:55:57 +04:00
|
|
|
if (format == HieroFormat) { // reformat line
|
2011-11-06 17:46:46 +04:00
|
|
|
delete line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // do nothing
|
|
|
|
}
|
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort and prune each target phrase collection
|
|
|
|
SortAndPrune(ruleTable);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|