segmenter_utils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef SYNTAXNET_SEGMENTER_UTILS_H_
  13. #define SYNTAXNET_SEGMENTER_UTILS_H_
  14. #include <string>
  15. #include <unordered_set>
  16. #include <vector>
  17. #include "syntaxnet/sentence.pb.h"
  18. #include "tensorflow/core/lib/strings/strcat.h"
  19. #include "util/utf8/unicodetext.h"
  20. namespace syntaxnet {
  21. // A set of common convenience functions.
  22. class SegmenterUtils {
  23. public:
  24. // Takes a text and convert it into a vector, where each element is a utf8
  25. // character.
  26. static void GetUTF8Chars(const string &text,
  27. std::vector<tensorflow::StringPiece> *chars);
  28. // Sets tokens in the sentence so that each token is a single character.
  29. // Assigns the start/end byte offsets.
  30. //
  31. // If the sentence is not empty, the current tokens will be cleared.
  32. static void SetCharsAsTokens(
  33. const string &text, const std::vector<tensorflow::StringPiece> &chars,
  34. Sentence *sentence);
  35. // Takes a sentence with its original text and gold tokens and outputs a new
  36. // sentence with the original text, but have a single utf8 character per token
  37. // and a break level that is set to:
  38. // 0 (NO_BREAK) iff in the gold tokenization there was no break from the
  39. // last token and merge with previous token.
  40. // 1 (SPACE_BREAK) iff in the gold tokenization there was a break from the
  41. // last token. SPACE_BREAK represents all breaks.
  42. // Returns true if sentence token start/end bytes are consistent with UTF-8
  43. // characters.
  44. static bool ConvertToCharTokenDoc(const Sentence &sentence,
  45. Sentence *char_sentence);
  46. // Returns true if the start/end byte offsets of a sentence's tokens are
  47. // consistent with UTF8 character boundaries.
  48. // Note: it must be the case that chars was constructed from sentence.text().
  49. static bool DocTokensUTF8Consistent(
  50. const std::vector<tensorflow::StringPiece> &chars,
  51. const Sentence &document);
  52. // Returns true for UTF-8 characters that cannot be 'real' tokens. This is
  53. // defined as any whitespace, line break or paragraph break.
  54. static bool IsBreakChar(const string &word) {
  55. if (word == "\n" || word == "\t") return true;
  56. UnicodeText text;
  57. text.PointToUTF8(word.c_str(), word.length());
  58. CHECK_EQ(text.size(), 1);
  59. return kBreakChars.find(*text.begin()) != kBreakChars.end();
  60. }
  61. // Returns the break level for the next token based on the current character.
  62. static Token::BreakLevel BreakLevel(const string &word) {
  63. UnicodeText text;
  64. text.PointToUTF8(word.c_str(), word.length());
  65. auto point = *text.begin();
  66. if (word == "\n" || point == kLineSeparator) {
  67. return Token::LINE_BREAK;
  68. } else if (point == kParagraphSeparator) {
  69. return Token::SENTENCE_BREAK; // No PARAGRAPH_BREAK in sentence proto.
  70. } else if (word == "\t" || kBreakChars.find(point) != kBreakChars.end()) {
  71. return Token::SPACE_BREAK;
  72. }
  73. return Token::NO_BREAK;
  74. }
  75. // Convenience function for computing start/end byte offsets of a character
  76. // StringPiece relative to original text.
  77. static void GetCharStartEndBytes(const string &text,
  78. tensorflow::StringPiece c,
  79. int *start,
  80. int *end) {
  81. *start = c.data() - text.data();
  82. *end = *start + c.size() - 1;
  83. }
  84. // Returns true if this segment is a valid segment. Currently checks:
  85. // 1) It is non-empty
  86. // 2) It is valid UTF8
  87. static bool IsValidSegment(const Sentence &sentence, const Token &token);
  88. // Set for utf8 break characters.
  89. static const std::unordered_set<int> kBreakChars;
  90. static const int kLineSeparator = 0x2028;
  91. static const int kParagraphSeparator = 0x2029;
  92. };
  93. } // namespace syntaxnet
  94. #endif // SYNTAXNET_SEGMENTER_UTILS_H_