boost-tokenizer学习

tokenizer库是一个专门用于分词(token)的字符串处理库;
可以使用简单易用的方法把一个字符串分解成若干个单词;
tokenizerl类是该库的核心,它以容器的外观提供分词序列;
TokenizerFunc:专门的分词函数对象,默认使用空格和标点分词

  • char_delimiters_separator         使用标点符号分词
  • char_separator                          使用字符集合作为分词符
  • escaped_list_separator             使用CSV的逗号分割
  • offset_separator                         使用偏移量来分词

缺陷:
1、只支持使用单个字符进行分词;
2、对wstring(UNICODE)缺乏完善的考虑;

正则表达式xpressive和string_algo可以提供更好的实现,可以对字符串操作工作的更好!

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
/*
    tokenizer库是一个专门用于分词(token)的字符串处理库;
    可以使用简单易用的方法把一个字符串分解成若干个单词;
    tokenizerl类是该库的核心,它以容器的外观提供分词序列;
    TokenizerFunc:专门的分词函数对象,默认使用空格和标点分词
    char_delimiters_separator    使用标点符号分词
    char_separator               使用字符集合作为分词符
    escaped_list_separator       使用CSV的逗号分割
    offset_separator             使用偏移量来分词

缺陷:
    1、只支持使用单个字符进行分词;
    2、对wstring(UNICODE)缺乏完善的考虑;

正则表达式xpressive和string_algo可以提供更好的实现,可以对字符串操作工作的更好!
*/

/*
template <
typename TokenizerFunc = char_delimiters_separator<char>, 
typename Iterator = std::string::const_iterator,
typename Type = std::string
>
class tokenizer {
private:
typedef token_iterator_generator<TokenizerFunc,Iterator,Type> TGen;

// It seems that MSVC does not like the unqualified use of iterator,
// Thus we use iter internally when it is used unqualified and
// the users of this class will always qualify iterator.     
typedef typename TGen::type iter;

public:

typedef iter iterator;
typedef iter const_iterator;
typedef Type value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const pointer const_pointer;
typedef void size_type;
typedef void difference_type;

tokenizer(Iterator first, Iterator last,
const TokenizerFunc& f = TokenizerFunc()) 
: first_(first), last_(last), f_(f) { }

template <typename Container>
tokenizer(const Container& c)
: first_(c.begin()), last_(c.end()), f_() { }

template <typename Container>
tokenizer(const Container& c,const TokenizerFunc& f)
: first_(c.begin()), last_(c.end()), f_(f) { }

void assign(Iterator first, Iterator last){
first_ = first;
last_ = last;
}

void assign(Iterator first, Iterator last, const TokenizerFunc& f){
assign(first,last);
f_ = f;
}

template <typename Container>
void assign(const Container& c){
assign(c.begin(),c.end());
}

template <typename Container>
void assign(const Container& c, const TokenizerFunc& f){
assign(c.begin(),c.end(),f);
}

iter begin() const { return iter(f_,first_,last_); }
iter end() const { return iter(f_,last_,last_); }

*/

/************************************************************************/
/* C++ stl Library                                                        */
/************************************************************************/
#include <iostream>
#include <string>

/************************************************************************/
/* C++ boost Library                                                   */
/************************************************************************/
#include "boost/tokenizer.hpp"
#include <boost/typeof/typeof.hpp>

using namespace boost;
using namespace std;

template<typename T>
void print(T &tok)
{
    for(BOOST_AUTO(pos, tok.begin()); pos != tok.end(); pos++)
    {
        cout << "[" << *pos << "]" ;
    }
    cout << endl;
}

int main(void)
{
    //char_delimiters_separator
    string str1 = "I love my town!xian";
    tokenizer<> tok1(str1);          //默认使用空格和标点分词
    print(tok1);

string str2 = "I,love,my,town!";
    tokenizer<> tok2(str2);          //默认使用空格和标点分词
    print(tok2);

//char_separator 
    string str3("I love my town!xian");  
    char_separator<char> sep;  
    tokenizer<char_separator<char> > tok3(str3, sep);  
    print(tok3);

string str4 = ";!!;Hello|world||-Michael--Joessy;yoo;handsome|";  
    char_separator<char> sep1("-;|");  
    tokenizer<char_separator<char> > tok4(str4, sep1);  
    print(tok4);

char_separator<char> sep2("-;", "|", keep_empty_tokens);  
    tokenizer<char_separator<char> > tok5(str4, sep2);  
    print(tok5);

//escaped_list_separator 
    string str5 = "aa,Int32,localTag1,23";  
    tokenizer<escaped_list_separator<char> > tok6(str5); 
    print(tok6);

//offset_separator             
    string str6 = "1225200140023";

};  
    offset_separator f(offsets, offsets + );  
    tokenizer<offset_separator> tok7(str6, f);  
    print(tok7);

