随笔 - 7, 文章 - 1, 评论 - 4, 引用 - 0
数据加载中……

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

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

#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");
}

posted on 2008-09-23 21:29 大志 阅读(1362) 评论(1)  编辑 收藏 引用

评论

# re: 自己写的正向最大匹配分词[未登录]  回复  更多评论   

请问有一个比较丰富的词典吗?dict.txt有的话,能发到我的邮箱吗?
573505443@qq.com
2011-06-15 18:48 | Jane

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理