2012-11-03 03:30:51 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2012- 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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2015-02-05 19:23:47 +03:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2012-11-03 03:30:51 +04:00
|
|
|
#include "ScoreFeature.h"
|
2014-01-29 22:37:42 +04:00
|
|
|
#include "DomainFeature.h"
|
2013-09-13 14:45:46 +04:00
|
|
|
#include "InternalStructFeature.h"
|
2012-11-03 03:30:51 +04:00
|
|
|
|
|
|
|
using namespace std;
|
2015-02-05 19:23:47 +03:00
|
|
|
using namespace boost::algorithm;
|
2012-11-03 03:30:51 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace MosesTraining
|
2012-11-03 03:30:51 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const string& ScoreFeatureManager::usage() const
|
|
|
|
{
|
|
|
|
const static string& usage = "[--[Sparse]Domain[Indicator|Ratio|Subset|Bin] domain-file [bins]]" ;
|
|
|
|
return usage;
|
|
|
|
}
|
2012-11-03 03:30:51 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void ScoreFeatureManager::configure(const std::vector<std::string> args)
|
|
|
|
{
|
|
|
|
bool domainAdded = false;
|
|
|
|
bool sparseDomainAdded = false;
|
2013-09-13 14:45:46 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
for (size_t i = 0; i < args.size(); ++i) {
|
2014-01-15 19:49:57 +04:00
|
|
|
if (args[i] == "--IgnoreSentenceId") {
|
2013-05-29 21:16:15 +04:00
|
|
|
m_includeSentenceId = true;
|
2015-02-05 19:23:47 +03:00
|
|
|
} else if (starts_with(args[i], "--Domain")) {
|
2013-05-29 21:16:15 +04:00
|
|
|
string type = args[i].substr(8);
|
|
|
|
++i;
|
|
|
|
UTIL_THROW_IF(i == args.size(), ScoreFeatureArgumentException, "Missing domain file");
|
|
|
|
string domainFile = args[i];
|
|
|
|
UTIL_THROW_IF(domainAdded, ScoreFeatureArgumentException,
|
|
|
|
"Only allowed one domain feature");
|
|
|
|
if (type == "Subset") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new SubsetDomainFeature(domainFile)));
|
|
|
|
} else if (type == "Ratio") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new RatioDomainFeature(domainFile)));
|
|
|
|
} else if (type == "Indicator") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new IndicatorDomainFeature(domainFile)));
|
|
|
|
} else {
|
|
|
|
UTIL_THROW(ScoreFeatureArgumentException, "Unknown domain feature type " << type);
|
2013-05-01 22:20:05 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
domainAdded = true;
|
|
|
|
m_includeSentenceId = true;
|
2015-02-05 19:23:47 +03:00
|
|
|
} else if (starts_with(args[i], "--SparseDomain")) {
|
2013-05-29 21:16:15 +04:00
|
|
|
string type = args[i].substr(14);
|
|
|
|
++i;
|
|
|
|
UTIL_THROW_IF(i == args.size(), ScoreFeatureArgumentException, "Missing domain file");
|
|
|
|
string domainFile = args[i];
|
|
|
|
UTIL_THROW_IF(sparseDomainAdded, ScoreFeatureArgumentException,
|
|
|
|
"Only allowed one sparse domain feature");
|
|
|
|
if (type == "Subset") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new SparseSubsetDomainFeature(domainFile)));
|
|
|
|
} else if (type == "Ratio") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new SparseRatioDomainFeature(domainFile)));
|
|
|
|
} else if (type == "Indicator") {
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new SparseIndicatorDomainFeature(domainFile)));
|
2012-11-15 16:35:43 +04:00
|
|
|
} else {
|
2013-05-29 21:16:15 +04:00
|
|
|
UTIL_THROW(ScoreFeatureArgumentException, "Unknown domain feature type " << type);
|
2012-11-03 03:30:51 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
sparseDomainAdded = true;
|
|
|
|
m_includeSentenceId = true;
|
2014-05-19 17:35:08 +04:00
|
|
|
} else if(args[i] == "--TreeFeatureSparse") {
|
|
|
|
//MARIA
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new InternalStructFeatureSparse()));
|
|
|
|
} else if(args[i] == "--TreeFeatureDense") {
|
|
|
|
//MARIA
|
|
|
|
m_features.push_back(ScoreFeaturePtr(new InternalStructFeatureDense()));
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
|
|
|
UTIL_THROW(ScoreFeatureArgumentException,"Unknown score argument " << args[i]);
|
2014-01-15 19:49:57 +04:00
|
|
|
}
|
2013-09-13 14:45:46 +04:00
|
|
|
|
2012-11-03 03:30:51 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:35:08 +04:00
|
|
|
void ScoreFeatureManager::addPropertiesToPhrasePair(ExtractionPhrasePair &phrasePair,
|
|
|
|
float count,
|
|
|
|
int sentenceId) const
|
2013-05-29 21:16:15 +04:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_features.size(); ++i) {
|
2014-01-29 22:37:42 +04:00
|
|
|
m_features[i]->addPropertiesToPhrasePair(phrasePair, count, sentenceId);
|
2012-11-03 03:30:51 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
2012-11-03 03:30:51 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void ScoreFeatureManager::addFeatures(const ScoreFeatureContext& context,
|
|
|
|
std::vector<float>& denseValues,
|
|
|
|
std::map<std::string,float>& sparseValues) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_features.size(); ++i) {
|
|
|
|
m_features[i]->add(context, denseValues, sparseValues);
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 03:30:51 +04:00
|
|
|
}
|
|
|
|
|