Guest User

Untitled

a guest
Dec 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/core/core.hpp"
  3. #include <iostream>
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. struct pts
  9. {
  10.     float dot,high,space,num;
  11.     bool IsSpam;
  12.     pts()
  13.     {
  14.         dot=high=space=num=0;
  15.     }
  16. };
  17.  
  18. pts Str2Point(char str[])
  19. {
  20.     pts ret;
  21.     int i;
  22.  
  23.     if(str[0]=='h')
  24.     {
  25.         i=4;
  26.         ret.IsSpam=0;
  27.     }
  28.     else if(str[0]=='s')
  29.     {
  30.         i=5;
  31.         ret.IsSpam=1;
  32.     }
  33.     else
  34.     {
  35.         printf("Error str!\n");
  36.         return ret;
  37.     }
  38.  
  39.     for(;str[i]!=0;i++)
  40.     {
  41.         if((str[i]>=65) && (str[i]<91))
  42.             ret.high++;
  43.         if((str[i]=='.') || (str[i]==',') || (str[i]=='?') || (str[i]=='!') || (str[i]=='-') || (str[i]==':') || (str[i]==';'))
  44.             ret.dot++;
  45.         if(str[i]==' ')
  46.             ret.space++;
  47.         if((str[i]>=48) && (str[i]<58))
  48.             ret.num++;
  49.     }
  50.     //ret.high/=(float)i;
  51.     ret.high=(float)i/*/905.0*/;
  52.     //ret.dot/=(float)i;
  53.     //ret.num/=(float)i;
  54.     //ret.space/=(float)i;
  55.     ret.space=ret.high/*/(float)i*/;
  56.  
  57.     //printf("dot=%f\nspace=%f\nnum=%f\nhigh=%f\ni=%d\n\n",ret.dot,ret.space,ret.num,ret.high,i);
  58.     //system("pause>nul");
  59.     return ret;
  60. }
  61.  
  62. pts* FRead(char* Filename, int* SCount)
  63. {
  64.     FILE* f=fopen(Filename,"r");
  65.  
  66.     char tmp[1024];
  67.     int i=0;
  68.  
  69.     while(!feof(f))
  70.     {
  71.         fgets(tmp,1023,f);
  72.         *SCount=*SCount+1;
  73.     }
  74.  
  75.     pts* P = new pts[*SCount];
  76.     fseek(f,0,SEEK_SET);
  77.  
  78.     while(i<*SCount)
  79.     {
  80.         fgets(tmp,1023,f);
  81.         P[i] = Str2Point(tmp);
  82.         i++;
  83.     }
  84.  
  85.     return P;
  86. }
  87.  
  88. int main()
  89. {
  90.     int CLUSTERS;
  91.     int SCount=0;
  92.  
  93.     printf("Enter number of clusters: ");
  94.     scanf("%d",&CLUSTERS);
  95.  
  96.     pts* p=FRead("F:\\Second\\Second\\SMSSpamCollection",&SCount);
  97.  
  98.     Mat points(SCount, 1, CV_32FC4), labels(SCount, 1, CV_32FC4);
  99.     Mat centers(CLUSTERS, 1, CV_32FC4/*points.type()*/);
  100.  
  101.     for(int j=0;j<SCount;j++)
  102.     {
  103.         points.at<float>(j,0) = p[j].high;
  104.         points.at<float>(j,1) = p[j].num;
  105.         points.at<float>(j,2) = p[j].space;
  106.         points.at<float>(j,3) = p[j].dot;
  107.     }
  108.  
  109.     kmeans(points, CLUSTERS, labels,
  110.     TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 20, 0.01),
  111.     5, KMEANS_RANDOM_CENTERS, centers);
  112.  
  113.     int* spam=new int[CLUSTERS], *ham=new int[CLUSTERS];
  114.     for(int i=0;i<CLUSTERS;i++)
  115.     {
  116.         spam[i]=ham[i]=0;
  117.         for(int j=0;j<SCount;j++)
  118.         if(labels.at<int>(j)==i)
  119.         {
  120.             if(p[j].IsSpam)
  121.             spam[i]++;
  122.             else
  123.             ham[i]++;
  124.         }
  125.     }
  126.  
  127.     for(int i=0;i<CLUSTERS;i++)
  128.     printf("CLUSTER #%d: \n\tSPAM: %d\n\tHAM: %d\n\n",1+i,spam[i],ham[i]);
  129.  
  130.     system("pause>nul");
  131.     return 0;
  132. }
Add Comment
Please, Sign In to add comment