forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartypes.hpp
180 lines (151 loc) · 5.16 KB
/
chartypes.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef __LTP_STRUTILS_CHARTYPES_HPP__
#define __LTP_STRUTILS_CHARTYPES_HPP__
#include "chartypes.tab"
#include <string.h>
#ifdef _WIN32
#include <hash_map>
#else
#include <tr1/unordered_map>
#endif
namespace ltp {
namespace strutils {
namespace chartypes {
enum{
// level 1
CHAR_LETTER = 1,
CHAR_DIGIT = 2,
CHAR_PUNC = 3,
CHAR_OTHER = 0,
// level 2
CHAR_LETTER_SBC = 11,
CHAR_LETTER_DBC = 12,
CHAR_DIGIT_SBC = 21,
CHAR_DIGIT_DBC = 22,
CHAR_PUNC_SBC = 31,
CHAR_PUNC_DBC = 32,
// level 3
CHAR_LETTER_SBC_UPPERCASE = 111,
CHAR_LETTER_SBC_LOWERCASE = 112,
CHAR_LETTER_DBC_UPPERCASE = 121,
CHAR_LETTER_DBC_LOWERCASE = 122,
CHAR_DIGIT_DBC_CL1 = 221,
CHAR_DIGIT_DBC_CL2 = 222,
CHAR_DIGIT_DBC_CL3 = 223,
CHAR_PUNC_DBC_NORMAL = 321,
CHAR_PUNC_DBC_CHINESE = 322,
CHAR_PUNC_DBC_EXT = 323,
};
struct __chartype_char_array_equal_function {
bool operator() (const char * s1, const char * s2) const {
return (strcmp(s1, s2) == 0);
}
};
struct __chartype_char_array_hash_function
#ifdef _WIN32
: public stdext::hash_compare<const char *>
#endif
{
size_t operator() (const char * s) const {
unsigned int hashTemp = 0;
while (*s) {
hashTemp = hashTemp * 101 + *s ++;
}
return (size_t(hashTemp));
}
bool operator() (const char * s1, const char * s2) const {
return (strcmp(s1, s2) < 0);
}
};
// chartype dictionary
// it's a singleton of key-value structure
template<typename T>
class __chartype_collections {
public:
static __chartype_collections * get_collections() {
if (0 == instance_) {
instance_ = new __chartype_collections;
}
return instance_;
}
int chartype(const char * key) {
internal_collection_t::const_iterator itx = collections.find(key);
if (itx != collections.end()) {
return itx->second;
}
return CHAR_OTHER;
}
protected:
__chartype_collections() {
const char * buff = 0;
buff = __chartype_dbc_punc_ext_utf8_buff__;
for (int i = 0; i < __chartype_dbc_punc_ext_utf8_size__; ++ i) {
collections[buff] = CHAR_PUNC;
do { ++ buff; } while (*(buff - 1));
}
buff = __chartype_dbc_chinese_punc_utf8_buff__;
for (int i = 0; i < __chartype_dbc_chinese_punc_utf8_size__; ++ i) {
collections[buff] = CHAR_PUNC;
do {++ buff; } while (*(buff - 1));
}
buff = __chartype_dbc_digit_utf8_buff__;
for (int i = 0; i < __chartype_dbc_digit_utf8_size__; ++ i) {
collections[buff] = CHAR_DIGIT;
do {++ buff; } while (*(buff - 1));
}
buff = __chartype_dbc_punc_utf8_buff__;
for (int i = 0; i < __chartype_dbc_punc_utf8_size__; ++ i) {
collections[buff] = CHAR_PUNC;
do {++ buff; } while (*(buff - 1));
}
buff = __chartype_dbc_uppercase_utf8_buff__;
for (int i = 0; i < __chartype_dbc_uppercase_utf8_size__; ++ i) {
collections[buff] = CHAR_LETTER;
do {++ buff; } while (*(buff - 1));
}
buff = __chartype_dbc_lowercase_utf8_buff__;
for (int i = 0; i < __chartype_dbc_lowercase_utf8_size__; ++ i) {
collections[buff] = CHAR_LETTER;
do { ++ buff; } while (*(buff - 1));
}
buff = __chartype_sbc_uppercase_utf8_buff__;
for (int i = 0; i < __chartype_sbc_uppercase_utf8_size__; ++ i) {
collections[buff] = CHAR_LETTER;
do { ++ buff; } while (*(buff - 1));
}
buff = __chartype_sbc_digit_utf8_buff__;
for (int i = 0; i < __chartype_sbc_digit_utf8_size__; ++ i) {
collections[buff] = CHAR_DIGIT;
do { ++ buff; } while (*(buff - 1));
}
buff = __chartype_sbc_punc_utf8_buff__;
for (int i = 0; i < __chartype_sbc_punc_utf8_size__; ++ i) {
collections[buff] = CHAR_PUNC;
do { ++ buff; } while (*(buff - 1));
}
buff = __chartype_sbc_lowercase_utf8_buff__;
for (int i = 0; i < __chartype_sbc_lowercase_utf8_size__; ++ i) {
collections[buff] = CHAR_LETTER;
do { ++ buff; } while (*(buff - 1));
}
}
private:
#ifdef _WIN32
typedef stdext::hash_map<const char *, int,
__chartype_char_array_hash_function> internal_collection_t;
#else
typedef std::tr1::unordered_map<const char *, int,
__chartype_char_array_hash_function,
__chartype_char_array_equal_function> internal_collection_t;
#endif // end for _WIN32
static __chartype_collections * instance_;
internal_collection_t collections;
};
template<typename T> __chartype_collections<T> * __chartype_collections<T>::instance_ = 0;
//template<typename T> internal_collection_t __chartype_collections<T>::collections;
inline int chartype(const std::string & ch) {
return __chartype_collections<void>::get_collections()->chartype(ch.c_str());
}
} // end for namespace chartypes
} // end for namespace strutils
} // end for namespace ltp
#endif // end for __LTP_STRUTILS_CHARTYPES_HPP__