Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <cctype>
- #include <string>
- #include <fstream>
- #include <math.h>
- using namespace std;
- #define MAX_LEN_FILE 256
- #define M_LOG2E 1.44269504088896340736 //log2(e)
- inline long double log2(const long double x){
- return log(x) * M_LOG2E;
- }
- struct TreeNode
- {
- string word;
- int grade, count;
- TreeNode *left;
- TreeNode *right;
- TreeNode(string str, int g)
- {
- word = str;
- grade = g;
- count = 1;
- left = NULL;
- right = NULL;
- }
- };
- void Find(TreeNode *node, string str, int g)
- {
- if (g == node->grade)
- {
- if (str == node->word)
- {
- // Yes, the item has been found in the root node.
- node->count++;
- return;
- }
- }
- else if (g < node->grade) {
- // If the item occurs, it must be in the left subtree.
- if (node->left == NULL)
- {
- node->left = new TreeNode(str, g);
- return;
- }
- else
- return Find( node->left, str, g );
- }
- else {
- // If the item occurs, it must be in the right subtree.
- if (node->right == NULL)
- {
- node->right = new TreeNode(str, g);
- return;
- }
- else
- return Find( node->right, str, g );
- }
- }
- double ComputeEntropy(TreeNode *root, int count)
- {
- double entropy = 0;
- entropy += (-log2(root->count/(double)count)) * root->count;
- if (root->left != NULL)
- entropy += ComputeEntropy(root->left, count);
- if (root->right != NULL)
- entropy += ComputeEntropy(root->right, count);
- return entropy;
- }
- int main()
- {
- TreeNode *root;
- char name[MAX_LEN_FILE], temp;
- bool first = true;
- int wordsCount = 0;
- root = NULL;
- cout << "Zadejte cestu k souboru: ";
- cin >> name;
- ifstream file;
- file.open (name, ifstream::in);
- if(!file.is_open())
- {
- cout << "Spatne zadana cesta k souboru!" << endl;
- getche();
- return 0;
- }
- int grade = 0;
- string word;
- while (file.get(temp) && !file.eof())
- {
- if(isalpha(temp))
- {
- word += temp;
- grade += temp;
- }
- else if (word != "")
- {
- if (first)
- {
- root = new TreeNode(word, grade);
- first = false;
- }
- else
- {
- Find(root, word, grade);
- }
- wordsCount++;
- grade = 0;
- word = "";
- }
- }
- file.close();
- cout.precision(10); // ??
- cout << fixed << ComputeEntropy(root, wordsCount);
- getche();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement