File size: 17,348 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
 * Rule.cpp
 *
 *  Created on: 20 Feb 2014
 *      Author: hieu
 */

#include <sstream>
#include <algorithm>
#include "Rule.h"
#include "AlignedSentence.h"
#include "ConsistentPhrase.h"
#include "NonTerm.h"
#include "Parameter.h"

using namespace std;

Rule::Rule(const NonTerm &lhsNonTerm, const AlignedSentence &alignedSentence)
  :m_lhs(lhsNonTerm)
  ,m_alignedSentence(alignedSentence)
  ,m_isValid(true)
  ,m_canRecurse(true)
{
  CreateSource();
}

Rule::Rule(const Rule &copy, const NonTerm &nonTerm)
  :m_lhs(copy.m_lhs)
  ,m_alignedSentence(copy.m_alignedSentence)
  ,m_nonterms(copy.m_nonterms)
  ,m_isValid(true)
  ,m_canRecurse(true)
{
  m_nonterms.push_back(&nonTerm);
  CreateSource();

}

Rule::~Rule()
{
  // TODO Auto-generated destructor stub
}

const ConsistentPhrase &Rule::GetConsistentPhrase() const
{
  return m_lhs.GetConsistentPhrase();
}

void Rule::CreateSource()
{
  const NonTerm *cp = NULL;
  size_t nonTermInd = 0;
  if (nonTermInd < m_nonterms.size()) {
    cp = m_nonterms[nonTermInd];
  }

  for (int sourcePos = m_lhs.GetConsistentPhrase().corners[0];
       sourcePos <= m_lhs.GetConsistentPhrase().corners[1];
       ++sourcePos) {

    const RuleSymbol *ruleSymbol;
    if (cp && cp->GetConsistentPhrase().corners[0] <= sourcePos && sourcePos <= cp->GetConsistentPhrase().corners[1]) {
      // replace words with non-term
      ruleSymbol = cp;
      sourcePos = cp->GetConsistentPhrase().corners[1];
      if (m_nonterms.size()) {
        cp = m_nonterms[nonTermInd];
      }

      // move to next non-term
      ++nonTermInd;
      cp = (nonTermInd < m_nonterms.size()) ? m_nonterms[nonTermInd] : NULL;
    } else {
      // terminal
      ruleSymbol = m_alignedSentence.GetPhrase(Moses::Input)[sourcePos];
    }

    m_source.Add(ruleSymbol);
  }
}

int Rule::GetNextSourcePosForNonTerm() const
{
  if (m_nonterms.empty()) {
    // no non-terms so far. Can start next non-term on left corner
    return m_lhs.GetConsistentPhrase().corners[0];
  } else {
    // next non-term can start just left of previous
    const ConsistentPhrase &cp = m_nonterms.back()->GetConsistentPhrase();
    int nextPos = cp.corners[1] + 1;
    return nextPos;
  }
}

std::string Rule::Debug() const
{
  stringstream out;

  // source
  for (size_t i =  0; i < m_source.GetSize(); ++i) {
    const RuleSymbol &symbol = *m_source[i];
    out << symbol.Debug() << " ";
  }

  // target
  out << "||| ";
  for (size_t i =  0; i < m_target.GetSize(); ++i) {
    const RuleSymbol &symbol = *m_target[i];
    out << symbol.Debug() << " ";
  }

  out << "||| ";
  Alignments::const_iterator iterAlign;
  for (iterAlign =  m_alignments.begin(); iterAlign != m_alignments.end(); ++iterAlign) {
    const std::pair<int,int> &alignPair = *iterAlign;
    out << alignPair.first << "-" << alignPair.second << " ";
  }

  // overall range
  out << "||| LHS=" << m_lhs.Debug();

  return out.str();
}

