| 1 | // |
| 2 | // Copyright RIME Developers |
| 3 | // |
| 4 | #include <algorithm> |
| 5 | #include <iostream> |
| 6 | #include <rime/common.h> |
| 7 | #include "gram_db.h" |
| 8 | #include "gram_encoding.h" |
| 9 | #include "octagram.h" |
| 10 | |
| 11 | using namespace rime; |
| 12 | |
| 13 | int main(int argc, char* argv[]) { |
| 14 | string language = argc > 1 ? string(argv[1]) : kGrammarDefaultLanguage; |
| 15 | LOG(INFO) << "building grammar for language: " << language; |
| 16 | |
| 17 | vector<pair<string, double>> data; |
| 18 | while (std::cin) { |
| 19 | string key; |
| 20 | double value; |
| 21 | std::cin >> key; |
| 22 | if (key.empty()) |
| 23 | break; |
| 24 | std::cin >> value; |
| 25 | data.push_back({grammar::encode(key), value}); |
| 26 | } |
| 27 | std::sort(data.begin(), data.end()); |
| 28 | |
| 29 | GramDb db(path{language + kGramDbType.suffix}); |
| 30 | LOG(INFO) << "creating " << db.file_path(); |
| 31 | if (!db.Build(data) || !db.Save()) { |
| 32 | LOG(ERROR) << "failed to build " << db.file_path(); |
| 33 | return 1; |
| 34 | } |
| 35 | LOG(INFO) << "created: " << db.file_path(); |
| 36 | return 0; |
| 37 | } |