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. Python 二维码解码

    二维码解析 Python中关于二维码解析的现成模块有很多,比较著名的就是Zbar以及ZXing.然而很不幸的是,官方的版本都是支持到python2.x,下面是在python2.x的例子: import ...

  2. 微信小程序innerAudioContext接口

    voice:function(){ var word = this.data.word; var src = 'https://--/'+word['word']+'.mp3'; console.lo ...

  3. HTML 的超链接 a 标签中如何设置其宽度和高度?

    HTML 的超链接 a 标签中如何设置其宽度和高度? 在DIV CSS布局中,html 中 a 超链接标签,直接对其设置宽度和高度不能生效,设置宽度和高度也不起作用,这里为大家分享如何实现 a 标签宽 ...

  4. 【Statistics】CAP曲线

    功能描述 CAP曲线(Cumulative Accuracy Profile)/Power Curve(准确率/AR)是描述整个评级结果下,累计违约客户比例与累计客户比例的关系. 在完美的模型下,CA ...

  5. Linux命令-网络命令:lastlog

    last 显示所有用户最后登录信息(会显示系统用户) last -u 只看某一个用户wangyunpeng的最后登录信息 last -u 查看系统用户root的最后登录信息 root用户的ID是0.从 ...

  6. Shell 基础笔记

    1-22-shell脚本的基础 本节所讲内容:      shell 基本语法      变量 第1章 什么是SHELL?.. 2 1.1 shell编程.. 3 第2章 shell变量及运用.. ...

  7. mysql创建账号对应的数据库方法

    增加一个用户mydb密码为123450, 让他只可以在(localhost/%)%表示可以支持远程上登录,并可以对数据库mydata5_db进行查询.插入.修改.删除的操作. grant select ...

  8. [个人开发人员赚钱九]做一个日收入10元的APP!

    [导语]尽管讲了非常多个人开发人员的文章.但新手开发人员怎样赚自己的第一个10块钱.确是最难的事情.群里有人说都不知道干什么app赚钱.全然没有想法.而且常常问我有什么高速赚钱的方法.我仅仅能遗憾地 ...

  9. Oracle之配置客户端登陆多个远程数据库

    一.引言 一直搞不明白Oracle数据库的客户端是怎么回事,怎么配置,前几天由于工作中需要用到Oracle,而且需要连接两个不同的数据库,就通过上网和请教同事终于把客户端的配置搞定了,记录之,学习之 ...

  10. 【网络爬虫】Httpclient4.X中使用HTTPS的方法采集12306网站

    HttpClient请求https的实例: package train; import java.io.IOException; import java.security.NoSuchAlgorith ...