void Rule::Output(std::ostream &out, bool forward) const
{
  if (forward) {
    // source
    m_source.Output(out);
    m_lhs.Output(out, Moses::Input);

    out << " ||| ";

    // target
    m_target.Output(out);
    m_lhs.Output(out, Moses::Output);
  } else {
    // target
    m_target.Output(out);
    m_lhs.Output(out, Moses::Output);

    out << " ||| ";

    // source
    m_source.Output(out);
    m_lhs.Output(out, Moses::Input);
  }

  out << " ||| ";

  // alignment
  Alignments::const_iterator iterAlign;
  for (iterAlign =  m_alignments.begin(); iterAlign != m_alignments.end(); ++iterAlign) {
    const std::pair<int,int> &alignPair = *iterAlign;

    if (forward) {
      out << alignPair.first << "-" << alignPair.second << " ";
    } else {
      out << alignPair.second << "-" << alignPair.first << " ";
    }
  }

  out << "||| ";

  // count
  out << m_count;

  out << " ||| ";

  // properties
  if (forward) {
    for (size_t i = 0; i < m_properties.size(); ++i) {
      const Property &prop = m_properties[i];
      out << prop << " ";
    }
  }
}

void Rule::NonTermContextFactor(int factor, const Word &word, std::ostream &out) const
{
  out << word.GetString(factor) << " ";
}

void Rule::NonTermContext(int sourceTarget, int factor, size_t ntInd, const ConsistentPhrase &cp, std::ostream &out) const
{
  int startPos, endPos;
  const Phrase *phrase;

  if (sourceTarget == 1) {
    startPos = cp.corners[0];
    endPos = cp.corners[1];
    phrase = &m_alignedSentence.GetPhrase(Moses::Input);
  } else if (sourceTarget == 2) {
    startPos = cp.corners[2];
    endPos = cp.corners[3];
    phrase = &m_alignedSentence.GetPhrase(Moses::Output);
  } else {
    abort();
  }

  out << ntInd << " ";

  // left outside
  if (startPos == 0) {
    out << "<s> ";
  } else {
    NonTermContextFactor(factor, *phrase->at(startPos - 1), out);
  }

  // left inside
  NonTermContextFactor(factor, *phrase->at(startPos), out);

  // right inside
  NonTermContextFactor(factor, *phrase->at(endPos), out);

  // right outside
  if (endPos == phrase->size() - 1) {
    out << "</s> ";
  } else {
    NonTermContextFactor(factor, *phrase->at(endPos + 1), out);
  }


}

