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

Phrase.h « moses2 - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1007014837a9539cfca9f6cf39c61c03a1354919 (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
/*
 * PhraseImpl.h
 *
 *  Created on: 23 Oct 2015
 *      Author: hieu
 */

#pragma once

#include <cstddef>
#include <string>
#include <sstream>
#include <iostream>
#include "Word.h"
#include "MemPool.h"
#include "TypeDef.h"
#include "legacy/FactorCollection.h"
#include "SCFG/Word.h"

namespace Moses2
{

template<typename WORD>
class SubPhrase;

class Scores;
class PhraseTable;
class MemPool;
class System;

template<typename WORD>
class Phrase
{
public:
  virtual ~Phrase() {
  }
  virtual const WORD& operator[](size_t pos) const = 0;
  virtual size_t GetSize() const = 0;

  virtual const WORD& Back() const {
    return (*this)[GetSize() - 1];
  }

  virtual size_t hash() const {
    size_t seed = 0;

    for (size_t i = 0; i < GetSize(); ++i) {
      const WORD &word = (*this)[i];
      size_t wordHash = word.hash();
      boost::hash_combine(seed, wordHash);
    }

    return seed;
  }

  virtual bool operator==(const Phrase &compare) const {
    if (GetSize() != compare.GetSize()) {
      return false;
    }

    for (size_t i = 0; i < GetSize(); ++i) {
      const WORD &word = (*this)[i];
      const WORD &otherWord = compare[i];
      if (word != otherWord) {
        return false;
      }
    }

    return true;
  }

  virtual bool operator!=(const Phrase &compare) const {
    return !((*this) == compare);
  }

  virtual std::string GetString(const FactorList &factorTypes) const {
    if (GetSize() == 0) {
      return "";
    }

    std::stringstream ret;

    const WORD &word = (*this)[0];
    ret << word.GetString(factorTypes);
    for (size_t i = 1; i < GetSize(); ++i) {
      const WORD &word = (*this)[i];
      ret << " " << word.GetString(factorTypes);
    }
    return ret.str();
  }

  virtual SubPhrase<WORD> GetSubPhrase(size_t start, size_t size) const = 0;

  virtual std::string Debug(const System &system) const {
    std::stringstream out;
    size_t size = GetSize();
    if (size) {
      out << (*this)[0].Debug(system);
      for (size_t i = 1; i < size; ++i) {
        const WORD &word = (*this)[i];
        out << " " << word.Debug(system);
      }
    }

    return out.str();
  }

  virtual void OutputToStream(const System &system, std::ostream &out) const {
    size_t size = GetSize();
    if (size) {
      (*this)[0].OutputToStream(system, out);
      for (size_t i = 1; i < size; ++i) {
        const WORD &word = (*this)[i];
        out << " ";
        word.OutputToStream(system, out);
      }
    }
  }


};

////////////////////////////////////////////////////////////////////////
template<typename WORD>
class PhraseOrdererLexical
{
public:
  bool operator()(const Phrase<WORD> &a, const Phrase<WORD> &b) const {
    size_t minSize = std::min(a.GetSize(), b.GetSize());
    for (size_t i = 0; i < minSize; ++i) {
      const Word &aWord = a[i];
      const Word &bWord = b[i];
      int cmp = aWord.Compare(bWord);
      //std::cerr << "WORD: " << aWord << " ||| " << bWord << " ||| " << lessThan << std::endl;
      if (cmp) {
        return (cmp < 0);
      }
    }
    return a.GetSize() < b.GetSize();
  }
};

}