boost-tokenizer分词库学习

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" ;  

    
int  offsets[] = { 2 2 4 };  
    offset_separator f(offsets, offsets + 
3 );  
    tokenizer<offset_separator> tok7(str6, f);  
    print(tok7);

    cin.get();
    
return   0 ;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值