Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OnlineCommand.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 93024b92c67e38a477a566924d7463713ce9085b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// $Id: OnlineCommand.cpp 3428 2010-09-13 17:55:23Z nicolabertoldi $
// 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
***********************************************************************/

#include <stdexcept>

#include "StaticData.h"  // needed for debugging purpose only

#include "OnlineCommand.h"
#include "Util.h"

using namespace std;

#define COMMAND_KEYWORD "@CMD@"

namespace Moses
{

OnlineCommand::OnlineCommand()	
{
	VERBOSE(3,"OnlineCommand::OnlineCommand()" << std::endl);
  VERBOSE(3,"COMMAND_KEYWORD: " << COMMAND_KEYWORD << std::endl);
  command_type = '\0';
  command_value = '\0';

	accepted_commands.push_back("-weight-l"); // weight(s) for language models
	accepted_commands.push_back("-weight-t"); // weight(s) for translation model components
	accepted_commands.push_back("-weight-d"); // weight(s) for distortion (reordering components)
	accepted_commands.push_back("-weight-w"); // weight for word penalty
	accepted_commands.push_back("-weight-u"); // weight for unknown words penalty
	accepted_commands.push_back("-weight-g"); // weight(s) for global lexical model components
	accepted_commands.push_back("-weight-b"); // weight(s) for global lexical model components
	accepted_commands.push_back("-verbose"); // weights for translation model components
}
	
bool OnlineCommand::Parse(std::string& line) 
{
	VERBOSE(3,"OnlineCommand::Parse(std::string& line)" << std::endl);

	int next_string_pos = 0;
	string firststring = GetFirstString(line, next_string_pos);
	bool flag = false;

	if (firststring.compare(COMMAND_KEYWORD) == 0){
		
		command_type = GetFirstString(line, next_string_pos);


		for (vector<string>::const_iterator iterParam = accepted_commands.begin(); iterParam!=accepted_commands.end(); ++iterParam) {
    	if (command_type.compare(*iterParam) == 0){   //requested command is found
				command_value = line.substr(next_string_pos);
				flag = true;
			}
		}
		if (!flag){
			VERBOSE(3,"OnlineCommand::Parse: This command |" << command_type << "| is unknown." << std::endl);
	  }
		return true;
	}else{
		return false;
	}
}

void OnlineCommand::Execute() const
{
	std::cerr << "void OnlineCommand::Execute() const" << std::endl;
	VERBOSE(3,"OnlineCommand::Execute() const" << std::endl);
	
	StaticData &staticData = StaticData::InstanceNonConst();

	VERBOSE(1,"Handling online command: " << COMMAND_KEYWORD << " " << command_type << " " << command_value << std::endl);
  // weights
  vector<float> actual_weights;
  vector<float> weights;
  PARAM_VEC values;

  bool flag = false;
  for(vector<std::string>::const_iterator iterParam = accepted_commands.begin(); iterParam != accepted_commands.end(); iterParam++) 
  {
  	std::string paramName = *iterParam;
	
  	if (command_type.compare(paramName) == 0){   //requested command is paramName

			Tokenize(values, command_value);

		//remove initial "-" character
			paramName.erase(0,1);

			staticData.GetParameter()->OverwriteParam(paramName, values);

			staticData.ReLoadParameter();
    // check on weights

    const ScoreComponentCollection& weights = staticData.GetAllWeights();
    IFVERBOSE(2) {
        TRACE_ERR("The global weight vector looks like this: ");
        TRACE_ERR(weights);
        TRACE_ERR("\n");
    }


			flag = true;
  	}
	}
	if (!flag){
		TRACE_ERR("ERROR: The command |" << command_type << "| is unknown." << std::endl);
	}
}

void OnlineCommand::Print(std::ostream& out) const
{
	VERBOSE(3,"OnlineCommand::Print(std::ostream& out) const" << std::endl);
	out << command_type << " -> " << command_value << "\n";
}
 
void OnlineCommand::Clean()
{
  VERBOSE(3,"OnlineCommand::Clean() const" << std::endl);
  command_type = '\0';
  command_value = '\0';
}


}