Welcome to mirror list, hosted at ThFree Co, Russian Federation.

main.cpp « publisher - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 108803d1a4cb636427c4f1ce47e3b7a1e2a3aaad (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "aard_dictionary.hpp"
#include "slof_indexer.hpp"
#include "../coding/bzip2_compressor.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_writer.hpp"
#include "../base/base.hpp"
#include "../base/assert.hpp"
#include "../base/logging.hpp"
#include "../base/stl_add.hpp"
#include "../std/algorithm.hpp"
#include "../std/bind.hpp"
#include "../std/fstream.hpp"
#include "../std/iterator.hpp"
#include "../std/string.hpp"
#include "../3party/gflags/src/gflags/gflags.h"
#include "../3party/jansson/myjansson.hpp"

DEFINE_int32(max_uncompressed_article_chunk_size, 899950,
             "Max size of chunk of articles, uncompressed.");
DEFINE_int32(compression_level, 9, "BZip2 compression level.");
DEFINE_string(input, "", "Input file.");
DEFINE_string(output, "", "Output dictionary file.");
DEFINE_string(article_file_suffix, "", "Suffix of the article files.");
DEFINE_string(redirects, "", "JSON file with redirects.");

void IndexAard(sl::SlofIndexer & indexer)
{
  FileReader inputReader(FLAGS_input.c_str());
  sl::AardDictionary inputDictionary(inputReader);

  LOG(LINFO, ("Starting indexing, keys:", inputDictionary.KeyCount()));
  for (uint32_t id = 0; id < inputDictionary.KeyCount(); ++id)
  {
    if ((id % 5000) == 0)
    {
      LOG(LINFO, (id, "done."));
    }
    // TODO: Handle redirects.
    // TODO: Handle several keys for article?
    string key, article;
    inputDictionary.KeyById(id, key);
    inputDictionary.ArticleById(id, article);
    if (article.empty())
    {
      LOG(LWARNING, ("Skipping empty article for:", key));
    }
    else
    {
      uint64_t const articleId = indexer.AddArticle(article);
      indexer.AddKey(key, articleId);
    }
  }
  LOG(LINFO, ("Logging stats."));
  indexer.LogStats();
  LOG(LINFO, ("Finishing indexing."));
}

void IndexJson(sl::SlofIndexer & indexer)
{
  vector<vector<string> > articles;
  LOG(LINFO, ("Reading list of articles."));
  {
    ifstream fin(FLAGS_input.c_str());
    string line;
    for (int i = 0; getline(fin, line); ++i)
    {
      if (line.empty())
        continue;

      my::Json root(line.c_str());
      CHECK_EQUAL(json_typeof(root), JSON_ARRAY, (i, line));
      CHECK_EQUAL(3, json_array_size(root), (i, line));
      articles.push_back(vector<string>());
      for (int j = 0; j < 3; ++j)
      {
        json_t * pJsonElement = json_array_get(root, j);
        CHECK(pJsonElement, (i, line));
        CHECK_EQUAL(json_typeof(pJsonElement), JSON_STRING, (i, j, line));
        articles.back().push_back(json_string_value(pJsonElement));
      }
    }
  }
  LOG(LINFO, ("Articles found:", articles.size()));

  LOG(LINFO, ("Sorting list of articles."));
  sort(articles.begin(), articles.end());

  LOG(LINFO, ("Adding articles."));
  map<string, uint64_t> keysToArticles;
  for (size_t i = 0; i < articles.size(); ++i)
  {
    string const url = articles[i][0];
    string const title = articles[i][1];
    string const fileName = articles[i][2] + FLAGS_article_file_suffix;
    articles[i].clear();

    FileReader articleReader(fileName);
    string article(static_cast<size_t>(articleReader.Size()), 0);
    articleReader.Read(0, &article[0], article.size());

    uint64_t const articleId = indexer.AddArticle(article);
    indexer.AddKey(title, articleId);
    CHECK(keysToArticles.insert(make_pair(title, articleId)).second, (i));

    if ((i & 127) == 0)
    {
      LOG(LINFO, ("Done:", i));
    }
  }
  articles.clear();

  LOG(LINFO, ("Adding redirects."));
  map<string, string> redirects;
  {
    ifstream fin(FLAGS_redirects.c_str());
    string line;
    for (int i = 0; getline(fin, line); ++i)
    {
      if (line.empty())
        continue;

      my::Json root(line.c_str());
      CHECK_EQUAL(json_typeof(root), JSON_ARRAY, (i, line));
      CHECK_EQUAL(2, json_array_size(root), (i, line));
      string s[2];
      for (int j = 0; j < 2; ++j)
      {
        json_t * pJsonElement = json_array_get(root, j);
        CHECK(pJsonElement, (i, j, line));
        CHECK_EQUAL(json_typeof(pJsonElement), JSON_STRING, (i, line));
        s[j] = json_string_value(pJsonElement);
      }
      CHECK(redirects.insert(make_pair(s[0], s[1])).second, (s[0], s[1]));
    }
  }
  for (map<string, string>::const_iterator it = redirects.begin(); it != redirects.end(); ++it)
  {
    string const & src = it->first;
    string dst = it->second;

    if (keysToArticles.count(src))
    {
      LOG(LWARNING, ("Conflicting redirect", src, dst));
      continue;
    }

    uint64_t articleId = -1;
    for (size_t depth = 0; articleId == -1 && !dst.empty() && depth < 5; ++depth)
    {
      articleId = ValueForKey(keysToArticles, dst, uint64_t(-1));
      dst = ValueForKey(redirects, dst, string());
    }

    if (articleId == -1)
      LOG(LWARNING, ("Redirect not found", it->first, it->second));
    else
      indexer.AddKey(src, articleId);
  }
}

int main(int argc, char ** argv)
{
  google::ParseCommandLineFlags(&argc, &argv, true);
  CHECK(!FLAGS_input.empty(), ());
  CHECK(!FLAGS_output.empty(), ());

  FileWriter outputWriter(FLAGS_output.c_str());
  {
    sl::SlofIndexer indexer(outputWriter,
                            FLAGS_max_uncompressed_article_chunk_size,
                            bind(&CompressBZip2, FLAGS_compression_level, _1, _2, _3));

    size_t const & inputSize = FLAGS_input.size();
    if (inputSize > 5 && FLAGS_input.substr(inputSize - 5) == ".aard")
      IndexAard(indexer);
    else if (inputSize > 5 && FLAGS_input.substr(inputSize - 5) == ".json")
      IndexJson(indexer);
    else
      CHECK(false, (FLAGS_input));

    LOG(LINFO, ("Finishing indexing."));
  }
  LOG(LINFO, ("Output size:", outputWriter.Pos()));
}