void Rule::Prevalidate(const Parameter &params)
{
  // check number of source symbols in rule
  if (m_source.GetSize() > params.maxSymbolsSource) {
    m_isValid = false;
  }

  // check that last non-term added isn't too small
  if (m_nonterms.size()) {
    const NonTerm &lastNonTerm = *m_nonterms.back();
    const ConsistentPhrase &cp = lastNonTerm.GetConsistentPhrase();

    int sourceWidth = cp.GetWidth(Moses::Input);
    if (lastNonTerm.IsHiero(params)) {
      if (sourceWidth < params.minHoleSource) {
        m_isValid = false;
        m_canRecurse = false;
        return;
      }
    } else if (sourceWidth < params.minHoleSourceSyntax) {
      m_isValid = false;
      m_canRecurse = false;
      return;
    }

  }

  // check number of non-terms
  int numNonTerms = 0;
  int numHieroNonTerms = 0;
  for (size_t i = 0; i < m_source.GetSize(); ++i) {
    const RuleSymbol *arc = m_source[i];
    if (arc->IsNonTerm()) {
      ++numNonTerms;
      const NonTerm &nonTerm = *static_cast<const NonTerm*>(arc);
      bool isHiero = nonTerm.IsHiero(params);
      if (isHiero) {
        ++numHieroNonTerms;
      }
    }
  }

  if (numNonTerms >= params.maxNonTerm) {
    m_canRecurse = false;
    if (numNonTerms > params.maxNonTerm) {
      m_isValid = false;
      return;
    }
  }

  if (numHieroNonTerms >= params.maxHieroNonTerm) {
    m_canRecurse = false;
    if (numHieroNonTerms > params.maxHieroNonTerm) {
      m_isValid = false;
      return;
    }
  }

  // check if 2 consecutive non-terms in source
  if (!params.nonTermConsecSource && m_nonterms.size() >= 2) {
    const NonTerm &lastNonTerm = *m_nonterms.back();
    const NonTerm &secondLastNonTerm = *m_nonterms[m_nonterms.size() - 2];
    if (secondLastNonTerm.GetConsistentPhrase().corners[1] + 1 ==
        lastNonTerm.GetConsistentPhrase().corners[0]) {
      if (params.mixedSyntaxType == 0) {
        // ordinary hiero or syntax model
        m_isValid = false;
        m_canRecurse = false;
        return;
      } else {
        // Hieu's mixed syntax
        switch (params.nonTermConsecSourceMixedSyntax) {
        case 0:
          m_isValid = false;
          m_canRecurse = false;
          return;
        case 1:
          if (lastNonTerm.IsHiero(Moses::Input, params)
              && secondLastNonTerm.IsHiero(Moses::Input, params)) {
            m_isValid = false;
            m_canRecurse = false;
            return;
          }
          break;
        case 2:
          if (lastNonTerm.IsHiero(Moses::Input, params)
              || secondLastNonTerm.IsHiero(Moses::Input, params)) {
            m_isValid = false;
            m_canRecurse = false;
            return;
          }
          break;
        case 3:
          break;
        } // switch
      }
    }
  }

  //check to see if it overlaps with any other non-terms
  if (m_nonterms.size() >= 2) {
    const NonTerm &lastNonTerm = *m_nonterms.back();

    for (size_t i = 0; i < m_nonterms.size() - 1; ++i) {
      const NonTerm &otherNonTerm = *m_nonterms[i];
      bool overlap = lastNonTerm.GetConsistentPhrase().TargetOverlap(otherNonTerm.GetConsistentPhrase());

      if (overlap) {
        m_isValid = false;
        m_canRecurse = false;
        return;
      }
    }
  }

  // check that at least 1 word is aligned
  if (params.requireAlignedWord) {
    bool ok = false;
    for (size_t i = 0; i < m_source.GetSize(); ++i) {
      const RuleSymbol &symbol = *m_source[i];
      if (!symbol.IsNonTerm()) {
        const Word &word = static_cast<const Word&>(symbol);
        if (word.GetAlignment().size()) {
          ok = true;
          break;
        }
      }
    }

    if (!ok) {
      m_isValid = false;
      m_canRecurse = false;
      return;
    }
  }

  if (params.maxSpanFreeNonTermSource) {
    const NonTerm *front = dynamic_cast<const NonTerm*>(m_source[0]);
    if (front) {
      int width = front->GetWidth(Moses::Input);
      if (width > params.maxSpanFreeNonTermSource) {
        m_isValid = false;
        m_canRecurse = false;
        return;
      }
    }

    const NonTerm *back = dynamic_cast<const NonTerm*>(m_source.Back());
    if (back) {
      int width = back->GetWidth(Moses::Input);
      if (width > params.maxSpanFreeNonTermSource) {
        m_isValid = false;
        m_canRecurse = false;
        return;
      }
    }
  }

  if (!params.nieceTerminal) {
    // collect terminal in a rule
    std::set<const Word*> terms;
    for (size_t i = 0; i < m_source.GetSize(); ++i) {
      const Word *word = dynamic_cast<const Word*>(m_source[i]);
      if (word) {
        terms.insert(word);
      }
    }

    // look in non-terms
    for (size_t i = 0; i < m_source.GetSize(); ++i) {
      const NonTerm *nonTerm = dynamic_cast<const NonTerm*>(m_source[i]);
      if (nonTerm) {
        const ConsistentPhrase &cp = nonTerm->GetConsistentPhrase();
        bool containTerm = ContainTerm(cp, terms);

        if (containTerm) {
          //cerr << "ruleSource=" << *ruleSource << " ";
          //cerr << "ntRange=" << ntRange << endl;

          // non-term contains 1 of the terms in the rule.
          m_isValid = false;
          m_canRecurse = false;
          return;
        }
      }
    }
  }

  if (params.maxScope != UNDEFINED || params.minScope > 0) {
    int scope = GetScope(params);
    if (scope > params.maxScope) {
      // scope of subsequent rules will be the same or increase
      // therefore can NOT recurse
      m_isValid = false;
      m_canRecurse = false;
      return;
    }

    if (scope < params.minScope) {
      // scope of subsequent rules may increase
      // therefore can recurse
      m_isValid = false;
    }
  }

  // min/max span per scope
  if (params.scopeSpan.size()) {
    size_t scope = GetScope(params);
    if (scope >= params.scopeSpan.size()) {
      // no constraint on it. It's ok
    } else {
      const std::pair<int,int> &constraint = params.scopeSpan[scope];
      int sourceWidth = m_lhs.GetWidth(Moses::Input);
      if (sourceWidth < constraint.first || sourceWidth > constraint.second) {
        m_isValid = false;
        m_canRecurse = false;
        return;
      }
    }
  }
}

