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 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright 2020 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 "minddata/dataset/include/text.h"
  17. #include "minddata/dataset/text/kernels/lookup_op.h"
  18. namespace mindspore {
  19. namespace dataset {
  20. namespace api {
  21. // Transform operations for text.
  22. namespace text {
  23. // FUNCTIONS TO CREATE TEXT OPERATIONS
  24. // (In alphabetical order)
  25. std::shared_ptr<LookupOperation> Lookup(const std::shared_ptr<Vocab> &vocab, const std::string &unknown_token,
  26. const DataType &data_type) {
  27. auto op = std::make_shared<LookupOperation>(vocab, unknown_token, data_type);
  28. if (!op->ValidateParams()) {
  29. return nullptr;
  30. }
  31. return op;
  32. }
  33. /* ####################################### Validator Functions ############################################ */
  34. /* ####################################### Derived TensorOperation classes ################################# */
  35. // (In alphabetical order)
  36. // LookupOperation
  37. LookupOperation::LookupOperation(const std::shared_ptr<Vocab> &vocab, const std::string &unknown_token,
  38. const DataType &data_type)
  39. : vocab_(vocab), unknown_token_(unknown_token), default_id_(Vocab::kNoTokenExists), data_type_(data_type) {}
  40. Status LookupOperation::ValidateParams() {
  41. if (vocab_ == nullptr) {
  42. std::string err_msg = "Lookup: vocab object type is incorrect or null.";
  43. MS_LOG(ERROR) << err_msg;
  44. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  45. }
  46. default_id_ = vocab_->Lookup(unknown_token_);
  47. if (default_id_ == Vocab::kNoTokenExists) {
  48. std::string err_msg = "Lookup: " + unknown_token_ + " doesn't exist in vocab.";
  49. MS_LOG(ERROR) << err_msg;
  50. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  51. }
  52. return Status::OK();
  53. }
  54. std::shared_ptr<TensorOp> LookupOperation::Build() {
  55. std::shared_ptr<LookupOp> tensor_op = std::make_shared<LookupOp>(vocab_, default_id_, data_type_);
  56. return tensor_op;
  57. }
  58. } // namespace text
  59. } // namespace api
  60. } // namespace dataset
  61. } // namespace mindspore