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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. bool LookupOperation::ValidateParams() {
  41. if (vocab_ == nullptr) {
  42. MS_LOG(ERROR) << "Lookup: vocab object type is incorrect or null.";
  43. return false;
  44. }
  45. default_id_ = vocab_->Lookup(unknown_token_);
  46. if (default_id_ == Vocab::kNoTokenExists) {
  47. MS_LOG(ERROR) << "Lookup: " << unknown_token_ << " doesn't exist in vocab.";
  48. return false;
  49. }
  50. return true;
  51. }
  52. std::shared_ptr<TensorOp> LookupOperation::Build() {
  53. std::shared_ptr<LookupOp> tensor_op = std::make_shared<LookupOp>(vocab_, default_id_, data_type_);
  54. return tensor_op;
  55. }
  56. } // namespace text
  57. } // namespace api
  58. } // namespace dataset
  59. } // namespace mindspore