瀏覽代碼

[quickfix] some diff adjustments

B Thibault 9 年之前
父節點
當前提交
664649a4d4
共有 5 個文件被更改,包括 58 次插入3 次删除
  1. 4 0
      include/jsonPrimitive.hh
  2. 3 1
      include/levenshteinMatrice.hpp
  3. 21 0
      src/jsonPrimitive.cpp
  4. 19 2
      src/levenshtein.cpp
  5. 11 0
      test/testDiffObj.7.json

+ 4 - 0
include/jsonPrimitive.hh

@@ -14,6 +14,8 @@ class AJSonPrimitive
 {
     public:
         virtual ~AJSonPrimitive();
+        virtual std::string getTypeStr() const =0;
+        bool sameType(const AJSonPrimitive *other) const;
 };
 
 template <typename T>
@@ -32,6 +34,8 @@ class JSonPrimitive: public JSonElement, public AJSonPrimitive
         **/
         std::string stringify() const;
 
+        std::string getTypeStr() const;
+
         bool operator<(const JSonPrimitive<T> &other) const;
         bool operator==(const JSonPrimitive<T> &other) const;
 

+ 3 - 1
include/levenshteinMatrice.hpp

@@ -48,6 +48,7 @@ class LevenshteinMatriceWithScore: public LevenshteinMatrice_base
 {
     public:
         LevenshteinMatriceWithScore(float score, const JSonElement *a, const JSonElement *b);
+        LevenshteinMatriceWithScore(float score, const JSonElement *a, const JSonElement *b, bool sameType);
 
         std::map<const JSonElement *, const JSonElement *> getEquivalences() const;
         virtual const JSonElement * getEquivalence(const JSonElement *) const;
@@ -114,7 +115,8 @@ class LevenshteinMatrice: public LevenshteinMatrice_base
 
             result->levenDist = matrice[n][m];
             result->levenRelativeDist = 1 -(matrice[n][m] / std::max(n, m));
-            result->shortestPath<T>(matrice, subMatrice, n, m, --a, --b);
+            if (result->levenRelativeDist > LEVENSHTEIN_SENSIBILITY)
+                result->shortestPath<T>(matrice, subMatrice, n, m, --a, --b);
             cleanMatrice(matrice, subMatrice, n, m);
             return result;
         };

+ 21 - 0
src/jsonPrimitive.cpp

@@ -9,6 +9,9 @@
 AJSonPrimitive::~AJSonPrimitive()
 {}
 
+bool AJSonPrimitive::sameType(const AJSonPrimitive *o) const
+{ return getTypeStr() == o->getTypeStr(); }
+
 template<> JSonPrimitive<Null>::~JSonPrimitive() {}
 template<> JSonPrimitive<double>::~JSonPrimitive() {}
 template<> JSonPrimitive<bool>::~JSonPrimitive() {}
@@ -40,3 +43,21 @@ template<> std::string JSonPrimitive<int>::toString() const
 template<> std::string JSonPrimitive<bool>::toString() const
 { return value ? "true" : "false"; }
 
+template<> std::string JSonPrimitive<std::string>::getTypeStr() const
+{ return "str"; }
+
+template<> std::string JSonPrimitive<Null>::getTypeStr() const
+{ return "null"; }
+
+template<> std::string JSonPrimitive<double>::getTypeStr() const
+{ return "number"; }
+
+template<> std::string JSonPrimitive<long long>::getTypeStr() const
+{ return "number"; }
+
+template<> std::string JSonPrimitive<int>::getTypeStr() const
+{ return "number"; }
+
+template<> std::string JSonPrimitive<bool>::getTypeStr() const
+{ return "bool"; }
+

+ 19 - 2
src/levenshtein.cpp

@@ -1,6 +1,7 @@
 #include <climits>
 #include "levenshteinMatrice.hpp"
 #include "jsonObjectEntry.hh"
+#include "jsonPrimitive.hh"
 
 size_t levenshtein(const std::string &a, const std::string &b)
 {
@@ -90,13 +91,13 @@ LevenshteinMatrice_base *LevenshteinMatrice_base::Builder::build(const JSonEleme
 
         if (aIsObject && bIsObject)
         {
-            if (a->stringify() == b->stringify() && (*(const JSonObjectEntry&)(*a))->stringify() == (*(const JSonObjectEntry&)(*b))->stringify())
+            if (a->stringify() == b->stringify())
                 return LevenshteinMatrice_base::Builder::build(*(const JSonObjectEntry&)(*a), *(const JSonObjectEntry&)(*b));
             return new LevenshteinMatriceWithScore(0.f, a, b);
         }
         else if (aIsObject || bIsObject)
             return new LevenshteinMatriceWithScore(0.f, a, b);
-        return new LevenshteinMatriceWithScore(levenshteinPercent(a->stringify(), b->stringify()), a, b);
+        return new LevenshteinMatriceWithScore(levenshteinPercent(a->stringify(), b->stringify()), a, b, ((const AJSonPrimitive*)a)->sameType((const AJSonPrimitive *)b));
     }
 }
 
@@ -179,6 +180,22 @@ LevenshteinMatriceWithScore::LevenshteinMatriceWithScore(float s, const JSonElem
     operations[a] = operations[b] = (_result ? eLevenshteinOperator::equ : eLevenshteinOperator::add);
 }
 
+LevenshteinMatriceWithScore::LevenshteinMatriceWithScore(float s, const JSonElement *a, const JSonElement *b, bool sameType)
+{
+    _result = s > LEVENSHTEIN_SENSIBILITY;
+    if (_result)
+    {
+        equivalentA = a;
+        equivalentB = b;
+    }
+    else
+        equivalentA = equivalentB = nullptr;
+    if (_result)
+        operations[a] = operations[b] = (sameType ? eLevenshteinOperator::equ : eLevenshteinOperator::mod);
+    else
+        operations[a] = operations[b] = eLevenshteinOperator::mod;
+}
+
 const JSonElement * LevenshteinMatriceWithScore::getEquivalence(const JSonElement *a) const
 {
     if (equivalentA == a)

+ 11 - 0
test/testDiffObj.7.json

@@ -0,0 +1,11 @@
+{
+    "a": false
+    ,"b": false
+    ,"c": true
+    ,"d": {
+        "1": 12
+        ,"2": 13
+        ,"3": 14
+        ,"4": 15
+    }
+}