自己写的正向最大匹配分词

自己写的正向最大匹配分词

      最近闲的没什么事,花了好长时间写了一个正向最大匹配程序,由于自己在写程序上是一个菜鸟,望看到此程序的见谅!

#include < iostream >
#include
< fstream >
#include
< string >
#include
< vector >
#include
< algorithm >

using   namespace  std;

int  main()
{
    ifstream infile;
    infile.open(
"C:\\Documents and Settings\\dazhi\\桌面\\dict.txt");
    vector
<string>  ChDic;
    
string word;
    
string text="党中央和国务院";
    vector
<string>arr;
    
//load dict 
    while(getline(infile,word ))
    
{         
       ChDic.push_back(word);         
    }
   
    
    
string temp = text; 
    
string temp1 = temp;
   
int p1 = 0
  
while(strlen(temp1.c_str())!=0)
   

     
for(int i=strlen(temp1.c_str());i>=0;i=i-2)
    
{
       
if(find(ChDic.begin(),ChDic.end(),temp)!=ChDic.end())
        
{
         temp 
+="|";       
         arr.push_back(temp);
         p1 
= i ;                               
         
break;
        }
              
      
else
       
{
         temp 
= temp1.substr(0,i-2);   
       }
          
    }
  
      temp1 
= temp1.substr(p1); 
      temp 
= temp1;
    
   }

    
for(vector<string>::iterator iter=arr.begin();iter!=arr.end();++iter)
    
{
       cout 
<<*iter;
    }
 
    cout 
<<endl;
    infile.close();
    system(
"pause");
}

你可能感兴趣的:(自己写的正向最大匹配分词)