// El corrector como programa: spell <diccionario> <texto> [cuántas palabras mostrar]
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

#include "dictionaries.h"
#include "load_words.h"
#include "spell_checker.h"

int main(int argc, char** argv) {
    if (argc < 3) {
        std::cerr << "uso: spell <diccionario> <texto> [cuántas palabras mostrar]\n";
        return 2;
    }
    const std::vector<std::string> entries = load_lines(argv[1]);
    const std::vector<std::string> words = extract_words(load_text(argv[2]));
    const std::size_t shown = argc > 3 ? std::strtoul(argv[3], nullptr, 10) : 10;

    const TableDict<ProbingHashMap> dict(entries, 0.7);
    const std::vector<Misspelling> report = check_words(dict, words);

    std::size_t occurrences = 0;
    for (const Misspelling& item : report) {
        occurrences += item.count;
    }
    std::cout << "diccionario: " << entries.size() << " palabras\n"
              << "texto: " << words.size() << " palabras\n"
              << "desconocidas: " << occurrences << " apariciones de " << report.size()
              << " palabras distintas\n";
    for (std::size_t i = 0; i < report.size() && i < shown; i++) {
        std::cout << report[i].word << " (" << report[i].count << "):";
        for (std::size_t j = 0; j < report[i].suggestions.size() && j < 6; j++) {
            std::cout << " " << report[i].suggestions[j];
        }
        if (report[i].suggestions.size() > 6) {
            std::cout << " ...";
        }
        std::cout << "\n";
    }
    return 0;
}
