<commit_before>#include "markov.h"
#include <algorithm>
#include <sstream>
#include <iterator>

using namespace std;

string markovText = "";

void markovPrepare(std::string filePath, bool debug) {
    vector<string> wordList = readWords(filePath, debug);
    for (int i = 0; i < (int) (wordList.size() - (markovOrder + 1)); i++) {
        cout << "Lettura: " << (i * 100) / (wordList.size() - markovOrder) <<
        "% (parola " << i << " di " << (wordList.size() - markovOrder) << ")" << '\r' << flush;

        string words = "";
        string nextWord = "";
        int wordCount = 0;
        while (true) {
            if (wordCount < (markovOrder - 1)) {
                words.append(wordList[i + wordCount]);
                words.append(" ");
                wordCount++;
            } else {
                words.append(wordList[i + wordCount]);
                nextWord = wordList[i + wordCount + 1];
                addToDictionary(words, nextWord);
                break;
            }
        }
    }
    cout << "Lettura: 100% (parola " << (wordList.size() - markovOrder) <<
    " di " << (wordList.size() - markovOrder) << ")" << endl;

    if (debug) {
        printDictionary();
    }
}

void err_noTerms() {
    cout << endl << endl << "[ERROR] Non sono stati trovati abbastanza"
            " termini coerenti. Diminuisci la precisione"
            " o il numero di parole richieste." << endl;
    exit(1);
}

int lastWordsKey(vector<string> words) {
    string lastWords = "";

    // prendi le ultime due parole
    if ((int) words.size() <= markovOrder) {  // se non ci sono abastanza parole
        for (int i = 0; i < ((int) words.size() - 1); i++) { // prendi tutto
            lastWords.append(words[i] + " ");
        }
        lastWords.append(words[(int) words.size() - 1]);
    } else {  // se ci sono abbastanza parole
        int wordCount = 0;
        int curIndex = ((int) words.size() - 1) - markovOrder;
        while (wordCount < (markovOrder - 1)) {
            lastWords.append(words[curIndex] + " ");
            curIndex++;
            wordCount++;
        }
        lastWords.append(words[curIndex]);
    }

    vector<dictionary> dict = getDictionary();
    for (int keyIndex = 0; keyIndex < ((int) dict.size() - 1); keyIndex++) {
        if (dict[keyIndex].key.find(lastWords) != string::npos) {
            return keyIndex;
        }
    }
    err_noTerms();
    return -1;
}

void markovCreate(int wordsNumber) {
    vector<dictionary> wordsDictionary = getDictionary();
    vector<string> result;
    int wordCount = 0;
    int value_index;

    while (wordCount < wordsNumber) {
        cout << "Scrittura: " << (wordCount * 100) / wordsNumber <<<commit_msg>Fixato errore off-by-one nel prendere nuova chiave<commit_after>#include "markov.h"
#include <algorithm>
#include <sstream>
#include <iterator>

using namespace std;

string markovText = "";

void markovPrepare(std::string filePath, bool debug) {
    vector<string> wordList = readWords(filePath, debug);
    for (int i = 0; i < (int) (wordList.size() - (markovOrder + 1)); i++) {
        cout << "Lettura: " << (i * 100) / (wordList.size() - markovOrder) <<
        "% (parola " << i << " di " << (wordList.size() - markovOrder) << ")" << '\r' << flush;

        string words = "";
        string nextWord = "";
        int wordCount = 0;
        while (true) {
            if (wordCount < (markovOrder - 1)) {
                words.append(wordList[i + wordCount]);
                words.append(" ");
                wordCount++;
            } else {
                words.append(wordList[i + wordCount]);
                nextWord = wordList[i + wordCount + 1];
                addToDictionary(words, nextWord);
                break;
            }
        }
    }
    cout << "Lettura: 100% (parola " << (wordList.size() - markovOrder) <<
    " di " << (wordList.size() - markovOrder) << ")" << endl;

    if (debug) {
        printDictionary();
    }
}

void err_noTerms() {
    cout << endl << endl << "[ERROR] Non sono stati trovati abbastanza"
            " termini coerenti. Diminuisci la precisione"
            " o il numero di parole richieste." << endl;
    exit(1);
}

int lastWordsKey(vector<string> words) {
    string lastWords = "";

    // prendi le ultime due parole
    if ((int) words.size() <= markovOrder) {  // se non ci sono abastanza parole
        for (int i = 0; i < ((int) words.size() - 1); i++) { // prendi tutto
            lastWords.append(words[i] + " ");
        }
        lastWords.append(words[(int) words.size() - 1]);
    } else {  // se ci sono abbastanza parole
        int wordCount = 0;
        int curIndex = ((int) words.size()) - markovOrder;
        while (wordCount < (markovOrder - 1)) {
            lastWords.append(words[curIndex] + " ");
            curIndex++;
            wordCount++;
        }
        lastWords.append(words[curIndex]);
    }

    vector<dictionary> dict = getDictionary();
    for (int keyIndex = 0; keyIndex < ((int) dict.size() - 1); keyIndex++) {
        if (dict[keyIndex].key.find(lastWords) != string::npos) {
            return keyIndex;
        }
    }
    err_noTerms();
    return -1;
}

void markovCreate(int wordsNumber) {
    vector<dictionary> wordsDictionary = getDictionary();
    vector<string> result;
    int wordCount = 0;
    int value_index;

    while (wordCount < wordsNumber) {
        cout << "Scrittura: " << (wordCount * 100) / wordsNumber <<
{'fraction_non_alphanumeric': 0.15428361768482848, 'fraction_numerical': 0.008989176297926987, 'mean_word_length': 2.122565864833906, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2422472', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1694, 'n_words': 421}