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.

build_vocab_test.cc 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * Copyright 2020-2021 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 <memory>
  17. #include <vector>
  18. #include <string>
  19. #include "common/common.h"
  20. #include "include/api/status.h"
  21. #include "minddata/dataset/text/vocab.h"
  22. using mindspore::dataset::Tensor;
  23. using mindspore::dataset::Vocab;
  24. class MindDataTestVocab : public UT::DatasetOpTesting {
  25. protected:
  26. };
  27. TEST_F(MindDataTestVocab, TestVocabFromUnorderedMap) {
  28. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromUnorderedMap.";
  29. // Build a map
  30. std::unordered_map<std::string, int32_t> dict;
  31. dict["banana"] = 0;
  32. dict["apple"] = 1;
  33. dict["cat"] = 2;
  34. dict["dog"] = 3;
  35. // Build vocab from map
  36. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  37. Status s = Vocab::BuildFromUnorderedMap(dict, &vocab);
  38. EXPECT_EQ(s, Status::OK());
  39. // Look up specified words
  40. std::vector<std::string> words = {"apple", "dog", "egg"};
  41. std::vector<int64_t> expected = {1, 3, -1};
  42. for (uint32_t i = 0; i < words.size(); ++i) {
  43. int32_t x = vocab->Lookup(words[i]);
  44. EXPECT_EQ(x, expected[i]);
  45. }
  46. }
  47. TEST_F(MindDataTestVocab, TestVocabFromEmptyMap) {
  48. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromEmptyMap.";
  49. // Build vocab from empty map
  50. std::unordered_map<std::string, int32_t> dict;
  51. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  52. Status s = Vocab::BuildFromUnorderedMap(dict, &vocab);
  53. EXPECT_EQ(s, Status::OK());
  54. // Look up specified words
  55. // Expect that we will return -1 when word is not in vocab
  56. std::vector<std::string> words = {"apple", "dog", "egg"};
  57. std::vector<int64_t> expected = {-1, -1, -1};
  58. for (uint32_t i = 0; i < words.size(); ++i) {
  59. int32_t x = vocab->Lookup(words[i]);
  60. EXPECT_EQ(x, expected[i]);
  61. }
  62. }
  63. TEST_F(MindDataTestVocab, TestVocabFromMapFail) {
  64. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromMapFail.";
  65. // Build a map
  66. std::unordered_map<std::string, int32_t> dict;
  67. dict["banana"] = 0;
  68. dict["apple"] = -1;
  69. // Expected failure: index of word can not be negative
  70. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  71. Status s = Vocab::BuildFromUnorderedMap(dict, &vocab);
  72. EXPECT_NE(s, Status::OK());
  73. }
  74. TEST_F(MindDataTestVocab, TestVocabFromVectorPrependSpTokens) {
  75. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorPrependSpTokens.";
  76. // Build vocab from a vector of words, special tokens are prepended to vocab
  77. std::vector<std::string> list = {"apple", "banana", "cat", "dog", "egg"};
  78. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  79. Status s = Vocab::BuildFromVector(list, {"<unk>"}, true, &vocab);
  80. EXPECT_EQ(s, Status::OK());
  81. // Look up specified words
  82. // Expect that we will return -1 when word is not in vocab
  83. std::vector<std::string> words = {"apple", "banana", "fox"};
  84. std::vector<int64_t> expected = {1, 2, -1};
  85. for (uint32_t i = 0; i < words.size(); ++i) {
  86. int32_t x = vocab->Lookup(words[i]);
  87. EXPECT_EQ(x, expected[i]);
  88. }
  89. }
  90. TEST_F(MindDataTestVocab, TestVocabFromVectorAppendSpTokens) {
  91. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorAppendSpTokens.";
  92. // Build vocab from a vector of words, special tokens are appended to vocab
  93. std::vector<std::string> list = {"apple", "banana", "cat", "dog", "egg"};
  94. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  95. Status s = Vocab::BuildFromVector(list, {"<unk>"}, false, &vocab);
  96. EXPECT_EQ(s, Status::OK());
  97. // Look up specified words
  98. std::vector<std::string> words = {"apple", "<unk>", "fox"};
  99. std::vector<int64_t> expected = {0, 5, -1};
  100. for (uint32_t i = 0; i < words.size(); ++i) {
  101. int32_t x = vocab->Lookup(words[i]);
  102. EXPECT_EQ(x, expected[i]);
  103. }
  104. }
  105. TEST_F(MindDataTestVocab, TestVocabFromVectorWithNoSpTokens) {
  106. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorWithNoSpTokens.";
  107. // Build vocab from a vector of words with no special tokens
  108. std::vector<std::string> list = {"apple", "banana", "cat", "dog", "egg"};
  109. std::vector<std::string> sp_tokens = {};
  110. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  111. Status s = Vocab::BuildFromVector(list, sp_tokens, true, &vocab);
  112. EXPECT_EQ(s, Status::OK());
  113. // Look up specified words
  114. std::vector<std::string> words = {"apple", "banana", "fox", "<pad>"};
  115. std::vector<int64_t> expected = {0, 1, -1, -1};
  116. for (uint32_t i = 0; i < words.size(); ++i) {
  117. int32_t x = vocab->Lookup(words[i]);
  118. EXPECT_EQ(x, expected[i]);
  119. }
  120. }
  121. TEST_F(MindDataTestVocab, TestVocabFromEmptyVector) {
  122. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromEmptyVector.";
  123. // Build vocab from empty vector
  124. std::vector<std::string> list = {};
  125. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  126. Status s = Vocab::BuildFromVector(list, {}, false, &vocab);
  127. EXPECT_EQ(s, Status::OK());
  128. // Look up specified words
  129. // Expect that we will return -1 when word is not in vocab
  130. std::vector<std::string> words = {"apple", "banana", "fox"};
  131. std::vector<int64_t> expected = {-1, -1, -1};
  132. for (uint32_t i = 0; i < words.size(); ++i) {
  133. int32_t x = vocab->Lookup(words[i]);
  134. EXPECT_EQ(x, expected[i]);
  135. }
  136. }
  137. TEST_F(MindDataTestVocab, TestVocabFromVectorFail1) {
  138. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorFail1.";
  139. // Build vocab from a vector of words
  140. std::vector<std::string> list = {"apple", "apple", "cat", "cat", "egg"};
  141. std::vector<std::string> sp_tokens = {};
  142. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  143. // Expected failure: duplicate word apple
  144. Status s = Vocab::BuildFromVector(list, sp_tokens, true, &vocab);
  145. EXPECT_NE(s, Status::OK());
  146. }
  147. TEST_F(MindDataTestVocab, TestVocabFromVectorFail2) {
  148. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorFail2.";
  149. // Build vocab from a vector
  150. std::vector<std::string> list = {"apple", "dog", "egg"};
  151. std::vector<std::string> sp_tokens = {"<pad>", "<unk>", "<pad>", "<unk>", "<none>"};
  152. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  153. // Expected failure: duplicate special token <pad> <unk>
  154. Status s = Vocab::BuildFromVector(list, sp_tokens, true, &vocab);
  155. EXPECT_NE(s, Status::OK());
  156. }
  157. TEST_F(MindDataTestVocab, TestVocabFromVectorFail3) {
  158. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromVectorFail3.";
  159. // Build vocab from a vector
  160. std::vector<std::string> list = {"apple", "dog", "egg", "<unk>", ""};
  161. std::vector<std::string> sp_tokens = {"", "<unk>"};
  162. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  163. // Expected failure: special tokens are already existed in word_list
  164. Status s = Vocab::BuildFromVector(list, sp_tokens, true, &vocab);
  165. EXPECT_NE(s, Status::OK());
  166. }
  167. TEST_F(MindDataTestVocab, TestVocabFromFile) {
  168. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromFile.";
  169. // Build vocab from local file
  170. std::string vocab_dir = datasets_root_path_ + "/testVocab/vocab_list.txt";
  171. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  172. Status s = Vocab::BuildFromFileCpp(vocab_dir, ",", -1, {"<pad>", "<unk>"}, true, &vocab);
  173. EXPECT_EQ(s, Status::OK());
  174. // Look up specified words
  175. std::vector<std::string> words = {"not", "all"};
  176. std::vector<int64_t> expected = {2, 3};
  177. for (uint32_t i = 0; i < words.size(); ++i) {
  178. int32_t x = vocab->Lookup(words[i]);
  179. EXPECT_EQ(x, expected[i]);
  180. }
  181. }
  182. TEST_F(MindDataTestVocab, TestVocabFromFileFail1) {
  183. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromFileFail1.";
  184. // Build vocab from local file which is not exist
  185. std::string vocab_dir = datasets_root_path_ + "/testVocab/not_exist.txt";
  186. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  187. Status s = Vocab::BuildFromFileCpp(vocab_dir, ",", -1, {}, true, &vocab);
  188. EXPECT_NE(s, Status::OK());
  189. }
  190. TEST_F(MindDataTestVocab, TestVocabFromFileFail2) {
  191. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromFileFail2.";
  192. // Build vocab from local file
  193. std::string vocab_dir = datasets_root_path_ + "/testVocab/vocab_list.txt";
  194. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  195. // Expected failure: vocab_size should be either -1 or positive integer
  196. Status s = Vocab::BuildFromFileCpp(vocab_dir, ",", -2, {}, true, &vocab);
  197. EXPECT_NE(s, Status::OK());
  198. }
  199. TEST_F(MindDataTestVocab, TestVocabFromFileFail3) {
  200. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromFileFail3.";
  201. // Build vocab from local file
  202. std::string vocab_dir = datasets_root_path_ + "/testVocab/vocab_list.txt";
  203. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  204. // Expected failure: duplicate special token <unk>
  205. Status s = Vocab::BuildFromFileCpp(vocab_dir, ",", -1, {"<unk>", "<unk>"}, true, &vocab);
  206. EXPECT_NE(s, Status::OK());
  207. }
  208. TEST_F(MindDataTestVocab, TestVocabFromFileFail4) {
  209. MS_LOG(INFO) << "Doing MindDataTestVocab-TestVocabFromFileFail4.";
  210. // Build vocab from local file
  211. std::string vocab_dir = datasets_root_path_ + "/testVocab/vocab_list.txt";
  212. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  213. // Expected failure: special_tokens and word_list contain duplicate word
  214. Status s = Vocab::BuildFromFileCpp(vocab_dir, ",", -1, {"home"}, true, &vocab);
  215. EXPECT_NE(s, Status::OK());
  216. }