karlangadas

Entropy text analyzer

Dec 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <sstream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. map<string,int>m;
  11. int total;
  12. void Procesar(string & s){
  13.     for (unsigned int i=0;i<s.size();i++){
  14.         if ((s[i]==',') || (s[i]=='.') ||(s[i]==';') ||(s[i]=='(') ||(s[i]==')') ||(s[i]==':') ||(s[i]=='?') ||(s[i]=='!') ||(s[i]=='"'))
  15.             s[i]=' ';
  16.         else if (s[i]>='A' && s[i]<='Z')
  17.             s[i]=s[i]+'a'-'A';
  18.    
  19.     }
  20.     istringstream ss(s);
  21.     string w;
  22.     while (ss >> w) {
  23.         m[w]++;
  24.         //cout<<w<<endl;
  25.         total++;
  26.     }
  27. }
  28. int main(){
  29.     char s1[100];
  30.     while(gets(s1)){
  31.         //cout<<s1<<endl;
  32.         string s(s1);
  33.         if (s=="****END_OF_INPUT****") break;
  34.         m.clear();total=0;
  35.         if (s=="****END_OF_TEXT****"){
  36.             printf("0 0.0 0\n");
  37.             continue;  
  38.         }
  39.         while (s!="****END_OF_TEXT****"){
  40.             //cout<<s1<<endl;
  41.             Procesar(s);
  42.             //printf("s:-%s-",s);
  43.             gets(s1);
  44.             s=string(s1);
  45.         }
  46.         float et=0,gg=log10(total);
  47.         for (map<string,int>::iterator it=m.begin(); it!=m.end(); it++){
  48.             et=et+(it->second)*( gg - log10(it->second));
  49.         }
  50.         et=et/total;
  51.         float erel=et/log10(total)*100.0;
  52.         if (total==0)
  53.             printf("0 0.0 0\n");
  54.         else
  55.             printf("%d %.1f %.0f\n",total,et,erel);
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment