You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

text.cc 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <unistd.h>
  17. #include "minddata/dataset/include/text.h"
  18. namespace mindspore {
  19. namespace dataset {
  20. // Transform operations for text.
  21. namespace text {
  22. // FUNCTIONS TO CREATE TEXT OPERATIONS
  23. // (In alphabetical order)
  24. #ifndef _WIN32
  25. std::shared_ptr<BasicTokenizerOperation> BasicTokenizer(bool lower_case, bool keep_whitespace,
  26. const NormalizeForm normalize_form, bool preserve_unused_token,
  27. bool with_offsets) {
  28. auto op = std::make_shared<BasicTokenizerOperation>(lower_case, keep_whitespace, normalize_form,
  29. preserve_unused_token, with_offsets);
  30. return op->ValidateParams() ? op : nullptr;
  31. }
  32. std::shared_ptr<BertTokenizerOperation> BertTokenizer(const std::shared_ptr<Vocab> &vocab,
  33. const std::string &suffix_indicator, int32_t max_bytes_per_token,
  34. const std::string &unknown_token, bool lower_case,
  35. bool keep_whitespace, const NormalizeForm normalize_form,
  36. bool preserve_unused_token, bool with_offsets) {
  37. auto op =
  38. std::make_shared<BertTokenizerOperation>(vocab, suffix_indicator, max_bytes_per_token, unknown_token, lower_case,
  39. keep_whitespace, normalize_form, preserve_unused_token, with_offsets);
  40. return op->ValidateParams() ? op : nullptr;
  41. }
  42. std::shared_ptr<CaseFoldOperation> CaseFold() {
  43. auto op = std::make_shared<CaseFoldOperation>();
  44. return op->ValidateParams() ? op : nullptr;
  45. }
  46. #endif
  47. std::shared_ptr<JiebaTokenizerOperation> JiebaTokenizer(const std::string &hmm_path, const std::string &mp_path,
  48. const JiebaMode &mode, bool with_offsets) {
  49. auto op = std::make_shared<JiebaTokenizerOperation>(hmm_path, mp_path, mode, with_offsets);
  50. return op->ValidateParams() ? op : nullptr;
  51. }
  52. std::shared_ptr<LookupOperation> Lookup(const std::shared_ptr<Vocab> &vocab,
  53. const std::optional<std::string> &unknown_token, const std::string &data_type) {
  54. auto op = std::make_shared<LookupOperation>(vocab, unknown_token, data_type);
  55. return op->ValidateParams() ? op : nullptr;
  56. }
  57. std::shared_ptr<NgramOperation> Ngram(const std::vector<int32_t> &ngrams,
  58. const std::pair<std::string, int32_t> &left_pad,
  59. const std::pair<std::string, int32_t> &right_pad, const std::string &separator) {
  60. auto op = std::make_shared<NgramOperation>(ngrams, left_pad, right_pad, separator);
  61. return op->ValidateParams() ? op : nullptr;
  62. }
  63. #ifndef _WIN32
  64. std::shared_ptr<NormalizeUTF8Operation> NormalizeUTF8(NormalizeForm normalize_form) {
  65. auto op = std::make_shared<NormalizeUTF8Operation>(normalize_form);
  66. return op->ValidateParams() ? op : nullptr;
  67. }
  68. std::shared_ptr<RegexReplaceOperation> RegexReplace(std::string pattern, std::string replace, bool replace_all) {
  69. auto op = std::make_shared<RegexReplaceOperation>(pattern, replace, replace_all);
  70. return op->ValidateParams() ? op : nullptr;
  71. }
  72. std::shared_ptr<RegexTokenizerOperation> RegexTokenizer(std::string delim_pattern, std::string keep_delim_pattern,
  73. bool with_offsets) {
  74. auto op = std::make_shared<RegexTokenizerOperation>(delim_pattern, keep_delim_pattern, with_offsets);
  75. return op->ValidateParams() ? op : nullptr;
  76. }
  77. #endif
  78. std::shared_ptr<SentencePieceTokenizerOperation> SentencePieceTokenizer(
  79. const std::shared_ptr<SentencePieceVocab> &vocab, SPieceTokenizerOutType out_type) {
  80. auto op = std::make_shared<SentencePieceTokenizerOperation>(vocab, out_type);
  81. return op->ValidateParams() ? op : nullptr;
  82. }
  83. std::shared_ptr<SentencePieceTokenizerOperation> SentencePieceTokenizer(const std::string &vocab_path,
  84. SPieceTokenizerOutType out_type) {
  85. auto op = std::make_shared<SentencePieceTokenizerOperation>(vocab_path, out_type);
  86. return op->ValidateParams() ? op : nullptr;
  87. }
  88. std::shared_ptr<SlidingWindowOperation> SlidingWindow(const int32_t width, const int32_t axis) {
  89. auto op = std::make_shared<SlidingWindowOperation>(width, axis);
  90. return op->ValidateParams() ? op : nullptr;
  91. }
  92. std::shared_ptr<ToNumberOperation> ToNumber(const std::string &data_type) {
  93. auto op = std::make_shared<ToNumberOperation>(data_type);
  94. return op->ValidateParams() ? op : nullptr;
  95. }
  96. std::shared_ptr<TruncateSequencePairOperation> TruncateSequencePair(int32_t max_length) {
  97. auto op = std::make_shared<TruncateSequencePairOperation>(max_length);
  98. return op->ValidateParams() ? op : nullptr;
  99. }
  100. std::shared_ptr<UnicodeCharTokenizerOperation> UnicodeCharTokenizer(bool with_offsets) {
  101. auto op = std::make_shared<UnicodeCharTokenizerOperation>(with_offsets);
  102. return op->ValidateParams() ? op : nullptr;
  103. }
  104. #ifndef _WIN32
  105. std::shared_ptr<UnicodeScriptTokenizerOperation> UnicodeScriptTokenizer(bool keep_whitespace, bool with_offsets) {
  106. auto op = std::make_shared<UnicodeScriptTokenizerOperation>(keep_whitespace, with_offsets);
  107. return op->ValidateParams() ? op : nullptr;
  108. }
  109. std::shared_ptr<WhitespaceTokenizerOperation> WhitespaceTokenizer(bool with_offsets) {
  110. auto op = std::make_shared<WhitespaceTokenizerOperation>(with_offsets);
  111. return op->ValidateParams() ? op : nullptr;
  112. }
  113. #endif
  114. } // namespace text
  115. } // namespace dataset
  116. } // namespace mindspore