Skip to content
Snippets Groups Projects
Commit a6269f75 authored by Lukáš Paukert's avatar Lukáš Paukert
Browse files

Save inverted index as JSON file

parent e9ca83d0
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment