binary_segment_state.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <string>
  14. #include "syntaxnet/segmenter_utils.h"
  15. #include "syntaxnet/sentence.pb.h"
  16. namespace syntaxnet {
  17. ParserTransitionState *BinarySegmentState::Clone() const {
  18. return new BinarySegmentState();
  19. }
  20. string BinarySegmentState::ToString(const ParserState &state) const {
  21. string str("[");
  22. for (int i = NumStarts(state) - 1; i >=0; --i) {
  23. int start = LastStart(i, state);
  24. int end = 0;
  25. if (i - 1 >= 0) {
  26. end = LastStart(i - 1, state) - 1;
  27. } else if (state.EndOfInput()) {
  28. end = state.sentence().token_size() - 1;
  29. } else {
  30. end = state.Next() - 1;
  31. }
  32. for (int k = start; k <= end; ++k) {
  33. str.append(state.GetToken(k).word());
  34. }
  35. if (i >= 1) str.append(" ");
  36. }
  37. str.append("] ");
  38. for (int i = state.Next(); i < state.NumTokens(); ++i) {
  39. str.append(state.GetToken(i).word());
  40. }
  41. return str;
  42. }
  43. void BinarySegmentState::AddParseToDocument(const ParserState &state,
  44. bool rewrite_root_labels,
  45. Sentence *sentence) const {
  46. if (sentence->token_size() == 0) return;
  47. vector<bool> is_starts(sentence->token_size(), false);
  48. for (int i = 0; i < NumStarts(state); ++i) {
  49. is_starts[LastStart(i, state)] = true;
  50. }
  51. // Break level of the current token is determined based on its previous token.
  52. Token::BreakLevel break_level = Token::NO_BREAK;
  53. bool is_first_token = true;
  54. Sentence new_sentence;
  55. for (int i = 0; i < sentence->token_size(); ++i) {
  56. const Token &token = sentence->token(i);
  57. const string &word = token.word();
  58. bool is_break = SegmenterUtils::IsBreakChar(word);
  59. if (is_starts[i] || is_first_token) {
  60. if (!is_break) {
  61. // The current character is the first char of a new token/word.
  62. Token *new_token = new_sentence.add_token();
  63. new_token->set_start(token.start());
  64. new_token->set_end(token.end());
  65. new_token->set_word(word);
  66. // For the first token, keep the old break level to make sure that the
  67. // number of sentences stays unchanged.
  68. new_token->set_break_level(break_level);
  69. is_first_token = false;
  70. }
  71. } else {
  72. // Append the character to the previous token.
  73. if (!is_break) {
  74. int index = new_sentence.token_size() - 1;
  75. auto *last_token = new_sentence.mutable_token(index);
  76. last_token->mutable_word()->append(word);
  77. last_token->set_end(token.end());
  78. }
  79. }
  80. // Update break level. Note we do not introduce new sentences in the
  81. // transition system, thus anything goes beyond line break would be reduced
  82. // to line break.
  83. break_level = is_break ? SegmenterUtils::BreakLevel(word) : Token::NO_BREAK;
  84. if (break_level >= Token::LINE_BREAK) break_level = Token::LINE_BREAK;
  85. }
  86. sentence->mutable_token()->Swap(new_sentence.mutable_token());
  87. }
  88. } // namespace syntaxnet