int Rule::GetScope(const Parameter &params) const
{
  size_t scope = 0;
  bool previousIsAmbiguous = false;

  if (m_source[0]->IsNonTerm()) {
    scope++;
    previousIsAmbiguous = true;
  }

  for (size_t i = 1; i < m_source.GetSize(); ++i) {
    const RuleSymbol *symbol = m_source[i];
    bool isAmbiguous = symbol->IsNonTerm();
    if (isAmbiguous) {
      // mixed syntax
      const NonTerm *nt = static_cast<const NonTerm*>(symbol);
      isAmbiguous = nt->IsHiero(Moses::Input, params);
    }

    if (isAmbiguous && previousIsAmbiguous) {
      scope++;
    }
    previousIsAmbiguous = isAmbiguous;
  }

  if (previousIsAmbiguous) {
    scope++;
  }

  return scope;

  /*
  int scope = 0;
  if (m_source.GetSize() > 1) {
    const RuleSymbol &front = *m_source.Front();
    if (front.IsNonTerm()) {
  	  ++scope;
    }

    const RuleSymbol &back = *m_source.Back();
    if (back.IsNonTerm()) {
  	  ++scope;
    }
  }
  return scope;
  */
}

template<typename T>
bool Contains(const T *sought, const set<const T*> &coll)
{
  std::set<const Word*>::const_iterator iter;
  for (iter = coll.begin(); iter != coll.end(); ++iter) {
    const Word *found = *iter;
    if (sought->CompareString(*found) == 0) {
      return true;
    }
  }
  return false;
}

bool Rule::ContainTerm(const ConsistentPhrase &cp, const std::set<const Word*> &terms) const
{
  const Phrase &sourceSentence = m_alignedSentence.GetPhrase(Moses::Input);

  for (int pos = cp.corners[0]; pos <= cp.corners[1]; ++pos) {
    const Word *soughtWord = sourceSentence[pos];

    // find same word in set
    if (Contains(soughtWord, terms)) {
      return true;
    }
  }
  return false;
}

bool CompareTargetNonTerms(const NonTerm *a, const NonTerm *b)
{
  // compare just start target pos
  return a->GetConsistentPhrase().corners[2] < b->GetConsistentPhrase().corners[2];
}

void Rule::CreateTarget(const Parameter &params)
{
  if (!m_isValid) {
    return;
  }

  vector<const NonTerm*> targetNonTerm(m_nonterms);
  std::sort(targetNonTerm.begin(), targetNonTerm.end(), CompareTargetNonTerms);

  const NonTerm *cp = NULL;
  size_t nonTermInd = 0;
  if (nonTermInd < targetNonTerm.size()) {
    cp = targetNonTerm[nonTermInd];
  }

  for (int targetPos = m_lhs.GetConsistentPhrase().corners[2];
       targetPos <= m_lhs.GetConsistentPhrase().corners[3];
       ++targetPos) {

    const RuleSymbol *ruleSymbol;
    if (cp && cp->GetConsistentPhrase().corners[2] <= targetPos && targetPos <= cp->GetConsistentPhrase().corners[3]) {
      // replace words with non-term
      ruleSymbol = cp;
      targetPos = cp->GetConsistentPhrase().corners[3];
      if (targetNonTerm.size()) {
        cp = targetNonTerm[nonTermInd];
      }

      // move to next non-term
      ++nonTermInd;
      cp = (nonTermInd < targetNonTerm.size()) ? targetNonTerm[nonTermInd] : NULL;
    } else {
      // terminal
      ruleSymbol = m_alignedSentence.GetPhrase(Moses::Output)[targetPos];
    }

    m_target.Add(ruleSymbol);
  }

  CreateAlignments();
}


