summaryrefslogtreecommitdiff
path: root/mteval/external_scorer.h
blob: 59ece269ad56b93c4b27fa01f3eb65b5ab3228c5 (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
#ifndef _EXTERNAL_SCORER_H_
#define _EXTERNAL_SCORER_H_

#include <vector>
#include <cstdio>
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>

#include "scorer.h"

class ScoreServer {
  friend class ScoreServerManager;
 protected:
  explicit ScoreServer(const std::string& cmd);
  virtual ~ScoreServer();

 public:
  float ComputeScore(const std::vector<float>& fields);
  void Evaluate(const std::vector<std::vector<WordID> >& refs, const std::vector<WordID>& hyp, std::vector<float>* fields);

 private:
  void RequestResponse(const std::string& request, std::string* response);
  FILE* pipe_;
};

struct ScoreServerManager {
  static ScoreServer* Instance(const std::string& score_type);
 private:
  static std::map<std::string, boost::shared_ptr<ScoreServer> > servers_;
};

class ExternalSentenceScorer : public SentenceScorer {
 public:
  ExternalSentenceScorer(ScoreServer* server, const std::vector<std::vector<WordID> >& r) :
    SentenceScorer("External", r), eval_server(server) {}
  virtual ScoreP ScoreCandidate(const Sentence& hyp) const;
  virtual ScoreP ScoreCCandidate(const Sentence& hyp) const;
  static ScoreP ScoreFromString(ScoreServer* s, const std::string& data);

 protected:
  ScoreServer* eval_server;
};

#endif