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

ranker.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05202a40d63354030eb092df64a87464d6767d45 (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
#pragma once

#include "search/cancel_exception.hpp"
#include "search/emitter.hpp"
#include "search/geocoder.hpp"
#include "search/intermediate_result.hpp"
#include "search/keyword_lang_matcher.hpp"
#include "search/locality_finder.hpp"
#include "search/mode.hpp"
#include "search/region_info_getter.hpp"
#include "search/result.hpp"
#include "search/reverse_geocoder.hpp"
#include "search/search_params.hpp"
#include "search/suggest.hpp"
#include "search/utils.hpp"

#include "indexer/categories_holder.hpp"
#include "indexer/feature_decl.hpp"

#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"

#include "base/string_utils.hpp"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <string>
#include <vector>

class CategoriesHolder;
class DataSource;

namespace storage
{
class CountryInfoGetter;
}  // namespace storage

namespace search
{
class CitiesBoundariesTable;
class RankerResultMaker;
class VillagesCache;

class Ranker
{
public:
  struct Params
  {
    int8_t m_currentLocaleCode = CategoriesHolder::kEnglishCode;
    m2::RectD m_viewport;
    m2::PointD m_pivot;
    std::string m_pivotRegion;
    std::vector<uint32_t> m_preferredTypes;
    bool m_suggestsEnabled = false;
    bool m_needAddress = false;
    bool m_needHighlighting = false;
    bool m_viewportSearch = false;
    bool m_categorialRequest = false;

    std::string m_query;
    QueryTokens m_tokens;
    // Prefix of the last token in the query.
    // We need it here to make suggestions.
    strings::UniString m_prefix;

    m2::PointD m_accuratePivotCenter = m2::PointD(0, 0);

    // Minimal distance between search results in meters, needed for
    // filtering of identical search results.
    double m_minDistanceBetweenResultsM = 100.0;

    Locales m_categoryLocales;

    // The maximum number of results in a single emit.
    size_t m_batchSize = 0;

    // The maximum total number of results to be emitted in all batches.
    size_t m_limit = 0;
  };

  Ranker(DataSource const & dataSource, CitiesBoundariesTable const & boundariesTable,
         storage::CountryInfoGetter const & infoGetter, KeywordLangMatcher & keywordsScorer,
         Emitter & emitter, CategoriesHolder const & categories,
         std::vector<Suggest> const & suggests, VillagesCache & villagesCache,
         base::Cancellable const & cancellable);
  virtual ~Ranker() = default;

  void Init(Params const & params, Geocoder::Params const & geocoderParams);

  void Finish(bool cancelled);

  bool IsFull() const { return m_emitter.GetResults().GetCount() >= m_params.m_limit; }

  // Makes the final result that is shown to the user from a ranker's result.
  // |needAddress| and |needHighlighting| enable filling of optional fields
  // that may take a considerable amount of time to compute.
  Result MakeResult(RankerResult const & r, bool needAddress, bool needHighlighting) const;

  void SuggestStrings();

  virtual void AddPreRankerResults(std::vector<PreRankerResult> && preRankerResults)
  {
    std::move(preRankerResults.begin(), preRankerResults.end(),
              std::back_inserter(m_preRankerResults));
  }

  virtual void UpdateResults(bool lastUpdate);

  void ClearCaches();

  void BailIfCancelled() { ::search::BailIfCancelled(m_cancellable); }

  void SetLocale(std::string const & locale);

  void LoadCountriesTree();

private:
  friend class RankerResultMaker;

  void MakeRankerResults();

  void GetBestMatchName(FeatureType & f, std::string & name) const;
  void MatchForSuggestions(strings::UniString const & token, int8_t locale,
                           std::string const & prolog);
  void ProcessSuggestions(std::vector<RankerResult> & vec) const;

  std::string GetLocalizedRegionInfoForResult(RankerResult const & result) const;

  Params m_params;
  Geocoder::Params m_geocoderParams;
  ReverseGeocoder const m_reverseGeocoder;
  base::Cancellable const & m_cancellable;
  KeywordLangMatcher & m_keywordsScorer;

  mutable LocalityFinder m_localities;
  RegionInfoGetter m_regionInfoGetter;

  DataSource const & m_dataSource;
  storage::CountryInfoGetter const & m_infoGetter;
  Emitter & m_emitter;
  CategoriesHolder const & m_categories;
  std::vector<Suggest> const & m_suggests;

  std::vector<PreRankerResult> m_preRankerResults;
  std::vector<RankerResult> m_tentativeResults;
};
}  // namespace search