Счетчик слов не работает для больших файлов и имеет некоторые проблемы - PullRequest
0 голосов
/ 17 декабря 2018

Я должен прочитать и сосчитать определенный столбец (в моем файле это 6-й столбец) в файле .csv, но я не смог, а также то, что я написал, работает для небольших файлов, он читает каждое слово в файле и печатает, но когдаэто касается больших файлов (в моем проекте у меня есть файл .csv, который содержит 1 миллион твитов), появляется консольное окно, и мой процессор достигает 35-40 (иногда выше), но он не печатает данные. Я ждал около часано потом я закрыл его, потому что я должен заставить это работать около 5 минут. У меня есть несколько стоп-слов, и я не знаю, как их игнорировать.Я знаю, у меня много вопросов, но я новичок в этом

#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cctype>
#include <chrono>

const int MAX = 1000000;
std::string words[MAX];
int instances[MAX];
int count =0;


void insert(std::string input) {
    for (int i = 0; i < count; i++)
        if (input == words[i]) {
            instances[i]++;
            return;
        }
    if (count < MAX) {
        words[count] = input;
        instances[count] = 1;
        count++;
    }

}

int findTop(std::string &word) {
    int topCount = instances[0];
    int topIndex = 0;
    for (int i=1; i<count; i++)
        if (instances[i] > topCount) {
            topCount = instances[i];
            topIndex = i;
        }
    instances[topIndex] = 0;
    word = words[topIndex];
    return topCount;

}

int main() {
    using clock = std::chrono::system_clock;
    using s = std::chrono::seconds;
    const auto before = clock::now();

    std::string word;
    std::ifstream data("eray.csv");
    while (data >> word)
        insert(word);
    data.close();
    int topCount = 0;
    for (int i = 0; i < 10 ; i++) {
        std::cout << word << "    " << findTop(word) << std::endl;
    }
    const auto duration = std::chrono::duration_cast<s>(clock::now() - before);
    std::cout << "\nTotal Elapsed Time : " << duration.count() << "s" << std::endl;
}
...