Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
candidate-set-proto-writer.C
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
36 
38 
39 namespace reranker {
40 
41 void
43  CandidateSetMessage *candidate_set_message)
44  const {
45  for (CandidateSet::const_iterator it = set.begin(); it != set.end(); ++it) {
46  // Serialize each Candidate to a CandidateMessage.
47  // TODO(dbikel): Possibly create CandidateWriter class to handle this.
48  CandidateMessage *candidate_message =
49  candidate_set_message->add_candidate();
50  const Candidate &candidate = *(*it);
51  if (candidate.raw_data() != "") {
52  candidate_message->set_raw_data(candidate.raw_data());
53  }
54 
55  // Add loss.
56  ScoreMessage *loss_message = candidate_message->add_score();
57  loss_message->set_type(ScoreMessage::LOSS);
58  loss_message->set_score(candidate.loss());
59  // Add model score.
60  ScoreMessage *model_score_message = candidate_message->add_score();
61  model_score_message->set_type(ScoreMessage::OUTPUT_SCORE);
62  model_score_message->set_score(candidate.score());
63  // Add baseline score.
64  ScoreMessage *baseline_score_message = candidate_message->add_score();
65  baseline_score_message->set_type(ScoreMessage::SYSTEM_SCORE);
66  baseline_score_message->set_score(candidate.baseline_score());
67 
68  // Add features.
69  FeatureVecMessage *fv_message = candidate_message->mutable_feats();
70  fv_writer_.Write(candidate.features(), FeatureMessage::BASIC, fv_message);
71  symbolic_fv_writer_.Write(candidate.symbolic_features(),
72  FeatureMessage::BASIC, fv_message);
73  }
74 
75  // Add gold index and best-scoring indices.
76  candidate_set_message->set_gold_index(set.gold_index());
77  candidate_set_message->set_best_scoring_index(set.best_scoring_index());
78 
79  candidate_set_message->set_source_key(set.training_key());
80  candidate_set_message->set_reference_string(set.reference_string());
81 }
82 
83 } // namespace reranker
const string & reference_string() const
double score() const
Returns the reranker’s score for this candidate.
Definition: candidate.H:131
void Write(const CandidateSet &set, CandidateSetMessage *candidate_set_message) const
Serializes a CandidateSet instance to a CandidateSetMessage.
const string & raw_data() const
Returns the raw data (typically the sentence) for this candidate.
Definition: candidate.H:143
const string & training_key() const
Definition: candidate-set.H:99
const FeatureVector< string, double > & symbolic_features() const
Returns the symbolic feature vector for this candidate.
Definition: candidate.H:139
double baseline_score() const
Returns the baseline model score for this candidate.
Definition: candidate.H:133
Serializer for reranker::CandidateSet instances to CandidateSetMessage instances. ...
A class to hold a set of candidates, either for training or test.
Definition: candidate-set.H:62
A class to represent a candidate in a set of candidates that constitutes a training instance for a re...
Definition: candidate.H:60
vector< shared_ptr< Candidate > >::const_iterator const_iterator
Definition: candidate-set.H:74
size_t best_scoring_index() const
Definition: candidate-set.H:96
const_iterator begin() const
Definition: candidate-set.H:86
double loss() const
Returns the loss of this candidate.
Definition: candidate.H:129
void set_raw_data(const string &raw_data)
Sets the raw data (typically the sentence) for this candidate).
Definition: candidate.H:151
const_iterator end() const
Definition: candidate-set.H:88
size_t gold_index() const
Definition: candidate-set.H:97
const FeatureVector< int, double > & features() const
Returns the feature vector for this candidate.
Definition: candidate.H:137