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

SoftSourceSyntacticConstraintsFeature.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3802162bc6f06eec885207d79ad9ecdf325bf0fd (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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include <vector>
#include <limits>
#include <cassert>
#include "SoftSourceSyntacticConstraintsFeature.h"
#include "moses/StaticData.h"
#include "moses/InputFileStream.h"
#include "moses/ScoreComponentCollection.h"
#include "moses/Hypothesis.h"
#include "moses/ChartHypothesis.h"
#include "moses/ChartManager.h"
#include "moses/FactorCollection.h"
#include "moses/TreeInput.h"
#include "moses/PP/SourceLabelsPhraseProperty.h"


using namespace std;

namespace Moses
{


SoftSourceSyntacticConstraintsFeature::SoftSourceSyntacticConstraintsFeature(const std::string &line)
  : StatelessFeatureFunction(6, line)
  , m_useCoreSourceLabels(false)
  , m_useLogprobs(true)
  , m_useSparse(false)
  , m_useSparseLabelPairs(false)
  , m_noMismatches(false)
  , m_floor(1e-7)
{
  VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
  ReadParameters();
  VERBOSE(1, " Done.");
  VERBOSE(1, " Config:");
  VERBOSE(1, " Log probabilities");
  if ( m_useLogprobs )         {
    VERBOSE(1, " active.");
  } else {
    VERBOSE(1, " inactive.");
  }
  VERBOSE(1, " Sparse scores");
  if ( m_useSparse )           {
    VERBOSE(1, " active.");
  } else {
    VERBOSE(1, " inactive.");
  }
  VERBOSE(1, " Sparse label pair scores");
  if ( m_useSparseLabelPairs ) {
    VERBOSE(1, " active.");
  } else {
    VERBOSE(1, " inactive.");
  }
  VERBOSE(1, " Core labels");
  if ( m_useCoreSourceLabels ) {
    VERBOSE(1, " active.");
  } else {
    VERBOSE(1, " inactive.");
  }
  VERBOSE(1, " No mismatches");
  if ( m_noMismatches )        {
    VERBOSE(1, " active.");
  } else {
    VERBOSE(1, " inactive.");
  }
  VERBOSE(1, std::endl);
}


void SoftSourceSyntacticConstraintsFeature::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "sourceLabelSetFile") {
    m_sourceLabelSetFile = value;
  } else if (key == "coreSourceLabelSetFile") {
    m_coreSourceLabelSetFile = value;
    m_useCoreSourceLabels = true;
  } else if (key == "targetSourceLeftHandSideJointCountFile") {
    m_targetSourceLHSJointCountFile = value;
  } else if (key == "noMismatches") {
    m_noMismatches = Scan<bool>(value); // for a hard constraint, allow no mismatches (also set: weights 1 0 0 0 0 0, tuneable=false)
  } else if (key == "logProbabilities") {
    m_useLogprobs = Scan<bool>(value);
  } else if (key == "sparse") {
    m_useSparse = Scan<bool>(value);
  } else if (key == "sparseLabelPairs") {
    m_useSparseLabelPairs = Scan<bool>(value);
  } else {
    StatelessFeatureFunction::SetParameter(key, value);
  }
}

void SoftSourceSyntacticConstraintsFeature::Load()
{
  // don't change the loading order!
  LoadSourceLabelSet();
  if (!m_coreSourceLabelSetFile.empty()) {
    LoadCoreSourceLabelSet();
  }
  if (!m_targetSourceLHSJointCountFile.empty()) {
    LoadTargetSourceLeftHandSideJointCountFile();
  }
}

