binary_segment_state_test.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "syntaxnet/binary_segment_state.h"
  13. #include <memory>
  14. #include "syntaxnet/base.h"
  15. #include "syntaxnet/sentence.pb.h"
  16. #include "syntaxnet/term_frequency_map.h"
  17. #include "tensorflow/core/platform/test.h"
  18. namespace syntaxnet {
  19. class BinarySegmentStateTest : public ::testing::Test {
  20. protected:
  21. void SetUp() override {
  22. // Prepare a sentence.
  23. const char *str_sentence = "text: '测试 的 句子' "
  24. "token { word: '测' start: 0 end: 2 } "
  25. "token { word: '试' start: 3 end: 5 } "
  26. "token { word: ' ' start: 6 end: 6 } "
  27. "token { word: '的' start: 7 end: 9 } "
  28. "token { word: ' ' start: 10 end: 10 } "
  29. "token { word: '句' start: 11 end: 13 } "
  30. "token { word: '子' start: 14 end: 16 } ";
  31. sentence_ = std::unique_ptr<Sentence>(new Sentence());
  32. TextFormat::ParseFromString(str_sentence, sentence_.get());
  33. }
  34. // The test document, parse tree, and sentence.
  35. std::unique_ptr<Sentence> sentence_;
  36. TermFrequencyMap label_map_;
  37. };
  38. TEST_F(BinarySegmentStateTest, AddStartLastStartNumStartsTest) {
  39. BinarySegmentState *segment_state = new BinarySegmentState();
  40. ParserState state(sentence_.get(), segment_state, &label_map_);
  41. // Test segment_state initialized with zero starts.
  42. EXPECT_EQ(0, segment_state->NumStarts(state));
  43. // Adding the first token as a start token.
  44. segment_state->AddStart(0, &state);
  45. ASSERT_EQ(1, segment_state->NumStarts(state));
  46. EXPECT_EQ(0, segment_state->LastStart(0, state));
  47. // Adding more starts.
  48. segment_state->AddStart(2, &state);
  49. segment_state->AddStart(3, &state);
  50. segment_state->AddStart(4, &state);
  51. segment_state->AddStart(5, &state);
  52. ASSERT_EQ(5, segment_state->NumStarts(state));
  53. EXPECT_EQ(5, segment_state->LastStart(0, state));
  54. EXPECT_EQ(4, segment_state->LastStart(1, state));
  55. EXPECT_EQ(3, segment_state->LastStart(2, state));
  56. EXPECT_EQ(2, segment_state->LastStart(3, state));
  57. EXPECT_EQ(0, segment_state->LastStart(4, state));
  58. }
  59. TEST_F(BinarySegmentStateTest, AddParseToDocumentTest) {
  60. BinarySegmentState *segment_state = new BinarySegmentState();
  61. ParserState state(sentence_.get(), segment_state, &label_map_);
  62. // Test gold segmentation.
  63. // 0 1 2 3 4 5 6
  64. // 测 试 ' ' 的 ' ' 句 子
  65. // S M S S S S M
  66. segment_state->AddStart(0, &state);
  67. segment_state->AddStart(2, &state);
  68. segment_state->AddStart(3, &state);
  69. segment_state->AddStart(4, &state);
  70. segment_state->AddStart(5, &state);
  71. Sentence sentence_with_annotation = *sentence_;
  72. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  73. // Test the number of tokens as well as the start/end byte-offsets of each
  74. // token.
  75. ASSERT_EQ(3, sentence_with_annotation.token_size());
  76. // The first token is 测试.
  77. EXPECT_EQ(0, sentence_with_annotation.token(0).start());
  78. EXPECT_EQ(5, sentence_with_annotation.token(0).end());
  79. // The second token is 的.
  80. EXPECT_EQ(7, sentence_with_annotation.token(1).start());
  81. EXPECT_EQ(9, sentence_with_annotation.token(1).end());
  82. // The third token is 句子.
  83. EXPECT_EQ(11, sentence_with_annotation.token(2).start());
  84. EXPECT_EQ(16, sentence_with_annotation.token(2).end());
  85. // Test merge space to other tokens. Since spaces, or more generally break
  86. // characters, should never be a part of any word, they are skipped no matter
  87. // how they are tagged.
  88. // 0 1 2 3 4 5 6
  89. // 测 试 ' ' 的 ' ' 句 子
  90. // S M M S M M M
  91. while (!state.StackEmpty()) state.Pop();
  92. segment_state->AddStart(0, &state);
  93. segment_state->AddStart(3, &state);
  94. sentence_with_annotation = *sentence_;
  95. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  96. ASSERT_EQ(2, sentence_with_annotation.token_size());
  97. // The first token is 测试. Note even a space is tagged as "merge", it is not
  98. // attached to its previous word.
  99. EXPECT_EQ(0, sentence_with_annotation.token(0).start());
  100. EXPECT_EQ(5, sentence_with_annotation.token(0).end());
  101. // The second token is 的句子.
  102. EXPECT_EQ(7, sentence_with_annotation.token(1).start());
  103. EXPECT_EQ(16, sentence_with_annotation.token(1).end());
  104. // Test merge a token to space tokens. In such case, the current token would
  105. // be merged to the first non-space token on its left side.
  106. // 0 1 2 3 4 5 6
  107. // 测 试 ' ' 的 ' ' 句 子
  108. // S M S M S M M
  109. while (!state.StackEmpty()) state.Pop();
  110. segment_state->AddStart(0, &state);
  111. segment_state->AddStart(2, &state);
  112. segment_state->AddStart(4, &state);
  113. sentence_with_annotation = *sentence_;
  114. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  115. ASSERT_EQ(1, sentence_with_annotation.token_size());
  116. EXPECT_EQ(0, sentence_with_annotation.token(0).start());
  117. EXPECT_EQ(16, sentence_with_annotation.token(0).end());
  118. }
  119. TEST_F(BinarySegmentStateTest, SpaceDocumentTest) {
  120. const char *str_sentence = "text: ' \t\t' "
  121. "token { word: ' ' start: 0 end: 0 } "
  122. "token { word: '\t' start: 1 end: 1 } "
  123. "token { word: '\t' start: 2 end: 2 } ";
  124. TextFormat::ParseFromString(str_sentence, sentence_.get());
  125. BinarySegmentState *segment_state = new BinarySegmentState();
  126. ParserState state(sentence_.get(), segment_state, &label_map_);
  127. // Break-chars should always be skipped, no matter how they are tagged.
  128. // 0 1 2
  129. //' ' '\t' '\t'
  130. // M M M
  131. Sentence sentence_with_annotation = *sentence_;
  132. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  133. ASSERT_EQ(0, sentence_with_annotation.token_size());
  134. // 0 1 2
  135. //' ' '\t' '\t'
  136. // S S S
  137. segment_state->AddStart(0, &state);
  138. segment_state->AddStart(1, &state);
  139. segment_state->AddStart(2, &state);
  140. sentence_with_annotation = *sentence_;
  141. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  142. ASSERT_EQ(0, sentence_with_annotation.token_size());
  143. }
  144. TEST_F(BinarySegmentStateTest, DocumentBeginWithSpaceTest) {
  145. const char *str_sentence = "text: ' 空格' "
  146. "token { word: ' ' start: 0 end: 0 } "
  147. "token { word: '空' start: 1 end: 3 } "
  148. "token { word: '格' start: 4 end: 6 } ";
  149. TextFormat::ParseFromString(str_sentence, sentence_.get());
  150. BinarySegmentState *segment_state = new BinarySegmentState();
  151. ParserState state(sentence_.get(), segment_state, &label_map_);
  152. // 0 1 2
  153. //' ' 空 格
  154. // M M M
  155. Sentence sentence_with_annotation = *sentence_;
  156. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  157. ASSERT_EQ(1, sentence_with_annotation.token_size());
  158. // The first token is 空格.
  159. EXPECT_EQ(1, sentence_with_annotation.token(0).start());
  160. EXPECT_EQ(6, sentence_with_annotation.token(0).end());
  161. // 0 1 2
  162. //' ' 空 格
  163. // S M M
  164. while (!state.StackEmpty()) state.Pop();
  165. segment_state->AddStart(0, &state);
  166. sentence_with_annotation = *sentence_;
  167. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  168. ASSERT_EQ(1, sentence_with_annotation.token_size());
  169. // The first token is 空格.
  170. EXPECT_EQ(1, sentence_with_annotation.token(0).start());
  171. EXPECT_EQ(6, sentence_with_annotation.token(0).end());
  172. }
  173. TEST_F(BinarySegmentStateTest, EmptyDocumentTest) {
  174. const char *str_sentence = "text: '' ";
  175. TextFormat::ParseFromString(str_sentence, sentence_.get());
  176. BinarySegmentState *segment_state = new BinarySegmentState();
  177. ParserState state(sentence_.get(), segment_state, &label_map_);
  178. Sentence sentence_with_annotation = *sentence_;
  179. segment_state->AddParseToDocument(state, false, &sentence_with_annotation);
  180. ASSERT_EQ(0, sentence_with_annotation.token_size());
  181. }
  182. } // namespace syntaxnet