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

dumper.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8053344fa62393039ad3dd5d96c6fa92ce53f872 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "dumper.hpp"

#include "../indexer/search_delimiters.hpp"
#include "../indexer/search_string_utils.hpp"
#include "../indexer/classificator.hpp"
#include "../indexer/feature_processor.hpp"
#include "../indexer/search_trie.hpp"

#include "../coding/multilang_utf8_string.hpp"

#include "../base/logging.hpp"

#include "../std/algorithm.hpp"
#include "../std/bind.hpp"
#include "../std/iostream.hpp"
#include "../std/map.hpp"
#include "../std/queue.hpp"
#include "../std/vector.hpp"

namespace feature
{
  class TypesCollector
  {
    vector<uint32_t> m_currFeatureTypes;

  public:
    typedef map<vector<uint32_t>, size_t> value_type;
    value_type m_stats;
    size_t m_namesCount;
    size_t m_totalCount;

    TypesCollector() : m_namesCount(0), m_totalCount(0) {}

    void operator()(FeatureType & f, uint32_t)
    {
      ++m_totalCount;
      string s1, s2;
      f.GetPreferredDrawableNames(s1, s2);
      if (!s1.empty())
        ++m_namesCount;

      m_currFeatureTypes.clear();
      f.ForEachTypeRef(*this);
      CHECK(!m_currFeatureTypes.empty(), ("Feature without any type???"));
      pair<value_type::iterator, bool> found = m_stats.insert(make_pair(m_currFeatureTypes, 1));
      if (!found.second)
        found.first->second++;
    }

    void operator()(uint32_t type)
    {
      m_currFeatureTypes.push_back(type);
    }
  };

  template <class T>
  static bool SortFunc(T const & first, T const & second)
  {
    return first.second > second.second;
  }

  void DumpTypes(string const & fPath)
  {
    TypesCollector doClass;
    feature::ForEachFromDat(fPath, doClass);

    typedef pair<vector<uint32_t>, size_t> stats_elem_type;
    typedef vector<stats_elem_type> vec_to_sort;
    vec_to_sort vecToSort(doClass.m_stats.begin(), doClass.m_stats.end());
    sort(vecToSort.begin(), vecToSort.end(), &SortFunc<stats_elem_type>);

    for (vec_to_sort::iterator it = vecToSort.begin(); it != vecToSort.end(); ++it)
    {
      cout << it->second << " ";
      for (size_t i = 0; i < it->first.size(); ++i)
        cout << classif().GetFullObjectName(it->first[i]) << " ";
      cout << endl;
    }
    cout << "Total features: " << doClass.m_totalCount << endl;
    cout << "Features with names: " << doClass.m_namesCount << endl;
  }

  ///////////////////////////////////////////////////////////////////

  typedef map<int8_t, map<strings::UniString, pair<unsigned int, string> > > TokensContainerT;
  class PrefixesCollector
  {
  public:
    TokensContainerT m_stats;

    bool operator()(int8_t langCode, string const & name)
    {
      CHECK(!name.empty(), ("Feature name is empty"));

      vector<strings::UniString> tokens;
      search::SplitUniString(search::NormalizeAndSimplifyString(name),
                             MakeBackInsertFunctor(tokens), search::Delimiters());

      if (tokens.empty())
        return true;

      for (size_t i = 1; i < tokens.size(); ++i)
      {
        strings::UniString s;
        for (size_t numTokens = 0; numTokens < i; ++numTokens)
        {
          s.append(tokens[numTokens].begin(), tokens[numTokens].end());
          s.push_back(' ');
        }
        pair<TokensContainerT::mapped_type::iterator, bool> found =
            m_stats[langCode].insert(make_pair(s, make_pair(1U, name)));
        if (!found.second)
          found.first->second.first++;
      }
      return true;
    }

    void operator()(FeatureType & f, uint32_t)
    {
      f.ForEachNameRef(*this);
    }
  };

  static size_t const MIN_OCCURRENCE = 3;

  void Print(int8_t langCode, TokensContainerT::mapped_type const & container)
  {
    typedef pair<strings::UniString, pair<unsigned int, string> > NameElemT;
    typedef vector<NameElemT> VecToSortT;

    VecToSortT v(container.begin(), container.end());
    sort(v.begin(), v.end(), &SortFunc<NameElemT>);

    // do not display prefixes with low occurrences
    if (v[0].second.first > MIN_OCCURRENCE)
    {
      cout << "Language code: " << StringUtf8Multilang::GetLangByCode(langCode) << endl;

      for (VecToSortT::iterator it = v.begin(); it != v.end(); ++it)
      {
        if (it->second.first <= MIN_OCCURRENCE)
          break;
        cout << it->second.first << " " << strings::ToUtf8(it->first);
        cout << " \"" << it->second.second << "\"" << endl;
      }
    }
  }

  void DumpPrefixes(string const & fPath)
  {
    PrefixesCollector doClass;
    feature::ForEachFromDat(fPath, doClass);
    for (TokensContainerT::iterator it = doClass.m_stats.begin();
         it != doClass.m_stats.end(); ++it)
    {
      Print(it->first, it->second);
    }
  }

  struct SearchTokensCollector
  {
    priority_queue<pair<uint32_t, strings::UniString> > tokens;
    strings::UniString m_currentS;
    uint32_t m_currentCount;

    SearchTokensCollector() : m_currentS(), m_currentCount(0) {}

    void operator() (strings::UniString const & s, search::trie::ValueReader::ValueType const &)
    {
      if (m_currentS == s)
      {
        ++m_currentCount;
      }
      else
      {
        if (m_currentCount > 0)
        {
          tokens.push(make_pair(m_currentCount, m_currentS));
          if (tokens.size() > 100)
            tokens.pop();
        }
        m_currentS = s;
        m_currentCount = 0;
      }
    }

    void Finish()
    {
      if (m_currentCount > 0)
      {
        tokens.push(make_pair(m_currentCount, m_currentS));
        if (tokens.size() > 100)
          tokens.pop();
      }
    }
  };

  void DumpSearchTokens(string const & fPath)
  {
    FilesContainerR container(new FileReader(fPath));

    feature::DataHeader header;
    header.Load(container.GetReader(HEADER_FILE_TAG));

    serial::CodingParams cp(search::GetCPForTrie(header.GetDefCodingParams()));

    scoped_ptr<search::TrieIterator> pTrieRoot(
          ::trie::reader::ReadTrie(container.GetReader(SEARCH_INDEX_FILE_TAG),
                                   search::trie::ValueReader(cp),
                                   search::trie::EdgeValueReader()));

    SearchTokensCollector f;
    ::trie::ForEachRef(*pTrieRoot, f, strings::UniString());
    f.Finish();

    while (!f.tokens.empty())
    {
      strings::UniString const & s = f.tokens.top().second;
      cout << f.tokens.top().first << " '" << strings::ToUtf8(s) << "'" << endl;
      f.tokens.pop();
    }
  }

}  // namespace feature