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