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

Phrase.cpp « OnDiskPt - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1933338b88be5c3512ce7ac00402372ede9809d (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
// $Id$
/***********************************************************************
 Moses - factored phrase-based, hierarchical and syntactic language decoder
 Copyright (C) 2009 Hieu Hoang

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/
#include <iostream>
#include "moses/Util.h"
#include "Phrase.h"
#include "util/exception.hh"

using namespace std;

namespace OnDiskPt
{


void Phrase::AddWord(WordPtr word)
{
  m_words.push_back(word);
}

void Phrase::AddWord(WordPtr word, size_t pos)
{
  UTIL_THROW_IF2(!(pos < m_words.size()),
                 "Trying to get word " << pos << " when phrase size is " << m_words.size());
  m_words.insert(m_words.begin() + pos + 1, word);
}

int Phrase::Compare(const Phrase &compare) const
{
  int ret = 0;
  for (size_t pos = 0; pos < GetSize(); ++pos) {
    if (pos >= compare.GetSize()) {
      // we're bigger than the other. Put 1st
      ret = -1;
      break;
    }

    const Word &thisWord = GetWord(pos)
                           ,&compareWord = compare.GetWord(pos);
    int wordRet = thisWord.Compare(compareWord);
    if (wordRet != 0) {
      ret = wordRet;
      break;
    }
  }

  if (ret == 0) {
    assert(compare.GetSize() >= GetSize());
    ret = (compare.GetSize() > GetSize()) ? 1 : 0;
  }
  return ret;
}

//! transitive comparison
bool Phrase::operator<(const Phrase &compare) const
{
  int ret = Compare(compare);
  return ret < 0;
}

bool Phrase::operator>(const Phrase &compare) const
{
  int ret = Compare(compare);
  return ret > 0;
}

bool Phrase::operator==(const Phrase &compare) const
{
  int ret = Compare(compare);
  return ret == 0;
}

void Phrase::DebugPrint(ostream &out, const Vocab &vocab) const
{
  for (size_t pos = 0; pos < GetSize(); ++pos) {
    const Word &word = GetWord(pos);
    word.DebugPrint(out, vocab);
    out << " ";
  }
}

std::ostream& operator<<(std::ostream &out, const Phrase &phrase)
{
  for (size_t pos = 0; pos < phrase.GetSize(); ++pos) {
    const Word &word = phrase.GetWord(pos);
    out << word << " ";
  }

  return out;
}

}