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

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