cin.get();
    ;
}

boost-tokenizer分词库学习的更多相关文章

  1. 【Todo】Boost安装与学习

    现在这里找下载包 http://sourceforge.net/projects/boost 我找的是 1_62_0 下面是从公司wiki上找到的一个说明. boost & thrift安装步 ...

  2. 【Boost】boost::tokenizer详解

    分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报   目录(?)[+]   tokenizer 库提供预定义好的四个分词对象, 其中char ...

  3. boost::tokenizer详解

    tokenizer 库提供预定义好的四个分词对象, 其中char_delimiters_separator已弃用. 其他如下: 1. char_separator char_separator有两个构 ...

  4. boost::tuple 深入学习解说

    #include<iostream> #include<string> #include<boost/tuple/tuple.hpp> #include<bo ...

  5. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  6. boost asio io_service学习笔记

    构造函数 构造函数的主要动作就是调用CreateIoCompletionPort创建了一个初始iocp. Dispatch和post的区别 Post一定是PostQueuedCompletionSta ...

  7. boost timer代码学习笔记

    socket连接中需要判断超时 所以这几天看了看boost中计时器的文档和示例 一共有五个例子 从简单的同步等待到异步调用超时处理 先看第一个例子 // timer1.cpp: 定义控制台应用程序的入 ...

  8. Boost.Coroutine2:学习使用Coroutine(协程)

    function(函数)routine(例程)coroutine (协程) 函数,例程以及协程都是指一系列的操作的集合. 函数(有返回值)以及例程(没有返回值)也被称作subroutine(子例程), ...

  9. Lucene 中的Tokenizer, TokenFilter学习

      lucene中的TokenStream,TokenFilter之间关系   TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...

随机推荐

  1. Zoho CEO:云计算泡沫巨大 Salesforce仅仅是新的Siebel

    最近Zoho CEO - Sridhar Vembu接受科技博客媒体Diginomica的专訪,从独特的眼光和见解.讲述了云计算行业环境.SaaS公司的生存状态.商业观念以及Zoho的商业模式. Sr ...

  2. python集成开发eclipse环境安装

    1. 安装java7版本以及eclipse 2.安装Pydev 运行Eclipse之后,选择help-->Install new Software->ADD..,如下图所示 http:// ...

  3. JBoss jar包冲突及jar加载顺序

    http://blog.163.com/javaee_chen/blog/static/17919507720116149511489/将一个完整的.war包部署到Jboss容器中,启动后报如下错误: ...

  4. linux 下远程连接mysql命令详解

    http://hi.baidu.com/aaxh/blog/item/49bcb78ffe3dfae4f01f36b2.html一.MySQL 连接本地数据库,用户名为“root”,密码“123”(注 ...

  5. django inspectdb

    使用inspectdb  --通过已有数据库表生成 model.pyinspectdb辅助工具检查你的settings文件指向的数据库,决定你表示你的表的Django模型并打印Python模型代码到标 ...

  6. join和countDownLatch原理及区别详解

    先上结论 原理 join 原理:在当前线程中调用另一个线程线程 thread 的 join() 方法时,会调用该 thread 的 wait() 方法,直到这个 thread 执行完毕(JVM在 ru ...

  7. ubuntu安装Skype 4.3

    Install Skype 4.3 Step 1: Remove previous version sudo apt-get remove skype skype-bin:i386 skype:i38 ...

  8. WebApi接口传参不再困惑(4):传参详解

    前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望能帮初学者少走弯路.本 ...

  9. Android错误之--activity_main cannot be resolved or is not a field

    一般在copy别人的项目中会easy出现本错误,截图例如以下:

  10. 优化数据页面(22)——n:n的数据关系

    设计要点:优化数据页面.界面设计.美化exce 阿金:那n::n就复杂了,你倒是想留有空间. 可是现实社会有时却不同意. 秀秀:唉.说的也是. 阿金:那怎么表达才合适啊? 秀秀:仅仅实用网格了. 阿金 ...