-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyLib.h
388 lines (312 loc) · 9.73 KB
/
MyLib.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/////////////////////////////////////////////////////////////////////////////////////
// File Name : MyLib.h
// Project Name: IRLAS
// Author : Huipeng Zhang ([email protected])
// Environment : Microsoft Visual C++ 6.0
// Description : some utility functions
// Time : 2005.9
// History :
// CopyRight : HIT-IRLab (c) 2001-2005, all rights reserved.
/////////////////////////////////////////////////////////////////////////////////////
#ifndef _MYLIB_H_
#define _MYLIB_H_
#include <string>
#include <vector>
#include <fstream>
#include <cassert>
#include <deque>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cmath>
#include <ctime>
#include <cfloat>
#include <cstring>
#include <sstream>
#include <armadillo>
#include "Hash_map.hpp"
using namespace arma;
using namespace std;
typedef std::vector<std::string> CStringVector;
typedef std::vector<std::pair<std::string, std::string> > CTwoStringVector;
class string_less {
public:
bool operator()(const string &str1, const string &str2) const {
int ret = strcmp(str1.c_str(), str2.c_str());
if (ret < 0)
return true;
else
return false;
}
};
/*==============================================================
*
* CSentenceTemplate
*
*==============================================================*/
template<typename CSentenceNode>
class CSentenceTemplate: public std::vector<CSentenceNode> {
public:
CSentenceTemplate() {
}
virtual ~CSentenceTemplate() {
}
};
//==============================================================
template<typename CSentenceNode>
inline std::istream & operator >>(std::istream &is, CSentenceTemplate<CSentenceNode> &sent) {
sent.clear();
std::string line;
while (is && line.empty())
getline(is, line);
//getline(is, line);
while (is && !line.empty()) {
CSentenceNode node;
std::istringstream iss(line);
iss >> node;
sent.push_back(node);
getline(is, line);
}
return is;
}
template<typename CSentenceNode>
inline std::ostream & operator <<(std::ostream &os, const CSentenceTemplate<CSentenceNode> &sent) {
for (unsigned i = 0; i < sent.size(); ++i)
os << sent.at(i) << std::endl;
os << std::endl;
return os;
}
inline void print_time() {
time_t lt = time(NULL);
cout << ctime(<) << endl;
}
inline char* mystrcat(char *dst, const char *src) {
int n = (dst != 0 ? strlen(dst) : 0);
dst = (char*) realloc(dst, n + strlen(src) + 1);
strcat(dst, src);
return dst;
}
inline char* mystrdup(const char *src) {
char *dst = (char*) malloc(strlen(src) + 1);
if (dst != NULL) {
strcpy(dst, src);
}
return dst;
}
inline int message_callback(void *instance, const char *format, va_list args) {
vfprintf(stdout, format, args);
fflush(stdout);
return 0;
}
inline void normalize_mat_onerow(mat &m, int row) {
double sum = 0;
for (int idx = 0; idx < m.n_cols; idx++)
sum = sum + m(row, idx) * m(row, idx);
sum = sum + 0.000001;
double scale = sqrt(sum);
for (int idx = 0; idx < m.n_cols; idx++)
m(row, idx) = m(row, idx) / scale;
}
inline void normalize_mat_onecol(mat &m, int col) {
double sum = 0;
for (int idx = 0; idx < m.n_rows; idx++)
sum = sum + m(idx, col);
double avg = sum / m.n_rows;
for (int idx = 0; idx < m.n_rows; idx++)
m(idx, col) = m(idx, col) - avg;
sum = 0.0;
for (int idx = 0; idx < m.n_rows; idx++)
sum = sum + m(idx, col) * m(idx, col);
sum = sum + 0.000001;
double scale = sqrt(sum);
for (int idx = 0; idx < m.n_rows; idx++)
m(idx, col) = m(idx, col) / scale;
}
inline void Free(double** p)
{
if(*p != NULL) free(*p);
*p = NULL;
}
//(-scale,scale)
inline void randomMatAssign(double* p, int length, double scale = 1.0, int seed = 0)
{
srand(seed);
for(int idx = 0; idx < length; idx++)
{
p[idx] = 2.0 * rand() * scale / RAND_MAX - scale;
}
}
inline void assign(double* p, const mat &m)
{
int count = 0;
for(int idx = 0; idx < m.n_rows; idx++)
{
for(int idy = 0; idy < m.n_cols; idy++)
{
p[count] = m(idx, idy);
count++;
}
}
}
inline int mod(int v1, int v2)
{
if(v1 < 0 || v2 <= 0) return -1;
else
{
return v1%v2;
}
}
inline void ones(double* p, int length)
{
for(int idx = 0; idx < length; idx++)
{
p[idx] = 1.0;
}
}
inline void zeros(double* p, int length)
{
for(int idx = 0; idx < length; idx++)
{
p[idx] = 0.0;
}
}
inline void scaleMat(double* p, double scale, int length)
{
for(int idx = 0; idx < length; idx++)
{
p[idx] = p[idx] * scale;
}
}
inline void elemMulMat(double* p, double* q, int length)
{
for(int idx = 0; idx < length; idx++)
{
p[idx] = p[idx] * q[idx];
}
}
inline void elemMulMat(double* p, double* q, double *t, int length)
{
for(int idx = 0; idx < length; idx++)
{
t[idx] = p[idx] * q[idx];
}
}
inline void normalize_mat_onerow(double* p, int row, int rowSize, int colSize) {
double sum = 0.000001;
int start_pos = row * colSize;
int end_pos = start_pos + colSize;
for (int idx = start_pos; idx < end_pos; idx++)
sum = sum + p[idx] * p[idx];
double norm = sqrt(sum);
for (int idx = start_pos; idx < end_pos; idx++)
p[idx] = p[idx] / norm;
}
//shift to avg = 0, and then norm = 1
inline void normalize_mat_onecol(double* p, int col, int rowSize, int colSize)
{
double sum = 0.0;
int maxLength = rowSize * colSize;
for(int idx = col; idx < maxLength; idx += rowSize)
{
sum += p[idx];
}
double avg = sum / colSize;
sum = 0.000001;
for(int idx = col; idx < maxLength; idx += rowSize)
{
p[idx] = p[idx] - avg;
sum += p[idx] * p[idx];
}
double norm = sqrt(sum);
for(int idx = col; idx < maxLength; idx += rowSize)
{
p[idx] = p[idx] / norm;
}
}
inline bool isPunc(std::string thePostag) {
if (thePostag.compare("PU") == 0 || thePostag.compare("``") == 0 || thePostag.compare("''") == 0 || thePostag.compare(",") == 0 || thePostag.compare(".") == 0
|| thePostag.compare(":") == 0 || thePostag.compare("-LRB-") == 0 || thePostag.compare("-RRB-") == 0 || thePostag.compare("$") == 0
|| thePostag.compare("#") == 0) {
return true;
} else {
return false;
}
}
// start some assumptions, "-*-" is a invalid label.
inline bool validlabels(const string& curLabel)
{
if(curLabel[0]== '-' && curLabel[curLabel.length()-1] == '-')
{
return false;
}
return true;
}
inline bool is_start_label(const string& label)
{
if(label.length() < 3) return false;
return (label[0] == 'b' || label[0] == 'B' || label[0] == 's' || label[0] == 'S') && label[1] == '-';
}
inline bool is_end_label(const string& label, const string& nextlabel)
{
if(label.length() < 3) return false;
if((label[0] == 'e' || label[0] == 'E' || label[0] == 's' || label[0] == 'S') && label[1] == '-')
return true;
if(nextlabel.length() == 1 && (nextlabel[0] == 'o' || nextlabel[0] == 'O')) return true;
return false;
}
// end some assumptions
inline int cmpPairByValue(const pair<int, int> &x, const pair<int, int> &y) {
return x.second > y.second;
}
inline void sortMapbyValue(const hash_map<int, int> &t_map, vector<pair<int, int> > &t_vec) {
t_vec.clear();
for (hash_map<int,int>::const_iterator iter = t_map.begin(); iter != t_map.end(); iter++) {
t_vec.push_back(make_pair(iter->first, iter->second));
}
std::sort(t_vec.begin(), t_vec.end(), cmpPairByValue);
}
// split by each of the chars
void split_bychars(const string& str, vector<string> & vec, const char *sep = " ");
void replace_char_by_char(string &str, char c1, char c2);
// remove the blanks at the begin and end of string
void clean_str(string &str);
inline void remove_beg_end_spaces(string &str) {
clean_str(str);
}
bool my_getline(ifstream &inf, string &line);
void int2str_vec(const vector<int> &vecInt, vector<string> &vecStr);
void str2uint_vec(const vector<string> &vecStr, vector<unsigned int> &vecInt);
void str2int_vec(const vector<string> &vecStr, vector<int> &vecInt);
void join_bystr(const vector<string> &vec, string &str, const string &sep);
void split_bystr(const string &str, vector<string> &vec, const string &sep);
inline void split_bystr(const string &str, vector<string> &vec, const char *sep) {
split_bystr(str, vec, string(sep));
}
//split a sentence into a vector by separator which is a char
void split_bychar(const string& str, vector<string> & vec, const char separator = ' ');
//convert a string to a pair splited by separator which is '/' by default
void string2pair(const string& str, pair<string, string>& pairStr, const char separator = '/');
//convert every item separated by '/' in a vector to a pair
void convert_to_pair(vector<string>& vecString, vector<pair<string, string> >& vecPair);
//the combination of the two functions above
void split_to_pair(const string& str, vector<pair<string, string> >& vecPair);
void split_pair_vector(const vector<pair<int, string> > &vecPair, vector<int> &vecInt, vector<string> &vecStr);
//it is similar to split_bychar, except that the separator can be a string
void split_by_separator(const string& str, vector<string>& vec, const string separator);
//delete the white(space, Tab or a new line) on the two sides of a string
void chomp(string& str);
//get the length of the longest common string of two strings
int common_substr_len(string str1, string str2);
//compute the index of a Chinese character, the input
//can be any string whose length is larger than 2
int get_char_index(string& str);
//judge if a string is a Hanzi
bool is_chinese_char(string& str);
//find GB char which is two-char-width and the first char is negative
int find_GB_char(const string& str, string wideChar, int begPos);
string word(string& word_pos);
//judge if a string purely consist of ASCII characters
bool is_ascii_string(string& word);
//judge if a string starts with some other string
bool is_startwith(const string& word, const string& prefix);
#endif