From d84a23eb134a4ad6d24948894f82cfcff308b0a3 Mon Sep 17 00:00:00 2001 From: weirdwizardthomas <thomas.koristka@gmail.com> Date: Sat, 28 Mar 2020 21:10:26 +0100 Subject: [PATCH] Added an argument parser using cxxopts library --- querying/CMakeLists.txt | 14 ++++-- .../src/{inverted_index => }/Document.cpp | 0 querying/src/{inverted_index => }/Document.h | 0 querying/src/inverted_index/InvertedIndex.h | 2 +- querying/src/main.cpp | 33 ++++++++++--- querying/src/query/QueryJSONParser.cpp | 34 ++++++++++++++ querying/src/query/QueryJSONParser.h | 46 +++++++++++++++++++ 7 files changed, 118 insertions(+), 11 deletions(-) rename querying/src/{inverted_index => }/Document.cpp (100%) rename querying/src/{inverted_index => }/Document.h (100%) create mode 100644 querying/src/query/QueryJSONParser.cpp create mode 100644 querying/src/query/QueryJSONParser.h diff --git a/querying/CMakeLists.txt b/querying/CMakeLists.txt index 913dbf2..1b0f511 100644 --- a/querying/CMakeLists.txt +++ b/querying/CMakeLists.txt @@ -3,10 +3,16 @@ project(Querying) set(CMAKE_CXX_STANDARD 17) -add_subdirectory(lib/SQLiteCpp) - SET(CMAKE_CXX_FLAGS "-g -Wall -pedantic -Wextra") -add_executable(main src/main.cpp src/inverted_index/InvertedIndex.cpp src/inverted_index/InvertedIndex.h src/inverted_index/InvertedIndexJSONParser.cpp src/inverted_index/InvertedIndexJSONParser.h src/space/Space.cpp src/space/Space.h src/query/Query.cpp src/query/Query.h src/Computor.cpp src/Computor.h src/inverted_index/Document.cpp src/inverted_index/Document.h src/exceptions/Exceptions.h) +add_executable(main + src/main.cpp + src/inverted_index/InvertedIndex.cpp src/inverted_index/InvertedIndex.h + src/inverted_index/InvertedIndexJSONParser.cpp src/inverted_index/InvertedIndexJSONParser.h + src/space/Space.cpp src/space/Space.h + src/query/Query.cpp src/query/Query.h + src/Computor.cpp src/Computor.h + src/Document.cpp src/Document.h + src/exceptions/Exceptions.h src/query/QueryJSONParser.cpp src/query/QueryJSONParser.h src/argument_parser/argument_parser.cpp) -target_link_libraries(main SQLiteCpp) +target_link_libraries(main) diff --git a/querying/src/inverted_index/Document.cpp b/querying/src/Document.cpp similarity index 100% rename from querying/src/inverted_index/Document.cpp rename to querying/src/Document.cpp diff --git a/querying/src/inverted_index/Document.h b/querying/src/Document.h similarity index 100% rename from querying/src/inverted_index/Document.h rename to querying/src/Document.h diff --git a/querying/src/inverted_index/InvertedIndex.h b/querying/src/inverted_index/InvertedIndex.h index afcbd33..03a3800 100644 --- a/querying/src/inverted_index/InvertedIndex.h +++ b/querying/src/inverted_index/InvertedIndex.h @@ -4,7 +4,7 @@ #include <string> #include <deque> -#include "Document.h" +#include "../Document.h" /** * @brief A data class of a term's inverted index list diff --git a/querying/src/main.cpp b/querying/src/main.cpp index 6972a83..e104aa6 100644 --- a/querying/src/main.cpp +++ b/querying/src/main.cpp @@ -4,16 +4,37 @@ #include "query/Query.h" #include "space/Space.h" #include "Computor.h" +#include "../lib/cxxopts.hpp" +#include "query/QueryJSONParser.h" using namespace std; +using namespace cxxopts; -int main() { - Space space(InvertedIndexJSONParser("../../data/persistence/dummy.json").parse()); +int main(int argc, char **argv) { + Options options("Information retrieval - querying", "Queries against a collection."); + options.add_options() + ("q,query", "Search query", cxxopts::value<string>()) + ("c,collection", "Document collection", cxxopts::value<string>()) + ("t,threshold", "Document filter threshold", cxxopts::value<double>()->default_value("0.5")) + ("h,help", "Print usage"); - Query query({ - {"forest", 0.2}, - {"mountain", 0.1}, - {"nature", 0.8}}, 0.5); + + auto result = options.parse(argc, argv); + + if (result.count("help")) { + cout << options.help() << std::endl; + return EXIT_SUCCESS; + } + + if (!result.count("query") || !result.count("collection") || !result.count("threshold")) + return EXIT_FAILURE; + + auto threshold = result["threshold"].as<double>(); + auto queryPath = result["query"].as<string>(); + auto collectionPath = result["collection"].as<string>(); + + Space space(InvertedIndexJSONParser(collectionPath).parse()); + Query query(QueryJSONParser(queryPath).parse(), threshold); auto res = Computor(space, query).compute(); for (const auto &[id, value]: res) diff --git a/querying/src/query/QueryJSONParser.cpp b/querying/src/query/QueryJSONParser.cpp new file mode 100644 index 0000000..33aedd2 --- /dev/null +++ b/querying/src/query/QueryJSONParser.cpp @@ -0,0 +1,34 @@ +// +// Created by TomTom on 28.03.2020. +// + +#include "QueryJSONParser.h" + +using namespace std; + +QueryJSONParser::QueryJSONParser(string filePath) : + filePath(move(filePath)) {} + +map<string, double> QueryJSONParser::parse() { + json root = loadJsonFromFile(); + map<string, double> terms; + + for (const auto &[term, value]: root.items()) + terms.emplace(term, value); + + return terms; +} + + +nlohmann::json QueryJSONParser::loadJsonFromFile() { + fileStream = ifstream(filePath); + + if (fileStream.fail()) + throw invalid_argument("Invalid filepath to inverted list"); + + json root; + fileStream >> root; + + return root; +} + diff --git a/querying/src/query/QueryJSONParser.h b/querying/src/query/QueryJSONParser.h new file mode 100644 index 0000000..35dc2b3 --- /dev/null +++ b/querying/src/query/QueryJSONParser.h @@ -0,0 +1,46 @@ +// +// Created by TomTom on 28.03.2020. +// + +#ifndef QUERYING_QUERYJSONPARSER_H +#define QUERYING_QUERYJSONPARSER_H + +#include <iostream> +#include <fstream> + +#include "../../lib/json.hpp" + +using json = nlohmann::json; + +class QueryJSONParser { +private: + //Attributes------------- + std::ifstream fileStream; /**<File stream of the read JSON file*/ + const std::string filePath;/**<Path to the JSON file*/ + + //Methods---------------- + /** + * Loads a JSON from a file @ref filePath + * @throws invalid_argument if stream opening is not successful (i.e. file cannot be loaded) + * @return JSON structure + */ + nlohmann::json loadJsonFromFile(); + +public: + //Methods---------------- + /** + * Constructor + * @param filePath Path of the JSON file to be processed + */ + explicit QueryJSONParser(std::string filePath); + + /** + * Loads and parses a JSON file specified by @ref filePath + * @return Parsed data of the JSON file, + * where key = term name, value = inverted index list of the term + */ + std::map<std::string, double> parse(); +}; + + +#endif //QUERYING_QUERYJSONPARSER_H -- GitLab