大家同乐一下,搞了三天的字符串与STL!终于搞好了。。

先看一下代码~~

  1 #include<iostream>
  2 #include<string>
  3 #include<list>
  4 using namespace std;
  5 
  6 
  7 typedef struct Segmentation_Results
  8 {
  9     string words;//词语内容
 10     int length; //length,词语的长度
 11     string word_type;//word type,词性ID值,可以快速的获取词性表
 12     int nFreq;//词语的频度
 13 } SegResult;
 14 
 15 void Split(char* sInput,list<SegResult>* segmentation_result)//实现分词
 16 { 
 17     char *temp = (char *)malloc(strlen(sInput)+1);
 18     strcpy(temp,sInput);
 19     char *sTemp;
 20     list<char *> result_list;
 21 
 22 
 23     ///////////////下面实现文本分割,以及词频统计
 24     sTemp = strtok(temp,"  ");
 25     while(sTemp != '')
 26     {
 27         result_list.push_back(sTemp);
 28         sTemp = strtok(NULL,"  ");
 29     }
 30 
 31     string words;//词语内容
 32     string word_type;//词性
 33     int count = result_list.size();
 34     list<SegResult>* segmentation_temp = new list<SegResult>();
 35 
 36     for(int i = 0 ; i < count; i ++)
 37     {
 38         sTemp = result_list.front();
 39         result_list.pop_front();
 40 
 41         string temp = sTemp;
 42 
 43         words = strtok(sTemp,"/");
 44         word_type = temp.substr(words.size(),2);
 45 
 46         SegResult srTemp;
 47         srTemp.words = words;
 48         srTemp.length = words.size();
 49         srTemp.word_type = word_type;
 50         segmentation_temp->push_back(srTemp);
 51     }
 52 
 53 
 54     list<SegResult>::iterator it_i,it_j; 
 55     for(it_i =  segmentation_temp->begin(); it_i !=segmentation_temp->end() ; ++ it_i)
 56     {
 57         int count = 0;
 58         for(it_j =  segmentation_temp->begin(); it_j !=segmentation_temp->end() ; ++ it_j)
 59             if((*it_i).words == (*it_j).words)
 60                 count ++;
 61         (*it_i).nFreq = count;  
 62     }
 63 
 64 
 65     /////////////////////////下面处理list中的冗余数据
 66     int nTemp = 0;
 67     segmentation_result->clear();
 68     for(it_i = segmentation_temp->begin() ; it_i != segmentation_temp->end() ; ++it_i)
 69     {
 70         if(!segmentation_result->empty())
 71         {
 72             int i ;
 73             //for(it_j = segmentation_result->begin(),int i = 0; it_j != (--segmentation_temp->end() ); ++ it_j,++i)
 74             for(it_j = segmentation_result->begin(),i = 0; i < segmentation_result->size(); ++ it_j,++i) 
 75 
 76             {
 77                 string str1 = (*it_i).words,str2 = (*it_j).words;
 78                 if(str1 == str2)
 79                 {
 80                     nTemp = 1;
 81                     break;
 82                 }
 83             }
 84         }
 85         if(nTemp == 0)
 86             segmentation_result->push_back(*it_i);
 87         nTemp = 0;
 88     }
 89 }
 90 
 91 
 92 
 93 int main()
 94 {
 95     char *Input = "等待/v  是/v  一/m  种/q  际遇/n  ,/w  等待/v  是/v  一/m  场/q  寂寞/n  ";
 96 
 97     list<SegResult> *segmentation_result = new list<SegResult>() ;
 98     list<SegResult>::iterator it;
 99 
100 
101     Split(Input,segmentation_result);
102 
103     for(it =  segmentation_result->begin(); it !=segmentation_result->end() ; ++ it)
104         cout<<(*it).words<<" "<<(*it).length<<" "<<(*it).word_type<<" "<<(*it).nFreq<<endl;
105     return 0;
106 }

(一)关于字符串的切割,个人觉得只要有substr以及strtok两个函数足矣,只不过在用strtok的时候需要注意,这个函数会改变源字符串的值,因此在使用的过程中,需要对源字符串进行保存。关于strtok,大家可以去看一篇文章(http://tystudio.blog.163.com/blog/static/142018232009371154380/);

(二)就是关于STL的list中冗余数据的处理,所谓的冗余数据就是在list中有相同的item重复出现,其实方法很简单的:

1、遍历冗余表,取出每个数据;

2、如果取出的数据在结果表中存在,就不压入,如果不存在就压入。

可是,单单用list的end方法,却判断不了list是否结尾,当初郁闷死了,四五个网友帮忙看都不行的,到最后只好加了i这个中间变量才行,唉。。。真是服了(这句话有错的~~别误会了大家)

(三)以上只是本人写的最简单的方法,效率非常的低,网友也给我提供了其他的方法,如果大家对文本处理感兴趣,可以找本人私聊~~

回去睡觉了。。。

原文地址:https://www.cnblogs.com/yiyi-xuechen/p/3452233.html