Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FileHelper.h"
- bool comp(pair<char, int> a, pair<char, int> b) {
- return a.second > b.second;
- }
- vector<string> FileHelper::open(const char* filename) {
- streamsize size = -1;
- vector<string> text;
- vector<pair<char, int>> chars;
- size = this->proccessFile(filename, &text, &chars);
- int c = 1;
- for (auto it : chars) {
- map.insert(make_pair(it.first, c));
- c++;
- }
- return text;
- }
- streamsize FileHelper::save(const char* filename, vector<char> &text) {
- ofstream os(filename);
- for (auto ch : text) {
- os << ch;
- }
- os.close();
- return 0;
- }
- void FileHelper::loadReverseTable(const char* filename) {
- vector<pair<char, int>> chars;
- proccessFile(filename, nullptr, &chars);
- for (auto it : chars) {
- reverse_map.push_back(it.first);
- }
- }
- streamsize FileHelper::proccessFile(const char* filename, vector<string>* text, vector<pair<char, int>>* chars) {
- ifstream io(filename);
- io.seekg(0, io.end);
- streamsize size = io.tellg();
- io.seekg(0, io.beg);
- unordered_map<char, int> charmap;
- string line;
- totalChars = 0;
- while (getline(io, line)) {
- line.append("\n");
- for (auto c : line) {
- auto it = charmap.find(c);
- if (it != charmap.end()) {
- charmap.at(c) += 1;
- }
- else {
- charmap.insert(make_pair(c, 1));
- }
- totalChars++;
- }
- if (text != nullptr) {
- text->push_back(line);
- }
- }
- float entropy = 0.f;
- for (auto it : charmap) {
- float p = it.second / float(totalChars);
- if (p == 0.f) {
- continue;
- }
- entropy += (-p * log2(p));
- }
- printf("Entropy of %s: %f\n", filename, entropy);
- if (chars != nullptr) {
- chars->assign(charmap.begin(), charmap.end());
- sort(chars->begin(), chars->end(), comp);
- }
- io.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement