appear_total.cpp

来自「一个很好的贝叶斯分类器」· C++ 代码 · 共 81 行

CPP
81
字号

#include "stdafx.h"
#include"BeyesClassifier.h"

typedef map<string, int>::value_type sival_type;

map< string, int > *appear_total( const vector<string> *words )

{

       //  创建单词排除集合

       set<string> exclusion_set;

	  char* tempstr=(char*)malloc(_MAX_PATH);
	  strcpy(tempstr,str);
	  strcat(tempstr,"\\input\\pkg95.txt");

       ifstream exclusion_file(tempstr, ios::in );

       if (!exclusion_file) {

              cout << "Conn't open pkg95.txt !" << endl;

              exit (1);

       }

       string textline;

       while ( getline(exclusion_file, textline, '\n'))

       {
             //cout << "    " << textline << '\n';
              exclusion_set.insert(textline);

       }

       map<string, int> *word_map = new map<string, int>;

       //  开始向word_map中记录数据

       vector<string>::const_iterator iter = words->begin();

       for ( ; iter != words->end(); ++iter )

       {
              //  如果少于3个字符或在排除集合中存在,则不输入到map中
		      //Returns the number of elements in a set whose key matches a parameter-specified key
			  //1 if the set contains an element whose sort key matches the parameter key. 0 if the set does not contain an element with a matching key.

		   if(exclusion_set.count(*iter))
		   {
				continue;
		   }


             /* if ( (*iter).size() < 3||exclusion_set.count( *iter ) )  
              {
				  continue;
              }   */

              //  如果count()返回0,则单词不存在,加入它
              if ( !word_map->count(*iter) )

              {
                     word_map->insert( sival_type( (*iter), 1 ) );
              }

              else
              {
                     //将单词的出现次数加1
                     (*word_map)[ (*iter) ] += 1;
              }

       } //end of for

	   free(tempstr);
       return word_map;

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?