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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "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. std::shared_ptr<Tensor> output_tensor;
  38. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  39. std::shared_ptr<Tensor> input_tensor = std::make_shared<Tensor>("今天天气太好了我们一起去外面玩吧");
  40. Status s = op->Compute(input_tensor, &output_tensor);
  41. EXPECT_TRUE(s.IsOk());
  42. EXPECT_EQ(output_tensor->Rank(), 1);
  43. EXPECT_EQ(output_tensor->Size(), 7);
  44. CheckEqual(output_tensor, {0}, "今天天气");
  45. CheckEqual(output_tensor, {1}, "太好了");
  46. CheckEqual(output_tensor, {2}, "我们");
  47. CheckEqual(output_tensor, {3}, "一起");
  48. CheckEqual(output_tensor, {4}, "去");
  49. CheckEqual(output_tensor, {5}, "外面");
  50. CheckEqual(output_tensor, {6}, "玩吧");
  51. }
  52. TEST_F(MindDataTestJiebaTokenizerOp, TestJieba_opAdd) {
  53. MS_LOG(INFO) << "Doing MindDataTestJiebaTokenizerOp TestJieba_opAdd.";
  54. std::string dataset_path = datasets_root_path_ + "/jiebadict";
  55. std::string hmm_path = dataset_path + "/hmm_model.utf8";
  56. std::string mp_path = dataset_path + "/jieba.dict.utf8";
  57. std::shared_ptr<Tensor> output_tensor;
  58. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  59. op->AddWord("男默女泪");
  60. std::shared_ptr<Tensor> input_tensor = std::make_shared<Tensor>("男默女泪");
  61. Status s = op->Compute(input_tensor, &output_tensor);
  62. EXPECT_TRUE(s.IsOk());
  63. EXPECT_EQ(output_tensor->Rank(), 1);
  64. EXPECT_EQ(output_tensor->Size(), 1);
  65. CheckEqual(output_tensor, {0}, "男默女泪");
  66. }
  67. TEST_F(MindDataTestJiebaTokenizerOp, TestJieba_opEmpty) {
  68. MS_LOG(INFO) << "Doing MindDataTestJiebaTokenizerOp TestJieba_opEmpty.";
  69. std::string dataset_path = datasets_root_path_ + "/jiebadict";
  70. std::string hmm_path = dataset_path + "/hmm_model.utf8";
  71. std::string mp_path = dataset_path + "/jieba.dict.utf8";
  72. std::shared_ptr<Tensor> output_tensor;
  73. std::unique_ptr<JiebaTokenizerOp> op(new JiebaTokenizerOp(hmm_path, mp_path));
  74. op->AddWord("男默女泪");
  75. std::shared_ptr<Tensor> input_tensor = std::make_shared<Tensor>("");
  76. Status s = op->Compute(input_tensor, &output_tensor);
  77. EXPECT_TRUE(s.IsOk());
  78. EXPECT_EQ(output_tensor->Rank(), 1);
  79. EXPECT_EQ(output_tensor->Size(), 1);
  80. CheckEqual(output_tensor, {0}, "");
  81. }