2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
// vim:tabstop=2
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2009-10-02 20:58:02 +04:00
|
|
|
#include <stdexcept>
|
2013-10-03 14:05:53 +04:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2009-10-02 20:58:02 +04:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Sentence.h"
|
|
|
|
#include "TranslationOptionCollectionText.h"
|
|
|
|
#include "StaticData.h"
|
2013-07-24 18:55:17 +04:00
|
|
|
#include "ChartTranslationOptions.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Util.h"
|
2013-09-22 20:24:32 +04:00
|
|
|
#include "XmlOption.h"
|
2013-10-03 14:05:53 +04:00
|
|
|
#include "FactorCollection.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2010-01-28 15:12:57 +03:00
|
|
|
|
2011-11-21 15:14:05 +04:00
|
|
|
Sentence::Sentence()
|
2011-11-21 14:49:26 +04:00
|
|
|
: Phrase(0)
|
2011-02-24 16:14:42 +03:00
|
|
|
, InputType()
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
const StaticData& staticData = StaticData::Instance();
|
2012-10-12 17:09:45 +04:00
|
|
|
if (staticData.IsChart()) {
|
2011-02-24 16:14:42 +03:00
|
|
|
m_defaultLabelSet.insert(StaticData::Instance().GetInputDefaultNonTerminal());
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-07-23 19:46:30 +04:00
|
|
|
Sentence::~Sentence()
|
|
|
|
{
|
2013-07-30 18:04:37 +04:00
|
|
|
RemoveAllInColl(m_xmlOptions);
|
2013-07-23 19:46:30 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
int Sentence::Read(std::istream& in,const std::vector<FactorType>& factorOrder)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
const std::string& factorDelimiter = StaticData::Instance().GetFactorDelimiter();
|
|
|
|
std::string line;
|
|
|
|
std::map<std::string, std::string> meta;
|
|
|
|
|
|
|
|
if (getline(in, line, '\n').eof())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
//get covered words - if continual-partial-translation is switched on, parse input
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
m_frontSpanCoveredLength = 0;
|
|
|
|
m_sourceCompleted.resize(0);
|
|
|
|
if (staticData.ContinuePartialTranslation()) {
|
|
|
|
string initialTargetPhrase;
|
|
|
|
string sourceCompletedStr;
|
|
|
|
int loc1 = line.find( "|||", 0 );
|
|
|
|
int loc2 = line.find( "|||", loc1 + 3 );
|
|
|
|
if (loc1 > -1 && loc2 > -1) {
|
|
|
|
initialTargetPhrase = line.substr(0, loc1);
|
|
|
|
sourceCompletedStr = line.substr(loc1 + 3, loc2 - loc1 - 3);
|
|
|
|
line = line.substr(loc2 + 3);
|
|
|
|
sourceCompletedStr = Trim(sourceCompletedStr);
|
|
|
|
initialTargetPhrase = Trim(initialTargetPhrase);
|
|
|
|
m_initialTargetPhrase = initialTargetPhrase;
|
|
|
|
int len = sourceCompletedStr.size();
|
|
|
|
m_sourceCompleted.resize(len);
|
|
|
|
int contiguous = 1;
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
if (sourceCompletedStr.at(i) == '1') {
|
|
|
|
m_sourceCompleted[i] = true;
|
|
|
|
if (contiguous)
|
|
|
|
m_frontSpanCoveredLength ++;
|
|
|
|
} else {
|
|
|
|
m_sourceCompleted[i] = false;
|
|
|
|
contiguous = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove extra spaces
|
|
|
|
line = Trim(line);
|
|
|
|
|
|
|
|
// if sentences is specified as "<seg id=1> ... </seg>", extract id
|
2008-12-15 15:52:38 +03:00
|
|
|
meta = ProcessAndStripSGML(line);
|
2011-02-24 16:14:42 +03:00
|
|
|
if (meta.find("id") != meta.end()) {
|
|
|
|
this->SetTranslationId(atol(meta["id"].c_str()));
|
|
|
|
}
|
2012-07-03 19:25:04 +04:00
|
|
|
if (meta.find("docid") != meta.end()) {
|
|
|
|
this->SetDocumentId(atol(meta["docid"].c_str()));
|
2012-07-26 20:32:50 +04:00
|
|
|
this->SetUseTopicId(false);
|
|
|
|
this->SetUseTopicIdAndProb(false);
|
|
|
|
}
|
|
|
|
if (meta.find("topic") != meta.end()) {
|
|
|
|
vector<string> topic_params;
|
|
|
|
boost::split(topic_params, meta["topic"], boost::is_any_of("\t "));
|
|
|
|
if (topic_params.size() == 1) {
|
|
|
|
this->SetTopicId(atol(topic_params[0].c_str()));
|
|
|
|
this->SetUseTopicId(true);
|
|
|
|
this->SetUseTopicIdAndProb(false);
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2012-10-01 23:36:52 +04:00
|
|
|
this->SetTopicIdAndProb(topic_params);
|
2012-07-26 20:32:50 +04:00
|
|
|
this->SetUseTopicId(false);
|
|
|
|
this->SetUseTopicIdAndProb(true);
|
|
|
|
}
|
2012-07-03 19:25:04 +04:00
|
|
|
}
|
2013-05-31 15:28:57 +04:00
|
|
|
if (meta.find("weight-setting") != meta.end()) {
|
|
|
|
this->SetWeightSetting(meta["weight-setting"]);
|
|
|
|
this->SetSpecifiesWeightSetting(true);
|
2013-06-03 15:33:18 +04:00
|
|
|
} else {
|
2013-05-31 15:28:57 +04:00
|
|
|
this->SetSpecifiesWeightSetting(false);
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
// parse XML markup in translation line
|
|
|
|
//const StaticData &staticData = StaticData::Instance();
|
|
|
|
std::vector< size_t > xmlWalls;
|
2013-07-18 20:00:07 +04:00
|
|
|
std::vector< std::pair<size_t, std::string> > placeholders;
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
if (staticData.GetXmlInputType() != XmlPassThrough) {
|
2013-07-30 18:04:37 +04:00
|
|
|
int offset = 0;
|
2013-07-24 21:06:46 +04:00
|
|
|
if (staticData.IsChart()) {
|
|
|
|
offset = 1;
|
2013-07-30 18:04:37 +04:00
|
|
|
}
|
2013-07-24 21:06:46 +04:00
|
|
|
|
2013-07-23 19:46:30 +04:00
|
|
|
if (!ProcessAndStripXMLTags(line, m_xmlOptions, m_reorderingConstraint, xmlWalls, placeholders,
|
2013-07-24 21:06:46 +04:00
|
|
|
offset,
|
2013-07-30 18:04:37 +04:00
|
|
|
staticData.GetXmlBrackets().first,
|
2013-07-24 21:06:46 +04:00
|
|
|
staticData.GetXmlBrackets().second)) {
|
2011-02-24 16:14:42 +03:00
|
|
|
const string msg("Unable to parse XML in line: " + line);
|
|
|
|
TRACE_ERR(msg << endl);
|
|
|
|
throw runtime_error(msg);
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 20:00:07 +04:00
|
|
|
|
2013-05-22 15:03:00 +04:00
|
|
|
Phrase::CreateFromString(Input, factorOrder, line, factorDelimiter, NULL);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-07-18 20:00:07 +04:00
|
|
|
// placeholders
|
|
|
|
ProcessPlaceholders(placeholders);
|
|
|
|
|
2012-10-12 17:09:45 +04:00
|
|
|
if (staticData.IsChart()) {
|
2011-02-24 16:14:42 +03:00
|
|
|
InitStartEndWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
//now that we have final word positions in phrase (from CreateFromString),
|
|
|
|
//we can make input phrase objects to go with our XmlOptions and create TranslationOptions
|
|
|
|
|
|
|
|
//only fill the vector if we are parsing XML
|
|
|
|
if (staticData.GetXmlInputType() != XmlPassThrough ) {
|
|
|
|
for (size_t i=0; i<GetSize(); i++) {
|
|
|
|
m_xmlCoverageMap.push_back(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//iterXMLOpts will be empty for XmlIgnore
|
|
|
|
//look at each column
|
2013-07-23 19:46:30 +04:00
|
|
|
for(std::vector<XmlOption*>::const_iterator iterXmlOpts = m_xmlOptions.begin();
|
|
|
|
iterXmlOpts != m_xmlOptions.end(); iterXmlOpts++) {
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
const XmlOption *xmlOption = *iterXmlOpts;
|
2013-07-23 19:46:30 +04:00
|
|
|
const WordsRange &range = xmlOption->range;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-07-23 19:46:30 +04:00
|
|
|
for(size_t j=range.GetStartPos(); j<=range.GetEndPos(); j++) {
|
2011-02-24 16:14:42 +03:00
|
|
|
m_xmlCoverageMap[j]=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:28:57 +04:00
|
|
|
// reordering walls and zones
|
2011-02-24 16:14:42 +03:00
|
|
|
m_reorderingConstraint.InitializeWalls( GetSize() );
|
|
|
|
|
|
|
|
// set reordering walls, if "-monotone-at-punction" is set
|
2012-02-16 09:14:56 +04:00
|
|
|
if (staticData.UseReorderingConstraint() && GetSize()>0) {
|
2011-02-24 16:14:42 +03:00
|
|
|
m_reorderingConstraint.SetMonotoneAtPunctuation( GetSubString( WordsRange(0,GetSize()-1 ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set walls obtained from xml
|
|
|
|
for(size_t i=0; i<xmlWalls.size(); i++)
|
|
|
|
if( xmlWalls[i] < GetSize() ) // no buggy walls, please
|
|
|
|
m_reorderingConstraint.SetWall( xmlWalls[i], true );
|
|
|
|
m_reorderingConstraint.FinalizeWalls();
|
|
|
|
|
|
|
|
return 1;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 20:00:07 +04:00
|
|
|
void Sentence::ProcessPlaceholders(const std::vector< std::pair<size_t, std::string> > &placeholders)
|
|
|
|
{
|
2013-09-22 17:09:49 +04:00
|
|
|
FactorType placeholderFactor = StaticData::Instance().GetPlaceholderFactor();
|
2013-07-19 21:23:38 +04:00
|
|
|
if (placeholderFactor == NOT_FOUND) {
|
2013-07-18 20:00:44 +04:00
|
|
|
return;
|
2013-07-18 20:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < placeholders.size(); ++i) {
|
2013-07-18 20:00:44 +04:00
|
|
|
size_t pos = placeholders[i].first;
|
|
|
|
const string &str = placeholders[i].second;
|
|
|
|
const Factor *factor = FactorCollection::Instance().AddFactor(str);
|
|
|
|
Word &word = Phrase::GetWord(pos);
|
2013-07-19 21:23:38 +04:00
|
|
|
word[placeholderFactor] = factor;
|
2013-07-18 20:00:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
TranslationOptionCollection*
|
2013-05-11 17:13:26 +04:00
|
|
|
Sentence::CreateTranslationOptionCollection() const
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t maxNoTransOptPerCoverage = StaticData::Instance().GetMaxNoTransOptPerCoverage();
|
|
|
|
float transOptThreshold = StaticData::Instance().GetTranslationOptionThreshold();
|
2013-05-11 17:13:26 +04:00
|
|
|
TranslationOptionCollection *rv= new TranslationOptionCollectionText(*this, maxNoTransOptPerCoverage, transOptThreshold);
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(rv);
|
2011-02-24 16:14:42 +03:00
|
|
|
return rv;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
void Sentence::Print(std::ostream& out) const
|
|
|
|
{
|
2012-07-03 19:25:04 +04:00
|
|
|
out<<*static_cast<Phrase const*>(this);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
bool Sentence::XmlOverlap(size_t startPos, size_t endPos) const
|
|
|
|
{
|
|
|
|
for (size_t pos = startPos; pos <= endPos ; pos++) {
|
|
|
|
if (pos < m_xmlCoverageMap.size() && m_xmlCoverageMap[pos]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-08-16 00:14:04 +04:00
|
|
|
void Sentence::GetXmlTranslationOptions(std::vector <TranslationOption*> &list) const
|
2013-08-15 16:36:56 +04:00
|
|
|
{
|
|
|
|
for (std::vector<XmlOption*>::const_iterator iterXMLOpts = m_xmlOptions.begin();
|
|
|
|
iterXMLOpts != m_xmlOptions.end(); ++iterXMLOpts) {
|
|
|
|
const XmlOption &xmlOption = **iterXMLOpts;
|
|
|
|
const WordsRange &range = xmlOption.range;
|
|
|
|
const TargetPhrase &targetPhrase = xmlOption.targetPhrase;
|
|
|
|
TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
|
|
|
|
list.push_back(transOpt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void Sentence::GetXmlTranslationOptions(std::vector <TranslationOption*> &list, size_t startPos, size_t endPos) const
|
|
|
|
{
|
|
|
|
//iterate over XmlOptions list, find exact source/target matches
|
|
|
|
|
2013-07-23 19:46:30 +04:00
|
|
|
for (std::vector<XmlOption*>::const_iterator iterXMLOpts = m_xmlOptions.begin();
|
|
|
|
iterXMLOpts != m_xmlOptions.end(); ++iterXMLOpts) {
|
2013-07-30 18:04:37 +04:00
|
|
|
const XmlOption &xmlOption = **iterXMLOpts;
|
2013-07-23 19:46:30 +04:00
|
|
|
const WordsRange &range = xmlOption.range;
|
|
|
|
|
|
|
|
if (startPos == range.GetStartPos()
|
2013-07-30 18:04:37 +04:00
|
|
|
&& endPos == range.GetEndPos()) {
|
2013-07-23 19:46:30 +04:00
|
|
|
const TargetPhrase &targetPhrase = xmlOption.targetPhrase;
|
|
|
|
|
|
|
|
TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
|
|
|
|
list.push_back(transOpt);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:55:17 +04:00
|
|
|
std::vector <ChartTranslationOptions*> Sentence::GetXmlChartTranslationOptions() const
|
|
|
|
{
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
std::vector <ChartTranslationOptions*> ret;
|
|
|
|
|
|
|
|
// XML Options
|
|
|
|
// this code is a copy of the 1 in Sentence.
|
|
|
|
|
|
|
|
//only fill the vector if we are parsing XML
|
|
|
|
if (staticData.GetXmlInputType() != XmlPassThrough ) {
|
2013-07-30 18:04:37 +04:00
|
|
|
//TODO: needed to handle exclusive
|
|
|
|
//for (size_t i=0; i<GetSize(); i++) {
|
|
|
|
// m_xmlCoverageMap.push_back(false);
|
|
|
|
//}
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
//iterXMLOpts will be empty for XmlIgnore
|
|
|
|
//look at each column
|
|
|
|
for(std::vector<XmlOption*>::const_iterator iterXmlOpts = m_xmlOptions.begin();
|
|
|
|
iterXmlOpts != m_xmlOptions.end(); iterXmlOpts++) {
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
const XmlOption &xmlOption = **iterXmlOpts;
|
|
|
|
TargetPhrase *targetPhrase = new TargetPhrase(xmlOption.targetPhrase);
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
WordsRange *range = new WordsRange(xmlOption.range);
|
|
|
|
const StackVec emptyStackVec; // hmmm... maybe dangerous, but it is never consulted
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
TargetPhraseCollection *tpc = new TargetPhraseCollection;
|
|
|
|
tpc->Add(targetPhrase);
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
ChartTranslationOptions *transOpt = new ChartTranslationOptions(*tpc, emptyStackVec, *range, 0.0f);
|
|
|
|
ret.push_back(transOpt);
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2013-07-30 18:04:37 +04:00
|
|
|
//TODO: needed to handle exclusive
|
|
|
|
//for(size_t j=transOpt->GetSourceWordsRange().GetStartPos(); j<=transOpt->GetSourceWordsRange().GetEndPos(); j++) {
|
|
|
|
// m_xmlCoverageMap[j]=true;
|
|
|
|
//}
|
|
|
|
}
|
2013-07-24 18:55:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-01 00:32:18 +04:00
|
|
|
void Sentence::CreateFromString(const std::vector<FactorType> &factorOrder
|
2011-02-24 16:14:42 +03:00
|
|
|
, const std::string &phraseString
|
|
|
|
, const std::string &factorDelimiter)
|
2009-06-01 00:32:18 +04:00
|
|
|
{
|
2013-05-22 15:03:00 +04:00
|
|
|
Phrase::CreateFromString(Input, factorOrder, phraseString, factorDelimiter, NULL);
|
2009-06-01 00:32:18 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|