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.

jieba_tokenizer_op_test.cc 3.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <string>
  17. #include <string_view>
  18. #include "common/common.h"
  19. #include "minddata/dataset/text/kernels/jieba_tokenizer_op.h"
  20. #include "gtest/gtest.h"
  21. #include "utils/log_adapter.h"
  22. using namespace mindspore::dataset;
  23. class MindDataTestJiebaTokenizerOp : public UT::DatasetOpTesting {
  24. public:
  25. void CheckEqual(const std::shared_ptr<Tensor> &o, const std::vector<dsize_t> &index, const std::string &expect) {
  26. std::string_view str;
  27. Status s = o->GetItemAt(&str, index);
  28. EXPECT_TRUE(s.IsOk());
  29. EXPECT_EQ(str, expect);
  30. }
  31. };
  32. TEST_F(MindDataTestJiebaTokenizerOp, TestJieba_opFuntions) {
  33. MS_LOG(INFO) << "Doing MindDataTestJiebaTokenizerOp TestJieba_opFuntions.";
  34. std::string dataset_path = datasets_root_path_ + "/jiebadict";
  35. std::string hmm_path = dataset_path + "/hmm_model.utf8";
  36. std::string mp_path = dataset_path + "/jieba.dict.utf8";
  37. TensorRow input, output;
  38. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  39. std::shared_ptr<Tensor> input_tensor;
  40. Tensor::CreateScalar<std::string>("今天天气太好了我们一起去外面玩吧", &input_tensor);
  41. input.push_back(input_tensor);
  42. Status s = op->Compute(input, &output);
  43. EXPECT_TRUE(s.IsOk());
  44. EXPECT_EQ(output[0]->Rank(), 1);
  45. EXPECT_EQ(output[0]->Size(), 7);
  46. CheckEqual(output[0], {0}, "今天天气");
  47. CheckEqual(output[0], {1}, "太好了");
  48. CheckEqual(output[0], {2}, "我们");
  49. CheckEqual(output[0], {3}, "一起");
  50. CheckEqual(output[0], {4}, "去");
  51. CheckEqual(output[0], {5}, "外面");
  52. CheckEqual(output[0], {6}, "玩吧");
  53. }
  54. TEST_F(MindDataTestJiebaTokenizerOp, TestJieba_opAdd) {
  55. MS_LOG(INFO) << "Doing MindDataTestJiebaTokenizerOp TestJieba_opAdd.";
  56. std::string dataset_path = datasets_root_path_ + "/jiebadict";
  57. std::string hmm_path = dataset_path + "/hmm_model.utf8";
  58. std::string mp_path = dataset_path + "/jieba.dict.utf8";
  59. TensorRow input, output;
  60. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  61. op->AddWord("男默女泪");
  62. std::shared_ptr<Tensor> input_tensor;
  63. Tensor::CreateScalar<std::string>("男默女泪", &input_tensor);
  64. input.push_back(input_tensor);
  65. Status s = op->Compute(input, &output);
  66. EXPECT_TRUE(s.IsOk());
  67. EXPECT_EQ(output[0]->Rank(), 1);
  68. EXPECT_EQ(output[0]->Size(), 1);
  69. CheckEqual(output[0], {0}, "男默女泪");
  70. }
  71. TEST_F(MindDataTestJiebaTokenizerOp, TestJieba_opEmpty) {
  72. MS_LOG(INFO) << "Doing MindDataTestJiebaTokenizerOp TestJieba_opEmpty.";
  73. std::string dataset_path = datasets_root_path_ + "/jiebadict";
  74. std::string hmm_path = dataset_path + "/hmm_model.utf8";
  75. std::string mp_path = dataset_path + "/jieba.dict.utf8";
  76. TensorRow input, output;
  77. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  78. op->AddWord("男默女泪");
  79. std::shared_ptr<Tensor> input_tensor;
  80. Tensor::CreateScalar<std::string>("", &input_tensor);
  81. input.push_back(input_tensor);
  82. Status s = op->Compute(input, &output);
  83. EXPECT_TRUE(s.IsOk());
  84. EXPECT_EQ(output[0]->Rank(), 1);
  85. EXPECT_EQ(output[0]->Size(), 1);
  86. CheckEqual(output[0], {0}, "");
  87. }