segmenter_utils.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <vector>
  16. #include <unordered_set>
  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. 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(const string &text,
  33. const vector<tensorflow::StringPiece> &chars,
  34. Sentence *sentence);
  35. // Returns true for UTF-8 characters that cannot be 'real' tokens. This is
  36. // defined as any whitespace, line break or paragraph break.
  37. static bool IsBreakChar(const string &word) {
  38. if (word == "\n" || word == "\t") return true;
  39. UnicodeText text;
  40. text.PointToUTF8(word.c_str(), word.length());
  41. CHECK_EQ(text.size(), 1);
  42. return kBreakChars.find(*text.begin()) != kBreakChars.end();
  43. }
  44. // Returns the break level for the next token based on the current character.
  45. static Token::BreakLevel BreakLevel(const string &word) {
  46. UnicodeText text;
  47. text.PointToUTF8(word.c_str(), word.length());
  48. auto point = *text.begin();
  49. if (word == "\n" || point == kLineSeparator) {
  50. return Token::LINE_BREAK;
  51. } else if (point == kParagraphSeparator) {
  52. return Token::SENTENCE_BREAK; // No PARAGRAPH_BREAK in sentence proto.
  53. } else if (word == "\t" || kBreakChars.find(point) != kBreakChars.end()) {
  54. return Token::SPACE_BREAK;
  55. }
  56. return Token::NO_BREAK;
  57. }
  58. // Convenience function for computing start/end byte offsets of a character
  59. // StringPiece relative to original text.
  60. static void GetCharStartEndBytes(const string &text,
  61. tensorflow::StringPiece c,
  62. int *start,
  63. int *end) {
  64. *start = c.data() - text.data();
  65. *end = *start + c.size() - 1;
  66. }
  67. // Returns true if this segment is a valid segment. Currently checks:
  68. // 1) It is non-empty
  69. // 2) It is valid UTF8
  70. static bool IsValidSegment(const Sentence &sentence, const Token &token);
  71. // Set for utf8 break characters.
  72. static const std::unordered_set<int> kBreakChars;
  73. static const int kLineSeparator = 0x2028;
  74. static const int kParagraphSeparator = 0x2029;
  75. };
  76. } // namespace syntaxnet
  77. #endif // SYNTAXNET_SEGMENTER_UTILS_H_