void SoftSourceSyntacticConstraintsFeature::LoadSourceLabelSet()
{
  FEATUREVERBOSE(2, "Loading source label set from file " << m_sourceLabelSetFile << " ...");
  InputFileStream inFile(m_sourceLabelSetFile);

  FactorCollection &factorCollection = FactorCollection::Instance();

  // read source label set
  std::string line;
  m_sourceLabels.clear();
  m_sourceLabelsByIndex.clear();
  m_sourceLabelsByIndex_RHS_1.clear();
  m_sourceLabelsByIndex_RHS_0.clear();
  m_sourceLabelsByIndex_LHS_1.clear();
  m_sourceLabelsByIndex_LHS_0.clear();
  m_sourceLabelIndexesByFactor.clear();
  while (getline(inFile, line)) {
    std::istringstream tokenizer(line);
    std::string label;
    size_t index;
    try {
      tokenizer >> label >> index;
    } catch (const std::exception &e) {
      UTIL_THROW2(GetScoreProducerDescription()
                  << ": Error reading source label set file " << m_sourceLabelSetFile << " .");
    }
    std::pair< boost::unordered_map<std::string,size_t>::iterator, bool > inserted = m_sourceLabels.insert( std::pair<std::string,size_t>(label,index) );
    UTIL_THROW_IF2(!inserted.second, GetScoreProducerDescription()
                   << ": Source label set file " << m_sourceLabelSetFile << " should contain each syntactic label only once.");

    if (index >= m_sourceLabelsByIndex.size()) {
      m_sourceLabelsByIndex.resize(index+1);
      m_sourceLabelsByIndex_RHS_1.resize(index+1);
      m_sourceLabelsByIndex_RHS_0.resize(index+1);
      m_sourceLabelsByIndex_LHS_1.resize(index+1);
      m_sourceLabelsByIndex_LHS_0.resize(index+1);
    }
    m_sourceLabelsByIndex[index] = label;
    m_sourceLabelsByIndex_RHS_1[index] = "RHS_1_" + label;
    m_sourceLabelsByIndex_RHS_0[index] = "RHS_0_" + label;
    m_sourceLabelsByIndex_LHS_1[index] = "LHS_1_" + label;
    m_sourceLabelsByIndex_LHS_0[index] = "LHS_0_" + label;
    const Factor* sourceLabelFactor = factorCollection.AddFactor(label,true);
    m_sourceLabelIndexesByFactor[sourceLabelFactor] = index;
  }

  inFile.Close();

  std::list<std::string> specialLabels;
  specialLabels.push_back("GlueTop");
  specialLabels.push_back("GlueX");
//  specialLabels.push_back("XRHS");
//  specialLabels.push_back("XLHS");
  for (std::list<std::string>::const_iterator iter=specialLabels.begin();
       iter!=specialLabels.end(); ++iter) {
    boost::unordered_map<std::string,size_t>::iterator found = m_sourceLabels.find(*iter);
    UTIL_THROW_IF2(found == m_sourceLabels.end(), GetScoreProducerDescription()
                   << ": Source label set file " << m_sourceLabelSetFile << " should contain an entry for the special label \"" << *iter << "\".");
    if (!(found->first).compare("GlueTop")) {
      m_GlueTopLabel = found->second;
//    } else if (!(found->first).compare("XRHS")) {
//      m_XRHSLabel = found->second;
//    } else if (!(found->first).compare("XLHS")) {
//      m_XLHSLabel = found->second;
    }
  }
  FEATUREVERBOSE2(2, " Done." << std::endl);
}


void SoftSourceSyntacticConstraintsFeature::LoadCoreSourceLabelSet()
{
  FEATUREVERBOSE(2, "Loading core source label set from file " << m_coreSourceLabelSetFile << " ...");
  // read core source label set
  LoadLabelSet(m_coreSourceLabelSetFile, m_coreSourceLabels);
  FEATUREVERBOSE2(2, " Done." << std::endl);
}

