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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. namespace text {
  22. std::shared_ptr<LookupOperation> Lookup(const std::shared_ptr<Vocab> &vocab, const std::string &unknown_token) {
  23. auto op = std::make_shared<LookupOperation>(vocab, unknown_token);
  24. if (!op->ValidateParams()) {
  25. return nullptr;
  26. }
  27. return op;
  28. }
  29. // LookupOperation
  30. LookupOperation::LookupOperation(const std::shared_ptr<Vocab> &vocab, const std::string &unknown_token)
  31. : vocab_(vocab), unknown_token_(unknown_token), default_id_(Vocab::kNoTokenExists) {}
  32. bool LookupOperation::ValidateParams() {
  33. if (vocab_ == nullptr) {
  34. MS_LOG(ERROR) << "Lookup: vocab object type is incorrect or null.";
  35. return false;
  36. }
  37. if (unknown_token_.empty()) {
  38. MS_LOG(ERROR) << "Lookup: no unknown token is specified.";
  39. return false;
  40. } else {
  41. default_id_ = vocab_->Lookup(unknown_token_);
  42. if (default_id_ == Vocab::kNoTokenExists) {
  43. MS_LOG(ERROR) << "Lookup: unknown_token: [" + unknown_token_ + "], does not exist in vocab.";
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. std::shared_ptr<TensorOp> LookupOperation::Build() {
  50. std::shared_ptr<LookupOp> tensor_op = std::make_shared<LookupOp>(vocab_, default_id_);
  51. return tensor_op;
  52. }
  53. } // namespace text
  54. } // namespace api
  55. } // namespace dataset
  56. } // namespace mindspore