2015-03-21 18:54:52 +03:00
# include "TranslationRequest.h"
2015-11-01 17:51:33 +03:00
# include "PackScores.h"
2015-07-02 03:32:34 +03:00
# include "moses/ContextScope.h"
2015-03-18 16:46:15 +03:00
# include <boost/foreach.hpp>
2015-08-09 22:54:58 +03:00
# include "moses/Util.h"
2015-11-17 17:06:48 +03:00
# include "moses/Hypothesis.h"
2015-03-04 03:07:11 +03:00
namespace MosesServer
{
2015-05-02 13:45:24 +03:00
using namespace std ;
using Moses : : Hypothesis ;
using Moses : : StaticData ;
2015-10-25 16:37:59 +03:00
using Moses : : Range ;
2015-05-02 13:45:24 +03:00
using Moses : : ChartHypothesis ;
using Moses : : Phrase ;
using Moses : : Manager ;
using Moses : : SearchGraphNode ;
using Moses : : TrellisPathList ;
using Moses : : TranslationOptionCollection ;
using Moses : : TranslationOptionList ;
using Moses : : TranslationOption ;
using Moses : : TargetPhrase ;
using Moses : : FValue ;
using Moses : : PhraseDictionaryMultiModel ;
using Moses : : FindPhraseDictionary ;
using Moses : : Sentence ;
2016-11-12 20:28:26 +03:00
using Moses : : TokenizeMultiCharSeparator ;
using Moses : : FeatureFunction ;
2016-11-14 22:26:34 +03:00
using Moses : : Scan ;
2015-05-02 13:45:24 +03:00
boost : : shared_ptr < TranslationRequest >
TranslationRequest : :
2015-08-03 02:29:34 +03:00
create ( Translator * translator , xmlrpc_c : : paramList const & paramList ,
2015-10-31 15:50:52 +03:00
boost : : condition_variable & cond , boost : : mutex & mut )
2015-05-02 13:45:24 +03:00
{
boost : : shared_ptr < TranslationRequest > ret ;
2015-08-09 22:54:58 +03:00
ret . reset ( new TranslationRequest ( paramList , cond , mut ) ) ;
2015-05-02 13:45:24 +03:00
ret - > m_self = ret ;
2015-08-03 02:29:34 +03:00
ret - > m_translator = translator ;
2015-05-02 13:45:24 +03:00
return ret ;
}
2015-04-30 08:05:11 +03:00
2015-11-04 21:12:49 +03:00
void
SetContextWeights ( Moses : : ContextScope & s , xmlrpc_c : : value const & w )
{
SPTR < std : : map < std : : string , float > > M ( new std : : map < std : : string , float > ) ;
typedef std : : map < std : : string , xmlrpc_c : : value > tmap ;
tmap const tmp = static_cast < tmap > ( xmlrpc_c : : value_struct ( w ) ) ;
for ( tmap : : const_iterator m = tmp . begin ( ) ; m ! = tmp . end ( ) ; + + m )
( * M ) [ m - > first ] = xmlrpc_c : : value_double ( m - > second ) ;
s . SetContextWeights ( M ) ;
}
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
Run ( )
{
2015-10-21 02:56:29 +03:00
typedef std : : map < std : : string , xmlrpc_c : : value > param_t ;
param_t const & params = m_paramList . getStruct ( 0 ) ;
2015-08-04 03:59:28 +03:00
parse_request ( params ) ;
2015-08-03 02:29:34 +03:00
// cerr << "SESSION ID" << ret->m_session_id << endl;
2015-10-21 02:56:29 +03:00
2015-04-30 08:05:11 +03:00
2015-10-21 02:56:29 +03:00
// settings within the session scope
param_t : : const_iterator si = params . find ( " context-weights " ) ;
2015-11-04 21:12:49 +03:00
if ( si ! = params . end ( ) ) SetContextWeights ( * m_scope , si - > second ) ;
2015-05-02 13:45:24 +03:00
Moses : : StaticData const & SD = Moses : : StaticData : : Instance ( ) ;
2015-04-30 08:05:11 +03:00
2015-12-11 13:12:54 +03:00
if ( is_syntax ( m_options - > search . algo ) )
2015-05-02 13:45:24 +03:00
run_chart_decoder ( ) ;
else
run_phrase_decoder ( ) ;
2015-04-30 08:05:11 +03:00
2015-03-04 03:07:11 +03:00
{
2015-05-02 13:45:24 +03:00
boost : : lock_guard < boost : : mutex > lock ( m_mutex ) ;
m_done = true ;
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
m_cond . notify_one ( ) ;
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
}
2015-03-04 03:07:11 +03:00
2015-05-02 13:45:24 +03:00
/// add phrase alignment information from a Hypothesis
void
TranslationRequest : :
add_phrase_aln_info ( Hypothesis const & h , vector < xmlrpc_c : : value > & aInfo ) const
{
2017-02-25 00:52:39 +03:00
if ( ! m_withAlignInfo ) return ;
// if (!options()->output.ReportSegmentation) return;
2015-10-25 16:37:59 +03:00
Range const & trg = h . GetCurrTargetWordsRange ( ) ;
Range const & src = h . GetCurrSourceWordsRange ( ) ;
2015-05-02 13:45:24 +03:00
std : : map < std : : string , xmlrpc_c : : value > pAlnInfo ;
pAlnInfo [ " tgt-start " ] = xmlrpc_c : : value_int ( trg . GetStartPos ( ) ) ;
2017-02-25 00:52:39 +03:00
pAlnInfo [ " tgt-end " ] = xmlrpc_c : : value_int ( trg . GetEndPos ( ) ) ;
2015-05-02 13:45:24 +03:00
pAlnInfo [ " src-start " ] = xmlrpc_c : : value_int ( src . GetStartPos ( ) ) ;
pAlnInfo [ " src-end " ] = xmlrpc_c : : value_int ( src . GetEndPos ( ) ) ;
aInfo . push_back ( xmlrpc_c : : value_struct ( pAlnInfo ) ) ;
}
2015-03-04 03:07:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
outputChartHypo ( ostream & out , const ChartHypothesis * hypo )
{
Phrase outPhrase ( 20 ) ;
hypo - > GetOutputPhrase ( outPhrase ) ;
// delete 1st & last
assert ( outPhrase . GetSize ( ) > = 2 ) ;
outPhrase . RemoveWord ( 0 ) ;
outPhrase . RemoveWord ( outPhrase . GetSize ( ) - 1 ) ;
for ( size_t pos = 0 ; pos < outPhrase . GetSize ( ) ; pos + + )
out < < * outPhrase . GetFactor ( pos , 0 ) < < " " ;
}
2015-03-04 03:07:11 +03:00
2015-05-02 13:45:24 +03:00
bool
TranslationRequest : :
compareSearchGraphNode ( const Moses : : SearchGraphNode & a ,
const Moses : : SearchGraphNode & b )
{
return a . hypo - > GetId ( ) < b . hypo - > GetId ( ) ;
}
void
TranslationRequest : :
insertGraphInfo ( Manager & manager , map < string , xmlrpc_c : : value > & retData )
{
using xmlrpc_c : : value_int ;
using xmlrpc_c : : value_double ;
using xmlrpc_c : : value_struct ;
using xmlrpc_c : : value_string ;
vector < xmlrpc_c : : value > searchGraphXml ;
vector < SearchGraphNode > searchGraph ;
manager . GetSearchGraph ( searchGraph ) ;
std : : sort ( searchGraph . begin ( ) , searchGraph . end ( ) ) ;
BOOST_FOREACH ( Moses : : SearchGraphNode const & n , searchGraph ) {
map < string , xmlrpc_c : : value > x ; // search graph xml node
x [ " forward " ] = value_double ( n . forward ) ;
x [ " fscore " ] = value_double ( n . fscore ) ;
const Hypothesis * hypo = n . hypo ;
x [ " hyp " ] = value_int ( hypo - > GetId ( ) ) ;
x [ " stack " ] = value_int ( hypo - > GetWordsBitmap ( ) . GetNumWordsCovered ( ) ) ;
if ( hypo - > GetId ( ) ! = 0 ) {
const Hypothesis * prevHypo = hypo - > GetPrevHypo ( ) ;
x [ " back " ] = value_int ( prevHypo - > GetId ( ) ) ;
x [ " score " ] = value_double ( hypo - > GetScore ( ) ) ;
x [ " transition " ] = value_double ( hypo - > GetScore ( ) - prevHypo - > GetScore ( ) ) ;
if ( n . recombinationHypo )
x [ " recombined " ] = value_int ( n . recombinationHypo - > GetId ( ) ) ;
x [ " cover-start " ] = value_int ( hypo - > GetCurrSourceWordsRange ( ) . GetStartPos ( ) ) ;
x [ " cover-end " ] = value_int ( hypo - > GetCurrSourceWordsRange ( ) . GetEndPos ( ) ) ;
2015-12-10 02:05:00 +03:00
x [ " out " ] = value_string ( hypo - > GetCurrTargetPhrase ( ) . GetStringRep ( options ( ) - > output . factor_order ) ) ;
2015-05-02 13:45:24 +03:00
}
searchGraphXml . push_back ( value_struct ( x ) ) ;
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
retData [ " sg " ] = xmlrpc_c : : value_array ( searchGraphXml ) ;
}
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
outputNBest ( const Manager & manager , map < string , xmlrpc_c : : value > & retData )
{
TrellisPathList nBestList ;
vector < xmlrpc_c : : value > nBestXml ;
2015-12-06 03:12:01 +03:00
2015-12-10 02:05:00 +03:00
Moses : : NBestOptions const & nbo = m_options - > nbest ;
2015-12-06 03:12:01 +03:00
manager . CalcNBest ( nbo . nbest_size , nBestList , nbo . only_distinct ) ;
manager . OutputNBest ( cout , nBestList ) ;
2015-05-02 13:45:24 +03:00
BOOST_FOREACH ( Moses : : TrellisPath const * path , nBestList ) {
vector < const Hypothesis * > const & E = path - > GetEdges ( ) ;
if ( ! E . size ( ) ) continue ;
std : : map < std : : string , xmlrpc_c : : value > nBestXmlItem ;
2015-11-17 17:06:48 +03:00
pack_hypothesis ( manager , E , " hyp " , nBestXmlItem ) ;
2015-05-02 13:45:24 +03:00
if ( m_withScoreBreakdown ) {
// should the score breakdown be reported in a more structured manner?
ostringstream buf ;
2015-12-06 03:12:01 +03:00
bool with_labels = nbo . include_feature_labels ;
2015-10-31 15:50:52 +03:00
path - > GetScoreBreakdown ( ) - > OutputAllFeatureScores ( buf , with_labels ) ;
2015-05-02 13:45:24 +03:00
nBestXmlItem [ " fvals " ] = xmlrpc_c : : value_string ( buf . str ( ) ) ;
2015-11-01 17:51:33 +03:00
nBestXmlItem [ " scores " ] = PackScores ( * path - > GetScoreBreakdown ( ) ) ;
2015-05-02 13:45:24 +03:00
}
// weighted score
2015-11-09 15:44:39 +03:00
nBestXmlItem [ " totalScore " ] = xmlrpc_c : : value_double ( path - > GetFutureScore ( ) ) ;
2015-05-02 13:45:24 +03:00
nBestXml . push_back ( xmlrpc_c : : value_struct ( nBestXmlItem ) ) ;
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
retData [ " nbest " ] = xmlrpc_c : : value_array ( nBestXml ) ;
}
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
insertTranslationOptions ( Moses : : Manager & manager ,
std : : map < std : : string , xmlrpc_c : : value > & retData )
{
2015-12-10 02:05:00 +03:00
std : : vector < Moses : : FactorType > const & ofactor_order = options ( ) - > output . factor_order ;
2015-12-06 03:12:01 +03:00
const TranslationOptionCollection * toptsColl = manager . getSntTranslationOptions ( ) ;
2015-05-02 13:45:24 +03:00
vector < xmlrpc_c : : value > toptsXml ;
size_t const stop = toptsColl - > GetSource ( ) . GetSize ( ) ;
TranslationOptionList const * tol ;
for ( size_t s = 0 ; s < stop ; + + s ) {
2015-12-06 03:12:01 +03:00
for ( size_t e = s ; ( tol = toptsColl - > GetTranslationOptionList ( s , e ) ) ! = NULL ; + + e ) {
2015-05-02 13:45:24 +03:00
BOOST_FOREACH ( TranslationOption const * topt , * tol ) {
std : : map < std : : string , xmlrpc_c : : value > toptXml ;
TargetPhrase const & tp = topt - > GetTargetPhrase ( ) ;
2015-12-06 03:12:01 +03:00
std : : string tphrase = tp . GetStringRep ( ofactor_order ) ;
2015-05-02 13:45:24 +03:00
toptXml [ " phrase " ] = xmlrpc_c : : value_string ( tphrase ) ;
toptXml [ " fscore " ] = xmlrpc_c : : value_double ( topt - > GetFutureScore ( ) ) ;
toptXml [ " start " ] = xmlrpc_c : : value_int ( s ) ;
toptXml [ " end " ] = xmlrpc_c : : value_int ( e ) ;
vector < xmlrpc_c : : value > scoresXml ;
const std : : valarray < FValue > & scores
2015-12-06 03:12:01 +03:00
= topt - > GetScoreBreakdown ( ) . getCoreFeatures ( ) ;
2015-05-02 13:45:24 +03:00
for ( size_t j = 0 ; j < scores . size ( ) ; + + j )
scoresXml . push_back ( xmlrpc_c : : value_double ( scores [ j ] ) ) ;
toptXml [ " scores " ] = xmlrpc_c : : value_array ( scoresXml ) ;
2017-01-02 21:52:01 +03:00
ostringstream buf ;
topt - > GetScoreBreakdown ( ) . OutputAllFeatureScores ( buf , true ) ;
toptXml [ " labelledScores " ] = PackScores ( topt - > GetScoreBreakdown ( ) ) ;
2015-05-02 13:45:24 +03:00
toptsXml . push_back ( xmlrpc_c : : value_struct ( toptXml ) ) ;
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
}
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
retData [ " topt " ] = xmlrpc_c : : value_array ( toptsXml ) ;
}
2015-03-18 16:46:15 +03:00
2015-05-02 13:45:24 +03:00
TranslationRequest : :
TranslationRequest ( xmlrpc_c : : paramList const & paramList ,
boost : : condition_variable & cond , boost : : mutex & mut )
: m_cond ( cond ) , m_mutex ( mut ) , m_done ( false ) , m_paramList ( paramList )
2015-08-02 23:28:47 +03:00
, m_session_id ( 0 )
2015-08-09 22:54:58 +03:00
{
2015-12-10 02:05:00 +03:00
2015-08-09 22:54:58 +03:00
}
2015-04-30 08:05:11 +03:00
2015-10-31 15:50:52 +03:00
bool
check ( std : : map < std : : string , xmlrpc_c : : value > const & param ,
std : : string const key )
{
2015-11-17 17:16:39 +03:00
std : : map < std : : string , xmlrpc_c : : value > : : const_iterator m = param . find ( key ) ;
if ( m = = param . end ( ) ) return false ;
2016-04-15 18:38:07 +03:00
if ( m - > second . type ( ) = = xmlrpc_c : : value : : TYPE_BOOLEAN )
return xmlrpc_c : : value_boolean ( m - > second ) ;
2015-11-17 17:16:39 +03:00
std : : string val = string ( xmlrpc_c : : value_string ( m - > second ) ) ;
if ( val = = " true " | | val = = " True " | | val = = " TRUE " | | val = = " 1 " ) return true ;
return false ;
2015-10-31 15:50:52 +03:00
}
2015-10-21 02:56:29 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
parse_request ( std : : map < std : : string , xmlrpc_c : : value > const & params )
{
// parse XMLRPC request
m_paramList . verifyEnd ( 1 ) ; // ??? UG
2015-08-07 21:17:37 +03:00
2015-05-02 13:45:24 +03:00
typedef std : : map < std : : string , xmlrpc_c : : value > params_t ;
2015-12-10 02:05:00 +03:00
params_t : : const_iterator si ;
si = params . find ( " session-id " ) ;
if ( si ! = params . end ( ) )
{
m_session_id = xmlrpc_c : : value_int ( si - > second ) ;
Session const & S = m_translator - > get_session ( m_session_id ) ;
m_scope = S . scope ;
m_session_id = S . id ;
}
else
{
m_session_id = 0 ;
m_scope . reset ( new Moses : : ContextScope ) ;
}
2015-12-10 06:17:36 +03:00
boost : : shared_ptr < Moses : : AllOptions > opts ( new Moses : : AllOptions ( * StaticData : : Instance ( ) . options ( ) ) ) ;
2015-12-10 02:05:00 +03:00
opts - > update ( params ) ;
m_withGraphInfo = check ( params , " sg " ) ;
if ( m_withGraphInfo | | opts - > nbest . nbest_size > 0 ) {
opts - > output . SearchGraph = " true " ;
opts - > nbest . enabled = true ;
}
m_options = opts ;
// source text must be given, or we don't know what to translate
si = params . find ( " text " ) ;
2015-05-02 13:45:24 +03:00
if ( si = = params . end ( ) )
throw xmlrpc_c : : fault ( " Missing source text " , xmlrpc_c : : fault : : CODE_PARSE ) ;
m_source_string = xmlrpc_c : : value_string ( si - > second ) ;
XVERBOSE ( 1 , " Input: " < < m_source_string < < endl ) ;
2015-10-21 02:56:29 +03:00
2015-05-02 13:45:24 +03:00
m_withTopts = check ( params , " topt " ) ;
m_withScoreBreakdown = check ( params , " add-score-breakdown " ) ;
si = params . find ( " lambda " ) ;
2015-08-02 23:28:47 +03:00
if ( si ! = params . end ( ) )
{
// muMo = multiModel
xmlrpc_c : : value_array muMoArray = xmlrpc_c : : value_array ( si - > second ) ;
vector < xmlrpc_c : : value > muMoValVec ( muMoArray . vectorValueValue ( ) ) ;
vector < float > w ( muMoValVec . size ( ) ) ;
for ( size_t i = 0 ; i < muMoValVec . size ( ) ; + + i )
w [ i ] = xmlrpc_c : : value_double ( muMoValVec [ i ] ) ;
if ( w . size ( ) & & ( si = params . find ( " model_name " ) ) ! = params . end ( ) )
{
string const model_name = xmlrpc_c : : value_string ( si - > second ) ;
PhraseDictionaryMultiModel * pdmm
= ( PhraseDictionaryMultiModel * ) FindPhraseDictionary ( model_name ) ;
pdmm - > SetTemporaryMultiModelWeightsVector ( w ) ;
}
2015-05-02 13:45:24 +03:00
}
2015-08-02 23:28:47 +03:00
2015-06-24 16:34:27 +03:00
si = params . find ( " context " ) ;
2015-08-02 23:28:47 +03:00
if ( si ! = params . end ( ) )
{
string context = xmlrpc_c : : value_string ( si - > second ) ;
VERBOSE ( 1 , " CONTEXT " < < context ) ;
m_context . reset ( new std : : vector < std : : string > ( 1 , context ) ) ;
}
2015-10-31 15:50:52 +03:00
2016-11-12 20:28:26 +03:00
si = params . find ( " context-scope " ) ;
if ( si ! = params . end ( ) )
{
string context = xmlrpc_c : : value_string ( si - > second ) ;
string groupSeparator ( " Moses::ContextScope::GroupSeparator " ) ;
string recordSeparator ( " Moses::ContextScope::RecordSeparator " ) ;
// Here, we assume that any XML-RPC value
// associated with the key "context-scope"
// has the following format:
//
// FeatureFunctionName followed by recordSeparator
// followed by the value of interest
// followed by groupSeparator
//
// In the following code, the value of interest will be stored
// in contextScope under the key FeatureFunctionName,
// where FeatureFunctionName is the actual name of the feature function
boost : : shared_ptr < Moses : : ContextScope > contextScope = GetScope ( ) ;
BOOST_FOREACH ( string group , TokenizeMultiCharSeparator ( context , groupSeparator ) ) {
vector < string > record = TokenizeMultiCharSeparator ( group , recordSeparator ) ;
// Use the feature function whose name is record[0] as a key
FeatureFunction & ff = Moses : : FeatureFunction : : FindFeatureFunction ( record [ 0 ] ) ;
void const * key = static_cast < void const * > ( & ff ) ;
// Store (in the context scope) record[1] as the value associated with that key
boost : : shared_ptr < string > value = contextScope - > get < string > ( key , true ) ;
value - > replace ( value - > begin ( ) , value - > end ( ) , record [ 1 ] ) ;
}
}
2017-02-25 00:52:39 +03:00
// Report alignment info if Moses config says to or if XML request says to
m_withAlignInfo = options ( ) - > output . ReportSegmentation | | check ( params , " align " ) ;
// Report word alignment info if Moses config says to or if XML request says to
m_withWordAlignInfo = options ( ) - > output . PrintAlignmentInfo | | check ( params , " word-align " ) ;
2016-11-14 22:26:34 +03:00
si = params . find ( " weights " ) ;
if ( si ! = params . end ( ) )
{
boost : : unordered_map < string , FeatureFunction * > map ;
{
const vector < FeatureFunction * > & ffs = FeatureFunction : : GetFeatureFunctions ( ) ;
BOOST_FOREACH ( FeatureFunction * const & ff , ffs ) {
map [ ff - > GetScoreProducerDescription ( ) ] = ff ;
}
}
string allValues = xmlrpc_c : : value_string ( si - > second ) ;
BOOST_FOREACH ( string values , TokenizeMultiCharSeparator ( allValues , " \t " ) ) {
vector < string > record = TokenizeMultiCharSeparator ( values , " = " ) ;
if ( record . size ( ) = = 2 ) {
string featureName = record [ 0 ] ;
string featureWeights = record [ 1 ] ;
boost : : unordered_map < string , FeatureFunction * > : : iterator ffi = map . find ( featureName ) ;
if ( ffi ! = map . end ( ) ) {
FeatureFunction * ff = ffi - > second ;
size_t prevNumWeights = ff - > GetNumScoreComponents ( ) ;
vector < float > ffWeights ;
BOOST_FOREACH ( string weight , TokenizeMultiCharSeparator ( featureWeights , " " ) ) {
ffWeights . push_back ( Scan < float > ( weight ) ) ;
}
if ( ffWeights . size ( ) = = ff - > GetNumScoreComponents ( ) ) {
// XXX: This is NOT thread-safe
Moses : : StaticData : : InstanceNonConst ( ) . SetWeights ( ff , ffWeights ) ;
VERBOSE ( 1 , " WARNING: THIS IS NOT THREAD-SAFE! \t Updating weights for " < < featureName < < " to " < < featureWeights < < " \n " ) ;
} else {
TRACE_ERR ( " ERROR: Unable to update weights for " < < featureName < < " because " < < ff - > GetNumScoreComponents ( ) < < " weights are required but only " < < ffWeights . size ( ) < < " were provided \n " ) ;
}
} else {
TRACE_ERR ( " ERROR: No FeatureFunction with name " < < featureName < < " , no weight update \n " ) ;
}
} else {
TRACE_ERR ( " WARNING: XML-RPC weights update was improperly formatted: \t " < < values < < " \n " ) ;
}
}
}
2015-10-31 15:50:52 +03:00
2015-05-02 13:45:24 +03:00
// // biased sampling for suffix-array-based sampling phrase table?
// if ((si = params.find("bias")) != params.end())
// {
// std::vector<xmlrpc_c::value> tmp
// = xmlrpc_c::value_array(si->second).cvalue();
// for (size_t i = 1; i < tmp.size(); i += 2)
// m_bias[xmlrpc_c::value_int(tmp[i-1])] = xmlrpc_c::value_double(tmp[i]);
// }
2016-02-01 19:26:58 +03:00
if ( is_syntax ( m_options - > search . algo ) ) {
2016-06-29 02:29:26 +03:00
m_source . reset ( new Sentence ( m_options , 0 , m_source_string ) ) ;
2016-02-01 19:26:58 +03:00
} else {
m_source . reset ( new Sentence ( m_options , 0 , m_source_string ) ) ;
}
2016-11-08 22:45:33 +03:00
interpret_dlt ( ) ;
2015-05-02 13:45:24 +03:00
} // end of Translationtask::parse_request()
2015-03-04 03:07:11 +03:00
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
run_chart_decoder ( )
{
Moses : : ChartManager manager ( this - > self ( ) ) ;
manager . Decode ( ) ;
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
const Moses : : ChartHypothesis * hypo = manager . GetBestHypothesis ( ) ;
ostringstream out ;
2016-01-29 20:13:03 +03:00
if ( hypo ) outputChartHypo ( out , hypo ) ;
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
m_target_string = out . str ( ) ;
m_retData [ " text " ] = xmlrpc_c : : value_string ( m_target_string ) ;
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
if ( m_withGraphInfo ) {
std : : ostringstream sgstream ;
manager . OutputSearchGraphMoses ( sgstream ) ;
m_retData [ " sg " ] = xmlrpc_c : : value_string ( sgstream . str ( ) ) ;
}
} // end of TranslationRequest::run_chart_decoder()
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
2015-12-06 04:43:17 +03:00
pack_hypothesis ( const Moses : : Manager & manager ,
vector < Hypothesis const * > const & edges , string const & key ,
2015-05-02 13:45:24 +03:00
map < string , xmlrpc_c : : value > & dest ) const
{
// target string
ostringstream target ;
2015-11-19 19:59:15 +03:00
BOOST_REVERSE_FOREACH ( Hypothesis const * e , edges ) {
2015-12-02 23:43:56 +03:00
manager . OutputSurface ( target , * e ) ;
2015-11-19 19:59:15 +03:00
}
2015-12-06 03:12:01 +03:00
XVERBOSE ( 1 , " BEST TRANSLATION: " < < * ( manager . GetBestHypothesis ( ) )
< < std : : endl ) ;
2015-05-02 13:45:24 +03:00
dest [ key ] = xmlrpc_c : : value_string ( target . str ( ) ) ;
2015-03-04 03:07:11 +03:00
2017-02-25 00:52:39 +03:00
if ( m_withAlignInfo ) {
// if (options()->output.ReportSegmentation) {
2015-05-02 13:45:24 +03:00
// phrase alignment, if requested
2015-03-04 03:07:11 +03:00
2015-05-02 13:45:24 +03:00
vector < xmlrpc_c : : value > p_aln ;
BOOST_REVERSE_FOREACH ( Hypothesis const * e , edges )
2015-11-18 18:21:50 +03:00
add_phrase_aln_info ( * e , p_aln ) ;
2015-05-02 13:45:24 +03:00
dest [ " align " ] = xmlrpc_c : : value_array ( p_aln ) ;
2015-03-04 03:07:11 +03:00
}
2017-02-25 00:52:39 +03:00
if ( m_withWordAlignInfo ) {
//if (options()->output.PrintAlignmentInfo) {
2015-05-02 13:45:24 +03:00
// word alignment, if requested
vector < xmlrpc_c : : value > w_aln ;
2015-11-24 23:42:53 +03:00
BOOST_REVERSE_FOREACH ( Hypothesis const * e , edges )
2015-11-18 18:21:50 +03:00
e - > OutputLocalWordAlignment ( w_aln ) ;
2015-05-02 13:45:24 +03:00
dest [ " word-align " ] = xmlrpc_c : : value_array ( w_aln ) ;
2015-03-04 03:07:11 +03:00
}
2015-05-02 13:45:24 +03:00
}
2015-03-04 03:07:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
2015-11-17 17:16:39 +03:00
pack_hypothesis ( const Moses : : Manager & manager , Hypothesis const * h , string const & key ,
2015-05-02 13:45:24 +03:00
map < string , xmlrpc_c : : value > & dest ) const
{
using namespace std ;
vector < Hypothesis const * > edges ;
for ( ; h ; h = h - > GetPrevHypo ( ) )
edges . push_back ( h ) ;
2015-11-17 17:16:39 +03:00
pack_hypothesis ( manager , edges , key , dest ) ;
2015-05-02 13:45:24 +03:00
}
2015-03-04 03:07:11 +03:00
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
void
TranslationRequest : :
run_phrase_decoder ( )
{
Manager manager ( this - > self ( ) ) ;
manager . Decode ( ) ;
2015-11-17 17:06:48 +03:00
pack_hypothesis ( manager , manager . GetBestHypothesis ( ) , " text " , m_retData ) ;
2015-08-03 02:29:34 +03:00
if ( m_session_id )
2015-08-04 03:59:28 +03:00
m_retData [ " session-id " ] = xmlrpc_c : : value_int ( m_session_id ) ;
2015-08-03 02:29:34 +03:00
2015-05-02 13:45:24 +03:00
if ( m_withGraphInfo ) insertGraphInfo ( manager , m_retData ) ;
if ( m_withTopts ) insertTranslationOptions ( manager , m_retData ) ;
2015-12-10 02:05:00 +03:00
if ( m_options - > nbest . nbest_size ) outputNBest ( manager , m_retData ) ;
2015-04-30 08:05:11 +03:00
2015-05-02 13:45:24 +03:00
}
2015-03-04 03:07:11 +03:00
}