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: 1d311af81a30f942283bba1de2ffc665862f05ce (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
#include "dumper.hpp"

#include "../indexer/feature_processor.hpp"
#include "../indexer/classificator.hpp"

#include "../std/vector.hpp"
#include "../std/unordered_map.hpp"
#include "../std/iostream.hpp"

namespace feature
{
  class TypesCollector
  { 
  public:
    typedef map<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;
      if (!f.GetPreferredDrawableName().empty())
        ++m_namesCount;

      f.ForEachTypeRef(*this);
    }

    void operator() (uint32_t type)
    {
      ++m_stats[type];
    }
  };

  struct GreaterSecond
  {
    template <class T> bool operator() (T const & t1, T const & t2)
    {
      return (t1.second > t2.second);
    }
  };

  namespace
  {
    void CallCollect(ClassifObject * p, vector<string> & path, TypesCollector & c);

    void CollectRecursive(ClassifObject * p, vector<string> & path, TypesCollector & c)
    {
      path.push_back(p->GetName());

      c(classif().GetTypeByPath(path));
      CallCollect(p, path, c);

      path.pop_back();
    }

    void CallCollect(ClassifObject * p, vector<string> & path, TypesCollector & c)
    {
      p->ForEachObject(bind(&CollectRecursive, _1, ref(path), ref(c)));
    }
  }

  void DumpTypes(string const & fPath)
  {
    // get all types from mwm file
    TypesCollector doClass;
    feature::ForEachFromDat(fPath, doClass);

    // add types from initial classificator (to get full mapping)
    vector<string> path;
    CallCollect(classif().GetMutableRoot(), path, doClass);

    // sort types by frequency
    typedef vector<pair<uint32_t, size_t> > vec_to_sort;
    vec_to_sort vecToSort(doClass.m_stats.begin(), doClass.m_stats.end());
    sort(vecToSort.begin(), vecToSort.end(), GreaterSecond());

    // print types to out stream
    Classificator & c = classif();
    for (vec_to_sort::iterator i = vecToSort.begin(); i != vecToSort.end(); ++i)
      cout << c.GetFullObjectName(i->first) << endl;

    //cout << "Total features: " << doClass.m_totalCount << endl;
    //cout << "Features with names: " << doClass.m_namesCount << endl;
  }

  class NamesCollector
  {
    typedef unordered_map<string, size_t> NamesContainerT;

    class LangsFunctor
    {
    public:
      vector<string> m_names;
      bool operator()(signed char, string const & name)
      {
        m_names.push_back(name);
        return true;
      }
    };

  public:
    NamesContainerT m_stats;
    void operator()(FeatureType & f, uint32_t)
    {
      LangsFunctor doLangs;
      f.ForEachNameRef(doLangs);
      for (size_t i = 0; i < doLangs.m_names.size(); ++i)
      {
        strings::SimpleTokenizer tok(doLangs.m_names[i], " ");
        while (tok)
        {
          pair<NamesContainerT::iterator, bool> found = m_stats.insert(make_pair(*tok, 1));
          if (!found.second)
            found.first->second++;
          ++tok;
        }
      }
    }
  };

  typedef pair<string, size_t> NameElemT;
  void DumpNames(string const & fPath)
  {
    NamesCollector doClass;
    feature::ForEachFromDat(fPath, doClass);

    typedef vector<NameElemT> VecToSortT;
    VecToSortT vecToSort(doClass.m_stats.begin(), doClass.m_stats.end());
    sort(vecToSort.begin(), vecToSort.end(), GreaterSecond());

    for (VecToSortT::iterator it = vecToSort.begin(); it != vecToSort.end(); ++it)
      cout << it->second << " " << it->first << endl;
  }
}