highlighter application  1.1
HCE project utils : highlighter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
HighlightStringGenerator.cpp
Go to the documentation of this file.
1 
15 
16 using namespace HCE::exception;
17 
18 HighlightStringGenerator::HighlightStringGenerator():maxNumber(0xFFFFFFFF), wordMapPtr(nullptr), contentPtr(nullptr), hlBegin(""), hlEnd(""), highlightStringStickerPtr(nullptr)
19 {
20 }
21 
23 {
24 }
25 
29 std::vector<WordPos> HighlightStringGenerator::stickWordOffsetMaps(std::vector<std::map<unsigned int, WordPos> > &wordOffsetMaps)
30 {
31  std::vector<WordPos> ret;
32  std::map<unsigned int, WordPos> resultingMap;
33  std::map<unsigned int, WordPos>::iterator resultingMapIt;
34  if(wordOffsetMaps.size() > 0)
35  {
36  resultingMap = wordOffsetMaps[0];
37  for(unsigned int i = 1; i < wordOffsetMaps.size(); i++)
38  {
39  for(auto it = wordOffsetMaps[i].begin(); it != wordOffsetMaps[i].end(); ++it)
40  {
41  if(it->first != it->second.beginOffset)
42  {
43  throw HighlightAlgorithmException("Error !!! in <wordOffsetMaps>, there's key not correspond WordPos.beginOffset field");
44  }
45  if((resultingMapIt = resultingMap.find(it->first)) != resultingMap.end())
46  {
47  if(resultingMapIt->second.endOffset < it->second.endOffset)
48  {
49  resultingMapIt->second.endOffset = it->second.endOffset;
50  }
51  }
52  else
53  {
54  resultingMap.insert(std::pair<unsigned int, WordPos>(it->first, it->second));
55  }
56  }
57  }
58  joinOverlapElements(resultingMap, ret);
59  }
60  return ret;
61 }
62 
66 void HighlightStringGenerator::joinOverlapElements(std::map<unsigned int, WordPos> &resultingMap, std::vector<WordPos> &ret)
67 {
68  if(!resultingMap.empty())
69  {
70  ret.push_back(resultingMap.begin()->second);
71  unsigned int retIndex = 1;
72  auto it = resultingMap.begin();
73  ++it;
74  for(; it != resultingMap.end(); ++it)
75  {
76  if(it->second.beginOffset <= ret[retIndex - 1].endOffset)
77  {
78  if(it->second.endOffset > ret[retIndex - 1].endOffset)
79  {
80  ret[retIndex - 1].endOffset = it->second.endOffset;
81  }
82  }
83  else
84  {
85  ret.push_back(it->second);
86  retIndex++;
87  }
88  }
89  }
90 }
91 
98 std::string HighlightStringGenerator::highlightStringGenerate(std::vector<std::vector<std::pair<SearchType, std::string> > > &vecSearchStrings)
99 {
100  std::string ret;
101  if(!contentPtr.isNull() && !contentPtr->empty())
102  {
103  if(!highlightStringStickerPtr.isNull())
104  {
105  highlightStringStickerPtr->setHighlightedCounter(0);
106  }
107  std::vector<std::map<unsigned int, WordPos> > wordOffsetMaps;
108  for(unsigned int i = 0; i < vecSearchStrings.size(); i++)
109  {
110  if(vecSearchStrings[i].empty())
111  {
112  continue;
113  }
114  if(i >= highlightingAlgorithms.size() || highlightingAlgorithms[i].isNull())
115  {
116  std::string exceptionString = "Here no highlight Algorithm for [" + std::to_string(i) + "] Search string";
117  throw HighlightAlgorithmException(exceptionString);
118  }
119  std::map<unsigned int, WordPos> wordOffsetMap;
120  highlightingAlgorithms[i]->process(vecSearchStrings[i], contentPtr, wordMapPtr, wordOffsetMap);
121  wordOffsetMaps.push_back(wordOffsetMap);
122  }
123  std::vector<WordPos> wordOffsets = stickWordOffsetMaps(wordOffsetMaps);
124 // Here's place for cutting algorithm
125  if(!highlightStringStickerPtr.isNull())
126  {
127  ret = highlightStringStickerPtr->fillOutPutStr(wordOffsets, (*contentPtr));
128  }
129  }
130  return ret;
131 }
135 std::string HighlightStringGenerator::generateHighlightingAlgorithmsString(const std::string &separator)
136 {
137  std::string ret;
138  for(unsigned int i = 0; i < highlightingAlgorithms.size(); i++)
139  {
140  if(i > 0)
141  {
142  ret += separator;
143  }
144  ret += highlightingAlgorithms[i]->getName();
145  }
146  return ret;
147 }