File size: 4,397 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
/***********************************************************************
  Moses - factored phrase-based language decoder
  Copyright (C) 2012- University of Edinburgh

  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
 ***********************************************************************/

/**
 * This contains extra features that can be added to the scorer. To add a new feature:
 * 1. Implement a subclass of ScoreFeature
 * 2. Updated ScoreFeatureManager.configure() to configure your feature, and usage() to
 *    display usage info.
 * 3. Write unit tests (see ScoreFeatureTest.cpp) and regression tests
**/

#pragma once

#include <string>
#include <map>
#include <vector>

#include <boost/shared_ptr.hpp>

#include "util/exception.hh"

#include "ExtractionPhrasePair.h"

namespace MosesTraining
{

struct MaybeLog {
  MaybeLog(bool useLog, float negativeLog):
    m_useLog(useLog), m_negativeLog(negativeLog) {}

  inline float operator() (float a) const {
    return m_useLog ? m_negativeLog*log(a) : a;
  }

  float m_useLog;
  float m_negativeLog;
};

class ScoreFeatureArgumentException : public util::Exception
{
public:
  ScoreFeatureArgumentException() throw() {
    *this << "Unable to configure features: ";
  }
  ~ScoreFeatureArgumentException() throw() {}
};

/** Passed to each feature to be used to calculate its values */
struct ScoreFeatureContext {
  ScoreFeatureContext(
    const ExtractionPhrasePair &thePhrasePair,
    const MaybeLog& theMaybeLog
  ) :
    phrasePair(thePhrasePair),
    maybeLog(theMaybeLog) {
  }

  const ExtractionPhrasePair &phrasePair;
  MaybeLog maybeLog;
};

/**
  * Abstract base class for extra features that can be added to the phrase table
  * during scoring.
  **/
class ScoreFeature
{
public:

  /** Some features might need to store properties in ExtractionPhrasePair,
   *  e.g. to pass along external information loaded by a feature
   *  which may distinguish several phrase occurrences based on sentence ID */
  virtual void addPropertiesToPhrasePair(ExtractionPhrasePair &phrasePair,
                                         float count,
                                         int sentenceId) const {};

  /** Add the values for this score feature. */
  virtual void add(const ScoreFeatureContext& context,
                   std::vector<float>& denseValues,
                   std::map<std::string,float>& sparseValues) const = 0;

  virtual ~ScoreFeature() {}

};

typedef boost::shared_ptr<ScoreFeature> ScoreFeaturePtr;
class ScoreFeatureManager
{
public:
  ScoreFeatureManager():
    m_includeSentenceId(false) {}

  /** To be appended to the score usage message */
  const std::string& usage() const;

  /** Pass the unused command-line arguments to configure the extra features */
  void configure(const std::vector<std::string> args);

  /** Some features might need to store properties in ExtractionPhrasePair,
   *  e.g. to pass along external information loaded by a feature
   *  which may distinguish several phrase occurrences based on sentence ID */
  void addPropertiesToPhrasePair(ExtractionPhrasePair &phrasePair,
                                 float count,
                                 int sentenceId) const;

  /** Add all the features */
  void addFeatures(const ScoreFeatureContext& context,
                   std::vector<float>& denseValues,
                   std::map<std::string,float>& sparseValues) const;

  const std::vector<ScoreFeaturePtr>& getFeatures() const {
    return m_features;
  }

  /** Do we need to include sentence ids in phrase pairs? */
  bool includeSentenceId() const {
    return m_includeSentenceId;
  }

private:
  std::vector<ScoreFeaturePtr> m_features;
  bool m_includeSentenceId;
};

}