void SoftSourceSyntacticConstraintsFeature::LoadLabelSet(std::string &filename,
    boost::unordered_set<size_t> &labelSet)
{
  InputFileStream inFile(filename);
  std::string line;
  labelSet.clear();
  while (getline(inFile, line)) {
    istringstream tokenizer(line);
    std::string label;
    tokenizer >> label;
    boost::unordered_map<std::string,size_t>::iterator foundSourceLabelIndex = m_sourceLabels.find( label );
    if ( foundSourceLabelIndex != m_sourceLabels.end() ) {
      labelSet.insert(foundSourceLabelIndex->second);
    } else {
      FEATUREVERBOSE(2, "Ignoring unknown source label \"" << label << "\" "
                     << "from core source label set file " << filename << "."
                     << std::endl);
    }
  }
  inFile.Close();
}


void SoftSourceSyntacticConstraintsFeature::LoadTargetSourceLeftHandSideJointCountFile()
{

  FEATUREVERBOSE(2, "Loading target/source label joint counts from file " << m_targetSourceLHSJointCountFile << " ...");
  InputFileStream inFile(m_targetSourceLHSJointCountFile);

  for (boost::unordered_map<const Factor*, std::vector< std::pair<float,float> >* >::iterator iter=m_labelPairProbabilities.begin();
       iter!=m_labelPairProbabilities.end(); ++iter) {
    delete iter->second;
  }
  m_labelPairProbabilities.clear();

  // read joint counts
  std::string line;
  FactorCollection &factorCollection = FactorCollection::Instance();
  boost::unordered_map<const Factor*,float> targetLHSCounts;
  std::vector<float> sourceLHSCounts(m_sourceLabels.size(),0.0);

  while (getline(inFile, line)) {
    istringstream tokenizer(line);
    std::string targetLabel;
    std::string sourceLabel;
    float count;
    tokenizer >> targetLabel;
    tokenizer >> sourceLabel;
    tokenizer >> count;

    boost::unordered_map<std::string,size_t>::iterator foundSourceLabelIndex = m_sourceLabels.find( sourceLabel );
    UTIL_THROW_IF2(foundSourceLabelIndex == m_sourceLabels.end(), GetScoreProducerDescription()
                   << ": Target/source label joint count file " << m_targetSourceLHSJointCountFile
                   << " contains unknown source label \"" << sourceLabel << "\".");

    const Factor* targetLabelFactor = factorCollection.AddFactor(targetLabel,true);

    sourceLHSCounts[foundSourceLabelIndex->second] += count;
    std::pair< boost::unordered_map<const Factor*,float >::iterator, bool > insertedTargetLHSCount =
      targetLHSCounts.insert( std::pair<const Factor*,float>(targetLabelFactor,count) );
    if (!insertedTargetLHSCount.second) {
      (insertedTargetLHSCount.first)->second += count;
      boost::unordered_map<const Factor*, std::vector< std::pair<float,float> >* >::iterator jointCountIt =
        m_labelPairProbabilities.find( targetLabelFactor );
      assert(jointCountIt != m_labelPairProbabilities.end());
      (jointCountIt->second)->at(foundSourceLabelIndex->second).first += count;
      (jointCountIt->second)->at(foundSourceLabelIndex->second).second += count;
    } else {
      std::pair<float,float> init(0.0,0.0);
      std::vector< std::pair<float,float> >* sourceVector = new std::vector< std::pair<float,float> >(m_sourceLabels.size(),init);
      sourceVector->at(foundSourceLabelIndex->second) = std::pair<float,float>(count,count);
      std::pair< boost::unordered_map<const Factor*, std::vector< std::pair<float,float> >* >::iterator, bool > insertedJointCount =
        m_labelPairProbabilities.insert( std::pair<const Factor*, std::vector< std::pair<float,float> >* >(targetLabelFactor,sourceVector) );
      UTIL_THROW_IF2(!insertedJointCount.second, GetScoreProducerDescription()
                     << ": Loading target/source label joint counts from file " << m_targetSourceLHSJointCountFile << " failed.");
    }
  }

  // normalization
  for (boost::unordered_map<const Factor*, std::vector< std::pair<float,float> >* >::iterator iter=m_labelPairProbabilities.begin();
       iter!=m_labelPairProbabilities.end(); ++iter) {
    float targetLHSCount = 0;
    boost::unordered_map<const Factor*,float >::const_iterator targetLHSCountIt = targetLHSCounts.find( iter->first );
    if ( targetLHSCountIt != targetLHSCounts.end() ) {
      targetLHSCount = targetLHSCountIt->second;
    }
    std::vector< std::pair<float,float> > &probabilities = *(iter->second);
    for (size_t index=0; index<probabilities.size(); ++index) {

      if ( probabilities[index].first != 0 ) {
        assert(targetLHSCount != 0);
        probabilities[index].first  /= targetLHSCount;
      }
      if ( probabilities[index].second != 0 ) {
        assert(sourceLHSCounts[index] != 0);
        probabilities[index].second /= sourceLHSCounts[index];
      }
    }
  }

  inFile.Close();
  FEATUREVERBOSE2(2, " Done." << std::endl);
}


