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
|
#include "unigrams.h"
#include <string>
#include <cmath>
#include "stringlib.h"
#include "filelib.h"
using namespace std;
void UnigramModel::LoadUnigrams(const string& fname) {
cerr << "Loading unigram probabilities from " << fname << " ..." << endl;
ReadFile rf(fname);
string line;
istream& in = *rf.stream();
assert(in);
getline(in, line);
assert(line.empty());
getline(in, line);
assert(line == "\\data\\");
getline(in, line);
size_t pos = line.find("ngram 1=");
assert(pos == 0);
assert(line.size() > 8);
const size_t num_unigrams = atoi(&line[8]);
getline(in, line);
assert(line.empty());
getline(in, line);
assert(line == "\\1-grams:");
for (size_t i = 0; i < num_unigrams; ++i) {
getline(in, line);
assert(line.size() > 0);
pos = line.find('\t');
assert(pos > 0);
assert(pos + 1 < line.size());
const WordID w = TD::Convert(line.substr(pos + 1));
line[pos] = 0;
float p = atof(&line[0]);
if (w < probs_.size()) probs_[w].logeq(p * log(10)); else cerr << "WARNING: don't know about '" << TD::Convert(w) << "'\n";
}
}
void UnigramWordModel::LoadUnigrams(const string& fname) {
cerr << "Loading unigram probabilities from " << fname << " ..." << endl;
ReadFile rf(fname);
string line;
istream& in = *rf.stream();
assert(in);
getline(in, line);
assert(line.empty());
getline(in, line);
assert(line == "\\data\\");
getline(in, line);
size_t pos = line.find("ngram 1=");
assert(pos == 0);
assert(line.size() > 8);
const size_t num_unigrams = atoi(&line[8]);
getline(in, line);
assert(line.empty());
getline(in, line);
assert(line == "\\1-grams:");
for (size_t i = 0; i < num_unigrams; ++i) {
getline(in, line);
assert(line.size() > 0);
pos = line.find('\t');
assert(pos > 0);
assert(pos + 1 < line.size());
size_t cur = pos + 1;
vector<WordID> w;
while (cur < line.size()) {
const size_t len = UTF8Len(line[cur]);
w.push_back(TD::Convert(line.substr(cur, len)));
cur += len;
}
line[pos] = 0;
float p = atof(&line[0]);
probs_[w].logeq(p * log(10.0));
}
}
|