forked from tensorflow/models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_segment_state_test.cc
More file actions
218 lines (182 loc) · 8.23 KB
/
Copy pathbinary_segment_state_test.cc
File metadata and controls
218 lines (182 loc) · 8.23 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
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
/* 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.
==============================================================================*/
#include "syntaxnet/binary_segment_state.h"
#include <memory>
#include "syntaxnet/base.h"
#include "syntaxnet/sentence.pb.h"
#include "syntaxnet/term_frequency_map.h"
#include "tensorflow/core/platform/test.h"
namespace syntaxnet {
class BinarySegmentStateTest : public ::testing::Test {
protected:
void SetUp() override {
// Prepare a sentence.
const char *str_sentence = "text: '测试 的 句子' "
"token { word: '测' start: 0 end: 2 } "
"token { word: '试' start: 3 end: 5 } "
"token { word: ' ' start: 6 end: 6 } "
"token { word: '的' start: 7 end: 9 } "
"token { word: ' ' start: 10 end: 10 } "
"token { word: '句' start: 11 end: 13 } "
"token { word: '子' start: 14 end: 16 } ";
sentence_ = std::unique_ptr<Sentence>(new Sentence());
TextFormat::ParseFromString(str_sentence, sentence_.get());
}
// The test document, parse tree, and sentence.
std::unique_ptr<Sentence> sentence_;
TermFrequencyMap label_map_;
};
TEST_F(BinarySegmentStateTest, AddStartLastStartNumStartsTest) {
BinarySegmentState *segment_state = new BinarySegmentState();
ParserState state(sentence_.get(), segment_state, &label_map_);
// Test segment_state initialized with zero starts.
EXPECT_EQ(0, segment_state->NumStarts(state));
// Adding the first token as a start token.
segment_state->AddStart(0, &state);
ASSERT_EQ(1, segment_state->NumStarts(state));
EXPECT_EQ(0, segment_state->LastStart(0, state));
// Adding more starts.
segment_state->AddStart(2, &state);
segment_state->AddStart(3, &state);
segment_state->AddStart(4, &state);
segment_state->AddStart(5, &state);
ASSERT_EQ(5, segment_state->NumStarts(state));
EXPECT_EQ(5, segment_state->LastStart(0, state));
EXPECT_EQ(4, segment_state->LastStart(1, state));
EXPECT_EQ(3, segment_state->LastStart(2, state));
EXPECT_EQ(2, segment_state->LastStart(3, state));
EXPECT_EQ(0, segment_state->LastStart(4, state));
}
TEST_F(BinarySegmentStateTest, AddParseToDocumentTest) {
BinarySegmentState *segment_state = new BinarySegmentState();
ParserState state(sentence_.get(), segment_state, &label_map_);
// Test gold segmentation.
// 0 1 2 3 4 5 6
// 测 试 ' ' 的 ' ' 句 子
// S M S S S S M
segment_state->AddStart(0, &state);
segment_state->AddStart(2, &state);
segment_state->AddStart(3, &state);
segment_state->AddStart(4, &state);
segment_state->AddStart(5, &state);
Sentence sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
// Test the number of tokens as well as the start/end byte-offsets of each
// token.
ASSERT_EQ(3, sentence_with_annotation.token_size());
// The first token is 测试.
EXPECT_EQ(0, sentence_with_annotation.token(0).start());
EXPECT_EQ(5, sentence_with_annotation.token(0).end());
// The second token is 的.
EXPECT_EQ(7, sentence_with_annotation.token(1).start());
EXPECT_EQ(9, sentence_with_annotation.token(1).end());
// The third token is 句子.
EXPECT_EQ(11, sentence_with_annotation.token(2).start());
EXPECT_EQ(16, sentence_with_annotation.token(2).end());
// Test merge space to other tokens. Since spaces, or more generally break
// characters, should never be a part of any word, they are skipped no matter
// how they are tagged.
// 0 1 2 3 4 5 6
// 测 试 ' ' 的 ' ' 句 子
// S M M S M M M
while (!state.StackEmpty()) state.Pop();
segment_state->AddStart(0, &state);
segment_state->AddStart(3, &state);
sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(2, sentence_with_annotation.token_size());
// The first token is 测试. Note even a space is tagged as "merge", it is not
// attached to its previous word.
EXPECT_EQ(0, sentence_with_annotation.token(0).start());
EXPECT_EQ(5, sentence_with_annotation.token(0).end());
// The second token is 的句子.
EXPECT_EQ(7, sentence_with_annotation.token(1).start());
EXPECT_EQ(16, sentence_with_annotation.token(1).end());
// Test merge a token to space tokens. In such case, the current token would
// be merged to the first non-space token on its left side.
// 0 1 2 3 4 5 6
// 测 试 ' ' 的 ' ' 句 子
// S M S M S M M
while (!state.StackEmpty()) state.Pop();
segment_state->AddStart(0, &state);
segment_state->AddStart(2, &state);
segment_state->AddStart(4, &state);
sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(1, sentence_with_annotation.token_size());
EXPECT_EQ(0, sentence_with_annotation.token(0).start());
EXPECT_EQ(16, sentence_with_annotation.token(0).end());
}
TEST_F(BinarySegmentStateTest, SpaceDocumentTest) {
const char *str_sentence = "text: ' \t\t' "
"token { word: ' ' start: 0 end: 0 } "
"token { word: '\t' start: 1 end: 1 } "
"token { word: '\t' start: 2 end: 2 } ";
TextFormat::ParseFromString(str_sentence, sentence_.get());
BinarySegmentState *segment_state = new BinarySegmentState();
ParserState state(sentence_.get(), segment_state, &label_map_);
// Break-chars should always be skipped, no matter how they are tagged.
// 0 1 2
//' ' '\t' '\t'
// M M M
Sentence sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(0, sentence_with_annotation.token_size());
// 0 1 2
//' ' '\t' '\t'
// S S S
segment_state->AddStart(0, &state);
segment_state->AddStart(1, &state);
segment_state->AddStart(2, &state);
sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(0, sentence_with_annotation.token_size());
}
TEST_F(BinarySegmentStateTest, DocumentBeginWithSpaceTest) {
const char *str_sentence = "text: ' 空格' "
"token { word: ' ' start: 0 end: 0 } "
"token { word: '空' start: 1 end: 3 } "
"token { word: '格' start: 4 end: 6 } ";
TextFormat::ParseFromString(str_sentence, sentence_.get());
BinarySegmentState *segment_state = new BinarySegmentState();
ParserState state(sentence_.get(), segment_state, &label_map_);
// 0 1 2
//' ' 空 格
// M M M
Sentence sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(1, sentence_with_annotation.token_size());
// The first token is 空格.
EXPECT_EQ(1, sentence_with_annotation.token(0).start());
EXPECT_EQ(6, sentence_with_annotation.token(0).end());
// 0 1 2
//' ' 空 格
// S M M
while (!state.StackEmpty()) state.Pop();
segment_state->AddStart(0, &state);
sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(1, sentence_with_annotation.token_size());
// The first token is 空格.
EXPECT_EQ(1, sentence_with_annotation.token(0).start());
EXPECT_EQ(6, sentence_with_annotation.token(0).end());
}
TEST_F(BinarySegmentStateTest, EmptyDocumentTest) {
const char *str_sentence = "text: '' ";
TextFormat::ParseFromString(str_sentence, sentence_.get());
BinarySegmentState *segment_state = new BinarySegmentState();
ParserState state(sentence_.get(), segment_state, &label_map_);
Sentence sentence_with_annotation = *sentence_;
segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
ASSERT_EQ(0, sentence_with_annotation.token_size());
}
} // namespace syntaxnet