add Parameter

git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@1638 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
nicolabertoldi 2008-05-14 11:05:32 +00:00
parent f9448b76a0
commit b68b41f7e2
4 changed files with 203 additions and 4 deletions

View File

@ -19,10 +19,24 @@ void FeatureArray::savetxt(std::ofstream& outFile)
{
FeatureStats entry;
outFile << FEATURES_BEGIN << " " << idx << " " << array_.size() << std::endl;
outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size() << std::endl;
for (vector<FeatureStats>::iterator i = array_.begin(); i !=array_.end(); i++)
(*i).savetxt(outFile);
outFile << FEATURES_END << std::endl;
outFile << FEATURES_TXT_END << std::endl;
}
void FeatureArray::savebin(std::ofstream& outFile)
{
FeatureStats entry;
TRACE_ERR("binary saving is not yet implemented!" << std::endl);
/*
NOT YET IMPLEMENTED
*/
outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size() << std::endl;
outFile << FEATURES_BIN_END << std::endl;
}
void FeatureArray::savetxt(const std::string &file)

View File

@ -9,8 +9,10 @@
#ifndef FEATURE_ARRAY_H
#define FEATURE_ARRAY_H
#define FEATURES_BEGIN "FEATURES_BEGIN_0"
#define FEATURES_END "FEATURES_END_0"
#define FEATURES_TXT_BEGIN "FEATURES_TXTB_BEGIN_0"
#define FEATURES_TXT_END "FEATURES_TXT_END_0"
#define FEATURES_BIN_BEGIN "FEATURES_BIN_BEGIN_0"
#define FEATURES_BIN_END "FEATURES_BIN_END_0"
using namespace std;

133
mert/Parameter.cpp Executable file
View File

@ -0,0 +1,133 @@
/*
* Parameter.cpp
* met - Minimum Error Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
using namespace std;
#include "Parameter.h"
/** define allowed parameters */
Parameter::Parameter()
{
AddParam("InputFile", "i", "Input file");
AddParam("Reference", "ref", "Reference");
AddParam("Score", "s", "Score");
AddParam("Help", "h", "Print this help");
};
/** initialize a parameter, sub of constructor */
void Parameter::AddParam(const string &paramName, const string &description)
{
m_description[paramName] = description;
}
/** initialize a parameter (including abbreviation), sub of constructor */
void Parameter::AddParam(const string &paramName, const string &abbrevName, const string &description)
{
m_abbreviation[paramName] = abbrevName;
m_description[paramName] = description;
}
/** print descriptions of all parameters */
void Parameter::Explain() {
std::cerr << "Usage:" << std::endl;
for(PARAM_STRING::const_iterator iterParam = m_description.begin(); iterParam != m_description.end(); iterParam++)
{
const string paramName = iterParam->first;
const string paramDescription = iterParam->second;
std::cerr << "\t-" << paramName;
PARAM_STRING::const_iterator iterAbbr = m_abbreviation.find( paramName );
if ( iterAbbr != m_abbreviation.end() )
std::cerr << " (" << iterAbbr->second << ")";
std::cerr << ": " << paramDescription << std::endl;
}
}
/** load all parameters from the command line */
bool Parameter::LoadParam(int argc, char* argv[])
{
for(PARAM_STRING::const_iterator iterParam = m_description.begin(); iterParam != m_description.end(); iterParam++)
{
const string paramName = iterParam->first;
WriteParam("-" + paramName, paramName, argc, argv);
}
// ... also shortcuts
for(PARAM_STRING::const_iterator iterParam = m_abbreviation.begin(); iterParam != m_abbreviation.end(); iterParam++)
{
const string paramName = iterParam->first;
const string paramShortName = iterParam->second;
WriteParam("-" + paramShortName, paramName, argc, argv);
}
// logging of parameters that were set in either config or switch
int verbose = 1;
if (m_setting.find("verbose") != m_setting.end() &&
m_setting["verbose"].size() > 0)
{
verbose = Scan<int>(m_setting["verbose"][0]);
}
if (verbose >= 1) { // only if verbose
std::cerr << "Defined parameters:" << std::endl;
for(PARAM_MAP::const_iterator iterParam = m_setting.begin() ; iterParam != m_setting.end(); iterParam++) {
std::cerr << "\t" << iterParam->first << ": ";
for ( size_t i = 0; i < iterParam->second.size(); i++ )
std::cerr << iterParam->second[i] << " ";
std::cerr << std::endl;
}
}
return true;
}
/** set parameters with command line swiches
* \param paramSwitch (potentially short) name of switch
* \param paramName full name of parameter
* \param argc number of arguments on command line
* \param argv values of paramters on command line */
void Parameter::WriteParam(const string &paramSwitch, const string &paramName, int argc, char* argv[])
{
int startPos = -1;
for (int i = 0 ; i < argc ; i++)
{
if (string(argv[i]) == paramSwitch)
{
startPos = i+1;
break;
}
}
if (startPos < 0)
return;
int index = 0;
m_setting[paramName]; // defines the parameter, important for boolean switches
while (startPos < argc && (!isOption(argv[startPos])))
{
if (m_setting[paramName].size() > (size_t)index)
m_setting[paramName][index] = argv[startPos];
else
m_setting[paramName].push_back(argv[startPos]);
index++;
startPos++;
}
}
/** check whether an item on the command line is a switch or a value
* \param token token on the command line to checked **/
bool Parameter::isOption(const char* token) {
if (! token) return false;
std::string tokenString(token);
size_t length = tokenString.size();
if (length > 0 && tokenString.substr(0,1) != "-") return false;
if (length > 1 && tokenString.substr(1,1).find_first_not_of("0123456789") == 0) return true;
return false;
}

50
mert/Parameter.h Executable file
View File

@ -0,0 +1,50 @@
/*
* Parameter.h
* met - Minimum Error Training
*
* Created by Nicola Bertoldi on 9/7/06.
* Copyright 2006 ITC-irst. All rights reserved.
*
*/
#ifndef PARAMETER_H
#define PARAMETER_H
using namespace std;
#include <vector>
#include <map>
#include <iostream>
#include <string>
#include "Util.h"
typedef std::vector<std::string> PARAM_VEC;
typedef std::map<std::string, PARAM_VEC > PARAM_MAP;
typedef std::map<std::string, std::string > PARAM_STRING;
/** Handles parameter values set in config file or on command line.
* Process raw parameter data (names and values as strings) */
class Parameter
{
protected:
PARAM_MAP m_setting;
PARAM_STRING m_abbreviation;
PARAM_STRING m_description;
void AddParam(const std::string &paramName, const std::string &description);
void AddParam(const std::string &paramName, const std::string &abbrevName, const std::string &description);
void WriteParam(const std::string &paramSwitch, const std::string &paramName, int argc, char* argv[]);
bool isOption(const char* token);
public:
Parameter();
bool LoadParam(int argc, char* argv[]);
void Explain();
/** return a vector of strings holding the whitespace-delimited values on the ini-file line corresponding to the given parameter name */
const PARAM_VEC &GetParam(const std::string &paramName) { return m_setting[paramName]; }
};
#endif