2011-09-27 04:34:46 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - statistical machine translation system
|
|
|
|
Copyright (C) 2006-2011 University of Edinburgh
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
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.
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
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.
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
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-11-12 23:56:18 +04:00
|
|
|
#include "LoaderStandard.h"
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
2012-10-14 23:52:12 +04:00
|
|
|
#include <iostream>
|
2011-09-27 04:34:46 +04:00
|
|
|
#include <sys/stat.h>
|
2012-10-14 20:35:58 +04:00
|
|
|
#include <stdlib.h>
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "Trie.h"
|
|
|
|
#include "moses/FactorCollection.h"
|
|
|
|
#include "moses/Word.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/InputFileStream.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/WordsRange.h"
|
|
|
|
#include "moses/UserMessage.h"
|
|
|
|
#include "moses/ChartTranslationOptionList.h"
|
|
|
|
#include "moses/FactorCollection.h"
|
2012-10-14 23:52:12 +04:00
|
|
|
#include "util/file_piece.hh"
|
2012-10-14 20:35:58 +04:00
|
|
|
#include "util/string_piece.hh"
|
|
|
|
#include "util/tokenize_piece.hh"
|
2012-10-28 21:47:22 +04:00
|
|
|
#include "util/double-conversion/double-conversion.h"
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
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
|
2012-10-14 23:52:12 +04:00
|
|
|
, const std::string &inFile
|
2011-11-04 21:20:12 +04:00
|
|
|
, size_t tableLimit
|
2012-01-24 00:41:49 +04:00
|
|
|
, RuleTableTrie &ruleTable)
|
2011-11-04 21:20:12 +04:00
|
|
|
{
|
|
|
|
bool ret = Load(MosesFormat
|
|
|
|
,input, output
|
2013-05-21 17:00:28 +04:00
|
|
|
,inFile
|
2013-05-21 16:00:26 +04:00
|
|
|
,tableLimit
|
2013-05-21 16:39:31 +04:00
|
|
|
,ruleTable);
|
2011-11-04 21:20:12 +04:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
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, " ");
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
for (size_t i = 0; i < toks.size(); ++i) {
|
2011-11-06 17:46:46 +04:00
|
|
|
string &tok = toks[i];
|
|
|
|
size_t tokLen = tok.size();
|
2013-05-29 21:16:15 +04:00
|
|
|
if (tok.substr(0, 1) == "[" && tok.substr(tokLen - 1, 1) == "]") {
|
|
|
|
// no-term
|
2011-11-06 17:46:46 +04:00
|
|
|
vector<string> split = Tokenize(tok, ",");
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(split.size() == 2);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
tok = "[X]" + split[0] + "]";
|
|
|
|
size_t coIndex = Scan<size_t>(split[1]);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
pair<size_t, size_t> &alignPoint = ntAlign[coIndex];
|
2013-05-29 21:16:15 +04:00
|
|
|
if (sourceTarget == 0) {
|
2011-11-06 17:46:46 +04:00
|
|
|
alignPoint.first = i;
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2011-11-06 17:46:46 +04:00
|
|
|
alignPoint.second = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
phrase = Join(" ", toks) + " [X]";
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReformateHieroScore(string &scoreString)
|
|
|
|
{
|
|
|
|
vector<string> toks;
|
|
|
|
Tokenize(toks, scoreString, " ");
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
for (size_t i = 0; i < toks.size(); ++i) {
|
2011-11-06 17:46:46 +04:00
|
|
|
string &tok = toks[i];
|
2013-05-28 18:54:11 +04:00
|
|
|
vector<string> nameValue = Tokenize(tok, "=");
|
|
|
|
CHECK(nameValue.size() == 2);
|
2011-11-06 17:46:46 +04:00
|
|
|
|
2013-05-28 18:54:11 +04:00
|
|
|
float score = Scan<float>(nameValue[1]);
|
2011-11-06 17:46:46 +04:00
|
|
|
score = exp(-score);
|
|
|
|
tok = SPrint(score);
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
scoreString = Join(" ", toks);
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
void ReformatHieroRule(const string &lineOrig, string &out)
|
2013-05-29 21:16:15 +04:00
|
|
|
{
|
2011-11-06 17:46:46 +04:00
|
|
|
vector<string> tokens;
|
|
|
|
vector<float> scoreVector;
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
TokenizeMultiCharSeparator(tokens, lineOrig, "|||" );
|
|
|
|
|
|
|
|
string &sourcePhraseString = tokens[1]
|
2013-05-29 21:16:15 +04:00
|
|
|
, &targetPhraseString = tokens[2]
|
|
|
|
, &scoreString = tokens[3];
|
2011-11-06 17:46:46 +04:00
|
|
|
|
|
|
|
map<size_t, pair<size_t, size_t> > ntAlign;
|
|
|
|
ReformatHieroRule(0, sourcePhraseString, ntAlign);
|
|
|
|
ReformatHieroRule(1, targetPhraseString, ntAlign);
|
|
|
|
ReformateHieroScore(scoreString);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
stringstream align;
|
|
|
|
map<size_t, pair<size_t, size_t> >::const_iterator iterAlign;
|
2013-05-29 21:16:15 +04:00
|
|
|
for (iterAlign = ntAlign.begin(); iterAlign != ntAlign.end(); ++iterAlign) {
|
2011-11-06 17:46:46 +04:00
|
|
|
const pair<size_t, size_t> &alignPoint = iterAlign->second;
|
|
|
|
align << alignPoint.first << "-" << alignPoint.second << " ";
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 17:46:46 +04:00
|
|
|
stringstream ret;
|
|
|
|
ret << sourcePhraseString << " ||| "
|
2013-05-29 21:16:15 +04:00
|
|
|
<< targetPhraseString << " ||| "
|
2011-11-06 17:46:46 +04:00
|
|
|
<< scoreString << " ||| "
|
|
|
|
<< align.str();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
out = ret.str();
|
2011-11-06 17:46:46 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-04 21:20:12 +04:00
|
|
|
bool RuleTableLoaderStandard::Load(FormatType format
|
2013-05-29 21:16:15 +04:00
|
|
|
, const std::vector<FactorType> &input
|
|
|
|
, const std::vector<FactorType> &output
|
|
|
|
, const std::string &inFile
|
|
|
|
, size_t /* tableLimit */
|
|
|
|
, RuleTableTrie &ruleTable)
|
2011-09-27 04:34:46 +04:00
|
|
|
{
|
2012-08-09 15:47:48 +04:00
|
|
|
PrintUserTime(string("Start loading text SCFG phrase table. ") + (format==MosesFormat?"Moses ":"Hiero ") + " format");
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
std::ostream *progress = NULL;
|
|
|
|
IFVERBOSE(1) progress = &std::cerr;
|
|
|
|
util::FilePiece in(inFile.c_str(), progress);
|
2011-09-27 04:34:46 +04:00
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
// reused variables
|
2012-10-14 20:35:58 +04:00
|
|
|
vector<float> scoreVector;
|
2012-10-14 23:52:12 +04:00
|
|
|
StringPiece line;
|
|
|
|
std::string hiero_before, hiero_after;
|
|
|
|
|
2012-10-28 21:47:22 +04:00
|
|
|
double_conversion::StringToDoubleConverter converter(double_conversion::StringToDoubleConverter::NO_FLAGS, NAN, NAN, "inf", "nan");
|
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
while(true) {
|
|
|
|
try {
|
|
|
|
line = in.ReadLine();
|
2013-05-29 21:16:15 +04:00
|
|
|
} catch (const util::EndOfFileException &e) {
|
|
|
|
break;
|
|
|
|
}
|
2012-10-14 23:52:12 +04:00
|
|
|
|
|
|
|
if (format == HieroFormat) { // inefficiently reformat line
|
|
|
|
hiero_before.assign(line.data(), line.size());
|
|
|
|
ReformatHieroRule(hiero_before, hiero_after);
|
|
|
|
line = hiero_after;
|
2011-11-06 17:46:46 +04:00
|
|
|
}
|
2011-09-27 04:34:46 +04:00
|
|
|
|
2012-10-14 23:52:12 +04:00
|
|
|
util::TokenIter<util::MultiCharacter> pipes(line, "|||");
|
2012-10-14 20:35:58 +04:00
|
|
|
StringPiece sourcePhraseString(*pipes);
|
|
|
|
StringPiece targetPhraseString(*++pipes);
|
|
|
|
StringPiece scoreString(*++pipes);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-03-07 23:12:49 +04:00
|
|
|
StringPiece alignString;
|
|
|
|
if (++pipes) {
|
|
|
|
StringPiece temp(*pipes);
|
|
|
|
alignString = temp;
|
|
|
|
}
|
|
|
|
|
2013-04-30 20:27:35 +04:00
|
|
|
if (++pipes) {
|
|
|
|
StringPiece str(*pipes); //counts
|
2011-09-27 04:34:46 +04:00
|
|
|
}
|
|
|
|
|
2013-08-29 16:36:01 +04:00
|
|
|
StringPiece propertiesString;
|
|
|
|
if (++pipes) {
|
|
|
|
StringPiece temp(*pipes);
|
|
|
|
propertiesString = temp;
|
|
|
|
}
|
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-14 20:35:58 +04:00
|
|
|
scoreVector.clear();
|
|
|
|
for (util::TokenIter<util::AnyCharacter, true> s(scoreString, " \t"); s; ++s) {
|
2012-10-28 21:47:22 +04:00
|
|
|
int processed;
|
|
|
|
float score = converter.StringToFloat(s->data(), s->length(), &processed);
|
|
|
|
UTIL_THROW_IF(isnan(score), util::Exception, "Bad score " << *s << " on line " << count);
|
|
|
|
scoreVector.push_back(FloorScore(TransformScore(score)));
|
2012-10-14 20:35:58 +04:00
|
|
|
}
|
2013-02-22 23:17:57 +04:00
|
|
|
const size_t numScoreComponents = ruleTable.GetNumScoreComponents();
|
2011-09-27 04:34:46 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse source & find pt node
|
|
|
|
|
|
|
|
// constituent labels
|
2013-05-22 17:41:28 +04:00
|
|
|
Word *sourceLHS;
|
|
|
|
Word *targetLHS;
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
// create target phrase obj
|
2012-10-22 20:40:23 +04:00
|
|
|
TargetPhrase *targetPhrase = new TargetPhrase();
|
2013-05-22 15:28:32 +04:00
|
|
|
targetPhrase->CreateFromString(Output, output, targetPhraseString, factorDelimiter, &targetLHS);
|
2012-10-23 22:16:50 +04:00
|
|
|
|
|
|
|
// source
|
2013-05-28 03:41:25 +04:00
|
|
|
Phrase sourcePhrase;
|
|
|
|
sourcePhrase.CreateFromString(Input, input, sourcePhraseString, factorDelimiter, &sourceLHS);
|
2011-09-27 04:34:46 +04:00
|
|
|
|
|
|
|
// rest of target phrase
|
2012-10-19 18:10:10 +04:00
|
|
|
targetPhrase->SetAlignmentInfo(alignString);
|
2011-09-27 04:34:46 +04:00
|
|
|
targetPhrase->SetTargetLHS(targetLHS);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-08-29 16:36:01 +04:00
|
|
|
targetPhrase->SetProperties(propertiesString);
|
|
|
|
|
2011-09-27 04:34:46 +04:00
|
|
|
//targetPhrase->SetDebugOutput(string("New Format pt ") + line);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-04-30 20:27:35 +04:00
|
|
|
if (++pipes) {
|
|
|
|
StringPiece sparseString(*pipes);
|
|
|
|
targetPhrase->SetSparseScore(&ruleTable, sparseString);
|
|
|
|
}
|
|
|
|
|
2013-05-13 18:36:09 +04:00
|
|
|
targetPhrase->GetScoreBreakdown().Assign(&ruleTable, scoreVector);
|
2013-05-30 21:34:10 +04:00
|
|
|
targetPhrase->Evaluate(sourcePhrase, ruleTable.GetFeaturesToApply());
|
2011-09-27 04:34:46 +04:00
|
|
|
|
2013-05-28 14:38:18 +04:00
|
|
|
TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection(ruleTable, sourcePhrase, *targetPhrase, sourceLHS);
|
2011-09-27 04:34:46 +04:00
|
|
|
phraseColl.Add(targetPhrase);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort and prune each target phrase collection
|
|
|
|
SortAndPrune(ruleTable);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|