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

mosesserver.cpp « server « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5609fac3006ef7ce616efb7321f0373030c4c8da (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include "util/check.hh"
#include <stdexcept>
#include <iostream>

#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>

#include "Hypothesis.h"
#include "Manager.h"
#include "StaticData.h"
#include "PhraseDictionaryDynSuffixArray.h"
#include "TranslationSystem.h"
#include "LMList.h"
#ifdef LM_ORLM
#  include "LanguageModelORLM.h"
#endif
#include <boost/algorithm/string.hpp>
#include "BleuScoreFeature.h"

using namespace Moses;
using namespace std;

typedef std::map<std::string, xmlrpc_c::value> params_t;

/** Find out which translation system to use */
const TranslationSystem& getTranslationSystem(params_t params)
{
  string system_id = TranslationSystem::DEFAULT;
  params_t::const_iterator pi = params.find("system");
  if (pi != params.end()) {
    system_id = xmlrpc_c::value_string(pi->second);
  }
  VERBOSE(1, "Using translation system " << system_id << endl;)
  return StaticData::Instance().GetTranslationSystem(system_id);
}

class Updater: public xmlrpc_c::method
{
public:
  Updater() {
    // signature and help strings are documentation -- the client
    // can query this information with a system.methodSignature and
    // system.methodHelp RPC.
    this->_signature = "S:S";
    this->_help = "Updates stuff";
  }
  void
  execute(xmlrpc_c::paramList const& paramList,
          xmlrpc_c::value *   const  retvalP) {
    const params_t params = paramList.getStruct(0);
    breakOutParams(params);
    const TranslationSystem& system = getTranslationSystem(params);
    const PhraseDictionaryFeature* pdf = system.GetPhraseDictionaries()[0];
    PhraseDictionaryDynSuffixArray* pdsa = (PhraseDictionaryDynSuffixArray*) pdf->GetDictionary();
    cerr << "Inserting into address " << pdsa << endl;
    pdsa->insertSnt(source_, target_, alignment_);
    if(add2ORLM_) {       
      updateORLM();
    }
    cerr << "Done inserting\n";
    //PhraseDictionary* pdsa = (PhraseDictionary*) pdf->GetDictionary(*dummy);
    map<string, xmlrpc_c::value> retData;
    //*retvalP = xmlrpc_c::value_struct(retData);
    pdf = 0;
    pdsa = 0;
    *retvalP = xmlrpc_c::value_string("Phrase table updated");
  }
  string source_, target_, alignment_;
  bool bounded_, add2ORLM_;
  void updateORLM() {
#ifdef LM_ORLM
    vector<string> vl;
    map<vector<string>, int> ngSet;
    LMList lms = StaticData::Instance().GetLMList(); // get LM
    LMList::const_iterator lmIter = lms.begin();
    const LanguageModel* lm = *lmIter; 
    /* currently assumes a single LM that is a ORLM */
#ifdef WITH_THREADS
    boost::shared_ptr<LanguageModelORLM> orlm; 
    orlm = boost::dynamic_pointer_cast<LanguageModelORLM>(lm->GetLMImplementation()); 
#else 
    LanguageModelORLM* orlm; 
    orlm = (LanguageModelORLM*)lm->GetLMImplementation(); 
#endif
    if(orlm == 0) {
      cerr << "WARNING: Unable to add target sentence to ORLM\n";
      return;
    }
    // break out new ngrams from sentence
    const int ngOrder(orlm->GetNGramOrder());
    const std::string sBOS = orlm->GetSentenceStart()->GetString();
    const std::string sEOS = orlm->GetSentenceEnd()->GetString();
    Utils::splitToStr(target_, vl, " ");
    // insert BOS and EOS 
    vl.insert(vl.begin(), sBOS); 
    vl.insert(vl.end(), sEOS);
    for(int j=0; j < vl.size(); ++j) {
      int i = (j<ngOrder) ? 0 : j-ngOrder+1;
      for(int t=j; t >= i; --t) {
        vector<string> ngVec;
        for(int s=t; s<=j; ++s) {
          ngVec.push_back(vl[s]);
          //cerr << vl[s] << " ";
        }
        ngSet[ngVec]++;
        //cerr << endl;
      }
    }
    // insert into LM in order from 1grams up (for LM well-formedness)
    cerr << "Inserting " << ngSet.size() << " ngrams into ORLM...\n";
    for(int i=1; i <= ngOrder; ++i) {
      iterate(ngSet, it) {
        if(it->first.size() == i)
          orlm->UpdateORLM(it->first, it->second);
      }
    }
#endif
  }
  void breakOutParams(const params_t& params) {
    params_t::const_iterator si = params.find("source");
    if(si == params.end())
      throw xmlrpc_c::fault("Missing source sentence", xmlrpc_c::fault::CODE_PARSE);
    source_ = xmlrpc_c::value_string(si->second);
    cerr << "source = " << source_ << endl;
    si = params.find("target");
    if(si == params.end())
      throw xmlrpc_c::fault("Missing target sentence", xmlrpc_c::fault::CODE_PARSE);
    target_ = xmlrpc_c::value_string(si->second);
    cerr << "target = " << target_ << endl;
    si = params.find("alignment");
    if(si == params.end())
      throw xmlrpc_c::fault("Missing alignment", xmlrpc_c::fault::CODE_PARSE);
    alignment_ = xmlrpc_c::value_string(si->second);
    cerr << "alignment = " << alignment_ << endl;
    si = params.find("bounded");
    bounded_ = (si != params.end());
    si = params.find("updateORLM");
    add2ORLM_ = (si != params.end());
  }
};

class Translator : public xmlrpc_c::method
{
public:
  Translator() {
    // signature and help strings are documentation -- the client
    // can query this information with a system.methodSignature and
    // system.methodHelp RPC.
    this->_signature = "S:S";
    this->_help = "Does translation";

    const StaticData &staticData = StaticData::Instance();
    m_bleuScoreFeature = staticData.GetBleuScoreFeature();
  }

  BleuScoreFeature *m_bleuScoreFeature;

  void execute(xmlrpc_c::paramList const& paramList,
          xmlrpc_c::value * const  retvalP) {
    const params_t params = paramList.getStruct(0);
    paramList.verifyEnd(1);
    params_t::const_iterator si = params.find("text");
    if (si == params.end()) {
      throw xmlrpc_c::fault(
        "Missing source text",
        xmlrpc_c::fault::CODE_PARSE);
    }
    const string source(
      (xmlrpc_c::value_string(si->second)));

    si = params.find("id");
    if (si == params.end()) {
      throw xmlrpc_c::fault("Missing sentence id",
			    xmlrpc_c::fault::CODE_PARSE);
    }
    int sentenceid = xmlrpc_c::value_int(si->second);
    if (sentenceid < -1) {
      throw xmlrpc_c::fault("Sentence id has to be >= 0",
                            xmlrpc_c::fault::CODE_PARSE);
    }

    cerr << "Loading reference info for sentence " << sentenceid << endl;
    vector<string> sourceTokens;
    boost::split(sourceTokens, source, boost::is_any_of("\t "));
    m_bleuScoreFeature->SetCurrSourceLength(sourceTokens.size());
    bool avgRefLength = false;
    if (avgRefLength)
      m_bleuScoreFeature->SetCurrAvgRefLength(sentenceid);
    else
      m_bleuScoreFeature->SetCurrShortestRefLength(sentenceid);
    m_bleuScoreFeature->SetCurrReferenceNgrams(sentenceid);

    cerr << "Input: " << source;
    si = params.find("align");
    bool addAlignInfo = (si != params.end());
    si = params.find("sg");
    bool addGraphInfo = (si != params.end());
    si = params.find("topt");
    bool addTopts = (si != params.end());
    si = params.find("report-all-factors");
    bool reportAllFactors = (si != params.end());

    const StaticData &staticData = StaticData::Instance();

    if (addGraphInfo) {
      (const_cast<StaticData&>(staticData)).SetOutputSearchGraph(true);
    }

    const TranslationSystem& system = getTranslationSystem(params);
    
    Sentence sentence;
    const vector<FactorType> &inputFactorOrder =
      staticData.GetInputFactorOrder();
    stringstream in(source + "\n");
    sentence.Read(in,inputFactorOrder);
    Manager manager(sentence,staticData.GetSearchAlgorithm(), &system);
    manager.ProcessSentence();
    const Hypothesis* hypo = manager.GetBestHypothesis();

    vector<xmlrpc_c::value> alignInfo;
    stringstream out, graphInfo, transCollOpts;
    outputHypo(out,hypo,addAlignInfo,alignInfo,reportAllFactors);

    map<string, xmlrpc_c::value> retData;
    pair<string, xmlrpc_c::value>
    text("text", xmlrpc_c::value_string(out.str()));
    cerr << "\nOutput: " << out.str() << endl << endl;
    if (addAlignInfo) {
      retData.insert(pair<string, xmlrpc_c::value>("align", xmlrpc_c::value_array(alignInfo)));
    }
    retData.insert(text);

    if(addGraphInfo) {
      insertGraphInfo(manager,retData);
      (const_cast<StaticData&>(staticData)).SetOutputSearchGraph(false);
    }
    if (addTopts) {
      insertTranslationOptions(manager,retData);
    }
    *retvalP = xmlrpc_c::value_struct(retData);
  }

  void outputHypo(ostream& out, const Hypothesis* hypo, bool addAlignmentInfo, vector<xmlrpc_c::value>& alignInfo, bool reportAllFactors = false) {
    if (hypo->GetPrevHypo() != NULL) {
      outputHypo(out,hypo->GetPrevHypo(),addAlignmentInfo, alignInfo, reportAllFactors);
      Phrase p = hypo->GetCurrTargetPhrase();
      if(reportAllFactors) {
        out << p << " ";
      } else {
        for (size_t pos = 0 ; pos < p.GetSize() ; pos++) {
          const Factor *factor = p.GetFactor(pos, 0);
          out << *factor << " ";
        }
      }

      if (addAlignmentInfo) {
        /**
         * Add the alignment info to the array. This is in target order and consists of
         *       (tgt-start, src-start, src-end) triples.
         **/
        map<string, xmlrpc_c::value> phraseAlignInfo;
        phraseAlignInfo["tgt-start"] = xmlrpc_c::value_int(hypo->GetCurrTargetWordsRange().GetStartPos());
        phraseAlignInfo["src-start"] = xmlrpc_c::value_int(hypo->GetCurrSourceWordsRange().GetStartPos());
        phraseAlignInfo["src-end"] = xmlrpc_c::value_int(hypo->GetCurrSourceWordsRange().GetEndPos());
        alignInfo.push_back(xmlrpc_c::value_struct(phraseAlignInfo));
      }
    }
  }

  void insertGraphInfo(Manager& manager, map<string, xmlrpc_c::value>& retData) {
    vector<xmlrpc_c::value> searchGraphXml;
    vector<SearchGraphNode> searchGraph;
    manager.GetSearchGraph(searchGraph);
    for (vector<SearchGraphNode>::const_iterator i = searchGraph.begin(); i != searchGraph.end(); ++i) {
      map<string, xmlrpc_c::value> searchGraphXmlNode;
      searchGraphXmlNode["forward"] = xmlrpc_c::value_double(i->forward);
      searchGraphXmlNode["fscore"] = xmlrpc_c::value_double(i->fscore);
      const Hypothesis* hypo = i->hypo;
      searchGraphXmlNode["hyp"] = xmlrpc_c::value_int(hypo->GetId());
      searchGraphXmlNode["stack"] = xmlrpc_c::value_int(hypo->GetWordsBitmap().GetNumWordsCovered());
      if (hypo->GetId() != 0) {
        const Hypothesis *prevHypo = hypo->GetPrevHypo();
        searchGraphXmlNode["back"] = xmlrpc_c::value_int(prevHypo->GetId());
        searchGraphXmlNode["score"] = xmlrpc_c::value_double(hypo->GetScore());
        searchGraphXmlNode["transition"] = xmlrpc_c::value_double(hypo->GetScore() - prevHypo->GetScore());
        if (i->recombinationHypo) {
          searchGraphXmlNode["recombined"] = xmlrpc_c::value_int(i->recombinationHypo->GetId());
        }
        searchGraphXmlNode["cover-start"] = xmlrpc_c::value_int(hypo->GetCurrSourceWordsRange().GetStartPos());
        searchGraphXmlNode["cover-end"] = xmlrpc_c::value_int(hypo->GetCurrSourceWordsRange().GetEndPos());
	searchGraphXmlNode["src-phrase"] = xmlrpc_c::value_string(hypo->GetSourcePhraseStringRep());
	searchGraphXmlNode["tgt-phrase"] = xmlrpc_c::value_string(hypo->GetTargetPhraseStringRep());
	ScoreComponentCollection scoreBreakdown = hypo->GetScoreBreakdown();
	scoreBreakdown.MinusEquals(prevHypo->GetScoreBreakdown());
        stringstream tmp;
	tmp << scoreBreakdown;
        searchGraphXmlNode["scores"] = xmlrpc_c::value_string(tmp.str());
      }
      searchGraphXml.push_back(xmlrpc_c::value_struct(searchGraphXmlNode));
    }
    retData.insert(pair<string, xmlrpc_c::value>("sg", xmlrpc_c::value_array(searchGraphXml)));
  }

  void insertTranslationOptions(Manager& manager, map<string, xmlrpc_c::value>& retData) {
    const TranslationOptionCollection* toptsColl = manager.getSntTranslationOptions();
    vector<xmlrpc_c::value> toptsXml;
    for (size_t startPos = 0 ; startPos < toptsColl->GetSize() ; ++startPos) {
      size_t maxSize = toptsColl->GetSize() - startPos;
      size_t maxSizePhrase = StaticData::Instance().GetMaxPhraseLength();
      maxSize = std::min(maxSize, maxSizePhrase);

      for (size_t endPos = startPos ; endPos < startPos + maxSize ; ++endPos) {
        WordsRange range(startPos,endPos);
        const TranslationOptionList& fullList = toptsColl->GetTranslationOptionList(range);
        for (size_t i = 0; i < fullList.size(); i++) {
          const TranslationOption* topt = fullList.Get(i);
          map<string, xmlrpc_c::value> toptXml;
          toptXml["phrase"] = xmlrpc_c::value_string(topt->GetTargetPhrase().
                              GetStringRep(StaticData::Instance().GetOutputFactorOrder()));
          toptXml["fscore"] = xmlrpc_c::value_double(topt->GetFutureScore());
          toptXml["start"] =  xmlrpc_c::value_int(startPos);
          toptXml["end"] =  xmlrpc_c::value_int(endPos);
          vector<xmlrpc_c::value> scoresXml;
          cerr << "Warning: not adding scores to translation options.." << endl;
          ScoreComponentCollection scores = topt->GetScoreBreakdown();
//          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);
          toptsXml.push_back(xmlrpc_c::value_struct(toptXml));
        }
      }
    }
    retData.insert(pair<string, xmlrpc_c::value>("topt", xmlrpc_c::value_array(toptsXml)));
  }
};

const char* ffNames[] = { "Distortion", "WordPenalty", "!UnknownWordPenalty 1", "LexicalReordering_wbe-msd-bidirectional-fe-allff_1",
		"LexicalReordering_wbe-msd-bidirectional-fe-allff_2", "LexicalReordering_wbe-msd-bidirectional-fe-allff_3", 
		"LexicalReordering_wbe-msd-bidirectional-fe-allff_4", "LexicalReordering_wbe-msd-bidirectional-fe-allff_5", 
		"LexicalReordering_wbe-msd-bidirectional-fe-allff_6", "LM", "PhraseModel_1", "PhraseModel_2", "PhraseModel_3",
		"PhraseModel_4", "PhraseModel_5", "BleuScoreFeature" };

//const char* ffNames[] = { "Distortion", "WordPenalty", "!UnknownWordPenalty 1", "LM", "PhraseModel_1", "PhraseModel_2" };

bool setCoreWeights(ScoreComponentCollection &weights, vector<string> &coreWeightVector, const vector<const ScoreProducer*> &featureFunctions) {
  vector< float > store_weights;
  for (size_t i=0; i<coreWeightVector.size(); ++i) {
    string name(ffNames[i]);
    float weight = Scan<float>(coreWeightVector[i]);  
    
    VERBOSE(1, "loading core weight " << name << endl);
    for (size_t i=0; i < featureFunctions.size(); ++i) {
      std::string prefix = featureFunctions[i]->GetScoreProducerDescription();
      if (name.substr( 0, prefix.length() ).compare( prefix ) == 0) {
	size_t numberScoreComponents = featureFunctions[i]->GetNumScoreComponents();
	if (numberScoreComponents == 1) {
	  VERBOSE(1, "assign 1 weight for " << featureFunctions[i]->GetScoreProducerDescription());
	  VERBOSE(1, " (" << weight << ")" << endl << endl);
	  weights.Assign(featureFunctions[i], weight);
  	      }
	else {
	  store_weights.push_back(weight);
	  if (store_weights.size() == numberScoreComponents) {
	    VERBOSE(1, "assign " << store_weights.size() << " weights for " << featureFunctions[i]->GetScoreProducerDescription() << " (");
	    for (size_t j=0; j < store_weights.size(); ++j)
	      VERBOSE(1, store_weights[j] << " ");
	    VERBOSE(1, ")" << endl << endl);
	    weights.Assign(featureFunctions[i], store_weights);
	    store_weights.clear();
	  }			
	}
      }
    }
  }
  return true;
}

class WeightSetter: public xmlrpc_c::method
{	
public:
  WeightSetter() {
    // signature and help strings are documentation -- the client
    // can query this information with a system.methodSignature and
    // system.methodHelp RPC.
    this->_signature = "S:S";
    this->_help = "Sets Moses weights";
  }
  
  void execute(xmlrpc_c::paramList const& paramList,
          xmlrpc_c::value * const  retvalP) {
    const params_t params = paramList.getStruct(0);
    paramList.verifyEnd(1);
    
    ScoreComponentCollection weightUpdate;
    
    params_t::const_iterator si = params.find("core-weights");
    string coreWeights;
    if (si == params.end()) {
      throw xmlrpc_c::fault(
        "Missing core weights",
        xmlrpc_c::fault::CODE_PARSE);
    }
    coreWeights = xmlrpc_c::value_string(si->second);
    VERBOSE(1, "core weights: " << coreWeights << endl);

    StaticData &staticData = StaticData::InstanceNonConst();
    const vector<const ScoreProducer*> featureFunctions = staticData.GetTranslationSystem(TranslationSystem::DEFAULT).GetFeatureFunctions();
    vector<string> coreWeightVector;
    boost::split(coreWeightVector, coreWeights, boost::is_any_of(","));
    setCoreWeights(weightUpdate, coreWeightVector, featureFunctions);
	    
    si = params.find("sparse-weights");
    string sparseWeights;
    if (si != params.end()) {
      sparseWeights = xmlrpc_c::value_string(si->second);
      VERBOSE(1, "sparse weights: " << sparseWeights << endl);
      
      if (sparseWeights != "") {
	vector<string> sparseWeightVector;
	boost::split(sparseWeightVector, sparseWeights, boost::is_any_of("\t "));
	for(size_t i=0; i<sparseWeightVector.size(); ++i) {
	  vector<string> name_value; 
	  boost::split(name_value, sparseWeightVector[i], boost::is_any_of("="));
	  if (name_value.size() > 2) {
	    string tmp1 = name_value[name_value.size()-1];
	    name_value.erase(name_value.end());
	    string tmp2 = boost::algorithm::join(name_value, "=");
	    name_value[0] = tmp2;
	    name_value[1] = tmp1;
	  }
	  const string name(name_value[0]);
	  float value = Scan<float>(name_value[1]);
	  VERBOSE(1, "Setting sparse weight " << name << " to value " << value << "." << endl);
	  weightUpdate.Assign(name, value);
	}
      }
    }

    // Set moses weights to new weights
    staticData.SetAllWeights(weightUpdate);
    cerr << "\nSet weights: " << staticData.GetAllWeights() << endl;
    *retvalP = xmlrpc_c::value_string("Weights set!");
  }
};

class WeightAdder: public xmlrpc_c::method
{	
public:
  WeightAdder() {
    // signature and help strings are documentation -- the client
    // can query this information with a system.methodSignature and
    // system.methodHelp RPC.
    this->_signature = "S:S";
    this->_help = "Adds an update to Moses weights";
  }
  
  void execute(xmlrpc_c::paramList const& paramList,
          xmlrpc_c::value * const  retvalP) {
    const params_t params = paramList.getStruct(0);
    paramList.verifyEnd(1);
    
    ScoreComponentCollection weightUpdate;
    
    params_t::const_iterator si = params.find("core-weights");
    string coreWeights;
    if (si == params.end()) {
      throw xmlrpc_c::fault(
        "Missing core weights",
        xmlrpc_c::fault::CODE_PARSE);
    }
    coreWeights = xmlrpc_c::value_string(si->second);
    VERBOSE(1, "core weights: " << coreWeights << endl);

    StaticData &staticData = StaticData::InstanceNonConst();
    const vector<const ScoreProducer*> featureFunctions = staticData.GetTranslationSystem(TranslationSystem::DEFAULT).GetFeatureFunctions();
    vector<string> coreWeightVector;
    boost::split(coreWeightVector, coreWeights, boost::is_any_of(","));
    setCoreWeights(weightUpdate, coreWeightVector, featureFunctions);
	    
    si = params.find("sparse-weights");
    string sparseWeights;
    if (si != params.end()) {
      sparseWeights = xmlrpc_c::value_string(si->second);
      VERBOSE(1, "sparse weights: " << sparseWeights << endl);
      
      if (sparseWeights != "") {
	vector<string> sparseWeightVector;
	boost::split(sparseWeightVector, sparseWeights, boost::is_any_of("\t "));
	for(size_t i=0; i<sparseWeightVector.size(); ++i) {
	  vector<string> name_value; 
	  boost::split(name_value, sparseWeightVector[i], boost::is_any_of("="));
	  if (name_value.size() > 2) {
	    string tmp1 = name_value[name_value.size()-1];
	    name_value.erase(name_value.end());
	    string tmp2 = boost::algorithm::join(name_value, "=");
	    name_value[0] = tmp2;
	    name_value[1] = tmp1;
	  }
	  const string name(name_value[0]);
	  float value = Scan<float>(name_value[1]);
	  VERBOSE(1, "Setting sparse weight " << name << " to value " << value << "." << endl);
	  weightUpdate.Assign(name, value);
	}
      }
    }

    // add new weights to Moses weights and write back to staticData
    weightUpdate.PlusEquals(staticData.GetAllWeights());
    staticData.SetAllWeights(weightUpdate);
    cerr << "\nUpdated weights: " << staticData.GetAllWeights() << endl;
    *retvalP = xmlrpc_c::value_string("Weights updated!");
  }
};

/**
  * Allocates a char* and copies string into it.
**/
static char* strToChar(const string& s) {
  char* c = new char[s.size()+1];
  strcpy(c,s.c_str());
  return c;
}

int main(int argc, char** argv)
{

  //Extract port and log, send other args to moses
  char** mosesargv = new char*[argc+2];
  int mosesargc = 0;
  int port = 50005;
  const char* logfile = "/dev/null";
  bool isSerial = false;

  for (int i = 0; i < argc; ++i) {
    if (!strcmp(argv[i],"--server-port")) {
      ++i;
      if (i >= argc) {
        cerr << "Error: Missing argument to --server-port" << endl;
        exit(1);
      } else {
        port = atoi(argv[i]);
      }
    } else if (!strcmp(argv[i],"--server-log")) {
      ++i;
      if (i >= argc) {
        cerr << "Error: Missing argument to --server-log" << endl;
        exit(1);
      } else {
        logfile = argv[i];
      }
    } else if (!strcmp(argv[i], "--serial")) {
      cerr << "Running single-threaded server" << endl;
      isSerial = true;
    } else {
      mosesargv[mosesargc] = new char[strlen(argv[i])+1];
      strcpy(mosesargv[mosesargc],argv[i]);
      ++mosesargc;
    }
  }
  
  cerr << "Switching off translation option cache.." << endl;
  mosesargv[mosesargc] = strToChar("-use-persistent-cache");
  ++mosesargc;
  mosesargv[mosesargc] = strToChar("0");
  ++mosesargc;
  mosesargv[mosesargc] = strToChar("-persistent-cache-size");
  ++mosesargc;
  mosesargv[mosesargc] = strToChar("0");
  ++mosesargc;
  mosesargv[mosesargc] = strToChar("-b");
  ++mosesargc;
  mosesargv[mosesargc] = strToChar("0");
  ++mosesargc;
  
  cerr << "mosesargs: " << endl;
  for (int i = 0 ; i < mosesargc ; i++) {
    cerr << mosesargv[i] << endl;
  }

  Parameter* params = new Parameter();
  if (!params->LoadParam(mosesargc,mosesargv)) {
    params->Explain();
    exit(1);
  }
  if (!StaticData::LoadDataStatic(params)) {
    exit(1);
  }

  cerr << "start weights: " << StaticData::Instance().GetAllWeights() << endl;

  xmlrpc_c::registry myRegistry;

  xmlrpc_c::methodPtr const translator(new Translator);
  xmlrpc_c::methodPtr const updater(new Updater);
  xmlrpc_c::methodPtr const weightSetter(new WeightSetter);
  xmlrpc_c::methodPtr const weightAdder(new WeightAdder);

  myRegistry.addMethod("translate", translator);
  myRegistry.addMethod("updater", updater);
  myRegistry.addMethod("setWeights", weightSetter);
  myRegistry.addMethod("addWeights", weightAdder);
  // backwards compatibility
  myRegistry.addMethod("updateWeights", weightSetter);

  xmlrpc_c::serverAbyss myAbyssServer(
    myRegistry,
    port,              // TCP port on which to listen
    logfile
  );

  cerr << "Listening on port " << port << endl;
  if (isSerial) {
    while(1) {
      myAbyssServer.runOnce();
    }
  } else {
    myAbyssServer.run();
  }
  // xmlrpc_c::serverAbyss.run() never returns
  CHECK(false);
  return 0;
}