给定字典做分词

分词算法实现与应用

最近需要用到分词,无聊写个算法。。。

算法:给定一个字典和一句话,做分词;

Target:输入词典,输出所有可能的分词结果

思路:dfs

加速:首先判断是不是这句话里所有的词在字典中都有(validate)



[cpp]  view plain copy
  1. //  
  2. //  Wordsplit.cpp  
  3. //    
  4. //  Target: Find all possible splitting of a sentence given a dictionary dict  
  5. //  Howto:  refer to main  
  6. //  
  7. //  Created by Rachel on 14-8-16.  
  8. //  Copyright (c) 2014年 ZJU. All rights reserved.  
  9. //  
  10.   
  11. #include <iostream>  
  12. #include <stdio.h>  
  13. #include "vector"  
  14. #include <set>  
  15. #include<unordered_set>  
  16. using namespace std;  
  17.   
  18. class Wordsplit {  
  19. private:  
  20.     vector<string> list;  
  21.     bool match(string s, string cur_ele){  
  22.         int l = cur_ele.length();  
  23.         if (s.substr(0,l)==cur_ele) {  
  24.             return true;  
  25.         }  
  26.         return false;  
  27.     }  
  28.       
  29.     bool validate(string s, unordered_set<string> &dict){  
  30.         //1. calculate all alphabets in the query  
  31.         set<char> alpha;  
  32.         for (int i=0; i<s.length(); i++) {  
  33.             alpha.insert(s[i]);  
  34.             }  
  35.         //2. calculate all alphabets in the dictionary  
  36.         set<char> beta;  
  37.         unordered_set<string>::iterator dict_it;  
  38.         for (dict_it = dict.begin(); dict_it!=dict.end(); dict_it++) {  
  39.             for (int i=0; i<(*dict_it).length(); i++) {  
  40.                 beta.insert((*dict_it)[i]);  
  41.             }  
  42.         }  
  43.         set<char>::iterator it;  
  44.         for (it = alpha.begin(); it!=alpha.end(); it++) {  
  45.             if (beta.find(*it)==beta.end()) {  
  46.                 return false;  
  47.             }  
  48.         }  
  49.         return true;  
  50.     }  
  51.       
  52. public:  
  53.     string split(string s, unordered_set<string> &dict, string cur_str){  
  54.         if (s.length()==0) {  
  55.             list.push_back(cur_str.substr(0,cur_str.length()-1));  
  56.             return s;  
  57.         }  
  58.         //cout<<s<<endl;  
  59.         unordered_set<string>::iterator it;  
  60.         for (it=dict.begin(); it!=dict.end(); it++) {  
  61.             if (match(s, (*it))) {  
  62.                 string tmp_str = cur_str;  
  63.                 string latter = s.substr(it->length(), s.length()-it->length());  
  64.                 cur_str += (*it) + " "// add current word to cur_str  
  65.                 cur_str += split(latter, dict, cur_str); // split remaining words  
  66.                 cur_str = tmp_str; //back to last status  
  67.             }  
  68.         }  
  69.         return "no result";  
  70.     }  
  71.       
  72.       
  73.     vector<string> main(string s, unordered_set<string> &dict) {  
  74.         if (!validate(s, dict)) {  
  75.             return list;  
  76.         }  
  77.         split(s, dict, "");  
  78.         return list;  
  79.     }  
  80. };  
  81.   
  82. int main()  
  83. {  
  84.     Wordsplit s;  
  85.     unordered_set<string> L={"程序员","公务员","员","我","喜","做","程序","一","欢","喜欢","做一个","一个"};  
  86.     vector<string> V = s.main("我喜欢做一个程序员", L);  
  87.     vector<string>::iterator it;  
  88.     for (it=V.begin(); it!=V.end(); it++) {  
  89.         cout<<(*it)<<endl;  
  90.     }  
  91.  

输出:

 喜欢 做一个 程序 

 喜欢 做一个 程序员

 喜欢  一个 程序 

 喜欢  一个 程序员

   做一个 程序 

   做一个 程序员

    一个 程序 

    一个 程序员


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值