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

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