forked from plowman/python-mcparseface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
171 lines (143 loc) · 5.06 KB
/
utils.h
File metadata and controls
171 lines (143 loc) · 5.06 KB
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
/* Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef $TARGETDIR_UTILS_H_
#define $TARGETDIR_UTILS_H_
#include <functional>
#include <string>
#include <vector>
#include <unordered_set>
#include "syntaxnet/base.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/default/integral_types.h"
#include "tensorflow/core/platform/mutex.h"
#include "util/utf8/unicodetext.h"
namespace syntaxnet {
namespace utils {
bool ParseInt32(const char *c_str, int *value);
bool ParseInt64(const char *c_str, int64 *value);
bool ParseDouble(const char *c_str, double *value);
template <typename T>
T ParseUsing(const string &str, std::function<bool(const char *, T *)> func) {
T value;
CHECK(func(str.c_str(), &value)) << "Failed to convert: " << str;
return value;
}
template <typename T>
T ParseUsing(const string &str, T defval,
std::function<bool(const char *, T *)> func) {
return str.empty() ? defval : ParseUsing<T>(str, func);
}
string CEscape(const string &src);
std::vector<string> Split(const string &text, char delim);
template <typename T>
string Join(const std::vector<T> &s, const char *sep) {
string result;
bool first = true;
for (const auto &x : s) {
tensorflow::strings::StrAppend(&result, (first ? "" : sep), x);
first = false;
}
return result;
}
string JoinPath(std::initializer_list<StringPiece> paths);
size_t RemoveLeadingWhitespace(tensorflow::StringPiece *text);
size_t RemoveTrailingWhitespace(tensorflow::StringPiece *text);
size_t RemoveWhitespaceContext(tensorflow::StringPiece *text);
uint32 Hash32(const char *data, size_t n, uint32 seed);
// Deletes all the elements in an STL container and clears the container. This
// function is suitable for use with a vector, set, hash_set, or any other STL
// container which defines sensible begin(), end(), and clear() methods.
// If container is NULL, this function is a no-op.
template <typename T>
void STLDeleteElements(T *container) {
if (!container) return;
auto it = container->begin();
while (it != container->end()) {
auto temp = it;
++it;
delete *temp;
}
container->clear();
}
// Returns lower-cased version of s.
string Lowercase(tensorflow::StringPiece s);
class PunctuationUtil {
public:
// Unicode character ranges for punctuation characters according to CoNLL.
struct CharacterRange {
int first;
int last;
};
static CharacterRange kPunctuation[];
// Returns true if Unicode character is a punctuation character.
static bool IsPunctuation(int u) {
int i = 0;
while (kPunctuation[i].first > 0) {
if (u < kPunctuation[i].first) return false;
if (u <= kPunctuation[i].last) return true;
++i;
}
return false;
}
// Determine if tag is a punctuation tag.
static bool IsPunctuationTag(const string &tag) {
for (size_t i = 0; i < tag.length(); ++i) {
int c = tag[i];
if (c != ',' && c != ':' && c != '.' && c != '\'' && c != '`') {
return false;
}
}
return true;
}
// Returns true if word consists of punctuation characters.
static bool IsPunctuationToken(const string &word) {
UnicodeText text;
text.PointToUTF8(word.c_str(), word.length());
UnicodeText::const_iterator it;
for (it = text.begin(); it != text.end(); ++it) {
if (!IsPunctuation(*it)) return false;
}
return true;
}
// Returns true if tag is non-empty and has only punctuation or parens
// symbols.
static bool IsPunctuationTagOrParens(const string &tag) {
if (tag.empty()) return false;
for (size_t i = 0; i < tag.length(); ++i) {
int c = tag[i];
if (c != '(' && c != ')' && c != ',' && c != ':' && c != '.' &&
c != '\'' && c != '`') {
return false;
}
}
return true;
}
// Decides whether to score a token, given the word, the POS tag and
// and the scoring type.
static bool ScoreToken(const string &word, const string &tag,
const string &scoring_type) {
if (scoring_type == "default") {
return tag.empty() || !IsPunctuationTag(tag);
} else if (scoring_type == "conllx") {
return !IsPunctuationToken(word);
} else if (scoring_type == "ignore_parens") {
return !IsPunctuationTagOrParens(tag);
}
CHECK(scoring_type.empty()) << "Unknown scoring strategy " << scoring_type;
return true;
}
};
void NormalizeDigits(string *form);
} // namespace utils
} // namespace syntaxnet
#endif // $TARGETDIR_UTILS_H_