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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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_number = text::ToNumber(DataType("string"));
  992. // Expect failure: invalid parameter with non numerical DataType
  993. EXPECT_EQ(to_number, nullptr);
  994. }
  995. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess1) {
  996. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess1.";
  997. // Testing basic TruncateSequencePair
  998. // Set seed for RandomDataset
  999. auto original_seed = config::get_seed();
  1000. bool status_set_seed = config::set_seed(0);
  1001. EXPECT_EQ(status_set_seed, true);
  1002. // Set num_parallel_workers for RandomDataset
  1003. auto original_worker = config::get_num_parallel_workers();
  1004. bool status_set_worker = config::set_num_parallel_workers(1);
  1005. EXPECT_EQ(status_set_worker, true);
  1006. // Create a RandomDataset which has column names "col1" and "col2"
  1007. std::shared_ptr<SchemaObj> schema = Schema();
  1008. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt16, {5});
  1009. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt32, {3});
  1010. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1011. EXPECT_NE(ds, nullptr);
  1012. // Create a truncate_sequence_pair operation on ds
  1013. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(4);
  1014. EXPECT_NE(truncate_sequence_pair, nullptr);
  1015. // Create Map operation on ds
  1016. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1017. EXPECT_NE(ds, nullptr);
  1018. // Create an iterator over the result of the above dataset
  1019. // This will trigger the creation of the Execution Tree and launch it.
  1020. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1021. EXPECT_NE(iter, nullptr);
  1022. // Iterate the dataset and get each row
  1023. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1024. iter->GetNextRow(&row);
  1025. std::vector<std::vector<int16_t>> expected1 = {{-29556, -29556}, {-18505, -18505}, {-25958, -25958}};
  1026. std::vector<std::vector<int32_t>> expected2 = {
  1027. {-1751672937, -1751672937}, {-656877352, -656877352}, {-606348325, -606348325}};
  1028. uint64_t i = 0;
  1029. while (row.size() != 0) {
  1030. auto ind1 = row["col1"];
  1031. auto ind2 = row["col2"];
  1032. std::shared_ptr<Tensor> expected_tensor1;
  1033. std::shared_ptr<Tensor> expected_tensor2;
  1034. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1035. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1036. EXPECT_EQ(*ind1, *expected_tensor1);
  1037. EXPECT_EQ(*ind2, *expected_tensor2);
  1038. iter->GetNextRow(&row);
  1039. i++;
  1040. }
  1041. EXPECT_EQ(i, 3);
  1042. // Manually terminate the pipeline
  1043. iter->Stop();
  1044. // Restore original seed and num_parallel_workers
  1045. status_set_seed = config::set_seed(original_seed);
  1046. EXPECT_EQ(status_set_seed, true);
  1047. status_set_worker = config::set_num_parallel_workers(original_worker);
  1048. EXPECT_EQ(status_set_worker, true);
  1049. }
  1050. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess2) {
  1051. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess2.";
  1052. // Testing basic TruncateSequencePair with odd max_length
  1053. // Set seed for RandomDataset
  1054. auto original_seed = config::get_seed();
  1055. bool status_set_seed = config::set_seed(1);
  1056. EXPECT_EQ(status_set_seed, true);
  1057. // Set num_parallel_workers for RandomDataset
  1058. auto original_worker = config::get_num_parallel_workers();
  1059. bool status_set_worker = config::set_num_parallel_workers(1);
  1060. EXPECT_EQ(status_set_worker, true);
  1061. // Create a RandomDataset which has column names "col1" and "col2"
  1062. std::shared_ptr<SchemaObj> schema = Schema();
  1063. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt32, {4});
  1064. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt64, {4});
  1065. std::shared_ptr<Dataset> ds = RandomData(4, schema);
  1066. EXPECT_NE(ds, nullptr);
  1067. // Create a truncate_sequence_pair operation on ds
  1068. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(5);
  1069. EXPECT_NE(truncate_sequence_pair, nullptr);
  1070. // Create Map operation on ds
  1071. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1072. EXPECT_NE(ds, nullptr);
  1073. // Create an iterator over the result of the above dataset
  1074. // This will trigger the creation of the Execution Tree and launch it.
  1075. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1076. EXPECT_NE(iter, nullptr);
  1077. // Iterate the dataset and get each row
  1078. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1079. iter->GetNextRow(&row);
  1080. std::vector<std::vector<int32_t>> expected1 = {{1785358954, 1785358954, 1785358954},
  1081. {-1195853640, -1195853640, -1195853640},
  1082. {0, 0, 0},
  1083. {1296911693, 1296911693, 1296911693}};
  1084. std::vector<std::vector<int64_t>> expected2 = {
  1085. {-1, -1}, {-1229782938247303442, -1229782938247303442}, {2314885530818453536, 2314885530818453536}, {-1, -1}};
  1086. uint64_t i = 0;
  1087. while (row.size() != 0) {
  1088. auto ind1 = row["col1"];
  1089. auto ind2 = row["col2"];
  1090. std::shared_ptr<Tensor> expected_tensor1;
  1091. std::shared_ptr<Tensor> expected_tensor2;
  1092. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1093. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1094. EXPECT_EQ(*ind1, *expected_tensor1);
  1095. EXPECT_EQ(*ind2, *expected_tensor2);
  1096. iter->GetNextRow(&row);
  1097. i++;
  1098. }
  1099. EXPECT_EQ(i, 4);
  1100. // Manually terminate the pipeline
  1101. iter->Stop();
  1102. // Restore original seed and num_parallel_workers
  1103. status_set_seed = config::set_seed(original_seed);
  1104. EXPECT_EQ(status_set_seed, true);
  1105. status_set_worker = config::set_num_parallel_workers(original_worker);
  1106. EXPECT_EQ(status_set_worker, true);
  1107. }
  1108. TEST_F(MindDataTestPipeline, TestTruncateSequencePairFail) {
  1109. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairFail.";
  1110. // Testing TruncateSequencePair with negative max_length
  1111. // Create a RandomDataset which has column names "col1" and "col2"
  1112. std::shared_ptr<SchemaObj> schema = Schema();
  1113. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt8, {3});
  1114. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt8, {3});
  1115. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1116. EXPECT_NE(ds, nullptr);
  1117. // Create a truncate_sequence_pair operation on ds
  1118. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(-1);
  1119. // Expect failure: invalid parameter with negative max_length
  1120. EXPECT_EQ(truncate_sequence_pair, nullptr);
  1121. }
  1122. TEST_F(MindDataTestPipeline, TestNgramSuccess) {
  1123. // Testing the parameter of Ngram interface.
  1124. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess.";
  1125. // Create a TextFile dataset
  1126. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1127. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1128. EXPECT_NE(ds, nullptr);
  1129. // Create white_tokenizer operation on ds
  1130. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1131. EXPECT_NE(white_tokenizer, nullptr);
  1132. // Create sliding_window operation on ds
  1133. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2}, {"_", 1}, {"_", 1}, " ");
  1134. EXPECT_NE(ngram_op, nullptr);
  1135. // Create Map operation on ds
  1136. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1137. EXPECT_NE(ds, nullptr);
  1138. // Create an iterator over the result of the above dataset
  1139. // This will trigger the creation of the Execution Tree and launch it.
  1140. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1141. EXPECT_NE(iter, nullptr);
  1142. // Iterate the dataset and get each row
  1143. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1144. iter->GetNextRow(&row);
  1145. std::vector<std::vector<std::string>> expected = {{"_ This", "This is", "is a", "a text", "text file.", "file. _"},
  1146. {"_ Be", "Be happy", "happy every", "every day.", "day. _"},
  1147. {"_ Good", "Good luck", "luck to", "to everyone.", "everyone. _"}};
  1148. uint64_t i = 0;
  1149. while (row.size() != 0) {
  1150. auto ind = row["text"];
  1151. std::shared_ptr<Tensor> expected_tensor;
  1152. int x = expected[i].size();
  1153. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1154. EXPECT_EQ(*ind, *expected_tensor);
  1155. iter->GetNextRow(&row);
  1156. i++;
  1157. }
  1158. EXPECT_EQ(i, 3);
  1159. // Manually terminate the pipeline
  1160. iter->Stop();
  1161. }
  1162. TEST_F(MindDataTestPipeline, TestNgramSuccess1) {
  1163. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess1.";
  1164. // Create a TextFile dataset
  1165. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1166. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1167. EXPECT_NE(ds, nullptr);
  1168. // Create white_tokenizer operation on ds
  1169. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1170. EXPECT_NE(white_tokenizer, nullptr);
  1171. // Create sliding_window operation on ds
  1172. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2, 3}, {"&", 2}, {"&", 2}, "-");
  1173. EXPECT_NE(ngram_op, nullptr);
  1174. // Create Map operation on ds
  1175. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1176. EXPECT_NE(ds, nullptr);
  1177. // Create an iterator over the result of the above dataset
  1178. // This will trigger the creation of the Execution Tree and launch it.
  1179. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1180. EXPECT_NE(iter, nullptr);
  1181. // Iterate the dataset and get each row
  1182. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1183. iter->GetNextRow(&row);
  1184. std::vector<std::vector<std::string>> expected = {
  1185. {"&-This", "This-is", "is-a", "a-text", "text-file.", "file.-&", "&-&-This", "&-This-is", "This-is-a", "is-a-text",
  1186. "a-text-file.", "text-file.-&", "file.-&-&"},
  1187. {"&-Be", "Be-happy", "happy-every", "every-day.", "day.-&", "&-&-Be", "&-Be-happy", "Be-happy-every",
  1188. "happy-every-day.", "every-day.-&", "day.-&-&"},
  1189. {"&-Good", "Good-luck", "luck-to", "to-everyone.", "everyone.-&", "&-&-Good", "&-Good-luck", "Good-luck-to",
  1190. "luck-to-everyone.", "to-everyone.-&", "everyone.-&-&"}};
  1191. uint64_t i = 0;
  1192. while (row.size() != 0) {
  1193. auto ind = row["text"];
  1194. std::shared_ptr<Tensor> expected_tensor;
  1195. int x = expected[i].size();
  1196. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1197. EXPECT_EQ(*ind, *expected_tensor);
  1198. iter->GetNextRow(&row);
  1199. i++;
  1200. }
  1201. EXPECT_EQ(i, 3);
  1202. // Manually terminate the pipeline
  1203. iter->Stop();
  1204. }
  1205. TEST_F(MindDataTestPipeline, TestNgramFail) {
  1206. // Testing the incorrect parameter of Ngram interface.
  1207. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail.";
  1208. // Create a TextFile dataset
  1209. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1210. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1211. EXPECT_NE(ds, nullptr);
  1212. // Create sliding_window operation on ds
  1213. // Testing the vector of ngram is empty
  1214. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({});
  1215. EXPECT_EQ(ngram_op, nullptr);
  1216. // Testing the value of ngrams vector less than and equal to 0
  1217. std::shared_ptr<TensorOperation> ngram_op1 = text::Ngram({0});
  1218. EXPECT_EQ(ngram_op1, nullptr);
  1219. // Testing the value of ngrams vector less than and equal to 0
  1220. std::shared_ptr<TensorOperation> ngram_op2 = text::Ngram({-2});
  1221. EXPECT_EQ(ngram_op2, nullptr);
  1222. // Testing the second parameter pad_width in left_pad vector less than 0
  1223. std::shared_ptr<TensorOperation> ngram_op3 = text::Ngram({2}, {"", -1});
  1224. EXPECT_EQ(ngram_op3, nullptr);
  1225. // Testing the second parameter pad_width in right_pad vector less than 0
  1226. std::shared_ptr<TensorOperation> ngram_op4 = text::Ngram({2}, {"", 1}, {"", -1});
  1227. EXPECT_EQ(ngram_op4, nullptr);
  1228. }
  1229. TEST_F(MindDataTestPipeline, TestTextOperationName) {
  1230. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTextOperationName.";
  1231. // Create object for the tensor op, and check the name
  1232. std::string data_file = datasets_root_path_ + "/testVocab/words.txt";
  1233. std::shared_ptr<TensorOperation> sentence_piece_tokenizer_op =
  1234. text::SentencePieceTokenizer(data_file, SPieceTokenizerOutType::kString);
  1235. std::string correct_name = "SentencepieceTokenizer";
  1236. EXPECT_EQ(correct_name, sentence_piece_tokenizer_op->Name());
  1237. }
  1238. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success) {
  1239. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkc.
  1240. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success.";
  1241. // Create a TextFile dataset
  1242. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1243. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1244. EXPECT_NE(ds, nullptr);
  1245. // Create normalizeutf8 operation on ds
  1246. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkc);
  1247. EXPECT_NE(normalizeutf8, nullptr);
  1248. // Create Map operation on ds
  1249. ds = ds->Map({normalizeutf8}, {"text"});
  1250. EXPECT_NE(ds, nullptr);
  1251. // Create an iterator over the result of the above dataset
  1252. // This will trigger the creation of the Execution Tree and launch it.
  1253. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1254. EXPECT_NE(iter, nullptr);
  1255. // Iterate the dataset and get each row
  1256. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1257. iter->GetNextRow(&row);
  1258. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1259. uint64_t i = 0;
  1260. while (row.size() != 0) {
  1261. auto ind = row["text"];
  1262. std::shared_ptr<Tensor> expected_tensor;
  1263. Tensor::CreateScalar(expected[i], &expected_tensor);
  1264. EXPECT_EQ(*ind, *expected_tensor);
  1265. iter->GetNextRow(&row);
  1266. i++;
  1267. }
  1268. EXPECT_EQ(i, 6);
  1269. // Manually terminate the pipeline
  1270. iter->Stop();
  1271. }
  1272. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success1) {
  1273. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfc.
  1274. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success1.";
  1275. // Create a TextFile dataset
  1276. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1277. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1278. EXPECT_NE(ds, nullptr);
  1279. // Create normalizeutf8 operation on ds
  1280. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfc);
  1281. EXPECT_NE(normalizeutf8, nullptr);
  1282. // Create Map operation on ds
  1283. ds = ds->Map({normalizeutf8}, {"text"});
  1284. EXPECT_NE(ds, nullptr);
  1285. // Create an iterator over the result of the above dataset
  1286. // This will trigger the creation of the Execution Tree and launch it.
  1287. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1288. EXPECT_NE(iter, nullptr);
  1289. // Iterate the dataset and get each row
  1290. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1291. iter->GetNextRow(&row);
  1292. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1293. uint64_t i = 0;
  1294. while (row.size() != 0) {
  1295. auto ind = row["text"];
  1296. std::shared_ptr<Tensor> expected_tensor;
  1297. Tensor::CreateScalar(expected[i], &expected_tensor);
  1298. EXPECT_EQ(*ind, *expected_tensor);
  1299. iter->GetNextRow(&row);
  1300. i++;
  1301. }
  1302. EXPECT_EQ(i, 6);
  1303. // Manually terminate the pipeline
  1304. iter->Stop();
  1305. }
  1306. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success2) {
  1307. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfd.
  1308. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success2.";
  1309. // Create a TextFile dataset
  1310. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1311. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1312. EXPECT_NE(ds, nullptr);
  1313. // Create normalizeutf8 operation on ds
  1314. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfd);
  1315. EXPECT_NE(normalizeutf8, nullptr);
  1316. // Create Map operation on ds
  1317. ds = ds->Map({normalizeutf8}, {"text"});
  1318. EXPECT_NE(ds, nullptr);
  1319. // Create an iterator over the result of the above dataset
  1320. // This will trigger the creation of the Execution Tree and launch it.
  1321. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1322. EXPECT_NE(iter, nullptr);
  1323. // Iterate the dataset and get each row
  1324. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1325. iter->GetNextRow(&row);
  1326. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1327. uint64_t i = 0;
  1328. while (row.size() != 0) {
  1329. auto ind = row["text"];
  1330. std::shared_ptr<Tensor> expected_tensor;
  1331. Tensor::CreateScalar(expected[i], &expected_tensor);
  1332. EXPECT_EQ(*ind, *expected_tensor);
  1333. iter->GetNextRow(&row);
  1334. i++;
  1335. }
  1336. EXPECT_EQ(i, 6);
  1337. // Manually terminate the pipeline
  1338. iter->Stop();
  1339. }
  1340. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success3) {
  1341. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkd.
  1342. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success3.";
  1343. // Create a TextFile dataset
  1344. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1345. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1346. EXPECT_NE(ds, nullptr);
  1347. // Create normalizeutf8 operation on ds
  1348. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkd);
  1349. EXPECT_NE(normalizeutf8, nullptr);
  1350. // Create Map operation on ds
  1351. ds = ds->Map({normalizeutf8}, {"text"});
  1352. EXPECT_NE(ds, nullptr);
  1353. // Create an iterator over the result of the above dataset
  1354. // This will trigger the creation of the Execution Tree and launch it.
  1355. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1356. EXPECT_NE(iter, nullptr);
  1357. // Iterate the dataset and get each row
  1358. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1359. iter->GetNextRow(&row);
  1360. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1361. uint64_t i = 0;
  1362. while (row.size() != 0) {
  1363. auto ind = row["text"];
  1364. std::shared_ptr<Tensor> expected_tensor;
  1365. Tensor::CreateScalar(expected[i], &expected_tensor);
  1366. EXPECT_EQ(*ind, *expected_tensor);
  1367. iter->GetNextRow(&row);
  1368. i++;
  1369. }
  1370. EXPECT_EQ(i, 6);
  1371. // Manually terminate the pipeline
  1372. iter->Stop();
  1373. }
  1374. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess) {
  1375. // Testing the parameter of RegexReplace interface when the replace_all is true.
  1376. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess.";
  1377. // Create a TextFile dataset
  1378. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1379. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1380. EXPECT_NE(ds, nullptr);
  1381. // Create regex_replace operation on ds
  1382. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", true);
  1383. EXPECT_NE(regex_replace, nullptr);
  1384. // Create Map operation on ds
  1385. ds = ds->Map({regex_replace}, {"text"});
  1386. EXPECT_NE(ds, nullptr);
  1387. // Create an iterator over the result of the above dataset
  1388. // This will trigger the creation of the Execution Tree and launch it.
  1389. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1390. EXPECT_NE(iter, nullptr);
  1391. // Iterate the dataset and get each row
  1392. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1393. iter->GetNextRow(&row);
  1394. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1395. "31:beijing", "Welcome_to_China!", "_我_不想_长大_", "Welcome_to_Shenzhen!"};
  1396. uint64_t i = 0;
  1397. while (row.size() != 0) {
  1398. auto ind = row["text"];
  1399. std::shared_ptr<Tensor> expected_tensor;
  1400. Tensor::CreateScalar(expected[i], &expected_tensor);
  1401. EXPECT_EQ(*ind, *expected_tensor);
  1402. iter->GetNextRow(&row);
  1403. i++;
  1404. }
  1405. EXPECT_EQ(i, 8);
  1406. // Manually terminate the pipeline
  1407. iter->Stop();
  1408. }
  1409. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess1) {
  1410. // Testing the parameter of RegexReplace interface when the replace_all is false.
  1411. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess1.";
  1412. // Create a TextFile dataset
  1413. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1414. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1415. EXPECT_NE(ds, nullptr);
  1416. // Create regex_replace operation on ds
  1417. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", false);
  1418. EXPECT_NE(regex_replace, nullptr);
  1419. // Create Map operation on ds
  1420. ds = ds->Map({regex_replace}, {"text"});
  1421. EXPECT_NE(ds, nullptr);
  1422. // Create an iterator over the result of the above dataset
  1423. // This will trigger the creation of the Execution Tree and launch it.
  1424. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1425. EXPECT_NE(iter, nullptr);
  1426. // Iterate the dataset and get each row
  1427. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1428. iter->GetNextRow(&row);
  1429. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1430. "31:beijing", "Welcome_to China!", "_我 不想 长大 ", "Welcome_to Shenzhen!"};
  1431. uint64_t i = 0;
  1432. while (row.size() != 0) {
  1433. auto ind = row["text"];
  1434. std::shared_ptr<Tensor> expected_tensor;
  1435. Tensor::CreateScalar(expected[i], &expected_tensor);
  1436. EXPECT_EQ(*ind, *expected_tensor);
  1437. iter->GetNextRow(&row);
  1438. i++;
  1439. }
  1440. EXPECT_EQ(i, 8);
  1441. // Manually terminate the pipeline
  1442. iter->Stop();
  1443. }
  1444. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess) {
  1445. // Testing the parameter of RegexTokenizer interface when the with_offsets is false.
  1446. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess.";
  1447. // Create a TextFile dataset
  1448. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1449. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1450. EXPECT_NE(ds, nullptr);
  1451. // Create regex_tokenizer operation on ds
  1452. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", false);
  1453. EXPECT_NE(regex_tokenizer, nullptr);
  1454. // Create Map operation on ds
  1455. ds = ds->Map({regex_tokenizer}, {"text"});
  1456. EXPECT_NE(ds, nullptr);
  1457. // Create an iterator over the result of the above dataset
  1458. // This will trigger the creation of the Execution Tree and launch it.
  1459. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1460. EXPECT_NE(iter, nullptr);
  1461. // Iterate the dataset and get each row
  1462. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1463. iter->GetNextRow(&row);
  1464. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1465. {"Let's", " ", "Go"},
  1466. {"1:hello"},
  1467. {"2:world"},
  1468. {"31:beijing"},
  1469. {"Welcome", " ", "to", " ", "China!"},
  1470. {" ", "我", " ", "不想", " ", "长大", " "},
  1471. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1472. uint64_t i = 0;
  1473. while (row.size() != 0) {
  1474. auto ind = row["text"];
  1475. std::shared_ptr<Tensor> expected_tensor;
  1476. int x = expected[i].size();
  1477. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1478. EXPECT_EQ(*ind, *expected_tensor);
  1479. iter->GetNextRow(&row);
  1480. i++;
  1481. }
  1482. EXPECT_EQ(i, 8);
  1483. // Manually terminate the pipeline
  1484. iter->Stop();
  1485. }
  1486. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess1) {
  1487. // Testing the parameter of RegexTokenizer interface when the with_offsets is true.
  1488. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess1.";
  1489. // Create a TextFile dataset
  1490. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1491. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1492. EXPECT_NE(ds, nullptr);
  1493. // Create regex_tokenizer operation on ds
  1494. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", true);
  1495. EXPECT_NE(regex_tokenizer, nullptr);
  1496. // Create Map operation on ds
  1497. ds = ds->Map({regex_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1498. {"token", "offsets_start", "offsets_limit"});
  1499. EXPECT_NE(ds, nullptr);
  1500. // Create an iterator over the result of the above dataset
  1501. // This will trigger the creation of the Execution Tree and launch it.
  1502. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1503. EXPECT_NE(iter, nullptr);
  1504. // Iterate the dataset and get each row
  1505. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1506. iter->GetNextRow(&row);
  1507. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1508. {"Let's", " ", "Go"},
  1509. {"1:hello"},
  1510. {"2:world"},
  1511. {"31:beijing"},
  1512. {"Welcome", " ", "to", " ", "China!"},
  1513. {" ", "我", " ", "不想", " ", "长大", " "},
  1514. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1515. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1516. {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}};
  1517. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1518. {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}};
  1519. uint64_t i = 0;
  1520. while (row.size() != 0) {
  1521. auto ind = row["offsets_start"];
  1522. auto ind1 = row["offsets_limit"];
  1523. auto token = row["token"];
  1524. std::shared_ptr<Tensor> expected_tensor;
  1525. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1526. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1527. int x = expected[i].size();
  1528. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1529. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1530. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1531. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1532. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1533. EXPECT_EQ(*token, *expected_tensor);
  1534. iter->GetNextRow(&row);
  1535. i++;
  1536. }
  1537. EXPECT_EQ(i, 8);
  1538. // Manually terminate the pipeline
  1539. iter->Stop();
  1540. }
  1541. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess) {
  1542. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is default.
  1543. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess.";
  1544. // Create a TextFile dataset
  1545. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1546. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1547. EXPECT_NE(ds, nullptr);
  1548. // Create unicodechar_tokenizer operation on ds
  1549. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer();
  1550. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1551. // Create Map operation on ds
  1552. ds = ds->Map({unicodechar_tokenizer}, {"text"});
  1553. EXPECT_NE(ds, nullptr);
  1554. // Create an iterator over the result of the above dataset
  1555. // This will trigger the creation of the Execution Tree and launch it.
  1556. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1557. EXPECT_NE(iter, nullptr);
  1558. // Iterate the dataset and get each row
  1559. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1560. iter->GetNextRow(&row);
  1561. std::vector<std::vector<std::string>> expected = {
  1562. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1563. {"北", "京", "欢", "迎", "您", "!"},
  1564. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1565. {" ", " "}};
  1566. uint64_t i = 0;
  1567. while (row.size() != 0) {
  1568. auto ind = row["text"];
  1569. std::shared_ptr<Tensor> expected_tensor;
  1570. int x = expected[i].size();
  1571. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1572. EXPECT_EQ(*ind, *expected_tensor);
  1573. iter->GetNextRow(&row);
  1574. i++;
  1575. }
  1576. EXPECT_EQ(i, 4);
  1577. // Manually terminate the pipeline
  1578. iter->Stop();
  1579. }
  1580. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess1) {
  1581. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is true.
  1582. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess1.";
  1583. // Create a TextFile dataset
  1584. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1585. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1586. EXPECT_NE(ds, nullptr);
  1587. // Create unicodechar_tokenizer operation on ds
  1588. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer(true);
  1589. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1590. // Create Map operation on ds
  1591. ds = ds->Map({unicodechar_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1592. {"token", "offsets_start", "offsets_limit"});
  1593. EXPECT_NE(ds, nullptr);
  1594. // Create an iterator over the result of the above dataset
  1595. // This will trigger the creation of the Execution Tree and launch it.
  1596. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1597. EXPECT_NE(iter, nullptr);
  1598. // Iterate the dataset and get each row
  1599. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1600. iter->GetNextRow(&row);
  1601. std::vector<std::vector<std::string>> expected = {
  1602. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1603. {"北", "京", "欢", "迎", "您", "!"},
  1604. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1605. {" ", " "}};
  1606. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1607. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
  1608. {0, 3, 6, 9, 12, 15},
  1609. {0, 3, 6, 9, 10, 11, 12, 13, 14, 15, 16},
  1610. {0, 1}};
  1611. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1612. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  1613. {3, 6, 9, 12, 15, 18},
  1614. {3, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  1615. {1, 2}};
  1616. uint64_t i = 0;
  1617. while (row.size() != 0) {
  1618. auto ind = row["offsets_start"];
  1619. auto ind1 = row["offsets_limit"];
  1620. auto token = row["token"];
  1621. std::shared_ptr<Tensor> expected_tensor;
  1622. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1623. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1624. int x = expected[i].size();
  1625. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1626. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1627. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1628. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1629. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1630. EXPECT_EQ(*token, *expected_tensor);
  1631. iter->GetNextRow(&row);
  1632. i++;
  1633. }
  1634. EXPECT_EQ(i, 4);
  1635. // Manually terminate the pipeline
  1636. iter->Stop();
  1637. }
  1638. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess) {
  1639. // Testing the parameter of UnicodeScriptTokenizer interface when the with_offsets and the keep_whitespace is default.
  1640. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess.";
  1641. // Create a TextFile dataset
  1642. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1643. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1644. EXPECT_NE(ds, nullptr);
  1645. // Create unicodescript_tokenizer operation on ds
  1646. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer();
  1647. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1648. // Create Map operation on ds
  1649. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1650. EXPECT_NE(ds, nullptr);
  1651. // Create an iterator over the result of the above dataset
  1652. // This will trigger the creation of the Execution Tree and launch it.
  1653. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1654. EXPECT_NE(iter, nullptr);
  1655. // Iterate the dataset and get each row
  1656. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1657. iter->GetNextRow(&row);
  1658. std::vector<std::vector<std::string>> expected = {
  1659. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1660. uint64_t i = 0;
  1661. while (row.size() != 0) {
  1662. auto ind = row["text"];
  1663. std::shared_ptr<Tensor> expected_tensor;
  1664. int x = expected[i].size();
  1665. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1666. EXPECT_EQ(*ind, *expected_tensor);
  1667. iter->GetNextRow(&row);
  1668. i++;
  1669. }
  1670. EXPECT_EQ(i, 4);
  1671. // Manually terminate the pipeline
  1672. iter->Stop();
  1673. }
  1674. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess1) {
  1675. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1676. // false.
  1677. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess1.";
  1678. // Create a TextFile dataset
  1679. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1680. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1681. EXPECT_NE(ds, nullptr);
  1682. // Create unicodescript_tokenizer operation on ds
  1683. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true);
  1684. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1685. // Create Map operation on ds
  1686. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1687. EXPECT_NE(ds, nullptr);
  1688. // Create an iterator over the result of the above dataset
  1689. // This will trigger the creation of the Execution Tree and launch it.
  1690. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1691. EXPECT_NE(iter, nullptr);
  1692. // Iterate the dataset and get each row
  1693. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1694. iter->GetNextRow(&row);
  1695. std::vector<std::vector<std::string>> expected = {
  1696. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1697. uint64_t i = 0;
  1698. while (row.size() != 0) {
  1699. auto ind = row["text"];
  1700. std::shared_ptr<Tensor> expected_tensor;
  1701. int x = expected[i].size();
  1702. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1703. EXPECT_EQ(*ind, *expected_tensor);
  1704. iter->GetNextRow(&row);
  1705. i++;
  1706. }
  1707. EXPECT_EQ(i, 4);
  1708. // Manually terminate the pipeline
  1709. iter->Stop();
  1710. }
  1711. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess2) {
  1712. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is false and the with_offsets is
  1713. // true.
  1714. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess2.";
  1715. // Create a TextFile dataset
  1716. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1717. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1718. EXPECT_NE(ds, nullptr);
  1719. // Create unicodescript_tokenizer operation on ds
  1720. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(false, true);
  1721. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1722. // Create Map operation on ds
  1723. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1724. {"token", "offsets_start", "offsets_limit"});
  1725. EXPECT_NE(ds, nullptr);
  1726. // Create an iterator over the result of the above dataset
  1727. // This will trigger the creation of the Execution Tree and launch it.
  1728. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1729. EXPECT_NE(iter, nullptr);
  1730. // Iterate the dataset and get each row
  1731. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1732. iter->GetNextRow(&row);
  1733. std::vector<std::vector<std::string>> expected = {
  1734. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1735. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1736. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 18, 19}, {15, 18}, {9, 16, 17}, {0}};
  1737. uint64_t i = 0;
  1738. while (row.size() != 0) {
  1739. auto ind = row["offsets_start"];
  1740. auto ind1 = row["offsets_limit"];
  1741. auto token = row["token"];
  1742. std::shared_ptr<Tensor> expected_tensor;
  1743. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1744. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1745. int x = expected[i].size();
  1746. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1747. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1748. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1749. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1750. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1751. EXPECT_EQ(*token, *expected_tensor);
  1752. iter->GetNextRow(&row);
  1753. i++;
  1754. }
  1755. EXPECT_EQ(i, 4);
  1756. // Manually terminate the pipeline
  1757. iter->Stop();
  1758. }
  1759. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess3) {
  1760. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1761. // true.
  1762. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess3.";
  1763. // Create a TextFile dataset
  1764. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1765. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1766. EXPECT_NE(ds, nullptr);
  1767. // Create unicodescript_tokenizer operation on ds
  1768. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true, true);
  1769. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1770. // Create Map operation on ds
  1771. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1772. {"token", "offsets_start", "offsets_limit"});
  1773. EXPECT_NE(ds, nullptr);
  1774. // Create an iterator over the result of the above dataset
  1775. // This will trigger the creation of the Execution Tree and launch it.
  1776. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1777. EXPECT_NE(iter, nullptr);
  1778. // Iterate the dataset and get each row
  1779. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1780. iter->GetNextRow(&row);
  1781. std::vector<std::vector<std::string>> expected = {
  1782. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1783. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 7, 8, 10, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1784. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 8, 10, 11, 18, 19}, {15, 18}, {9, 16, 17}, {2}};
  1785. uint64_t i = 0;
  1786. while (row.size() != 0) {
  1787. auto ind = row["offsets_start"];
  1788. auto ind1 = row["offsets_limit"];
  1789. auto token = row["token"];
  1790. std::shared_ptr<Tensor> expected_tensor;
  1791. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1792. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1793. int x = expected[i].size();
  1794. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1795. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1796. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1797. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1798. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1799. EXPECT_EQ(*token, *expected_tensor);
  1800. iter->GetNextRow(&row);
  1801. i++;
  1802. }
  1803. EXPECT_EQ(i, 4);
  1804. // Manually terminate the pipeline
  1805. iter->Stop();
  1806. }
  1807. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess) {
  1808. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is default.
  1809. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess.";
  1810. // Create a TextFile dataset
  1811. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1812. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1813. EXPECT_NE(ds, nullptr);
  1814. // Create white_tokenizer operation on ds
  1815. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1816. EXPECT_NE(white_tokenizer, nullptr);
  1817. // Create Map operation on ds
  1818. ds = ds->Map({white_tokenizer}, {"text"});
  1819. EXPECT_NE(ds, nullptr);
  1820. // Create an iterator over the result of the above dataset
  1821. // This will trigger the creation of the Execution Tree and launch it.
  1822. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1823. EXPECT_NE(iter, nullptr);
  1824. // Iterate the dataset and get each row
  1825. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1826. iter->GetNextRow(&row);
  1827. std::vector<std::vector<std::string>> expected = {
  1828. {"This", "is", "a", "text", "file."}, {"Be", "happy", "every", "day."}, {"Good", "luck", "to", "everyone."}};
  1829. uint64_t i = 0;
  1830. while (row.size() != 0) {
  1831. auto ind = row["text"];
  1832. std::shared_ptr<Tensor> expected_tensor;
  1833. int x = expected[i].size();
  1834. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1835. EXPECT_EQ(*ind, *expected_tensor);
  1836. iter->GetNextRow(&row);
  1837. i++;
  1838. }
  1839. EXPECT_EQ(i, 3);
  1840. // Manually terminate the pipeline
  1841. iter->Stop();
  1842. }
  1843. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess1) {
  1844. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is true.
  1845. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess1.";
  1846. // Create a TextFile dataset
  1847. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1848. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1849. EXPECT_NE(ds, nullptr);
  1850. // Create white_tokenizer operation on ds
  1851. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer(true);
  1852. EXPECT_NE(white_tokenizer, nullptr);
  1853. // Create Map operation on ds
  1854. ds = ds->Map({white_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1855. {"token", "offsets_start", "offsets_limit"});
  1856. EXPECT_NE(ds, nullptr);
  1857. // Create an iterator over the result of the above dataset
  1858. // This will trigger the creation of the Execution Tree and launch it.
  1859. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1860. EXPECT_NE(iter, nullptr);
  1861. // Iterate the dataset and get each row
  1862. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1863. iter->GetNextRow(&row);
  1864. std::vector<std::vector<std::string>> expected = {
  1865. {"Welcome", "to", "Beijing!"}, {"北京欢迎您!"}, {"我喜欢English!"}, {""}};
  1866. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11}, {0}, {0}, {0}};
  1867. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 19}, {18}, {17}, {0}};
  1868. uint64_t i = 0;
  1869. while (row.size() != 0) {
  1870. auto ind = row["offsets_start"];
  1871. auto ind1 = row["offsets_limit"];
  1872. auto token = row["token"];
  1873. std::shared_ptr<Tensor> expected_tensor;
  1874. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1875. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1876. int x = expected[i].size();
  1877. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1878. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1879. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1880. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1881. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1882. EXPECT_EQ(*token, *expected_tensor);
  1883. iter->GetNextRow(&row);
  1884. i++;
  1885. }
  1886. EXPECT_EQ(i, 4);
  1887. // Manually terminate the pipeline
  1888. iter->Stop();
  1889. }