From df54b988af6d1d3c4b930a0a666505e16cd0e93a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Paukert?= <paukeluk@fit.cvut.cz>
Date: Fri, 10 Apr 2020 18:27:22 +0200
Subject: [PATCH] Improved getting query

---
 querying/src/calculation/Space.cpp | 12 ------------
 querying/src/calculation/Space.h   |  8 --------
 querying/src/database/Database.cpp | 13 +++++++------
 querying/src/database/Database.h   |  6 +++---
 querying/src/ui/MainPage.cpp       |  2 +-
 5 files changed, 11 insertions(+), 30 deletions(-)

diff --git a/querying/src/calculation/Space.cpp b/querying/src/calculation/Space.cpp
index 77c608d..6b05dbf 100644
--- a/querying/src/calculation/Space.cpp
+++ b/querying/src/calculation/Space.cpp
@@ -17,15 +17,3 @@ const InvertedIndex &Space::operator[](const string &key) const {
 InvertedIndex &Space::getInvertedIndexByKey(const string &key) {
     return terms.at(key);
 }
-
-const map<string, double> Space::getTermsAndWeightsByID(Database & database, int document_id) {
-    map<string, double> terms;
-    vector<string> dummy = database.getTermsByDocumentID(document_id);
-
-    for (const string & term : dummy) {
-        InvertedIndex tmp = getInvertedIndexByKey(term);
-        terms[term] = tmp.getDocumentWeightByID(document_id);
-    }
-
-    return terms;
-}
diff --git a/querying/src/calculation/Space.h b/querying/src/calculation/Space.h
index d3e643a..f7eeadb 100644
--- a/querying/src/calculation/Space.h
+++ b/querying/src/calculation/Space.h
@@ -3,7 +3,6 @@
 
 
 #include <string>
-#include <set>
 #include <map>
 
 #include "InvertedIndex.h"
@@ -35,13 +34,6 @@ public:
      */
     InvertedIndex &getInvertedIndexByKey(const std::string &key);
 
-    /**
-     * @brief Finds all terms in DB which occurs in specific document
-     * @param database, document_id Instance of DB connection and document_id to process
-     * @return Map with terms as keys and weights as their values
-     */
-    const std::map<std::string, double> getTermsAndWeightsByID(Database & database, int document_id);
-
     /**
      * @brief Gets an element from @ref Space::terms with key @ref key
      * @param key Key of the element to find
diff --git a/querying/src/database/Database.cpp b/querying/src/database/Database.cpp
index 26887a4..6439c7f 100644
--- a/querying/src/database/Database.cpp
+++ b/querying/src/database/Database.cpp
@@ -24,17 +24,18 @@ Document Database::getDocumentByID(int id) {
     return {id, query.getColumn(0)};
 }
 
-vector<string> Database::getTermsByDocumentID(int document_id) {
-    vector<string> terms;  
-    SQLite::Statement query(db, "SELECT Term.value FROM Term "
+map<string, double> Database::getTermsAndWightsByDocumentID(int document_id) {
+    map<string, double> termsAndWeights;
+    SQLite::Statement query(db, "SELECT Term.value, TermDocumentOccurrence.weight FROM Term "
                                 "JOIN TermDocumentOccurrence ON Term.id = TermDocumentOccurrence.Term_id "
                                 "WHERE TermDocumentOccurrence.Document_id = :id");
     query.bind(":id", document_id);
 
-    while(query.executeStep())
-        terms.emplace_back(query.getColumn(0));
+    while(query.executeStep()) {
+        termsAndWeights[query.getColumn(0)] = query.getColumn(1);
+    }
 
-    return terms;
+    return termsAndWeights;
 }
 
 map<int, double> Database::getVectorSizes() {
diff --git a/querying/src/database/Database.h b/querying/src/database/Database.h
index 8e759c6..2bb4a66 100644
--- a/querying/src/database/Database.h
+++ b/querying/src/database/Database.h
@@ -31,11 +31,11 @@ public:
     Document getDocumentByID(int id);
 
     /**
-     * @brief Finds all terms from specified document
+     * @brief Finds all terms and their weights from specified document
      * @param document_id to process
-     * @return Vector with strings which are in specified document
+     * @return Map with terms as keys and weights as their values
      */
-    std::vector<std::string> getTermsByDocumentID(int document_id);
+    std::map<std::string, double> getTermsAndWightsByDocumentID(int document_id);
 
     /**
      * @brief Computes size of vector for every document in database
diff --git a/querying/src/ui/MainPage.cpp b/querying/src/ui/MainPage.cpp
index 716e4b7..03075ca 100644
--- a/querying/src/ui/MainPage.cpp
+++ b/querying/src/ui/MainPage.cpp
@@ -75,7 +75,7 @@ void MainPage::displayDetail(Space space, Wt::WContainerWidget * container, int
   Document document = database.getDocumentByID(document_id);
 
   // threshold je nyni nastaven na -1 --> ve vysledku budou i uplne rozdilne dokumenty
-  Query query(space.getTermsAndWeightsByID(database, document.id), -1);
+  Query query(database.getTermsAndWightsByDocumentID(document_id), -1);
   auto result = Computor(space, query).compute(database);
 
   // dodelat proklikavani na zobrazene podobne dokumenty
-- 
GitLab