Skip to content

Commit 1b1a6f5

Browse files
author
nokuno
committedSep 6, 2012
refactoring
1 parent 9435377 commit 1b1a6f5

File tree

8 files changed

+13
-29
lines changed

8 files changed

+13
-29
lines changed
 

‎TODO

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
N-best output
2+
online model interpolation
3+
support multiple tables
4+
benchmark script

‎src/decoder.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace jsc {
44

5-
Decoder::Decoder()
6-
: model_(Model::GetModel()) {
5+
Decoder::Decoder(Model &model) : model_(model) {
76
}
87

98
Decoder::~Decoder() {
@@ -131,13 +130,5 @@ string ToStringDebug(vector<Node> &nodes) {
131130
}
132131
return oss.str();
133132
}
134-
135-
136-
// static
137-
static Decoder g_decoder;
138-
Decoder &Decoder::GetDecoder() {
139-
return g_decoder;
140-
}
141-
142133
}
143134

‎src/decoder.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace jsc {
1414

1515
class Decoder {
1616
public:
17-
Decoder();
17+
Decoder(Model &model);
1818
virtual ~Decoder();
1919

2020
bool Decode(string input, vector<Node> &nodes, bool label=true);
@@ -27,8 +27,6 @@ class Decoder {
2727
// 2) Backward: search the best path using the total cost
2828
bool Viterbi(Lattice &lattice, vector<Node> &result);
2929

30-
static Decoder &GetDecoder();
31-
3230
private:
3331
Model &model_;
3432
};

‎src/model.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,5 @@ void Model::Clear() {
126126
entries_.clear();
127127
offsets_.clear();
128128
}
129-
// static
130-
static Model g_model;
131-
Model &Model::GetModel() {
132-
return g_model;
133-
}
134-
135129
}
136130

‎src/model.h

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class Model {
3535
void Clear();
3636
uint32_t GetSize() { return (uint32_t)entries_.size(); }
3737

38-
// Get reference from global object.
39-
static Model &GetModel();
40-
4138
private:
4239
void PushResult(uint32_t source_id, string &query, vector<Ngram> &result);
4340
marisa::Trie source_trie_;

‎tools/jsc-decode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using namespace jsc;
33

44
void Run(string prefix, string format, bool label, string table_mode) {
5-
Model &model = Model::GetModel();
5+
Model model;
66

77
if (!model.LoadFromBinary(prefix.c_str())) {
88
cerr << "Model " << prefix << "is not found." << endl;
99
return;
1010
}
1111

12-
Decoder &decoder = Decoder::GetDecoder();
12+
Decoder decoder(model);
1313
Table table = Table();
1414
if (table_mode == "both" || table_mode == "on")
1515
table.Load(prefix + "table.txt");

‎tools/jsc-server.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
#include "jsc.h"
2323
using namespace jsc;
2424

25+
Model model;
26+
2527
void readcb(struct bufferevent *bev, void *ctx) {
2628
struct evbuffer *input, *output;
2729
char *line;
2830
size_t n;
2931
input = bufferevent_get_input(bev);
3032
output = bufferevent_get_output(bev);
3133

32-
Decoder &decoder = Decoder::GetDecoder();
34+
Decoder decoder(model);
3335

3436
while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
3537
vector<Node> result;
@@ -127,7 +129,6 @@ void run(int port) {
127129
}
128130

129131
int main(int argc, char **argv) {
130-
Model &model = Model::GetModel();
131132
string prefix = "./";
132133
string format = "segment";
133134
string table_mode = "both";

‎tools/jsc-test.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void TestBuildNgram() {
2222
void TestModel() {
2323
TEST_START();
2424

25-
Model &model = Model::GetModel();
25+
Model model;
2626
if (!model.LoadFromBinary(TEST_DATA)) {
2727
cerr << "missing " << TEST_DATA << endl;
2828
return;
@@ -50,9 +50,9 @@ void TestModel() {
5050
void TestDecoder() {
5151
TEST_START();
5252

53-
Decoder &decoder = Decoder::GetDecoder();
54-
Model &model = Model::GetModel();
53+
Model model;
5554
model.LoadFromBinary(TEST_DATA);
55+
Decoder decoder(model);
5656

5757
{
5858
vector<Node> result;

0 commit comments

Comments
 (0)
Please sign in to comment.