From a6269f75f300318977ef2ced2fd78cdafe6a4edb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Paukert?= <paukeluk@fit.cvut.cz>
Date: Sat, 14 Mar 2020 21:54:30 +0100
Subject: [PATCH] Save inverted index as JSON file

---
 src/weight_calculation/main.cpp | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/weight_calculation/main.cpp b/src/weight_calculation/main.cpp
index 6ffb2e5..9c51b5e 100644
--- a/src/weight_calculation/main.cpp
+++ b/src/weight_calculation/main.cpp
@@ -13,19 +13,27 @@ bool calculateWeight(SQLite::Database & db, std::ofstream & ostream, const json
     uint32_t occurrences;
     double weight;
     maxOccurrences[term].get_to(occurrences);
+    bool firstOccurrence = true;
 
     try
     {
-        SQLite::Statement query(db, "SELECT * FROM TermDocumentOccurrence JOIN Term ON TermDocumentOccurrence.Term_id = Term.id WHERE Term.value = :term");
+        SQLite::Statement query(db, "SELECT * FROM TermDocumentOccurrence "
+                                        "JOIN Term ON TermDocumentOccurrence.Term_id = Term.id "
+                                        "WHERE Term.value = :term "
+                                        "ORDER BY TermDocumentOccurrence.Document_id ASC");
         query.bind(":term", term);
 
-        ostream << term << ":";
+        ostream << "\"" << term << "\":{";
         while(query.executeStep())
         {
+            if (!firstOccurrence)
+                ostream << ",";
+            firstOccurrence = false;
+
             weight = query.getColumn("count").getInt() / (occurrences*1.0);
-            ostream << " " << query.getColumn("Document_id") << " " << std::setprecision(20) << weight;
+            ostream << "\"" << query.getColumn("Document_id") << "\":" << std::setprecision(20) << weight;
         }
-        ostream << std::endl;
+        ostream << "}";
     }
 
     catch(const std::exception& e)
@@ -43,10 +51,20 @@ bool process(std::ofstream & ostream, const json & maxOccurrences)
     {
         SQLite::Database db("./../data/persistance/db", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
         SQLite::Statement query(db, "SELECT value FROM Term");
+        bool firstTerm = true;
 
+        ostream << "{";
         while (query.executeStep())
+        {
+            if(!firstTerm)
+                ostream << ",";
+            firstTerm = false;
+
             if(!calculateWeight(db, ostream, maxOccurrences, query.getColumn("value")))
                 return false;
+        }
+
+        ostream << "}";
     }
 
     catch(const std::exception& e)
-- 
GitLab