JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
PathJaccard.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 09/07/18.
3 //
4 
5 #ifndef JODA_PATHJACCARD_H
6 #define JODA_PATHJACCARD_H
7 
8 #include <joda/misc/RJFwd.h>
9 #include <rapidjson/istreamwrapper.h>
10 #include <algorithm>
11 #include <string>
12 #include <vector>
13 #include "../../../../src/RJPathsReader.h"
14 #include "IJSONSimilarityMeasure.h"
15 
16 class PathJaccard;
17 template <>
19  bool is_implemented = true;
21  typedef std::vector<std::string> Representation;
22 
23  inline const Representation getRepresentation(const RJDocument &lhs) const {
24  Representation paths;
25  paths.reserve(JODA_DEFAULT_PATH_DEPTH);
26  std::string prefix;
27  getPaths(lhs, prefix, paths);
28  std::sort(paths.begin(), paths.end());
29  return paths;
30  }
31 
32  inline const Representation getRepresentation(const std::string &lhs) {
33  handler.clear();
34  rapidjson::Reader reader;
35  rapidjson::StringStream stream(lhs.c_str());
36  reader.Parse(stream, handler);
37  auto paths = handler.getPaths();
38  std::sort(paths.begin(), paths.end());
39  return paths;
40  }
41 
43  rapidjson::IStreamWrapper &lhs) {
44  handler.clear();
45  rapidjson::Reader reader;
46  reader.Parse(lhs, handler);
47  auto paths = handler.getPaths();
48  return paths;
49  }
50 
51  template <class T>
52  inline static void getPaths(const T &doc, std::string &prefix,
53  std::vector<std::string> &set) {
54  if (doc.IsObject()) {
55  for (const auto &m : doc.GetObject()) {
56  std::string path = prefix + "/" + m.name.GetString();
57  set.emplace_back(path);
58  if (m.value.IsObject() || m.value.IsArray()) {
59  getPaths(m.value, path, set);
60  }
61  }
62  } else if (doc.IsArray()) {
63  int i = 0;
64  for (const auto &item : doc.GetArray()) {
65  std::string path = prefix + "/" + std::to_string(i);
66  i++;
67  if (item.IsObject() || item.IsArray()) {
68  getPaths(item, path, set);
69  }
70  }
71  }
72  }
73 };
74 
80  public:
81  double measure(const RJDocument &lhs, const RJDocument &rhs) override;
82  static double measure(
85 };
86 
87 #endif // JODA_PATHJACCARD_H
rapidjson::GenericDocument< RJChar, RJMemoryPoolAlloc, RJBaseAlloc > RJDocument
Definition: RJFwd.h:28
#define JODA_DEFAULT_PATH_DEPTH
Definition: RJPathsReader.h:11
Definition: IJSONSimilarityMeasure.h:24
Definition: PathJaccard.h:79
double measure(const RJDocument &lhs, const RJDocument &rhs) override
Definition: PathJaccard.cpp:8
Definition: RJPathsReader.h:14
const std::vector< std::string > & getPaths() const
Definition: RJPathsReader.cpp:114
void clear()
Definition: RJPathsReader.cpp:134
std::vector< std::string > Representation
Definition: PathJaccard.h:21
const Representation getRepresentation(const RJDocument &lhs) const
Definition: PathJaccard.h:23
const Representation getRepresentation(rapidjson::IStreamWrapper &lhs)
Definition: PathJaccard.h:42
RJPathsReader handler
Definition: PathJaccard.h:20
const Representation getRepresentation(const std::string &lhs)
Definition: PathJaccard.h:32
static void getPaths(const T &doc, std::string &prefix, std::vector< std::string > &set)
Definition: PathJaccard.h:52
Definition: IJSONSimilarityMeasure.h:36
bool is_implemented
Definition: IJSONSimilarityMeasure.h:56