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.

c_api_text_test.cc 89 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  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 <memory>
  17. #include <vector>
  18. #include <string>
  19. #include "common/common.h"
  20. #include "minddata/dataset/include/config.h"
  21. #include "minddata/dataset/include/datasets.h"
  22. #include "minddata/dataset/include/status.h"
  23. #include "minddata/dataset/include/transforms.h"
  24. #include "minddata/dataset/include/text.h"
  25. using namespace mindspore::dataset;
  26. using mindspore::dataset::DataType;
  27. using mindspore::dataset::ShuffleMode;
  28. using mindspore::dataset::Status;
  29. using mindspore::dataset::Tensor;
  30. using mindspore::dataset::Vocab;
  31. class MindDataTestPipeline : public UT::DatasetOpTesting {
  32. protected:
  33. };
  34. TEST_F(MindDataTestPipeline, TestBasicTokenizerSuccess1) {
  35. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBasicTokenizerSuccess1.";
  36. // Test BasicTokenizer with default parameters
  37. // Create a TextFile dataset
  38. std::string data_file = datasets_root_path_ + "/testTokenizerData/basic_tokenizer.txt";
  39. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  40. EXPECT_NE(ds, nullptr);
  41. // Create Take operation on ds
  42. ds = ds->Take(6);
  43. EXPECT_NE(ds, nullptr);
  44. // Create BasicTokenizer operation on ds
  45. std::shared_ptr<TensorOperation> basic_tokenizer = text::BasicTokenizer();
  46. EXPECT_NE(basic_tokenizer, nullptr);
  47. // Create Map operation on ds
  48. ds = ds->Map({basic_tokenizer}, {"text"});
  49. EXPECT_NE(ds, nullptr);
  50. // Create an iterator over the result of the above dataset
  51. // This will trigger the creation of the Execution Tree and launch it.
  52. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  53. EXPECT_NE(iter, nullptr);
  54. // Iterate the dataset and get each row
  55. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  56. iter->GetNextRow(&row);
  57. std::vector<std::vector<std::string>> expected = {
  58. {"Welcome", "to", "Beijing", "北", "京", "欢", "迎", "您"},
  59. {"長", "風", "破", "浪", "會", "有", "時", ",", "直", "掛", "雲", "帆", "濟", "滄", "海"},
  60. {"😀", "嘿", "嘿", "😃", "哈", "哈", "😄", "大", "笑", "😁", "嘻", "嘻"},
  61. {"明", "朝", "(", "1368", "—", "1644", "年", ")", "和", "清", "朝", "(", "1644", "—", "1911", "年", ")",
  62. ",", "是", "中", "国", "封", "建", "王", "朝", "史", "上", "最", "后", "两", "个", "朝", "代"},
  63. {"明", "代", "(", "1368", "-", "1644", ")", "と", "清", "代", "(", "1644",
  64. "-", "1911", ")", "は", "、", "中", "国", "の", "封", "建", "王", "朝",
  65. "の", "歴", "史", "における", "最", "後", "の2つの", "王", "朝", "でした"},
  66. {"명나라", "(", "1368", "-", "1644", ")", "와", "청나라", "(", "1644", "-",
  67. "1911", ")", "는", "중국", "봉건", "왕조의", "역사에서", "마지막", "두", "왕조였다"}};
  68. uint64_t i = 0;
  69. while (row.size() != 0) {
  70. auto ind = row["text"];
  71. std::shared_ptr<Tensor> expected_tensor;
  72. Tensor::CreateFromVector(expected[i], &expected_tensor);
  73. EXPECT_EQ(*ind, *expected_tensor);
  74. iter->GetNextRow(&row);
  75. i++;
  76. }
  77. EXPECT_EQ(i, 6);
  78. // Manually terminate the pipeline
  79. iter->Stop();
  80. }
  81. TEST_F(MindDataTestPipeline, TestBasicTokenizerSuccess2) {
  82. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBasicTokenizerSuccess2.";
  83. // Test BasicTokenizer with lower_case true
  84. // Create a TextFile dataset
  85. std::string data_file = datasets_root_path_ + "/testTokenizerData/basic_tokenizer.txt";
  86. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  87. EXPECT_NE(ds, nullptr);
  88. // Create Skip operation on ds
  89. ds = ds->Skip(6);
  90. EXPECT_NE(ds, nullptr);
  91. // Create BasicTokenizer operation on ds
  92. std::shared_ptr<TensorOperation> basic_tokenizer = text::BasicTokenizer(true);
  93. EXPECT_NE(basic_tokenizer, nullptr);
  94. // Create Map operation on ds
  95. ds = ds->Map({basic_tokenizer}, {"text"});
  96. EXPECT_NE(ds, nullptr);
  97. // Create an iterator over the result of the above dataset
  98. // This will trigger the creation of the Execution Tree and launch it.
  99. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  100. EXPECT_NE(iter, nullptr);
  101. // Iterate the dataset and get each row
  102. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  103. iter->GetNextRow(&row);
  104. std::vector<std::string> expected = {"this", "is", "a", "funky", "string"};
  105. uint64_t i = 0;
  106. while (row.size() != 0) {
  107. auto ind = row["text"];
  108. std::shared_ptr<Tensor> expected_tensor;
  109. Tensor::CreateFromVector(expected, &expected_tensor);
  110. EXPECT_EQ(*ind, *expected_tensor);
  111. iter->GetNextRow(&row);
  112. i++;
  113. }
  114. EXPECT_EQ(i, 1);
  115. // Manually terminate the pipeline
  116. iter->Stop();
  117. }
  118. TEST_F(MindDataTestPipeline, TestBasicTokenizerSuccess3) {
  119. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBasicTokenizerSuccess3.";
  120. // Test BasicTokenizer with with_offsets true and lower_case true
  121. // Create a TextFile dataset
  122. std::string data_file = datasets_root_path_ + "/testTokenizerData/basic_tokenizer.txt";
  123. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  124. EXPECT_NE(ds, nullptr);
  125. // Create Skip operation on ds
  126. ds = ds->Skip(6);
  127. EXPECT_NE(ds, nullptr);
  128. // Create BasicTokenizer operation on ds
  129. std::shared_ptr<TensorOperation> basic_tokenizer =
  130. text::BasicTokenizer(true, false, NormalizeForm::kNone, true, true);
  131. EXPECT_NE(basic_tokenizer, nullptr);
  132. // Create Map operation on ds
  133. ds = ds->Map({basic_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"});
  134. EXPECT_NE(ds, nullptr);
  135. // Create an iterator over the result of the above dataset
  136. // This will trigger the creation of the Execution Tree and launch it.
  137. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  138. EXPECT_NE(iter, nullptr);
  139. // Iterate the dataset and get each row
  140. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  141. iter->GetNextRow(&row);
  142. std::vector<std::string> expected_tokens = {"this", "is", "a", "funky", "string"};
  143. std::vector<uint32_t> expected_offsets_start = {0, 5, 8, 10, 16};
  144. std::vector<uint32_t> expected_offsets_limit = {4, 7, 9, 15, 22};
  145. uint64_t i = 0;
  146. while (row.size() != 0) {
  147. auto ind = row["token"];
  148. std::shared_ptr<Tensor> expected_token_tensor;
  149. Tensor::CreateFromVector(expected_tokens, &expected_token_tensor);
  150. EXPECT_EQ(*ind, *expected_token_tensor);
  151. auto start = row["offsets_start"];
  152. std::shared_ptr<Tensor> expected_start_tensor;
  153. Tensor::CreateFromVector(expected_offsets_start, &expected_start_tensor);
  154. EXPECT_EQ(*start, *expected_start_tensor);
  155. auto limit = row["offsets_limit"];
  156. std::shared_ptr<Tensor> expected_limit_tensor;
  157. Tensor::CreateFromVector(expected_offsets_limit, &expected_limit_tensor);
  158. EXPECT_EQ(*limit, *expected_limit_tensor);
  159. iter->GetNextRow(&row);
  160. i++;
  161. }
  162. EXPECT_EQ(i, 1);
  163. // Manually terminate the pipeline
  164. iter->Stop();
  165. }
  166. std::vector<std::string> list = {
  167. "床", "前", "明", "月", "光", "疑", "是", "地", "上", "霜", "举", "头",
  168. "望", "低", "思", "故", "乡", "繁", "體", "字", "嘿", "哈", "大", "笑",
  169. "嘻", "i", "am", "mak", "make", "small", "mistake", "##s", "during", "work", "##ing", "hour",
  170. "😀", "😃", "😄", "😁", "+", "/", "-", "=", "12", "28", "40", "16",
  171. " ", "I", "[CLS]", "[SEP]", "[UNK]", "[PAD]", "[MASK]", "[unused1]", "[unused10]"};
  172. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess1) {
  173. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess1.";
  174. // Test BertTokenizer with default parameters
  175. // Create a TextFile dataset
  176. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  177. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  178. EXPECT_NE(ds, nullptr);
  179. // Create Take operation on ds
  180. ds = ds->Take(4);
  181. EXPECT_NE(ds, nullptr);
  182. // Create a vocab from vector
  183. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  184. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  185. EXPECT_EQ(s, Status::OK());
  186. // Create BertTokenizer operation on ds
  187. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(vocab);
  188. EXPECT_NE(bert_tokenizer, nullptr);
  189. // Create Map operation on ds
  190. ds = ds->Map({bert_tokenizer}, {"text"});
  191. EXPECT_NE(ds, nullptr);
  192. // Create an iterator over the result of the above dataset
  193. // This will trigger the creation of the Execution Tree and launch it.
  194. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  195. EXPECT_NE(iter, nullptr);
  196. // Iterate the dataset and get each row
  197. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  198. iter->GetNextRow(&row);
  199. std::vector<std::vector<std::string>> expected = {{"床", "前", "明", "月", "光"},
  200. {"疑", "是", "地", "上", "霜"},
  201. {"举", "头", "望", "明", "月"},
  202. {"低", "头", "思", "故", "乡"}};
  203. uint64_t i = 0;
  204. while (row.size() != 0) {
  205. auto ind = row["text"];
  206. std::shared_ptr<Tensor> expected_tensor;
  207. Tensor::CreateFromVector(expected[i], &expected_tensor);
  208. EXPECT_EQ(*ind, *expected_tensor);
  209. iter->GetNextRow(&row);
  210. i++;
  211. }
  212. EXPECT_EQ(i, 4);
  213. // Manually terminate the pipeline
  214. iter->Stop();
  215. }
  216. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess2) {
  217. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess2.";
  218. // Test BertTokenizer with lower_case true
  219. // Create a TextFile dataset
  220. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  221. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  222. EXPECT_NE(ds, nullptr);
  223. // Create Skip operation on ds
  224. ds = ds->Skip(4);
  225. EXPECT_NE(ds, nullptr);
  226. // Create Take operation on ds
  227. ds = ds->Take(1);
  228. EXPECT_NE(ds, nullptr);
  229. // Create a vocab from vector
  230. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  231. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  232. EXPECT_EQ(s, Status::OK());
  233. // Create BertTokenizer operation on ds
  234. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(vocab, "##", 100, "[UNK]", true);
  235. EXPECT_NE(bert_tokenizer, nullptr);
  236. // Create Map operation on ds
  237. ds = ds->Map({bert_tokenizer}, {"text"});
  238. EXPECT_NE(ds, nullptr);
  239. // Create an iterator over the result of the above dataset
  240. // This will trigger the creation of the Execution Tree and launch it.
  241. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  242. EXPECT_NE(iter, nullptr);
  243. // Iterate the dataset and get each row
  244. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  245. iter->GetNextRow(&row);
  246. std::vector<std::string> expected = {"i", "am", "mak", "##ing", "small", "mistake",
  247. "##s", "during", "work", "##ing", "hour", "##s"};
  248. uint64_t i = 0;
  249. while (row.size() != 0) {
  250. auto ind = row["text"];
  251. std::shared_ptr<Tensor> expected_tensor;
  252. Tensor::CreateFromVector(expected, &expected_tensor);
  253. EXPECT_EQ(*ind, *expected_tensor);
  254. iter->GetNextRow(&row);
  255. i++;
  256. }
  257. EXPECT_EQ(i, 1);
  258. // Manually terminate the pipeline
  259. iter->Stop();
  260. }
  261. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess3) {
  262. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess3.";
  263. // Test BertTokenizer with normalization_form NFKC
  264. // Create a TextFile dataset
  265. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  266. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  267. EXPECT_NE(ds, nullptr);
  268. // Create Skip operation on ds
  269. ds = ds->Skip(5);
  270. EXPECT_NE(ds, nullptr);
  271. // Create Take operation on ds
  272. ds = ds->Take(2);
  273. EXPECT_NE(ds, nullptr);
  274. // Create a vocab from vector
  275. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  276. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  277. EXPECT_EQ(s, Status::OK());
  278. // Create BertTokenizer operation on ds
  279. std::shared_ptr<TensorOperation> bert_tokenizer =
  280. text::BertTokenizer(vocab, "##", 100, "[UNK]", false, false, NormalizeForm::kNfc);
  281. EXPECT_NE(bert_tokenizer, nullptr);
  282. // Create Map operation on ds
  283. ds = ds->Map({bert_tokenizer}, {"text"});
  284. EXPECT_NE(ds, nullptr);
  285. // Create an iterator over the result of the above dataset
  286. // This will trigger the creation of the Execution Tree and launch it.
  287. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  288. EXPECT_NE(iter, nullptr);
  289. // Iterate the dataset and get each row
  290. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  291. iter->GetNextRow(&row);
  292. std::vector<std::vector<std::string>> expected = {
  293. {"😀", "嘿", "嘿", "😃", "哈", "哈", "😄", "大", "笑", "😁", "嘻", "嘻"}, {"繁", "體", "字"}};
  294. uint64_t i = 0;
  295. while (row.size() != 0) {
  296. auto ind = row["text"];
  297. std::shared_ptr<Tensor> expected_tensor;
  298. Tensor::CreateFromVector(expected[i], &expected_tensor);
  299. EXPECT_EQ(*ind, *expected_tensor);
  300. iter->GetNextRow(&row);
  301. i++;
  302. }
  303. EXPECT_EQ(i, 2);
  304. // Manually terminate the pipeline
  305. iter->Stop();
  306. }
  307. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess4) {
  308. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess4.";
  309. // Test BertTokenizer with keep_whitespace true
  310. // Create a TextFile dataset
  311. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  312. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  313. EXPECT_NE(ds, nullptr);
  314. // Create Skip operation on ds
  315. ds = ds->Skip(7);
  316. EXPECT_NE(ds, nullptr);
  317. // Create Take operation on ds
  318. ds = ds->Take(1);
  319. EXPECT_NE(ds, nullptr);
  320. // Create a vocab from vector
  321. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  322. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  323. EXPECT_EQ(s, Status::OK());
  324. // Create BertTokenizer operation on ds
  325. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(vocab, "##", 100, "[UNK]", false, true);
  326. EXPECT_NE(bert_tokenizer, nullptr);
  327. // Create Map operation on ds
  328. ds = ds->Map({bert_tokenizer}, {"text"});
  329. EXPECT_NE(ds, nullptr);
  330. // Create an iterator over the result of the above dataset
  331. // This will trigger the creation of the Execution Tree and launch it.
  332. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  333. EXPECT_NE(iter, nullptr);
  334. // Iterate the dataset and get each row
  335. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  336. iter->GetNextRow(&row);
  337. std::vector<std::string> expected = {"[UNK]", " ", "[CLS]"};
  338. uint64_t i = 0;
  339. while (row.size() != 0) {
  340. auto ind = row["text"];
  341. std::shared_ptr<Tensor> expected_tensor;
  342. Tensor::CreateFromVector(expected, &expected_tensor);
  343. EXPECT_EQ(*ind, *expected_tensor);
  344. iter->GetNextRow(&row);
  345. i++;
  346. }
  347. EXPECT_EQ(i, 1);
  348. // Manually terminate the pipeline
  349. iter->Stop();
  350. }
  351. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess5) {
  352. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess5.";
  353. // Test BertTokenizer with unknown_token empty and keep_whitespace true
  354. // Create a TextFile dataset
  355. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  356. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  357. EXPECT_NE(ds, nullptr);
  358. // Create Skip operation on ds
  359. ds = ds->Skip(7);
  360. EXPECT_NE(ds, nullptr);
  361. // Create Take operation on ds
  362. ds = ds->Take(1);
  363. EXPECT_NE(ds, nullptr);
  364. // Create a vocab from vector
  365. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  366. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  367. EXPECT_EQ(s, Status::OK());
  368. // Create BertTokenizer operation on ds
  369. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(vocab, "##", 100, "", false, true);
  370. EXPECT_NE(bert_tokenizer, nullptr);
  371. // Create Map operation on ds
  372. ds = ds->Map({bert_tokenizer}, {"text"});
  373. EXPECT_NE(ds, nullptr);
  374. // Create an iterator over the result of the above dataset
  375. // This will trigger the creation of the Execution Tree and launch it.
  376. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  377. EXPECT_NE(iter, nullptr);
  378. // Iterate the dataset and get each row
  379. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  380. iter->GetNextRow(&row);
  381. std::vector<std::string> expected = {"unused", " ", "[CLS]"};
  382. uint64_t i = 0;
  383. while (row.size() != 0) {
  384. auto ind = row["text"];
  385. std::shared_ptr<Tensor> expected_tensor;
  386. Tensor::CreateFromVector(expected, &expected_tensor);
  387. EXPECT_EQ(*ind, *expected_tensor);
  388. iter->GetNextRow(&row);
  389. i++;
  390. }
  391. EXPECT_EQ(i, 1);
  392. // Manually terminate the pipeline
  393. iter->Stop();
  394. }
  395. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess6) {
  396. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess6.";
  397. // Test BertTokenizer with preserve_unused_token false, unknown_token empty and keep_whitespace true
  398. // Create a TextFile dataset
  399. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  400. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  401. EXPECT_NE(ds, nullptr);
  402. // Create Skip operation on ds
  403. ds = ds->Skip(7);
  404. EXPECT_NE(ds, nullptr);
  405. // Create Take operation on ds
  406. ds = ds->Take(1);
  407. EXPECT_NE(ds, nullptr);
  408. // Create a vocab from vector
  409. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  410. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  411. EXPECT_EQ(s, Status::OK());
  412. // Create BertTokenizer operation on ds
  413. std::shared_ptr<TensorOperation> bert_tokenizer =
  414. text::BertTokenizer(vocab, "##", 100, "", false, true, NormalizeForm::kNone, false);
  415. EXPECT_NE(bert_tokenizer, nullptr);
  416. // Create Map operation on ds
  417. ds = ds->Map({bert_tokenizer}, {"text"});
  418. EXPECT_NE(ds, nullptr);
  419. // Create an iterator over the result of the above dataset
  420. // This will trigger the creation of the Execution Tree and launch it.
  421. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  422. EXPECT_NE(iter, nullptr);
  423. // Iterate the dataset and get each row
  424. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  425. iter->GetNextRow(&row);
  426. std::vector<std::string> expected = {"unused", " ", "[", "CLS", "]"};
  427. uint64_t i = 0;
  428. while (row.size() != 0) {
  429. auto ind = row["text"];
  430. std::shared_ptr<Tensor> expected_tensor;
  431. Tensor::CreateFromVector(expected, &expected_tensor);
  432. EXPECT_EQ(*ind, *expected_tensor);
  433. iter->GetNextRow(&row);
  434. i++;
  435. }
  436. EXPECT_EQ(i, 1);
  437. // Manually terminate the pipeline
  438. iter->Stop();
  439. }
  440. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess7) {
  441. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess7.";
  442. // Test BertTokenizer with with_offsets true and lower_case true
  443. // Create a TextFile dataset
  444. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  445. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  446. EXPECT_NE(ds, nullptr);
  447. // Create Skip operation on ds
  448. ds = ds->Skip(4);
  449. EXPECT_NE(ds, nullptr);
  450. // Create Take operation on ds
  451. ds = ds->Take(1);
  452. EXPECT_NE(ds, nullptr);
  453. // Create a vocab from vector
  454. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  455. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  456. EXPECT_EQ(s, Status::OK());
  457. // Create BertTokenizer operation on ds
  458. std::shared_ptr<TensorOperation> bert_tokenizer =
  459. text::BertTokenizer(vocab, "##", 100, "[UNK]", true, false, NormalizeForm::kNone, true, true);
  460. EXPECT_NE(bert_tokenizer, nullptr);
  461. // Create Map operation on ds
  462. ds = ds->Map({bert_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"});
  463. EXPECT_NE(ds, nullptr);
  464. // Create an iterator over the result of the above dataset
  465. // This will trigger the creation of the Execution Tree and launch it.
  466. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  467. EXPECT_NE(iter, nullptr);
  468. // Iterate the dataset and get each row
  469. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  470. iter->GetNextRow(&row);
  471. std::vector<std::string> expected_tokens = {"i", "am", "mak", "##ing", "small", "mistake",
  472. "##s", "during", "work", "##ing", "hour", "##s"};
  473. std::vector<uint32_t> expected_offsets_start = {0, 2, 5, 8, 12, 18, 25, 27, 34, 38, 42, 46};
  474. std::vector<uint32_t> expected_offsets_limit = {1, 4, 8, 11, 17, 25, 26, 33, 38, 41, 46, 47};
  475. uint64_t i = 0;
  476. while (row.size() != 0) {
  477. auto ind = row["token"];
  478. std::shared_ptr<Tensor> expected_token_tensor;
  479. Tensor::CreateFromVector(expected_tokens, &expected_token_tensor);
  480. EXPECT_EQ(*ind, *expected_token_tensor);
  481. auto start = row["offsets_start"];
  482. std::shared_ptr<Tensor> expected_start_tensor;
  483. Tensor::CreateFromVector(expected_offsets_start, &expected_start_tensor);
  484. EXPECT_EQ(*start, *expected_start_tensor);
  485. auto limit = row["offsets_limit"];
  486. std::shared_ptr<Tensor> expected_limit_tensor;
  487. Tensor::CreateFromVector(expected_offsets_limit, &expected_limit_tensor);
  488. EXPECT_EQ(*limit, *expected_limit_tensor);
  489. iter->GetNextRow(&row);
  490. i++;
  491. }
  492. EXPECT_EQ(i, 1);
  493. // Manually terminate the pipeline
  494. iter->Stop();
  495. }
  496. TEST_F(MindDataTestPipeline, TestBertTokenizerFail1) {
  497. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerFail1.";
  498. // Test BertTokenizer with nullptr vocab
  499. // Create a TextFile dataset
  500. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  501. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  502. EXPECT_NE(ds, nullptr);
  503. // Create BertTokenizer operation on ds
  504. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(nullptr);
  505. // Expect failure: invalid BertTokenizer input with nullptr vocab
  506. EXPECT_EQ(bert_tokenizer, nullptr);
  507. }
  508. TEST_F(MindDataTestPipeline, TestBertTokenizerFail2) {
  509. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerFail2.";
  510. // Test BertTokenizer with negative max_bytes_per_token
  511. // Create a TextFile dataset
  512. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  513. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  514. EXPECT_NE(ds, nullptr);
  515. // Create a vocab from vector
  516. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  517. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  518. EXPECT_EQ(s, Status::OK());
  519. // Create BertTokenizer operation on ds
  520. std::shared_ptr<TensorOperation> bert_tokenizer = text::BertTokenizer(vocab, "##", -1);
  521. // Expect failure: invalid BertTokenizer input with nullptr vocab
  522. EXPECT_EQ(bert_tokenizer, nullptr);
  523. }
  524. TEST_F(MindDataTestPipeline, TestCaseFoldSuccess) {
  525. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCaseFoldSuccess.";
  526. // Create a TextFile dataset
  527. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  528. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  529. EXPECT_NE(ds, nullptr);
  530. // Create casefold operation on ds
  531. std::shared_ptr<TensorOperation> casefold = text::CaseFold();
  532. EXPECT_NE(casefold, nullptr);
  533. // Create Map operation on ds
  534. ds = ds->Map({casefold}, {"text"});
  535. EXPECT_NE(ds, nullptr);
  536. // Create an iterator over the result of the above dataset
  537. // This will trigger the creation of the Execution Tree and launch it.
  538. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  539. EXPECT_NE(iter, nullptr);
  540. // Iterate the dataset and get each row
  541. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  542. iter->GetNextRow(&row);
  543. std::vector<std::string> expected = {"welcome to beijing!", "北京欢迎您!", "我喜欢english!", " "};
  544. uint64_t i = 0;
  545. while (row.size() != 0) {
  546. auto ind = row["text"];
  547. std::shared_ptr<Tensor> expected_tensor;
  548. Tensor::CreateScalar(expected[i], &expected_tensor);
  549. EXPECT_EQ(*ind, *expected_tensor);
  550. iter->GetNextRow(&row);
  551. i++;
  552. }
  553. EXPECT_EQ(i, 4);
  554. // Manually terminate the pipeline
  555. iter->Stop();
  556. }
  557. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess) {
  558. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kMp and the with_offsets is false.
  559. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess.";
  560. // Create a TextFile dataset
  561. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  562. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  563. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  564. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  565. EXPECT_NE(ds, nullptr);
  566. // Create jieba_tokenizer operation on ds
  567. std::shared_ptr<TensorOperation> jieba_tokenizer = text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  568. EXPECT_NE(jieba_tokenizer, nullptr);
  569. // Create Map operation on ds
  570. ds = ds->Map({jieba_tokenizer}, {"text"});
  571. EXPECT_NE(ds, nullptr);
  572. // Create an iterator over the result of the above dataset
  573. // This will trigger the creation of the Execution Tree and launch it.
  574. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  575. EXPECT_NE(iter, nullptr);
  576. // Iterate the dataset and get each row
  577. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  578. iter->GetNextRow(&row);
  579. std::vector<std::string> expected = {"今天天气", "太好了", "我们", "一起", "去", "外面", "玩吧"};
  580. uint64_t i = 0;
  581. while (row.size() != 0) {
  582. auto ind = row["text"];
  583. std::shared_ptr<Tensor> expected_tensor;
  584. Tensor::CreateFromVector(expected, &expected_tensor);
  585. EXPECT_EQ(*ind, *expected_tensor);
  586. iter->GetNextRow(&row);
  587. i++;
  588. }
  589. EXPECT_EQ(i, 1);
  590. // Manually terminate the pipeline
  591. iter->Stop();
  592. }
  593. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess1) {
  594. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kHmm and the with_offsets is false.
  595. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess1.";
  596. // Create a TextFile dataset
  597. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  598. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  599. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  600. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  601. EXPECT_NE(ds, nullptr);
  602. // Create jieba_tokenizer operation on ds
  603. std::shared_ptr<TensorOperation> jieba_tokenizer = text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kHmm);
  604. EXPECT_NE(jieba_tokenizer, nullptr);
  605. // Create Map operation on ds
  606. ds = ds->Map({jieba_tokenizer}, {"text"});
  607. EXPECT_NE(ds, nullptr);
  608. // Create an iterator over the result of the above dataset
  609. // This will trigger the creation of the Execution Tree and launch it.
  610. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  611. EXPECT_NE(iter, nullptr);
  612. // Iterate the dataset and get each row
  613. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  614. iter->GetNextRow(&row);
  615. std::vector<std::string> expected = {"今天", "天气", "太", "好", "了", "我们", "一起", "去", "外面", "玩", "吧"};
  616. uint64_t i = 0;
  617. while (row.size() != 0) {
  618. auto ind = row["text"];
  619. std::shared_ptr<Tensor> expected_tensor;
  620. Tensor::CreateFromVector(expected, &expected_tensor);
  621. EXPECT_EQ(*ind, *expected_tensor);
  622. iter->GetNextRow(&row);
  623. i++;
  624. }
  625. EXPECT_EQ(i, 1);
  626. // Manually terminate the pipeline
  627. iter->Stop();
  628. }
  629. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess2) {
  630. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kMp and the with_offsets is true.
  631. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess2.";
  632. // Create a TextFile dataset
  633. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  634. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  635. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  636. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  637. EXPECT_NE(ds, nullptr);
  638. // Create jieba_tokenizer operation on ds
  639. std::shared_ptr<TensorOperation> jieba_tokenizer = text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp, true);
  640. EXPECT_NE(jieba_tokenizer, nullptr);
  641. // Create Map operation on ds
  642. ds = ds->Map({jieba_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  643. {"token", "offsets_start", "offsets_limit"});
  644. EXPECT_NE(ds, nullptr);
  645. // Create an iterator over the result of the above dataset
  646. // This will trigger the creation of the Execution Tree and launch it.
  647. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  648. EXPECT_NE(iter, nullptr);
  649. // Iterate the dataset and get each row
  650. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  651. iter->GetNextRow(&row);
  652. std::vector<std::string> expected = {"今天天气", "太好了", "我们", "一起", "去", "外面", "玩吧"};
  653. std::vector<uint32_t> expected_offsets_start = {0, 12, 21, 27, 33, 36, 42};
  654. std::vector<uint32_t> expected_offsets_limit = {12, 21, 27, 33, 36, 42, 48};
  655. uint64_t i = 0;
  656. while (row.size() != 0) {
  657. auto ind = row["offsets_start"];
  658. auto ind1 = row["offsets_limit"];
  659. auto token = row["token"];
  660. std::shared_ptr<Tensor> expected_tensor;
  661. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  662. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  663. Tensor::CreateFromVector(expected, &expected_tensor);
  664. Tensor::CreateFromVector(expected_offsets_start, &expected_tensor_offsets_start);
  665. Tensor::CreateFromVector(expected_offsets_limit, &expected_tensor_offsets_limit);
  666. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  667. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  668. EXPECT_EQ(*token, *expected_tensor);
  669. iter->GetNextRow(&row);
  670. i++;
  671. }
  672. EXPECT_EQ(i, 1);
  673. // Manually terminate the pipeline
  674. iter->Stop();
  675. }
  676. TEST_F(MindDataTestPipeline, TestJiebaTokenizerFail) {
  677. // Testing the incorrect parameter of JiebaTokenizer interface.
  678. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerFail.";
  679. // Create a TextFile dataset
  680. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  681. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  682. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  683. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  684. EXPECT_NE(ds, nullptr);
  685. // Create jieba_tokenizer operation on ds
  686. // Testing the parameter hmm_path is empty
  687. std::shared_ptr<TensorOperation> jieba_tokenizer = text::JiebaTokenizer("", mp_path, JiebaMode::kMp);
  688. EXPECT_EQ(jieba_tokenizer, nullptr);
  689. // Testing the parameter mp_path is empty
  690. std::shared_ptr<TensorOperation> jieba_tokenizer1 = text::JiebaTokenizer(hmm_path, "", JiebaMode::kMp);
  691. EXPECT_EQ(jieba_tokenizer1, nullptr);
  692. // Testing the parameter hmm_path is invalid path
  693. std::string hmm_path_invalid = datasets_root_path_ + "/jiebadict/1.txt";
  694. std::shared_ptr<TensorOperation> jieba_tokenizer2 = text::JiebaTokenizer(hmm_path_invalid, mp_path, JiebaMode::kMp);
  695. EXPECT_EQ(jieba_tokenizer2, nullptr);
  696. // Testing the parameter mp_path is invalid path
  697. std::string mp_path_invalid = datasets_root_path_ + "/jiebadict/1.txt";
  698. std::shared_ptr<TensorOperation> jieba_tokenizer3 = text::JiebaTokenizer(hmm_path, mp_path_invalid, JiebaMode::kMp);
  699. EXPECT_EQ(jieba_tokenizer3, nullptr);
  700. }
  701. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess) {
  702. // Testing the parameter of SlidingWindow interface when the axis is 0.
  703. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess.";
  704. // Create a TextFile dataset
  705. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  706. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  707. EXPECT_NE(ds, nullptr);
  708. // Create white_tokenizer operation on ds
  709. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  710. EXPECT_NE(white_tokenizer, nullptr);
  711. // Create sliding_window operation on ds
  712. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(3, 0);
  713. EXPECT_NE(sliding_window, nullptr);
  714. // Create Map operation on ds
  715. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  716. EXPECT_NE(ds, nullptr);
  717. // Create an iterator over the result of the above dataset
  718. // This will trigger the creation of the Execution Tree and launch it.
  719. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  720. EXPECT_NE(iter, nullptr);
  721. // Iterate the dataset and get each row
  722. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  723. iter->GetNextRow(&row);
  724. std::vector<std::vector<std::string>> expected = {{"This", "is", "a", "is", "a", "text", "a", "text", "file."},
  725. {"Be", "happy", "every", "happy", "every", "day."},
  726. {"Good", "luck", "to", "luck", "to", "everyone."}};
  727. uint64_t i = 0;
  728. while (row.size() != 0) {
  729. auto ind = row["text"];
  730. std::shared_ptr<Tensor> expected_tensor;
  731. int x = expected[i].size() / 3;
  732. Tensor::CreateFromVector(expected[i], TensorShape({x, 3}), &expected_tensor);
  733. EXPECT_EQ(*ind, *expected_tensor);
  734. iter->GetNextRow(&row);
  735. i++;
  736. }
  737. EXPECT_EQ(i, 3);
  738. // Manually terminate the pipeline
  739. iter->Stop();
  740. }
  741. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess1) {
  742. // Testing the parameter of SlidingWindow interface when the axis is -1.
  743. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess1.";
  744. // Create a TextFile dataset
  745. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  746. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  747. EXPECT_NE(ds, nullptr);
  748. // Create white_tokenizer operation on ds
  749. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  750. EXPECT_NE(white_tokenizer, nullptr);
  751. // Create sliding_window operation on ds
  752. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(2, -1);
  753. EXPECT_NE(sliding_window, nullptr);
  754. // Create Map operation on ds
  755. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  756. EXPECT_NE(ds, nullptr);
  757. // Create an iterator over the result of the above dataset
  758. // This will trigger the creation of the Execution Tree and launch it.
  759. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  760. EXPECT_NE(iter, nullptr);
  761. // Iterate the dataset and get each row
  762. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  763. iter->GetNextRow(&row);
  764. std::vector<std::vector<std::string>> expected = {{"This", "is", "is", "a", "a", "text", "text", "file."},
  765. {"Be", "happy", "happy", "every", "every", "day."},
  766. {"Good", "luck", "luck", "to", "to", "everyone."}};
  767. uint64_t i = 0;
  768. while (row.size() != 0) {
  769. auto ind = row["text"];
  770. std::shared_ptr<Tensor> expected_tensor;
  771. int x = expected[i].size() / 2;
  772. Tensor::CreateFromVector(expected[i], TensorShape({x, 2}), &expected_tensor);
  773. EXPECT_EQ(*ind, *expected_tensor);
  774. iter->GetNextRow(&row);
  775. i++;
  776. }
  777. EXPECT_EQ(i, 3);
  778. // Manually terminate the pipeline
  779. iter->Stop();
  780. }
  781. TEST_F(MindDataTestPipeline, TestSlidingWindowFail) {
  782. // Testing the incorrect parameter of SlidingWindow interface.
  783. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowFail.";
  784. // Create a TextFile dataset
  785. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  786. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  787. EXPECT_NE(ds, nullptr);
  788. // Create sliding_window operation on ds
  789. // Testing the parameter width less than or equal to 0
  790. // The parameter axis support 0 or -1 only for now
  791. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(0, 0);
  792. EXPECT_EQ(sliding_window, nullptr);
  793. // Testing the parameter width less than or equal to 0
  794. // The parameter axis support 0 or -1 only for now
  795. std::shared_ptr<TensorOperation> sliding_window1 = text::SlidingWindow(-2, 0);
  796. EXPECT_EQ(sliding_window1, nullptr);
  797. }
  798. TEST_F(MindDataTestPipeline, TestToNumberSuccess1) {
  799. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess1.";
  800. // Test ToNumber with integer numbers
  801. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  802. // Create a TextFile dataset
  803. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  804. EXPECT_NE(ds, nullptr);
  805. // Create a Take operation on ds
  806. ds = ds->Take(8);
  807. EXPECT_NE(ds, nullptr);
  808. // Create ToNumber operation on ds
  809. std::shared_ptr<TensorOperation> to_number = text::ToNumber(DataType("int64"));
  810. EXPECT_NE(to_number, nullptr);
  811. // Create a Map operation on ds
  812. ds = ds->Map({to_number}, {"text"});
  813. EXPECT_NE(ds, nullptr);
  814. // Create an iterator over the result of the above dataset
  815. // This will trigger the creation of the Execution Tree and launch it.
  816. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  817. EXPECT_NE(iter, nullptr);
  818. // Iterate the dataset and get each row
  819. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  820. iter->GetNextRow(&row);
  821. std::vector<int64_t> expected = {-121, 14, -2219, 7623, -8162536, 162371864, -1726483716, 98921728421};
  822. uint64_t i = 0;
  823. while (row.size() != 0) {
  824. auto ind = row["text"];
  825. std::shared_ptr<Tensor> expected_tensor;
  826. Tensor::CreateScalar(expected[i], &expected_tensor);
  827. EXPECT_EQ(*ind, *expected_tensor);
  828. iter->GetNextRow(&row);
  829. i++;
  830. }
  831. EXPECT_EQ(i, 8);
  832. // Manually terminate the pipeline
  833. iter->Stop();
  834. }
  835. TEST_F(MindDataTestPipeline, TestToNumberSuccess2) {
  836. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess2.";
  837. // Test ToNumber with float numbers
  838. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  839. // Create a TextFile dataset
  840. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  841. EXPECT_NE(ds, nullptr);
  842. // Create a Skip operation on ds
  843. ds = ds->Skip(8);
  844. EXPECT_NE(ds, nullptr);
  845. // Create a Take operation on ds
  846. ds = ds->Take(6);
  847. EXPECT_NE(ds, nullptr);
  848. // Create ToNumber operation on ds
  849. std::shared_ptr<TensorOperation> to_number = text::ToNumber(DataType("float64"));
  850. EXPECT_NE(to_number, nullptr);
  851. // Create a Map operation on ds
  852. ds = ds->Map({to_number}, {"text"});
  853. EXPECT_NE(ds, nullptr);
  854. // Create an iterator over the result of the above dataset
  855. // This will trigger the creation of the Execution Tree and launch it.
  856. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  857. EXPECT_NE(iter, nullptr);
  858. // Iterate the dataset and get each row
  859. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  860. iter->GetNextRow(&row);
  861. std::vector<double_t> expected = {-1.1, 1.4, -2219.321, 7623.453, -816256.234282, 162371864.243243};
  862. uint64_t i = 0;
  863. while (row.size() != 0) {
  864. auto ind = row["text"];
  865. std::shared_ptr<Tensor> expected_tensor;
  866. Tensor::CreateScalar(expected[i], &expected_tensor);
  867. EXPECT_EQ(*ind, *expected_tensor);
  868. iter->GetNextRow(&row);
  869. i++;
  870. }
  871. EXPECT_EQ(i, 6);
  872. // Manually terminate the pipeline
  873. iter->Stop();
  874. }
  875. TEST_F(MindDataTestPipeline, TestToNumberFail1) {
  876. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail1.";
  877. // Test ToNumber with overflow integer numbers
  878. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  879. // Create a TextFile dataset
  880. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  881. EXPECT_NE(ds, nullptr);
  882. // Create a Skip operation on ds
  883. ds = ds->Skip(2);
  884. EXPECT_NE(ds, nullptr);
  885. // Create a Take operation on ds
  886. ds = ds->Take(6);
  887. EXPECT_NE(ds, nullptr);
  888. // Create ToNumber operation on ds
  889. std::shared_ptr<TensorOperation> to_number = text::ToNumber(DataType("int8"));
  890. EXPECT_NE(to_number, nullptr);
  891. // Create a Map operation on ds
  892. ds = ds->Map({to_number}, {"text"});
  893. EXPECT_NE(ds, nullptr);
  894. // Create an iterator over the result of the above dataset
  895. // This will trigger the creation of the Execution Tree and launch it.
  896. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  897. EXPECT_NE(iter, nullptr);
  898. // Iterate the dataset and get each row
  899. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  900. // Expect error: input out of bounds of int8
  901. iter->GetNextRow(&row);
  902. uint64_t i = 0;
  903. while (row.size() != 0) {
  904. iter->GetNextRow(&row);
  905. i++;
  906. }
  907. // Expect failure: GetNextRow fail and return nothing
  908. EXPECT_EQ(i, 0);
  909. // Manually terminate the pipeline
  910. iter->Stop();
  911. }
  912. TEST_F(MindDataTestPipeline, TestToNumberFail2) {
  913. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail2.";
  914. // Test ToNumber with overflow float numbers
  915. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  916. // Create a TextFile dataset
  917. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  918. EXPECT_NE(ds, nullptr);
  919. // Create a Skip operation on ds
  920. ds = ds->Skip(12);
  921. EXPECT_NE(ds, nullptr);
  922. // Create a Take operation on ds
  923. ds = ds->Take(2);
  924. EXPECT_NE(ds, nullptr);
  925. // Create ToNumber operation on ds
  926. std::shared_ptr<TensorOperation> to_number = text::ToNumber(DataType("float16"));
  927. EXPECT_NE(to_number, nullptr);
  928. // Create a Map operation on ds
  929. ds = ds->Map({to_number}, {"text"});
  930. EXPECT_NE(ds, nullptr);
  931. // Create an iterator over the result of the above dataset
  932. // This will trigger the creation of the Execution Tree and launch it.
  933. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  934. EXPECT_NE(iter, nullptr);
  935. // Iterate the dataset and get each row
  936. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  937. // Expect error: input out of bounds of float16
  938. iter->GetNextRow(&row);
  939. uint64_t i = 0;
  940. while (row.size() != 0) {
  941. iter->GetNextRow(&row);
  942. i++;
  943. }
  944. // Expect failure: GetNextRow fail and return nothing
  945. EXPECT_EQ(i, 0);
  946. // Manually terminate the pipeline
  947. iter->Stop();
  948. }
  949. TEST_F(MindDataTestPipeline, TestToNumberFail3) {
  950. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail3.";
  951. // Test ToNumber with non numerical input
  952. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  953. // Create a TextFile dataset
  954. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  955. EXPECT_NE(ds, nullptr);
  956. // Create a Skip operation on ds
  957. ds = ds->Skip(14);
  958. EXPECT_NE(ds, nullptr);
  959. // Create ToNumber operation on ds
  960. std::shared_ptr<TensorOperation> to_number = text::ToNumber(DataType("int64"));
  961. EXPECT_NE(to_number, nullptr);
  962. // Create a Map operation on ds
  963. ds = ds->Map({to_number}, {"text"});
  964. EXPECT_NE(ds, nullptr);
  965. // Create an iterator over the result of the above dataset
  966. // This will trigger the creation of the Execution Tree and launch it.
  967. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  968. EXPECT_NE(iter, nullptr);
  969. // Iterate the dataset and get each row
  970. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  971. // Expect error: invalid input which is non numerical
  972. iter->GetNextRow(&row);
  973. uint64_t i = 0;
  974. while (row.size() != 0) {
  975. iter->GetNextRow(&row);
  976. i++;
  977. }
  978. // Expect failure: GetNextRow fail and return nothing
  979. EXPECT_EQ(i, 0);
  980. // Manually terminate the pipeline
  981. iter->Stop();
  982. }
  983. TEST_F(MindDataTestPipeline, TestToNumberFail4) {
  984. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail4.";
  985. // Test ToNumber with non numerical DataType
  986. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  987. // Create a TextFile dataset
  988. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  989. EXPECT_NE(ds, nullptr);
  990. // Create ToNumber operation on ds
  991. std::shared_ptr<TensorOperation> to_number1 = text::ToNumber(DataType("string"));
  992. // Expect failure: invalid parameter with non numerical DataType
  993. EXPECT_EQ(to_number1, nullptr);
  994. // Create ToNumber operation on ds
  995. std::shared_ptr<TensorOperation> to_number2 = text::ToNumber(DataType("bool"));
  996. // Expect failure: invalid parameter with non numerical DataType
  997. EXPECT_EQ(to_number2, nullptr);
  998. }
  999. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess1) {
  1000. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess1.";
  1001. // Testing basic TruncateSequencePair
  1002. // Set seed for RandomDataset
  1003. auto original_seed = config::get_seed();
  1004. bool status_set_seed = config::set_seed(0);
  1005. EXPECT_EQ(status_set_seed, true);
  1006. // Set num_parallel_workers for RandomDataset
  1007. auto original_worker = config::get_num_parallel_workers();
  1008. bool status_set_worker = config::set_num_parallel_workers(1);
  1009. EXPECT_EQ(status_set_worker, true);
  1010. // Create a RandomDataset which has column names "col1" and "col2"
  1011. std::shared_ptr<SchemaObj> schema = Schema();
  1012. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt16, {5});
  1013. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt32, {3});
  1014. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1015. EXPECT_NE(ds, nullptr);
  1016. // Create a truncate_sequence_pair operation on ds
  1017. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(4);
  1018. EXPECT_NE(truncate_sequence_pair, nullptr);
  1019. // Create Map operation on ds
  1020. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1021. EXPECT_NE(ds, nullptr);
  1022. // Create an iterator over the result of the above dataset
  1023. // This will trigger the creation of the Execution Tree and launch it.
  1024. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1025. EXPECT_NE(iter, nullptr);
  1026. // Iterate the dataset and get each row
  1027. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1028. iter->GetNextRow(&row);
  1029. std::vector<std::vector<int16_t>> expected1 = {{-29556, -29556}, {-18505, -18505}, {-25958, -25958}};
  1030. std::vector<std::vector<int32_t>> expected2 = {
  1031. {-1751672937, -1751672937}, {-656877352, -656877352}, {-606348325, -606348325}};
  1032. uint64_t i = 0;
  1033. while (row.size() != 0) {
  1034. auto ind1 = row["col1"];
  1035. auto ind2 = row["col2"];
  1036. std::shared_ptr<Tensor> expected_tensor1;
  1037. std::shared_ptr<Tensor> expected_tensor2;
  1038. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1039. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1040. EXPECT_EQ(*ind1, *expected_tensor1);
  1041. EXPECT_EQ(*ind2, *expected_tensor2);
  1042. iter->GetNextRow(&row);
  1043. i++;
  1044. }
  1045. EXPECT_EQ(i, 3);
  1046. // Manually terminate the pipeline
  1047. iter->Stop();
  1048. // Restore original seed and num_parallel_workers
  1049. status_set_seed = config::set_seed(original_seed);
  1050. EXPECT_EQ(status_set_seed, true);
  1051. status_set_worker = config::set_num_parallel_workers(original_worker);
  1052. EXPECT_EQ(status_set_worker, true);
  1053. }
  1054. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess2) {
  1055. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess2.";
  1056. // Testing basic TruncateSequencePair with odd max_length
  1057. // Set seed for RandomDataset
  1058. auto original_seed = config::get_seed();
  1059. bool status_set_seed = config::set_seed(1);
  1060. EXPECT_EQ(status_set_seed, true);
  1061. // Set num_parallel_workers for RandomDataset
  1062. auto original_worker = config::get_num_parallel_workers();
  1063. bool status_set_worker = config::set_num_parallel_workers(1);
  1064. EXPECT_EQ(status_set_worker, true);
  1065. // Create a RandomDataset which has column names "col1" and "col2"
  1066. std::shared_ptr<SchemaObj> schema = Schema();
  1067. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt32, {4});
  1068. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt64, {4});
  1069. std::shared_ptr<Dataset> ds = RandomData(4, schema);
  1070. EXPECT_NE(ds, nullptr);
  1071. // Create a truncate_sequence_pair operation on ds
  1072. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(5);
  1073. EXPECT_NE(truncate_sequence_pair, nullptr);
  1074. // Create Map operation on ds
  1075. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1076. EXPECT_NE(ds, nullptr);
  1077. // Create an iterator over the result of the above dataset
  1078. // This will trigger the creation of the Execution Tree and launch it.
  1079. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1080. EXPECT_NE(iter, nullptr);
  1081. // Iterate the dataset and get each row
  1082. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1083. iter->GetNextRow(&row);
  1084. std::vector<std::vector<int32_t>> expected1 = {{1785358954, 1785358954, 1785358954},
  1085. {-1195853640, -1195853640, -1195853640},
  1086. {0, 0, 0},
  1087. {1296911693, 1296911693, 1296911693}};
  1088. std::vector<std::vector<int64_t>> expected2 = {
  1089. {-1, -1}, {-1229782938247303442, -1229782938247303442}, {2314885530818453536, 2314885530818453536}, {-1, -1}};
  1090. uint64_t i = 0;
  1091. while (row.size() != 0) {
  1092. auto ind1 = row["col1"];
  1093. auto ind2 = row["col2"];
  1094. std::shared_ptr<Tensor> expected_tensor1;
  1095. std::shared_ptr<Tensor> expected_tensor2;
  1096. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1097. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1098. EXPECT_EQ(*ind1, *expected_tensor1);
  1099. EXPECT_EQ(*ind2, *expected_tensor2);
  1100. iter->GetNextRow(&row);
  1101. i++;
  1102. }
  1103. EXPECT_EQ(i, 4);
  1104. // Manually terminate the pipeline
  1105. iter->Stop();
  1106. // Restore original seed and num_parallel_workers
  1107. status_set_seed = config::set_seed(original_seed);
  1108. EXPECT_EQ(status_set_seed, true);
  1109. status_set_worker = config::set_num_parallel_workers(original_worker);
  1110. EXPECT_EQ(status_set_worker, true);
  1111. }
  1112. TEST_F(MindDataTestPipeline, TestTruncateSequencePairFail) {
  1113. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairFail.";
  1114. // Testing TruncateSequencePair with negative max_length
  1115. // Create a RandomDataset which has column names "col1" and "col2"
  1116. std::shared_ptr<SchemaObj> schema = Schema();
  1117. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt8, {3});
  1118. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt8, {3});
  1119. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1120. EXPECT_NE(ds, nullptr);
  1121. // Create a truncate_sequence_pair operation on ds
  1122. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(-1);
  1123. // Expect failure: invalid parameter with negative max_length
  1124. EXPECT_EQ(truncate_sequence_pair, nullptr);
  1125. }
  1126. TEST_F(MindDataTestPipeline, TestNgramSuccess) {
  1127. // Testing the parameter of Ngram interface.
  1128. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess.";
  1129. // Create a TextFile dataset
  1130. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1131. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1132. EXPECT_NE(ds, nullptr);
  1133. // Create white_tokenizer operation on ds
  1134. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1135. EXPECT_NE(white_tokenizer, nullptr);
  1136. // Create sliding_window operation on ds
  1137. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2}, {"_", 1}, {"_", 1}, " ");
  1138. EXPECT_NE(ngram_op, nullptr);
  1139. // Create Map operation on ds
  1140. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1141. EXPECT_NE(ds, nullptr);
  1142. // Create an iterator over the result of the above dataset
  1143. // This will trigger the creation of the Execution Tree and launch it.
  1144. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1145. EXPECT_NE(iter, nullptr);
  1146. // Iterate the dataset and get each row
  1147. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1148. iter->GetNextRow(&row);
  1149. std::vector<std::vector<std::string>> expected = {{"_ This", "This is", "is a", "a text", "text file.", "file. _"},
  1150. {"_ Be", "Be happy", "happy every", "every day.", "day. _"},
  1151. {"_ Good", "Good luck", "luck to", "to everyone.", "everyone. _"}};
  1152. uint64_t i = 0;
  1153. while (row.size() != 0) {
  1154. auto ind = row["text"];
  1155. std::shared_ptr<Tensor> expected_tensor;
  1156. int x = expected[i].size();
  1157. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1158. EXPECT_EQ(*ind, *expected_tensor);
  1159. iter->GetNextRow(&row);
  1160. i++;
  1161. }
  1162. EXPECT_EQ(i, 3);
  1163. // Manually terminate the pipeline
  1164. iter->Stop();
  1165. }
  1166. TEST_F(MindDataTestPipeline, TestNgramSuccess1) {
  1167. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess1.";
  1168. // Create a TextFile dataset
  1169. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1170. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1171. EXPECT_NE(ds, nullptr);
  1172. // Create white_tokenizer operation on ds
  1173. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1174. EXPECT_NE(white_tokenizer, nullptr);
  1175. // Create sliding_window operation on ds
  1176. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2, 3}, {"&", 2}, {"&", 2}, "-");
  1177. EXPECT_NE(ngram_op, nullptr);
  1178. // Create Map operation on ds
  1179. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1180. EXPECT_NE(ds, nullptr);
  1181. // Create an iterator over the result of the above dataset
  1182. // This will trigger the creation of the Execution Tree and launch it.
  1183. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1184. EXPECT_NE(iter, nullptr);
  1185. // Iterate the dataset and get each row
  1186. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1187. iter->GetNextRow(&row);
  1188. std::vector<std::vector<std::string>> expected = {
  1189. {"&-This", "This-is", "is-a", "a-text", "text-file.", "file.-&", "&-&-This", "&-This-is", "This-is-a", "is-a-text",
  1190. "a-text-file.", "text-file.-&", "file.-&-&"},
  1191. {"&-Be", "Be-happy", "happy-every", "every-day.", "day.-&", "&-&-Be", "&-Be-happy", "Be-happy-every",
  1192. "happy-every-day.", "every-day.-&", "day.-&-&"},
  1193. {"&-Good", "Good-luck", "luck-to", "to-everyone.", "everyone.-&", "&-&-Good", "&-Good-luck", "Good-luck-to",
  1194. "luck-to-everyone.", "to-everyone.-&", "everyone.-&-&"}};
  1195. uint64_t i = 0;
  1196. while (row.size() != 0) {
  1197. auto ind = row["text"];
  1198. std::shared_ptr<Tensor> expected_tensor;
  1199. int x = expected[i].size();
  1200. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1201. EXPECT_EQ(*ind, *expected_tensor);
  1202. iter->GetNextRow(&row);
  1203. i++;
  1204. }
  1205. EXPECT_EQ(i, 3);
  1206. // Manually terminate the pipeline
  1207. iter->Stop();
  1208. }
  1209. TEST_F(MindDataTestPipeline, TestNgramFail) {
  1210. // Testing the incorrect parameter of Ngram interface.
  1211. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail.";
  1212. // Create a TextFile dataset
  1213. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1214. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1215. EXPECT_NE(ds, nullptr);
  1216. // Create sliding_window operation on ds
  1217. // Testing the vector of ngram is empty
  1218. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({});
  1219. EXPECT_EQ(ngram_op, nullptr);
  1220. // Testing the value of ngrams vector less than and equal to 0
  1221. std::shared_ptr<TensorOperation> ngram_op1 = text::Ngram({0});
  1222. EXPECT_EQ(ngram_op1, nullptr);
  1223. // Testing the value of ngrams vector less than and equal to 0
  1224. std::shared_ptr<TensorOperation> ngram_op2 = text::Ngram({-2});
  1225. EXPECT_EQ(ngram_op2, nullptr);
  1226. // Testing the second parameter pad_width in left_pad vector less than 0
  1227. std::shared_ptr<TensorOperation> ngram_op3 = text::Ngram({2}, {"", -1});
  1228. EXPECT_EQ(ngram_op3, nullptr);
  1229. // Testing the second parameter pad_width in right_pad vector less than 0
  1230. std::shared_ptr<TensorOperation> ngram_op4 = text::Ngram({2}, {"", 1}, {"", -1});
  1231. EXPECT_EQ(ngram_op4, nullptr);
  1232. }
  1233. TEST_F(MindDataTestPipeline, TestTextOperationName) {
  1234. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTextOperationName.";
  1235. // Create object for the tensor op, and check the name
  1236. std::string data_file = datasets_root_path_ + "/testVocab/words.txt";
  1237. std::shared_ptr<TensorOperation> sentence_piece_tokenizer_op =
  1238. text::SentencePieceTokenizer(data_file, SPieceTokenizerOutType::kString);
  1239. std::string correct_name = "SentencepieceTokenizer";
  1240. EXPECT_EQ(correct_name, sentence_piece_tokenizer_op->Name());
  1241. }
  1242. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success) {
  1243. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkc.
  1244. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success.";
  1245. // Create a TextFile dataset
  1246. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1247. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1248. EXPECT_NE(ds, nullptr);
  1249. // Create normalizeutf8 operation on ds
  1250. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkc);
  1251. EXPECT_NE(normalizeutf8, nullptr);
  1252. // Create Map operation on ds
  1253. ds = ds->Map({normalizeutf8}, {"text"});
  1254. EXPECT_NE(ds, nullptr);
  1255. // Create an iterator over the result of the above dataset
  1256. // This will trigger the creation of the Execution Tree and launch it.
  1257. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1258. EXPECT_NE(iter, nullptr);
  1259. // Iterate the dataset and get each row
  1260. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1261. iter->GetNextRow(&row);
  1262. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1263. uint64_t i = 0;
  1264. while (row.size() != 0) {
  1265. auto ind = row["text"];
  1266. std::shared_ptr<Tensor> expected_tensor;
  1267. Tensor::CreateScalar(expected[i], &expected_tensor);
  1268. EXPECT_EQ(*ind, *expected_tensor);
  1269. iter->GetNextRow(&row);
  1270. i++;
  1271. }
  1272. EXPECT_EQ(i, 6);
  1273. // Manually terminate the pipeline
  1274. iter->Stop();
  1275. }
  1276. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success1) {
  1277. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfc.
  1278. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success1.";
  1279. // Create a TextFile dataset
  1280. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1281. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1282. EXPECT_NE(ds, nullptr);
  1283. // Create normalizeutf8 operation on ds
  1284. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfc);
  1285. EXPECT_NE(normalizeutf8, nullptr);
  1286. // Create Map operation on ds
  1287. ds = ds->Map({normalizeutf8}, {"text"});
  1288. EXPECT_NE(ds, nullptr);
  1289. // Create an iterator over the result of the above dataset
  1290. // This will trigger the creation of the Execution Tree and launch it.
  1291. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1292. EXPECT_NE(iter, nullptr);
  1293. // Iterate the dataset and get each row
  1294. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1295. iter->GetNextRow(&row);
  1296. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1297. uint64_t i = 0;
  1298. while (row.size() != 0) {
  1299. auto ind = row["text"];
  1300. std::shared_ptr<Tensor> expected_tensor;
  1301. Tensor::CreateScalar(expected[i], &expected_tensor);
  1302. EXPECT_EQ(*ind, *expected_tensor);
  1303. iter->GetNextRow(&row);
  1304. i++;
  1305. }
  1306. EXPECT_EQ(i, 6);
  1307. // Manually terminate the pipeline
  1308. iter->Stop();
  1309. }
  1310. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success2) {
  1311. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfd.
  1312. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success2.";
  1313. // Create a TextFile dataset
  1314. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1315. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1316. EXPECT_NE(ds, nullptr);
  1317. // Create normalizeutf8 operation on ds
  1318. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfd);
  1319. EXPECT_NE(normalizeutf8, nullptr);
  1320. // Create Map operation on ds
  1321. ds = ds->Map({normalizeutf8}, {"text"});
  1322. EXPECT_NE(ds, nullptr);
  1323. // Create an iterator over the result of the above dataset
  1324. // This will trigger the creation of the Execution Tree and launch it.
  1325. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1326. EXPECT_NE(iter, nullptr);
  1327. // Iterate the dataset and get each row
  1328. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1329. iter->GetNextRow(&row);
  1330. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1331. uint64_t i = 0;
  1332. while (row.size() != 0) {
  1333. auto ind = row["text"];
  1334. std::shared_ptr<Tensor> expected_tensor;
  1335. Tensor::CreateScalar(expected[i], &expected_tensor);
  1336. EXPECT_EQ(*ind, *expected_tensor);
  1337. iter->GetNextRow(&row);
  1338. i++;
  1339. }
  1340. EXPECT_EQ(i, 6);
  1341. // Manually terminate the pipeline
  1342. iter->Stop();
  1343. }
  1344. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success3) {
  1345. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkd.
  1346. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success3.";
  1347. // Create a TextFile dataset
  1348. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1349. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1350. EXPECT_NE(ds, nullptr);
  1351. // Create normalizeutf8 operation on ds
  1352. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkd);
  1353. EXPECT_NE(normalizeutf8, nullptr);
  1354. // Create Map operation on ds
  1355. ds = ds->Map({normalizeutf8}, {"text"});
  1356. EXPECT_NE(ds, nullptr);
  1357. // Create an iterator over the result of the above dataset
  1358. // This will trigger the creation of the Execution Tree and launch it.
  1359. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1360. EXPECT_NE(iter, nullptr);
  1361. // Iterate the dataset and get each row
  1362. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1363. iter->GetNextRow(&row);
  1364. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1365. uint64_t i = 0;
  1366. while (row.size() != 0) {
  1367. auto ind = row["text"];
  1368. std::shared_ptr<Tensor> expected_tensor;
  1369. Tensor::CreateScalar(expected[i], &expected_tensor);
  1370. EXPECT_EQ(*ind, *expected_tensor);
  1371. iter->GetNextRow(&row);
  1372. i++;
  1373. }
  1374. EXPECT_EQ(i, 6);
  1375. // Manually terminate the pipeline
  1376. iter->Stop();
  1377. }
  1378. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess) {
  1379. // Testing the parameter of RegexReplace interface when the replace_all is true.
  1380. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess.";
  1381. // Create a TextFile dataset
  1382. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1383. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1384. EXPECT_NE(ds, nullptr);
  1385. // Create regex_replace operation on ds
  1386. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", true);
  1387. EXPECT_NE(regex_replace, nullptr);
  1388. // Create Map operation on ds
  1389. ds = ds->Map({regex_replace}, {"text"});
  1390. EXPECT_NE(ds, nullptr);
  1391. // Create an iterator over the result of the above dataset
  1392. // This will trigger the creation of the Execution Tree and launch it.
  1393. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1394. EXPECT_NE(iter, nullptr);
  1395. // Iterate the dataset and get each row
  1396. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1397. iter->GetNextRow(&row);
  1398. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1399. "31:beijing", "Welcome_to_China!", "_我_不想_长大_", "Welcome_to_Shenzhen!"};
  1400. uint64_t i = 0;
  1401. while (row.size() != 0) {
  1402. auto ind = row["text"];
  1403. std::shared_ptr<Tensor> expected_tensor;
  1404. Tensor::CreateScalar(expected[i], &expected_tensor);
  1405. EXPECT_EQ(*ind, *expected_tensor);
  1406. iter->GetNextRow(&row);
  1407. i++;
  1408. }
  1409. EXPECT_EQ(i, 8);
  1410. // Manually terminate the pipeline
  1411. iter->Stop();
  1412. }
  1413. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess1) {
  1414. // Testing the parameter of RegexReplace interface when the replace_all is false.
  1415. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess1.";
  1416. // Create a TextFile dataset
  1417. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1418. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1419. EXPECT_NE(ds, nullptr);
  1420. // Create regex_replace operation on ds
  1421. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", false);
  1422. EXPECT_NE(regex_replace, nullptr);
  1423. // Create Map operation on ds
  1424. ds = ds->Map({regex_replace}, {"text"});
  1425. EXPECT_NE(ds, nullptr);
  1426. // Create an iterator over the result of the above dataset
  1427. // This will trigger the creation of the Execution Tree and launch it.
  1428. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1429. EXPECT_NE(iter, nullptr);
  1430. // Iterate the dataset and get each row
  1431. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1432. iter->GetNextRow(&row);
  1433. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1434. "31:beijing", "Welcome_to China!", "_我 不想 长大 ", "Welcome_to Shenzhen!"};
  1435. uint64_t i = 0;
  1436. while (row.size() != 0) {
  1437. auto ind = row["text"];
  1438. std::shared_ptr<Tensor> expected_tensor;
  1439. Tensor::CreateScalar(expected[i], &expected_tensor);
  1440. EXPECT_EQ(*ind, *expected_tensor);
  1441. iter->GetNextRow(&row);
  1442. i++;
  1443. }
  1444. EXPECT_EQ(i, 8);
  1445. // Manually terminate the pipeline
  1446. iter->Stop();
  1447. }
  1448. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess) {
  1449. // Testing the parameter of RegexTokenizer interface when the with_offsets is false.
  1450. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess.";
  1451. // Create a TextFile dataset
  1452. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1453. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1454. EXPECT_NE(ds, nullptr);
  1455. // Create regex_tokenizer operation on ds
  1456. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", false);
  1457. EXPECT_NE(regex_tokenizer, nullptr);
  1458. // Create Map operation on ds
  1459. ds = ds->Map({regex_tokenizer}, {"text"});
  1460. EXPECT_NE(ds, nullptr);
  1461. // Create an iterator over the result of the above dataset
  1462. // This will trigger the creation of the Execution Tree and launch it.
  1463. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1464. EXPECT_NE(iter, nullptr);
  1465. // Iterate the dataset and get each row
  1466. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1467. iter->GetNextRow(&row);
  1468. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1469. {"Let's", " ", "Go"},
  1470. {"1:hello"},
  1471. {"2:world"},
  1472. {"31:beijing"},
  1473. {"Welcome", " ", "to", " ", "China!"},
  1474. {" ", "我", " ", "不想", " ", "长大", " "},
  1475. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1476. uint64_t i = 0;
  1477. while (row.size() != 0) {
  1478. auto ind = row["text"];
  1479. std::shared_ptr<Tensor> expected_tensor;
  1480. int x = expected[i].size();
  1481. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1482. EXPECT_EQ(*ind, *expected_tensor);
  1483. iter->GetNextRow(&row);
  1484. i++;
  1485. }
  1486. EXPECT_EQ(i, 8);
  1487. // Manually terminate the pipeline
  1488. iter->Stop();
  1489. }
  1490. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess1) {
  1491. // Testing the parameter of RegexTokenizer interface when the with_offsets is true.
  1492. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess1.";
  1493. // Create a TextFile dataset
  1494. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1495. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1496. EXPECT_NE(ds, nullptr);
  1497. // Create regex_tokenizer operation on ds
  1498. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", true);
  1499. EXPECT_NE(regex_tokenizer, nullptr);
  1500. // Create Map operation on ds
  1501. ds = ds->Map({regex_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1502. {"token", "offsets_start", "offsets_limit"});
  1503. EXPECT_NE(ds, nullptr);
  1504. // Create an iterator over the result of the above dataset
  1505. // This will trigger the creation of the Execution Tree and launch it.
  1506. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1507. EXPECT_NE(iter, nullptr);
  1508. // Iterate the dataset and get each row
  1509. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1510. iter->GetNextRow(&row);
  1511. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1512. {"Let's", " ", "Go"},
  1513. {"1:hello"},
  1514. {"2:world"},
  1515. {"31:beijing"},
  1516. {"Welcome", " ", "to", " ", "China!"},
  1517. {" ", "我", " ", "不想", " ", "长大", " "},
  1518. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1519. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1520. {0, 5, 6}, {0, 5, 6}, {0}, {0}, {0}, {0, 7, 8, 10, 11}, {0, 2, 5, 6, 12, 14, 20}, {0, 7, 8, 10, 11}};
  1521. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1522. {5, 6, 11}, {5, 6, 8}, {7}, {7}, {10}, {7, 8, 10, 11, 17}, {2, 5, 6, 12, 14, 20, 21}, {7, 8, 10, 11, 20}};
  1523. uint64_t i = 0;
  1524. while (row.size() != 0) {
  1525. auto ind = row["offsets_start"];
  1526. auto ind1 = row["offsets_limit"];
  1527. auto token = row["token"];
  1528. std::shared_ptr<Tensor> expected_tensor;
  1529. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1530. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1531. int x = expected[i].size();
  1532. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1533. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1534. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1535. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1536. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1537. EXPECT_EQ(*token, *expected_tensor);
  1538. iter->GetNextRow(&row);
  1539. i++;
  1540. }
  1541. EXPECT_EQ(i, 8);
  1542. // Manually terminate the pipeline
  1543. iter->Stop();
  1544. }
  1545. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess) {
  1546. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is default.
  1547. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess.";
  1548. // Create a TextFile dataset
  1549. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1550. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1551. EXPECT_NE(ds, nullptr);
  1552. // Create unicodechar_tokenizer operation on ds
  1553. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer();
  1554. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1555. // Create Map operation on ds
  1556. ds = ds->Map({unicodechar_tokenizer}, {"text"});
  1557. EXPECT_NE(ds, nullptr);
  1558. // Create an iterator over the result of the above dataset
  1559. // This will trigger the creation of the Execution Tree and launch it.
  1560. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1561. EXPECT_NE(iter, nullptr);
  1562. // Iterate the dataset and get each row
  1563. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1564. iter->GetNextRow(&row);
  1565. std::vector<std::vector<std::string>> expected = {
  1566. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1567. {"北", "京", "欢", "迎", "您", "!"},
  1568. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1569. {" ", " "}};
  1570. uint64_t i = 0;
  1571. while (row.size() != 0) {
  1572. auto ind = row["text"];
  1573. std::shared_ptr<Tensor> expected_tensor;
  1574. int x = expected[i].size();
  1575. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1576. EXPECT_EQ(*ind, *expected_tensor);
  1577. iter->GetNextRow(&row);
  1578. i++;
  1579. }
  1580. EXPECT_EQ(i, 4);
  1581. // Manually terminate the pipeline
  1582. iter->Stop();
  1583. }
  1584. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess1) {
  1585. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is true.
  1586. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess1.";
  1587. // Create a TextFile dataset
  1588. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1589. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1590. EXPECT_NE(ds, nullptr);
  1591. // Create unicodechar_tokenizer operation on ds
  1592. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer(true);
  1593. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1594. // Create Map operation on ds
  1595. ds = ds->Map({unicodechar_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1596. {"token", "offsets_start", "offsets_limit"});
  1597. EXPECT_NE(ds, nullptr);
  1598. // Create an iterator over the result of the above dataset
  1599. // This will trigger the creation of the Execution Tree and launch it.
  1600. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1601. EXPECT_NE(iter, nullptr);
  1602. // Iterate the dataset and get each row
  1603. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1604. iter->GetNextRow(&row);
  1605. std::vector<std::vector<std::string>> expected = {
  1606. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1607. {"北", "京", "欢", "迎", "您", "!"},
  1608. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1609. {" ", " "}};
  1610. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1611. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
  1612. {0, 3, 6, 9, 12, 15},
  1613. {0, 3, 6, 9, 10, 11, 12, 13, 14, 15, 16},
  1614. {0, 1}};
  1615. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1616. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  1617. {3, 6, 9, 12, 15, 18},
  1618. {3, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  1619. {1, 2}};
  1620. uint64_t i = 0;
  1621. while (row.size() != 0) {
  1622. auto ind = row["offsets_start"];
  1623. auto ind1 = row["offsets_limit"];
  1624. auto token = row["token"];
  1625. std::shared_ptr<Tensor> expected_tensor;
  1626. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1627. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1628. int x = expected[i].size();
  1629. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1630. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1631. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1632. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1633. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1634. EXPECT_EQ(*token, *expected_tensor);
  1635. iter->GetNextRow(&row);
  1636. i++;
  1637. }
  1638. EXPECT_EQ(i, 4);
  1639. // Manually terminate the pipeline
  1640. iter->Stop();
  1641. }
  1642. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess) {
  1643. // Testing the parameter of UnicodeScriptTokenizer interface when the with_offsets and the keep_whitespace is default.
  1644. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess.";
  1645. // Create a TextFile dataset
  1646. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1647. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1648. EXPECT_NE(ds, nullptr);
  1649. // Create unicodescript_tokenizer operation on ds
  1650. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer();
  1651. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1652. // Create Map operation on ds
  1653. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1654. EXPECT_NE(ds, nullptr);
  1655. // Create an iterator over the result of the above dataset
  1656. // This will trigger the creation of the Execution Tree and launch it.
  1657. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1658. EXPECT_NE(iter, nullptr);
  1659. // Iterate the dataset and get each row
  1660. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1661. iter->GetNextRow(&row);
  1662. std::vector<std::vector<std::string>> expected = {
  1663. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1664. uint64_t i = 0;
  1665. while (row.size() != 0) {
  1666. auto ind = row["text"];
  1667. std::shared_ptr<Tensor> expected_tensor;
  1668. int x = expected[i].size();
  1669. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1670. EXPECT_EQ(*ind, *expected_tensor);
  1671. iter->GetNextRow(&row);
  1672. i++;
  1673. }
  1674. EXPECT_EQ(i, 4);
  1675. // Manually terminate the pipeline
  1676. iter->Stop();
  1677. }
  1678. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess1) {
  1679. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1680. // false.
  1681. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess1.";
  1682. // Create a TextFile dataset
  1683. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1684. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1685. EXPECT_NE(ds, nullptr);
  1686. // Create unicodescript_tokenizer operation on ds
  1687. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true);
  1688. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1689. // Create Map operation on ds
  1690. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1691. EXPECT_NE(ds, nullptr);
  1692. // Create an iterator over the result of the above dataset
  1693. // This will trigger the creation of the Execution Tree and launch it.
  1694. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1695. EXPECT_NE(iter, nullptr);
  1696. // Iterate the dataset and get each row
  1697. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1698. iter->GetNextRow(&row);
  1699. std::vector<std::vector<std::string>> expected = {
  1700. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1701. uint64_t i = 0;
  1702. while (row.size() != 0) {
  1703. auto ind = row["text"];
  1704. std::shared_ptr<Tensor> expected_tensor;
  1705. int x = expected[i].size();
  1706. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1707. EXPECT_EQ(*ind, *expected_tensor);
  1708. iter->GetNextRow(&row);
  1709. i++;
  1710. }
  1711. EXPECT_EQ(i, 4);
  1712. // Manually terminate the pipeline
  1713. iter->Stop();
  1714. }
  1715. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess2) {
  1716. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is false and the with_offsets is
  1717. // true.
  1718. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess2.";
  1719. // Create a TextFile dataset
  1720. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1721. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1722. EXPECT_NE(ds, nullptr);
  1723. // Create unicodescript_tokenizer operation on ds
  1724. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(false, true);
  1725. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1726. // Create Map operation on ds
  1727. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1728. {"token", "offsets_start", "offsets_limit"});
  1729. EXPECT_NE(ds, nullptr);
  1730. // Create an iterator over the result of the above dataset
  1731. // This will trigger the creation of the Execution Tree and launch it.
  1732. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1733. EXPECT_NE(iter, nullptr);
  1734. // Iterate the dataset and get each row
  1735. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1736. iter->GetNextRow(&row);
  1737. std::vector<std::vector<std::string>> expected = {
  1738. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1739. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1740. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 18, 19}, {15, 18}, {9, 16, 17}, {0}};
  1741. uint64_t i = 0;
  1742. while (row.size() != 0) {
  1743. auto ind = row["offsets_start"];
  1744. auto ind1 = row["offsets_limit"];
  1745. auto token = row["token"];
  1746. std::shared_ptr<Tensor> expected_tensor;
  1747. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1748. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1749. int x = expected[i].size();
  1750. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1751. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1752. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1753. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1754. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1755. EXPECT_EQ(*token, *expected_tensor);
  1756. iter->GetNextRow(&row);
  1757. i++;
  1758. }
  1759. EXPECT_EQ(i, 4);
  1760. // Manually terminate the pipeline
  1761. iter->Stop();
  1762. }
  1763. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess3) {
  1764. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1765. // true.
  1766. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess3.";
  1767. // Create a TextFile dataset
  1768. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1769. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1770. EXPECT_NE(ds, nullptr);
  1771. // Create unicodescript_tokenizer operation on ds
  1772. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true, true);
  1773. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1774. // Create Map operation on ds
  1775. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1776. {"token", "offsets_start", "offsets_limit"});
  1777. EXPECT_NE(ds, nullptr);
  1778. // Create an iterator over the result of the above dataset
  1779. // This will trigger the creation of the Execution Tree and launch it.
  1780. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1781. EXPECT_NE(iter, nullptr);
  1782. // Iterate the dataset and get each row
  1783. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1784. iter->GetNextRow(&row);
  1785. std::vector<std::vector<std::string>> expected = {
  1786. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1787. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 7, 8, 10, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1788. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 8, 10, 11, 18, 19}, {15, 18}, {9, 16, 17}, {2}};
  1789. uint64_t i = 0;
  1790. while (row.size() != 0) {
  1791. auto ind = row["offsets_start"];
  1792. auto ind1 = row["offsets_limit"];
  1793. auto token = row["token"];
  1794. std::shared_ptr<Tensor> expected_tensor;
  1795. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1796. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1797. int x = expected[i].size();
  1798. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1799. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1800. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1801. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1802. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1803. EXPECT_EQ(*token, *expected_tensor);
  1804. iter->GetNextRow(&row);
  1805. i++;
  1806. }
  1807. EXPECT_EQ(i, 4);
  1808. // Manually terminate the pipeline
  1809. iter->Stop();
  1810. }
  1811. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess) {
  1812. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is default.
  1813. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess.";
  1814. // Create a TextFile dataset
  1815. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1816. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1817. EXPECT_NE(ds, nullptr);
  1818. // Create white_tokenizer operation on ds
  1819. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1820. EXPECT_NE(white_tokenizer, nullptr);
  1821. // Create Map operation on ds
  1822. ds = ds->Map({white_tokenizer}, {"text"});
  1823. EXPECT_NE(ds, nullptr);
  1824. // Create an iterator over the result of the above dataset
  1825. // This will trigger the creation of the Execution Tree and launch it.
  1826. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1827. EXPECT_NE(iter, nullptr);
  1828. // Iterate the dataset and get each row
  1829. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1830. iter->GetNextRow(&row);
  1831. std::vector<std::vector<std::string>> expected = {
  1832. {"This", "is", "a", "text", "file."}, {"Be", "happy", "every", "day."}, {"Good", "luck", "to", "everyone."}};
  1833. uint64_t i = 0;
  1834. while (row.size() != 0) {
  1835. auto ind = row["text"];
  1836. std::shared_ptr<Tensor> expected_tensor;
  1837. int x = expected[i].size();
  1838. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1839. EXPECT_EQ(*ind, *expected_tensor);
  1840. iter->GetNextRow(&row);
  1841. i++;
  1842. }
  1843. EXPECT_EQ(i, 3);
  1844. // Manually terminate the pipeline
  1845. iter->Stop();
  1846. }
  1847. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess1) {
  1848. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is true.
  1849. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess1.";
  1850. // Create a TextFile dataset
  1851. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1852. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1853. EXPECT_NE(ds, nullptr);
  1854. // Create white_tokenizer operation on ds
  1855. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer(true);
  1856. EXPECT_NE(white_tokenizer, nullptr);
  1857. // Create Map operation on ds
  1858. ds = ds->Map({white_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1859. {"token", "offsets_start", "offsets_limit"});
  1860. EXPECT_NE(ds, nullptr);
  1861. // Create an iterator over the result of the above dataset
  1862. // This will trigger the creation of the Execution Tree and launch it.
  1863. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1864. EXPECT_NE(iter, nullptr);
  1865. // Iterate the dataset and get each row
  1866. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1867. iter->GetNextRow(&row);
  1868. std::vector<std::vector<std::string>> expected = {
  1869. {"Welcome", "to", "Beijing!"}, {"北京欢迎您!"}, {"我喜欢English!"}, {""}};
  1870. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11}, {0}, {0}, {0}};
  1871. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 19}, {18}, {17}, {0}};
  1872. uint64_t i = 0;
  1873. while (row.size() != 0) {
  1874. auto ind = row["offsets_start"];
  1875. auto ind1 = row["offsets_limit"];
  1876. auto token = row["token"];
  1877. std::shared_ptr<Tensor> expected_tensor;
  1878. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1879. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1880. int x = expected[i].size();
  1881. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1882. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1883. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1884. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1885. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1886. EXPECT_EQ(*token, *expected_tensor);
  1887. iter->GetNextRow(&row);
  1888. i++;
  1889. }
  1890. EXPECT_EQ(i, 4);
  1891. // Manually terminate the pipeline
  1892. iter->Stop();
  1893. }