void Rule::CreateAlignments()
{
  for (size_t sourcePos = 0; sourcePos < m_source.GetSize(); ++sourcePos) {
    const RuleSymbol *symbol = m_source[sourcePos];
    if (!symbol->IsNonTerm()) {
      // terminals
      const Word &sourceWord = static_cast<const Word&>(*symbol);
      const std::set<const Word *> &targetWords = sourceWord.GetAlignment();
      CreateAlignments(sourcePos, targetWords);
    } else {
      // non-terms. same object in both source & target
      CreateAlignments(sourcePos, symbol);
    }
  }
}

void Rule::CreateAlignments(int sourcePos, const std::set<const Word *> &targetWords)
{
  std::set<const Word *>::const_iterator iterTarget;
  for (iterTarget = targetWords.begin(); iterTarget != targetWords.end(); ++iterTarget) {
    const Word *targetWord = *iterTarget;
    CreateAlignments(sourcePos, targetWord);
  }
}

void Rule::CreateAlignments(int sourcePos, const RuleSymbol *targetSought)
{
  // should be in target phrase
  for (size_t targetPos = 0; targetPos < m_target.GetSize(); ++targetPos) {
    const RuleSymbol *foundSymbol = m_target[targetPos];
    if (targetSought == foundSymbol) {
      pair<int, int> alignPoint(sourcePos, targetPos);
      m_alignments.insert(alignPoint);
      return;
    }
  }

  throw "not found";
}

void Rule::CreateProperties(const Parameter &params)
{
  //cerr << Debug() << " " << m_nonterms.size() << endl;

  // span length
  if (params.spanLength && m_nonterms.size()) {
    stringstream strme;
    strme << "{{SpanLength ";

    for (size_t i = 0; i < m_nonterms.size(); ++i) {
      const NonTerm &nonTerm = *m_nonterms[i];
      const ConsistentPhrase &cp = nonTerm.GetConsistentPhrase();
      strme << i << "," << cp.GetWidth(Moses::Input) << "," << cp.GetWidth(Moses::Output) << " ";
    }
    strme << "}}";

    m_properties.push_back(strme.str());
  }

  if (params.ruleLength && m_nonterms.size()) {
    const ConsistentPhrase &cp = m_lhs.GetConsistentPhrase();

    stringstream strme;
    strme << "{{RuleLength ";
    strme << cp.GetWidth(Moses::Input);
    strme << "}}";

    m_properties.push_back(strme.str());
  }

  // non-term context (source)
  if (params.nonTermContext && m_nonterms.size()) {
    stringstream strme;
    strme << "{{NonTermContext ";

    int factor = params.nonTermContextFactor;

    for (size_t i = 0; i < m_nonterms.size(); ++i) {
      const NonTerm &nonTerm = *m_nonterms[i];
      const ConsistentPhrase &cp = nonTerm.GetConsistentPhrase();
      NonTermContext(1, factor, i, cp, strme);
    }
    strme << "}}";

    m_properties.push_back(strme.str());
  }

  // non-term context (target)
  if (params.nonTermContextTarget && m_nonterms.size()) {
    stringstream strme;
    strme << "{{NonTermContextTarget ";

    int factor = params.nonTermContextFactor;

    for (size_t i = 0; i < m_nonterms.size(); ++i) {
      const NonTerm &nonTerm = *m_nonterms[i];
      const ConsistentPhrase &cp = nonTerm.GetConsistentPhrase();
      NonTermContext(2, factor, i, cp, strme);
    }
    strme << "}}";

    m_properties.push_back(strme.str());
  }

}