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

ChartTranslationOptionList.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb1b8e2567ef3da6eaedf0eb0430166722639203 (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
// $Id$
/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2010 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 <algorithm>
#include <iostream>
#include "StaticData.h"
#include "ChartTranslationOptionList.h"
#include "ChartTranslationOption.h"
#include "ChartCellCollection.h"
#include "WordsRange.h"

namespace Moses
{

#ifdef USE_HYPO_POOL
ObjectPool<ChartTranslationOptionList> ChartTranslationOptionList::s_objectPool("ChartTranslationOptionList", 3000);
#endif

ChartTranslationOptionList::ChartTranslationOptionList(const WordsRange &range)
  :m_range(range)
{
  m_collection.reserve(200);
  m_scoreThreshold = std::numeric_limits<float>::infinity();
}

ChartTranslationOptionList::~ChartTranslationOptionList()
{
  RemoveAllInColl(m_collection);
}

class ChartTranslationOptionOrderer
{
public:
  bool operator()(const ChartTranslationOption* itemA, const ChartTranslationOption* itemB) const {
    return itemA->GetEstimateOfBestScore() > itemB->GetEstimateOfBestScore();
  }
};

void ChartTranslationOptionList::Add(const TargetPhraseCollection &targetPhraseCollection
                                     , const DottedRule &dottedRule
                                     , const ChartCellCollection &chartCellColl
                                     , bool /* adhereTableLimit */
                                     , size_t ruleLimit)
{
  if (targetPhraseCollection.IsEmpty()) {
    return;
  }

  if (m_collection.size() < ruleLimit) {
    // not yet filled out quota. add everything
    ChartTranslationOption *option = new ChartTranslationOption(
        targetPhraseCollection, dottedRule, m_range, chartCellColl);
    m_collection.push_back(option);
    float score = option->GetEstimateOfBestScore();
    m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold;
  }
  else {
    // full but not bursting. add if better than worst score
    ChartTranslationOption option(targetPhraseCollection, dottedRule,
                                  m_range, chartCellColl);
    float score = option.GetEstimateOfBestScore();
    if (score > m_scoreThreshold) {
      // dynamic allocation deferred until here on the assumption that most
      // options will score below the threshold.
      m_collection.push_back(new ChartTranslationOption(option));
    }
  }

  // prune if bursting
  if (m_collection.size() > ruleLimit * 2) {
    std::nth_element(m_collection.begin()
                     , m_collection.begin() + ruleLimit
                     , m_collection.end()
                     , ChartTranslationOptionOrderer());
    // delete the bottom half
    for (size_t ind = ruleLimit; ind < m_collection.size(); ++ind) {
      // make the best score of bottom half the score threshold
      float score = m_collection[ind]->GetEstimateOfBestScore();
      m_scoreThreshold = (score > m_scoreThreshold) ? score : m_scoreThreshold;
      delete m_collection[ind];
    }
    m_collection.resize(ruleLimit);
  }
}

void ChartTranslationOptionList::Add(ChartTranslationOption *transOpt)
{
  CHECK(transOpt);
  m_collection.push_back(transOpt);
}

void ChartTranslationOptionList::CreateChartRules(size_t ruleLimit)
{
  if (m_collection.size() > ruleLimit) {
    std::nth_element(m_collection.begin()
                     , m_collection.begin() + ruleLimit
                     , m_collection.end()
                     , ChartTranslationOptionOrderer());

    // delete the bottom half
    for (size_t ind = ruleLimit; ind < m_collection.size(); ++ind) {
      delete m_collection[ind];
    }
    m_collection.resize(ruleLimit);
  }
}


void ChartTranslationOptionList::Sort()
{
  // keep only those over best + threshold

  float scoreThreshold = -std::numeric_limits<float>::infinity();
  CollType::const_iterator iter;
  for (iter = m_collection.begin(); iter != m_collection.end(); ++iter) {
    const ChartTranslationOption *transOpt = *iter;
    float score = transOpt->GetEstimateOfBestScore();
    scoreThreshold = (score > scoreThreshold) ? score : scoreThreshold;
  }

  scoreThreshold += StaticData::Instance().GetTranslationOptionThreshold();

  size_t ind = 0;
  while (ind < m_collection.size()) {
    const ChartTranslationOption *transOpt = m_collection[ind];
    if (transOpt->GetEstimateOfBestScore() < scoreThreshold) {
      delete transOpt;
      m_collection.erase(m_collection.begin() + ind);
    } else {
      ind++;
    }
  }

  std::sort(m_collection.begin(), m_collection.end(), ChartTranslationOptionOrderer());
}

}