2010-11-06 03:40:16 +03:00
|
|
|
#include "lm/model.hh"
|
2013-01-18 19:58:54 +04:00
|
|
|
#include "lm/sizes.hh"
|
2010-12-08 06:15:37 +03:00
|
|
|
#include "util/file_piece.hh"
|
2013-01-18 19:58:54 +04:00
|
|
|
#include "util/usage.hh"
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2013-01-18 19:58:54 +04:00
|
|
|
#include <algorithm>
|
2011-02-24 22:37:39 +03:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <exception>
|
2010-09-15 01:33:11 +04:00
|
|
|
#include <iostream>
|
2010-12-08 06:15:37 +03:00
|
|
|
#include <iomanip>
|
2013-01-18 19:58:54 +04:00
|
|
|
#include <limits>
|
2010-12-08 06:15:37 +03:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-11-14 19:17:41 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "util/getopt.hh"
|
2012-09-28 18:04:48 +04:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2011-11-14 19:17:41 +04:00
|
|
|
#endif
|
|
|
|
|
2010-12-08 06:15:37 +03:00
|
|
|
namespace lm {
|
|
|
|
namespace ngram {
|
|
|
|
namespace {
|
|
|
|
|
2013-01-18 19:58:54 +04:00
|
|
|
void Usage(const char *name, const char *default_mem) {
|
|
|
|
std::cerr << "Usage: " << name << " [-u log10_unknown_probability] [-s] [-i] [-w mmap|after] [-p probing_multiplier] [-T trie_temporary] [-S trie_building_mem] [-q bits] [-b bits] [-a bits] [type] input.arpa [output.mmap]\n\n"
|
2011-07-14 00:53:18 +04:00
|
|
|
"-u sets the log10 probability for <unk> if the ARPA file does not have one.\n"
|
|
|
|
" Default is -100. The ARPA file will always take precedence.\n"
|
2011-05-17 20:43:05 +04:00
|
|
|
"-s allows models to be built even if they do not have <s> and </s>.\n"
|
2012-02-28 22:58:00 +04:00
|
|
|
"-i allows buggy models from IRSTLM by mapping positive log probability to 0.\n"
|
|
|
|
"-w mmap|after determines how writing is done.\n"
|
|
|
|
" mmap maps the binary file and writes to it. Default for trie.\n"
|
2012-06-28 18:58:59 +04:00
|
|
|
" after allocates anonymous memory, builds, and writes. Default for probing.\n"
|
|
|
|
"-r \"order1.arpa order2 order3 order4\" adds lower-order rest costs from these\n"
|
|
|
|
" model files. order1.arpa must be an ARPA file. All others may be ARPA or\n"
|
|
|
|
" the same data structure as being built. All files must have the same\n"
|
|
|
|
" vocabulary. For probing, the unigrams must be in the same order.\n\n"
|
2011-07-14 00:53:18 +04:00
|
|
|
"type is either probing or trie. Default is probing.\n\n"
|
2010-12-08 06:15:37 +03:00
|
|
|
"probing uses a probing hash table. It is the fastest but uses the most memory.\n"
|
|
|
|
"-p sets the space multiplier and must be >1.0. The default is 1.5.\n\n"
|
|
|
|
"trie is a straightforward trie with bit-level packing. It uses the least\n"
|
|
|
|
"memory and is still faster than SRI or IRST. Building the trie format uses an\n"
|
|
|
|
"on-disk sort to save memory.\n"
|
2013-01-18 19:58:54 +04:00
|
|
|
"-T is the temporary directory prefix. Default is the output file name.\n"
|
|
|
|
"-S determines memory use for sorting. Default is " << default_mem << ". This is compatible\n"
|
|
|
|
" with GNU sort. The number is followed by a unit: \% for percent of physical\n"
|
|
|
|
" memory, b for bytes, K for Kilobytes, M for megabytes, then G,T,P,E,Z,Y. \n"
|
|
|
|
" Default unit is K for Kilobytes.\n"
|
2011-06-27 02:21:44 +04:00
|
|
|
"-q turns quantization on and sets the number of bits (e.g. -q 8).\n"
|
2011-07-14 00:53:18 +04:00
|
|
|
"-b sets backoff quantization bits. Requires -q and defaults to that value.\n"
|
|
|
|
"-a compresses pointers using an array of offsets. The parameter is the\n"
|
|
|
|
" maximum number of bits encoded by the array. Memory is minimized subject\n"
|
|
|
|
" to the maximum, so pick 255 to minimize memory.\n\n"
|
|
|
|
"Get a memory estimate by passing an ARPA file without an output file name.\n";
|
2010-12-08 06:15:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I could really use boost::lexical_cast right about now.
|
|
|
|
float ParseFloat(const char *from) {
|
|
|
|
char *end;
|
|
|
|
float ret = strtod(from, &end);
|
|
|
|
if (*end) throw util::ParseNumberException(from);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
unsigned long int ParseUInt(const char *from) {
|
|
|
|
char *end;
|
|
|
|
unsigned long int ret = strtoul(from, &end, 10);
|
|
|
|
if (*end) throw util::ParseNumberException(from);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-27 02:21:44 +04:00
|
|
|
uint8_t ParseBitCount(const char *from) {
|
|
|
|
unsigned long val = ParseUInt(from);
|
|
|
|
if (val > 25) {
|
|
|
|
util::ParseNumberException e(from);
|
2012-02-28 22:58:00 +04:00
|
|
|
e << " bit counts are limited to 25.";
|
2011-06-27 02:21:44 +04:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-06-28 18:58:59 +04:00
|
|
|
void ParseFileList(const char *from, std::vector<std::string> &to) {
|
|
|
|
to.clear();
|
|
|
|
while (true) {
|
|
|
|
const char *i;
|
|
|
|
for (i = from; *i && *i != ' '; ++i) {}
|
|
|
|
to.push_back(std::string(from, i - from));
|
|
|
|
if (!*i) break;
|
|
|
|
from = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 02:21:44 +04:00
|
|
|
void ProbingQuantizationUnsupported() {
|
|
|
|
std::cerr << "Quantization is only implemented in the trie data structure." << std::endl;
|
|
|
|
exit(1);
|
2010-12-08 06:15:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ngram
|
|
|
|
} // namespace lm
|
|
|
|
} // namespace
|
2010-09-15 01:33:11 +04:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2011-02-24 22:37:39 +03:00
|
|
|
using namespace lm::ngram;
|
2010-12-08 06:15:37 +03:00
|
|
|
|
2013-01-18 19:58:54 +04:00
|
|
|
const char *default_mem = util::GuessPhysicalMemory() ? "80%" : "1G";
|
|
|
|
|
2011-03-21 17:40:21 +03:00
|
|
|
try {
|
2012-06-28 18:58:59 +04:00
|
|
|
bool quantize = false, set_backoff_bits = false, bhiksha = false, set_write_method = false, rest = false;
|
2011-03-21 17:40:21 +03:00
|
|
|
lm::ngram::Config config;
|
2013-01-18 19:58:54 +04:00
|
|
|
config.building_memory = util::ParseSize(default_mem);
|
2011-03-21 17:40:21 +03:00
|
|
|
int opt;
|
2013-01-18 19:58:54 +04:00
|
|
|
while ((opt = getopt(argc, argv, "q:b:a:u:p:t:T:m:S:w:sir:")) != -1) {
|
2011-03-21 17:40:21 +03:00
|
|
|
switch(opt) {
|
2011-06-27 02:21:44 +04:00
|
|
|
case 'q':
|
|
|
|
config.prob_bits = ParseBitCount(optarg);
|
|
|
|
if (!set_backoff_bits) config.backoff_bits = config.prob_bits;
|
|
|
|
quantize = true;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
config.backoff_bits = ParseBitCount(optarg);
|
|
|
|
set_backoff_bits = true;
|
|
|
|
break;
|
2011-07-14 00:53:18 +04:00
|
|
|
case 'a':
|
|
|
|
config.pointer_bhiksha_bits = ParseBitCount(optarg);
|
|
|
|
bhiksha = true;
|
2012-02-28 22:58:00 +04:00
|
|
|
break;
|
2011-03-21 17:40:21 +03:00
|
|
|
case 'u':
|
|
|
|
config.unknown_missing_logprob = ParseFloat(optarg);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
config.probing_multiplier = ParseFloat(optarg);
|
|
|
|
break;
|
2013-01-18 19:58:54 +04:00
|
|
|
case 't': // legacy
|
|
|
|
case 'T':
|
2011-03-21 17:40:21 +03:00
|
|
|
config.temporary_directory_prefix = optarg;
|
|
|
|
break;
|
2013-01-18 19:58:54 +04:00
|
|
|
case 'm': // legacy
|
2011-03-21 17:40:21 +03:00
|
|
|
config.building_memory = ParseUInt(optarg) * 1048576;
|
|
|
|
break;
|
2013-01-18 19:58:54 +04:00
|
|
|
case 'S':
|
|
|
|
config.building_memory = std::min(static_cast<uint64_t>(std::numeric_limits<std::size_t>::max()), util::ParseSize(optarg));
|
|
|
|
break;
|
2012-02-28 22:58:00 +04:00
|
|
|
case 'w':
|
|
|
|
set_write_method = true;
|
|
|
|
if (!strcmp(optarg, "mmap")) {
|
|
|
|
config.write_method = Config::WRITE_MMAP;
|
|
|
|
} else if (!strcmp(optarg, "after")) {
|
|
|
|
config.write_method = Config::WRITE_AFTER;
|
|
|
|
} else {
|
2013-01-18 19:58:54 +04:00
|
|
|
Usage(argv[0], default_mem);
|
2012-02-28 22:58:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2011-03-21 17:40:21 +03:00
|
|
|
case 's':
|
2011-05-23 06:23:01 +04:00
|
|
|
config.sentence_marker_missing = lm::SILENT;
|
2011-05-17 20:43:05 +04:00
|
|
|
break;
|
|
|
|
case 'i':
|
2011-05-23 06:23:01 +04:00
|
|
|
config.positive_log_probability = lm::SILENT;
|
2011-03-21 17:40:21 +03:00
|
|
|
break;
|
2012-06-28 18:58:59 +04:00
|
|
|
case 'r':
|
|
|
|
rest = true;
|
|
|
|
ParseFileList(optarg, config.rest_lower_files);
|
|
|
|
config.rest_function = Config::REST_LOWER;
|
|
|
|
break;
|
2011-03-21 17:40:21 +03:00
|
|
|
default:
|
2013-01-18 19:58:54 +04:00
|
|
|
Usage(argv[0], default_mem);
|
2011-03-21 17:40:21 +03:00
|
|
|
}
|
2011-02-24 22:37:39 +03:00
|
|
|
}
|
2011-06-27 02:21:44 +04:00
|
|
|
if (!quantize && set_backoff_bits) {
|
|
|
|
std::cerr << "You specified backoff quantization (-b) but not probability quantization (-q)" << std::endl;
|
|
|
|
abort();
|
|
|
|
}
|
2011-03-21 17:40:21 +03:00
|
|
|
if (optind + 1 == argc) {
|
|
|
|
ShowSizes(argv[optind], config);
|
2012-06-28 18:58:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char *model_type;
|
|
|
|
const char *from_file;
|
|
|
|
|
|
|
|
if (optind + 2 == argc) {
|
|
|
|
model_type = "probing";
|
|
|
|
from_file = argv[optind];
|
2011-03-21 17:40:21 +03:00
|
|
|
config.write_mmap = argv[optind + 1];
|
|
|
|
} else if (optind + 3 == argc) {
|
2012-06-28 18:58:59 +04:00
|
|
|
model_type = argv[optind];
|
|
|
|
from_file = argv[optind + 1];
|
2011-03-21 17:40:21 +03:00
|
|
|
config.write_mmap = argv[optind + 2];
|
2012-06-28 18:58:59 +04:00
|
|
|
} else {
|
2013-01-18 19:58:54 +04:00
|
|
|
Usage(argv[0], default_mem);
|
2012-06-28 18:58:59 +04:00
|
|
|
}
|
|
|
|
if (!strcmp(model_type, "probing")) {
|
|
|
|
if (!set_write_method) config.write_method = Config::WRITE_AFTER;
|
|
|
|
if (quantize || set_backoff_bits) ProbingQuantizationUnsupported();
|
|
|
|
if (rest) {
|
|
|
|
RestProbingModel(from_file, config);
|
|
|
|
} else {
|
2012-02-28 22:58:00 +04:00
|
|
|
ProbingModel(from_file, config);
|
2012-06-28 18:58:59 +04:00
|
|
|
}
|
|
|
|
} else if (!strcmp(model_type, "trie")) {
|
|
|
|
if (rest) {
|
|
|
|
std::cerr << "Rest + trie is not supported yet." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!set_write_method) config.write_method = Config::WRITE_MMAP;
|
|
|
|
if (quantize) {
|
|
|
|
if (bhiksha) {
|
|
|
|
QuantArrayTrieModel(from_file, config);
|
2011-06-27 02:21:44 +04:00
|
|
|
} else {
|
2012-06-28 18:58:59 +04:00
|
|
|
QuantTrieModel(from_file, config);
|
2011-06-27 02:21:44 +04:00
|
|
|
}
|
2011-03-21 17:40:21 +03:00
|
|
|
} else {
|
2012-06-28 18:58:59 +04:00
|
|
|
if (bhiksha) {
|
|
|
|
ArrayTrieModel(from_file, config);
|
|
|
|
} else {
|
|
|
|
TrieModel(from_file, config);
|
|
|
|
}
|
2011-03-21 17:40:21 +03:00
|
|
|
}
|
2010-12-08 06:15:37 +03:00
|
|
|
} else {
|
2013-01-18 19:58:54 +04:00
|
|
|
Usage(argv[0], default_mem);
|
2010-12-08 06:15:37 +03:00
|
|
|
}
|
2012-02-28 22:58:00 +04:00
|
|
|
}
|
|
|
|
catch (const std::exception &e) {
|
2011-03-21 17:40:21 +03:00
|
|
|
std::cerr << e.what() << std::endl;
|
2012-02-28 22:58:00 +04:00
|
|
|
std::cerr << "ERROR" << std::endl;
|
2011-07-14 00:53:18 +04:00
|
|
|
return 1;
|
2010-12-08 06:15:37 +03:00
|
|
|
}
|
2012-02-28 22:58:00 +04:00
|
|
|
std::cerr << "SUCCESS" << std::endl;
|
2011-02-24 22:37:39 +03:00
|
|
|
return 0;
|
2010-09-15 01:33:11 +04:00
|
|
|
}
|