summaryrefslogtreecommitdiff
path: root/decoder/network_decoder.cc
blob: aaa1842d287833f5357873df4ae5bbf12882e479 (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
#include <string>

#include "decoder.h"
#include "ff_register.h"
#include "filelib.h"
#include "verbose.h"
#include "viterbi.h"

#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
#include "nn.hpp"

using namespace std;

struct TheObserver : public DecoderObserver
{
  string translation;

  virtual void
  NotifyTranslationForest(const SentenceMetadata& smeta, Hypergraph* hg)
  {
    translation.clear();
    vector<WordID> trans;
    ViterbiESentence(*hg, &trans);
    translation = TD::GetString(trans);
  }
};

int send(nn::socket& sock, const string trans)
{
  cout << "sending translation '" << trans << "'" << endl;
  sock.send(trans.c_str(), trans.size()+1, 0);
}

bool
recv(nn::socket& sock, string& source)
{
  char *buf = NULL;
  sock.recv(&buf, NN_MSG, 0);
  if (buf) {
    string s(buf);
    source = s;

    return true;
  }

  return false;
}

void
loop(Decoder& decoder, nn::socket& sock)
{
  int to = 100;
  sock.setsockopt(NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to));
  TheObserver o;

  while(true)
  {
    string source;
    bool r = recv(sock, source);
    if (r) {
      cout << "received source '" << source << "'" << endl;
      decoder.Decode(source, &o);
      send(sock, o.translation);
    }
  }
}
  
int
main(int argc, char** argv)
{
  register_feature_functions();
  ReadFile f(argv[1]);
  Decoder decoder(f.stream());
  SetSilent(true);

  nn::socket sock(AF_SP, NN_PAIR);
  string url = "ipc:///tmp/network_decoder.ipc";
  sock.bind(url.c_str());

  loop(decoder, sock);

  return 0;
}