Skip to content
Snippets Groups Projects
Commit d84a23eb authored by weirdwizardthomas's avatar weirdwizardthomas
Browse files

Added an argument parser using cxxopts library

parent 0ee245fd
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,16 @@ project(Querying) ...@@ -3,10 +3,16 @@ project(Querying)
   
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
   
add_subdirectory(lib/SQLiteCpp)
SET(CMAKE_CXX_FLAGS "-g -Wall -pedantic -Wextra") 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)
File moved
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
   
#include <string> #include <string>
#include <deque> #include <deque>
#include "Document.h" #include "../Document.h"
   
/** /**
* @brief A data class of a term's inverted index list * @brief A data class of a term's inverted index list
......
...@@ -4,16 +4,37 @@ ...@@ -4,16 +4,37 @@
#include "query/Query.h" #include "query/Query.h"
#include "space/Space.h" #include "space/Space.h"
#include "Computor.h" #include "Computor.h"
#include "../lib/cxxopts.hpp"
#include "query/QueryJSONParser.h"
   
using namespace std; using namespace std;
using namespace cxxopts;
   
int main() { int main(int argc, char **argv) {
Space space(InvertedIndexJSONParser("../../data/persistence/dummy.json").parse()); 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}, auto result = options.parse(argc, argv);
{"mountain", 0.1},
{"nature", 0.8}}, 0.5); 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(); auto res = Computor(space, query).compute();
for (const auto &[id, value]: res) for (const auto &[id, value]: res)
......
//
// 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;
}
//
// 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
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