void SoftSourceSyntacticConstraintsFeature::EvaluateWithSourceContext(const InputType &input
    , const InputPath &inputPath
    , const TargetPhrase &targetPhrase
    , const StackVec *stackVec
    , ScoreComponentCollection &scoreBreakdown
    , ScoreComponentCollection *estimatedScores) const
{
  assert(stackVec);

  IFFEATUREVERBOSE(3) {
    FEATUREVERBOSE(3, targetPhrase << std::endl);
    FEATUREVERBOSE(3, inputPath << std::endl);
    for (size_t i = 0; i < stackVec->size(); ++i) {
      const ChartCellLabel &cell = *stackVec->at(i);
      const Range &ntRange = cell.GetCoverage();
      FEATUREVERBOSE(3, "stackVec[ " << i << " ] : " << ntRange.GetStartPos() << " - " << ntRange.GetEndPos() << std::endl);
    }

    for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignNonTerm().begin();
         it!=targetPhrase.GetAlignNonTerm().end(); ++it) {
      FEATUREVERBOSE(3, "alignNonTerm " << it->first << " " << it->second << std::endl);
    }
  }

  // dense scores
  std::vector<float> newScores(m_numScoreComponents,0);

  const TreeInput& treeInput = static_cast<const TreeInput&>(input);
  const StaticData& staticData = StaticData::Instance();
  const Word& outputDefaultNonTerminal = staticData.GetOutputDefaultNonTerminal();

  size_t nNTs = 1;
  bool treeInputMismatchLHSBinary = true;
  size_t treeInputMismatchRHSCount = 0;
  bool hasCompleteTreeInputMatch = false;
  float ruleLabelledProbability = 0.0;
  float treeInputMatchProbRHS = 0.0;
  float treeInputMatchProbLHS = 0.0;

  // read SourceLabels property
  const Factor* targetLHS = targetPhrase.GetTargetLHS()[0];
  bool isGlueGrammarRule = false;
  bool isUnkRule = false;

  if (const PhraseProperty *property = targetPhrase.GetProperty("SourceLabels")) {

    const SourceLabelsPhraseProperty *sourceLabelsPhraseProperty = static_cast<const SourceLabelsPhraseProperty*>(property);

    nNTs = sourceLabelsPhraseProperty->GetNumberOfNonTerminals();
    float totalCount = sourceLabelsPhraseProperty->GetTotalCount();

    // prepare for input tree label matching
    std::vector< boost::unordered_set<size_t> > treeInputLabelsRHS(nNTs-1);
    boost::unordered_set<size_t> treeInputLabelsLHS;

    // get index map for underlying hypotheses
    const Range& range = inputPath.GetWordsRange();
    size_t startPos = range.GetStartPos();
    size_t endPos = range.GetEndPos();
    const Phrase *sourcePhrase = targetPhrase.GetRuleSource();

    if (nNTs > 1) { // rule has right-hand side non-terminals, i.e. it's a hierarchical rule
      size_t nonTerminalNumber = 0;
      size_t sourceSentPos = startPos;

      for (size_t sourcePhrasePos=0; sourcePhrasePos<sourcePhrase->GetSize(); ++sourcePhrasePos) {
        // consult rule for either word or non-terminal
        const Word &word = sourcePhrase->GetWord(sourcePhrasePos);
        size_t symbolStartPos = sourceSentPos;
        size_t symbolEndPos = sourceSentPos;
        if ( word.IsNonTerminal() ) {
          // retrieve information that is required for input tree label matching (RHS)
          const ChartCellLabel &cell = *stackVec->at(nonTerminalNumber);
          const Range& prevWordsRange = cell.GetCoverage();
          symbolStartPos = prevWordsRange.GetStartPos();
          symbolEndPos = prevWordsRange.GetEndPos();
        }

        const NonTerminalSet& treeInputLabels = treeInput.GetLabelSet(symbolStartPos,symbolEndPos);

        for (NonTerminalSet::const_iterator treeInputLabelsIt = treeInputLabels.begin();
             treeInputLabelsIt != treeInputLabels.end(); ++treeInputLabelsIt) {
          if (*treeInputLabelsIt != outputDefaultNonTerminal) {
            boost::unordered_map<const Factor*,size_t>::const_iterator foundTreeInputLabel
            = m_sourceLabelIndexesByFactor.find((*treeInputLabelsIt)[0]);
            if (foundTreeInputLabel != m_sourceLabelIndexesByFactor.end()) {
              size_t treeInputLabelIndex = foundTreeInputLabel->second;
              treeInputLabelsRHS[sourcePhrasePos].insert(treeInputLabelIndex);
            }
          }
        }

        if ( word.IsNonTerminal() ) {
          ++nonTerminalNumber;
        }
        sourceSentPos = symbolEndPos + 1;
      }
    }

    // retrieve information that is required for input tree label matching (LHS)
    const NonTerminalSet& treeInputLabels = treeInput.GetLabelSet(startPos,endPos);

    for (NonTerminalSet::const_iterator treeInputLabelsIt = treeInputLabels.begin();
         treeInputLabelsIt != treeInputLabels.end(); ++treeInputLabelsIt) {
      if (*treeInputLabelsIt != outputDefaultNonTerminal) {
        boost::unordered_map<const Factor*,size_t>::const_iterator foundTreeInputLabel
        = m_sourceLabelIndexesByFactor.find((*treeInputLabelsIt)[0]);
        if (foundTreeInputLabel != m_sourceLabelIndexesByFactor.end()) {
          size_t treeInputLabelIndex = foundTreeInputLabel->second;
          treeInputLabelsLHS.insert(treeInputLabelIndex);
        }
      }
    }


    // inspect source-labelled rule items

    std::vector< boost::unordered_set<size_t> > sparseScoredTreeInputLabelsRHS(nNTs-1);
    boost::unordered_set<size_t> sparseScoredTreeInputLabelsLHS;

    std::vector<bool> sourceLabelSeenAsLHS(m_sourceLabels.size(),false);
    std::vector<bool> treeInputMatchRHSCountByNonTerminal(nNTs-1,false);
    std::vector<float> treeInputMatchProbRHSByNonTerminal(nNTs-1,0.0);

    const std::list<SourceLabelsPhrasePropertyItem> &sourceLabelItems = sourceLabelsPhraseProperty->GetSourceLabelItems();

    for (std::list<SourceLabelsPhrasePropertyItem>::const_iterator sourceLabelItem = sourceLabelItems.begin();
         sourceLabelItem != sourceLabelItems.end() && !hasCompleteTreeInputMatch; ++sourceLabelItem) {

      const std::list<size_t> &sourceLabelsRHS = sourceLabelItem->GetSourceLabelsRHS();
      const std::list< std::pair<size_t,float> > &sourceLabelsLHSList = sourceLabelItem->GetSourceLabelsLHSList();
      float sourceLabelsRHSCount = sourceLabelItem->GetSourceLabelsRHSCount();

      assert(sourceLabelsRHS.size() == nNTs-1);

      bool currentSourceLabelItemIsCompleteTreeInputMatch = true;

      size_t nonTerminalNumber=0;
      for (std::list<size_t>::const_iterator sourceLabelsRHSIt = sourceLabelsRHS.begin();
           sourceLabelsRHSIt != sourceLabelsRHS.end(); ++sourceLabelsRHSIt, ++nonTerminalNumber) {

        if (treeInputLabelsRHS[nonTerminalNumber].find(*sourceLabelsRHSIt) != treeInputLabelsRHS[nonTerminalNumber].end()) {

          treeInputMatchRHSCountByNonTerminal[nonTerminalNumber] = true;
          treeInputMatchProbRHSByNonTerminal[nonTerminalNumber] += sourceLabelsRHSCount; // to be normalized later on

          if ( m_useSparse &&
               (!m_useCoreSourceLabels || m_coreSourceLabels.find(*sourceLabelsRHSIt) != m_coreSourceLabels.end()) ) {
            // score sparse features: RHS match
            if (sparseScoredTreeInputLabelsRHS[nonTerminalNumber].find(*sourceLabelsRHSIt) == sparseScoredTreeInputLabelsRHS[nonTerminalNumber].end()) {
              // (only if no match has been scored for this tree input label and rule non-terminal with a previous sourceLabelItem)
              float score_RHS_1 = (float)1/treeInputLabelsRHS[nonTerminalNumber].size();
              scoreBreakdown.PlusEquals(this,
                                        m_sourceLabelsByIndex_RHS_1[*sourceLabelsRHSIt],
                                        score_RHS_1);
              sparseScoredTreeInputLabelsRHS[nonTerminalNumber].insert(*sourceLabelsRHSIt);
            }
          }

        } else {

          currentSourceLabelItemIsCompleteTreeInputMatch = false;

        }
      }

      for (std::list< std::pair<size_t,float> >::const_iterator sourceLabelsLHSIt = sourceLabelsLHSList.begin();
           sourceLabelsLHSIt != sourceLabelsLHSList.end(); ++sourceLabelsLHSIt) {

        if ( sourceLabelsLHSIt->first == m_GlueTopLabel ) {
          isGlueGrammarRule = true;
        }

        if (treeInputLabelsLHS.find(sourceLabelsLHSIt->first) != treeInputLabelsLHS.end()) {

          treeInputMismatchLHSBinary = false;
          treeInputMatchProbLHS += sourceLabelsLHSIt->second; // to be normalized later on

          if ( m_useSparse &&
               (!m_useCoreSourceLabels || m_coreSourceLabels.find(sourceLabelsLHSIt->first) != m_coreSourceLabels.end()) ) {
            // score sparse features: LHS match
            if (sparseScoredTreeInputLabelsLHS.find(sourceLabelsLHSIt->first) == sparseScoredTreeInputLabelsLHS.end()) {
              // (only if no match has been scored for this tree input label and rule non-terminal with a previous sourceLabelItem)
              float score_LHS_1 = (float)1/treeInputLabelsLHS.size();
              scoreBreakdown.PlusEquals(this,
                                        m_sourceLabelsByIndex_LHS_1[sourceLabelsLHSIt->first],
                                        score_LHS_1);
              sparseScoredTreeInputLabelsLHS.insert(sourceLabelsLHSIt->first);
            }
          }

          if ( currentSourceLabelItemIsCompleteTreeInputMatch ) {
            ruleLabelledProbability += sourceLabelsLHSIt->second; // to be normalized later on
            hasCompleteTreeInputMatch = true;
          }

        }
      }
    }

    // normalization
    for (std::vector<float>::iterator treeInputMatchProbRHSByNonTerminalIt = treeInputMatchProbRHSByNonTerminal.begin();
         treeInputMatchProbRHSByNonTerminalIt != treeInputMatchProbRHSByNonTerminal.end(); ++treeInputMatchProbRHSByNonTerminalIt) {
      *treeInputMatchProbRHSByNonTerminalIt /= totalCount;
      if ( *treeInputMatchProbRHSByNonTerminalIt != 0 ) {
        treeInputMatchProbRHS += ( m_useLogprobs ? TransformScore(*treeInputMatchProbRHSByNonTerminalIt) : *treeInputMatchProbRHSByNonTerminalIt );
      }
    }
    treeInputMatchProbLHS /= totalCount;
    ruleLabelledProbability /= totalCount;

    // input tree matching (RHS)
    if ( !hasCompleteTreeInputMatch ) {
      treeInputMismatchRHSCount = nNTs-1;
      for (std::vector<bool>::const_iterator treeInputMatchRHSCountByNonTerminalIt = treeInputMatchRHSCountByNonTerminal.begin();
           treeInputMatchRHSCountByNonTerminalIt != treeInputMatchRHSCountByNonTerminal.end(); ++treeInputMatchRHSCountByNonTerminalIt) {
        if (*treeInputMatchRHSCountByNonTerminalIt) {
          --treeInputMismatchRHSCount;
        }
      }
    }

    // score sparse features: mismatches
    if ( m_useSparse ) {

      // RHS

      for (size_t nonTerminalNumber = 0; nonTerminalNumber < nNTs-1; ++nonTerminalNumber) {
        // nNTs-1 because nNTs also counts the left-hand side non-terminal

        float score_RHS_0 = (float)1/treeInputLabelsRHS[nonTerminalNumber].size();
        for (boost::unordered_set<size_t>::const_iterator treeInputLabelsRHSIt = treeInputLabelsRHS[nonTerminalNumber].begin();
             treeInputLabelsRHSIt != treeInputLabelsRHS[nonTerminalNumber].end(); ++treeInputLabelsRHSIt) {

          if ( !m_useCoreSourceLabels || m_coreSourceLabels.find(*treeInputLabelsRHSIt) != m_coreSourceLabels.end() ) {

            if (sparseScoredTreeInputLabelsRHS[nonTerminalNumber].find(*treeInputLabelsRHSIt) == sparseScoredTreeInputLabelsRHS[nonTerminalNumber].end()) {
              // score sparse features: RHS mismatch
              scoreBreakdown.PlusEquals(this,
                                        m_sourceLabelsByIndex_RHS_0[*treeInputLabelsRHSIt],
                                        score_RHS_0);
            }
          }
        }
      }

      // LHS

      float score_LHS_0 = (float)1/treeInputLabelsLHS.size();
      for (boost::unordered_set<size_t>::const_iterator treeInputLabelsLHSIt = treeInputLabelsLHS.begin();
           treeInputLabelsLHSIt != treeInputLabelsLHS.end(); ++treeInputLabelsLHSIt) {

        if ( !m_useCoreSourceLabels || m_coreSourceLabels.find(*treeInputLabelsLHSIt) != m_coreSourceLabels.end() ) {

          if (sparseScoredTreeInputLabelsLHS.find(*treeInputLabelsLHSIt) == sparseScoredTreeInputLabelsLHS.end()) {
            // score sparse features: RHS mismatch
            scoreBreakdown.PlusEquals(this,
                                      m_sourceLabelsByIndex_LHS_0[*treeInputLabelsLHSIt],
                                      score_LHS_0);
          }
        }
      }

    }

    if ( m_useSparseLabelPairs && !isGlueGrammarRule ) {

      // left-hand side label pairs (target NT, source NT)
      float t2sLabelsScore = 0.0;
      float s2tLabelsScore = 0.0;
      for (boost::unordered_set<size_t>::const_iterator treeInputLabelsLHSIt = treeInputLabelsLHS.begin();
           treeInputLabelsLHSIt != treeInputLabelsLHS.end(); ++treeInputLabelsLHSIt) {

        scoreBreakdown.PlusEquals(this,
                                  "LHSPAIR_" + targetLHS->GetString().as_string() + "_" + m_sourceLabelsByIndex[*treeInputLabelsLHSIt],
                                  (float)1/treeInputLabelsLHS.size());

        if (!m_targetSourceLHSJointCountFile.empty()) {
          std::pair<float,float> probPair = GetLabelPairProbabilities( targetLHS, *treeInputLabelsLHSIt);
          t2sLabelsScore += probPair.first;
          s2tLabelsScore += probPair.second;
        }
      }
      if ( treeInputLabelsLHS.size() == 0 ) {
        scoreBreakdown.PlusEquals(this,
                                  "LHSPAIR_" + targetLHS->GetString().as_string() + "_" + outputDefaultNonTerminal[0]->GetString().as_string(),
                                  1);
        if (!m_targetSourceLHSJointCountFile.empty()) {
          t2sLabelsScore = TransformScore(m_floor);
          s2tLabelsScore = TransformScore(m_floor);
        }
      } else {
        if (!m_targetSourceLHSJointCountFile.empty()) {
          float norm = TransformScore(treeInputLabelsLHS.size());
          t2sLabelsScore = TransformScore(t2sLabelsScore) - norm;
          s2tLabelsScore = TransformScore(s2tLabelsScore) - norm;
        }
      }
      if (!m_targetSourceLHSJointCountFile.empty()) {
        scoreBreakdown.PlusEquals(this, "LHST2S", t2sLabelsScore);
        scoreBreakdown.PlusEquals(this, "LHSS2T", s2tLabelsScore);
      }
    }

  } else {

    // abort with error message if the phrase does not translate an unknown word
    UTIL_THROW_IF2(!targetPhrase.GetWord(0).IsOOV(), GetScoreProducerDescription()
                   << ": Missing SourceLabels property. "
                   << "Please check phrase table and glue rules.");

    // unknown word
    isUnkRule = true;
//    ruleLabelledProbability = 1;

  }

  // add scores

  // input tree matching
  newScores[0] = !hasCompleteTreeInputMatch;
  if ( m_noMismatches ) {
    newScores[0] = ( (hasCompleteTreeInputMatch || isGlueGrammarRule || isUnkRule) ? 0 : -std::numeric_limits<float>::infinity() );
  }
  newScores[1] = treeInputMismatchLHSBinary;
  newScores[2] = treeInputMismatchRHSCount;

  if ( m_useLogprobs ) {
    if ( ruleLabelledProbability != 0 ) {
      ruleLabelledProbability = TransformScore(ruleLabelledProbability);
    }
    if ( treeInputMatchProbLHS != 0 ) {
      treeInputMatchProbLHS = TransformScore(treeInputMatchProbLHS);
    }
  }

  newScores[3] = ruleLabelledProbability;
  newScores[4] = treeInputMatchProbLHS;
  newScores[5] = treeInputMatchProbRHS;

  scoreBreakdown.PlusEquals(this, newScores);
}


std::pair<float,float> SoftSourceSyntacticConstraintsFeature::GetLabelPairProbabilities(
  const Factor* target,
  const size_t source) const
{
  boost::unordered_map<const Factor*, std::vector< std::pair<float,float> >* >::const_iterator found =
    m_labelPairProbabilities.find(target);
  if ( found == m_labelPairProbabilities.end() ) {
    return std::pair<float,float>(m_floor,m_floor); // floor values
  }
  std::pair<float,float> ret = found->second->at(source);
  if ( ret == std::pair<float,float>(0,0) ) {
    return std::pair<float,float>(m_floor,m_floor); // floor values
  }
  return ret;
}


}