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 97 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  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/text.h"
  24. #include "minddata/dataset/include/transforms.h"
  25. #include "minddata/dataset/text/vocab.h"
  26. using namespace mindspore::dataset;
  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, TestJiebaTokenizerAddWord) {
  702. // Testing the parameter AddWord of JiebaTokenizer when the freq is not provided (default 0).
  703. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord.";
  704. // Create a TextFile dataset
  705. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  706. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  707. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  708. std::shared_ptr<Dataset> ds = TextFile({data_file});
  709. EXPECT_NE(ds, nullptr);
  710. // Create jieba_tokenizer operation on ds
  711. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer =
  712. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  713. EXPECT_NE(jieba_tokenizer, nullptr);
  714. // Add word with freq not provided (default 0)
  715. jieba_tokenizer->AddWord("男默女泪");
  716. // Create Map operation on ds
  717. ds = ds->Map({jieba_tokenizer}, {"text"});
  718. EXPECT_NE(ds, nullptr);
  719. // Create an iterator over the result of the above dataset
  720. // This will trigger the creation of the Execution Tree and launch it.
  721. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  722. EXPECT_NE(iter, nullptr);
  723. // Iterate the dataset and get each row
  724. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  725. iter->GetNextRow(&row);
  726. std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  727. uint64_t i = 0;
  728. while (row.size() != 0) {
  729. auto ind = row["text"];
  730. std::shared_ptr<Tensor> expected_tensor;
  731. Tensor::CreateFromVector(expected, &expected_tensor);
  732. EXPECT_EQ(*ind, *expected_tensor);
  733. iter->GetNextRow(&row);
  734. i++;
  735. }
  736. EXPECT_EQ(i, 1);
  737. // Manually terminate the pipeline
  738. iter->Stop();
  739. }
  740. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord1) {
  741. // Testing the parameter AddWord of JiebaTokenizer when the freq is set explicitly to 0.
  742. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord1.";
  743. // Create a TextFile dataset
  744. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  745. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  746. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  747. std::shared_ptr<Dataset> ds = TextFile({data_file});
  748. EXPECT_NE(ds, nullptr);
  749. // Create jieba_tokenizer operation on ds
  750. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer =
  751. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  752. EXPECT_NE(jieba_tokenizer, nullptr);
  753. // Add word with freq is set explicitly to 0
  754. jieba_tokenizer->AddWord("男默女泪", 0);
  755. // Create Map operation on ds
  756. ds = ds->Map({jieba_tokenizer}, {"text"});
  757. EXPECT_NE(ds, nullptr);
  758. // Create an iterator over the result of the above dataset
  759. // This will trigger the creation of the Execution Tree and launch it.
  760. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  761. EXPECT_NE(iter, nullptr);
  762. // Iterate the dataset and get each row
  763. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  764. iter->GetNextRow(&row);
  765. std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  766. uint64_t i = 0;
  767. while (row.size() != 0) {
  768. auto ind = row["text"];
  769. std::shared_ptr<Tensor> expected_tensor;
  770. Tensor::CreateFromVector(expected, &expected_tensor);
  771. EXPECT_EQ(*ind, *expected_tensor);
  772. iter->GetNextRow(&row);
  773. i++;
  774. }
  775. EXPECT_EQ(i, 1);
  776. // Manually terminate the pipeline
  777. iter->Stop();
  778. }
  779. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord2) {
  780. // Testing the parameter AddWord of JiebaTokenizer when the freq is 10.
  781. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord2.";
  782. // Create a TextFile dataset
  783. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  784. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  785. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  786. std::shared_ptr<Dataset> ds = TextFile({data_file});
  787. EXPECT_NE(ds, nullptr);
  788. // Create jieba_tokenizer operation on ds
  789. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer =
  790. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  791. EXPECT_NE(jieba_tokenizer, nullptr);
  792. // Add word with freq 10
  793. jieba_tokenizer->AddWord("男默女泪", 10);
  794. // Create Map operation on ds
  795. ds = ds->Map({jieba_tokenizer}, {"text"});
  796. EXPECT_NE(ds, nullptr);
  797. // Create an iterator over the result of the above dataset
  798. // This will trigger the creation of the Execution Tree and launch it.
  799. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  800. EXPECT_NE(iter, nullptr);
  801. // Iterate the dataset and get each row
  802. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  803. iter->GetNextRow(&row);
  804. std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  805. uint64_t i = 0;
  806. while (row.size() != 0) {
  807. auto ind = row["text"];
  808. std::shared_ptr<Tensor> expected_tensor;
  809. Tensor::CreateFromVector(expected, &expected_tensor);
  810. EXPECT_EQ(*ind, *expected_tensor);
  811. iter->GetNextRow(&row);
  812. i++;
  813. }
  814. EXPECT_EQ(i, 1);
  815. // Manually terminate the pipeline
  816. iter->Stop();
  817. }
  818. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord3) {
  819. // Testing the parameter AddWord of JiebaTokenizer when the freq is 20000 which affects the result of segmentation.
  820. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord3.";
  821. // Create a TextFile dataset
  822. std::string data_file = datasets_root_path_ + "/testJiebaDataset/6.txt";
  823. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  824. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  825. std::shared_ptr<Dataset> ds = TextFile({data_file});
  826. EXPECT_NE(ds, nullptr);
  827. // Create jieba_tokenizer operation on ds
  828. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer =
  829. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  830. EXPECT_NE(jieba_tokenizer, nullptr);
  831. // Add word with freq 20000
  832. jieba_tokenizer->AddWord("江大桥", 20000);
  833. // Create Map operation on ds
  834. ds = ds->Map({jieba_tokenizer}, {"text"});
  835. EXPECT_NE(ds, nullptr);
  836. // Create an iterator over the result of the above dataset
  837. // This will trigger the creation of the Execution Tree and launch it.
  838. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  839. EXPECT_NE(iter, nullptr);
  840. // Iterate the dataset and get each row
  841. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  842. iter->GetNextRow(&row);
  843. std::vector<std::string> expected = {"江州", "市长", "江大桥", "参加", "了", "长江大桥", "的", "通车", "仪式"};
  844. uint64_t i = 0;
  845. while (row.size() != 0) {
  846. auto ind = row["text"];
  847. std::shared_ptr<Tensor> expected_tensor;
  848. Tensor::CreateFromVector(expected, &expected_tensor);
  849. EXPECT_EQ(*ind, *expected_tensor);
  850. iter->GetNextRow(&row);
  851. i++;
  852. }
  853. EXPECT_EQ(i, 1);
  854. // Manually terminate the pipeline
  855. iter->Stop();
  856. }
  857. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWordFail) {
  858. // Testing the incorrect parameter of AddWord in JiebaTokenizer.
  859. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWordFail.";
  860. // Create a TextFile dataset
  861. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  862. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  863. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  864. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  865. EXPECT_NE(ds, nullptr);
  866. // Testing the parameter word of AddWord is empty
  867. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer =
  868. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  869. EXPECT_NE(jieba_tokenizer, nullptr);
  870. EXPECT_NE(jieba_tokenizer->AddWord("", 10), Status::OK());
  871. // Testing the parameter freq of AddWord is negative
  872. std::shared_ptr<text::JiebaTokenizerOperation> jieba_tokenizer1 =
  873. text::JiebaTokenizer(hmm_path, mp_path, JiebaMode::kMp);
  874. EXPECT_NE(jieba_tokenizer1, nullptr);
  875. EXPECT_NE(jieba_tokenizer1->AddWord("我们", -1), Status::OK());
  876. }
  877. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess) {
  878. // Testing the parameter of SlidingWindow interface when the axis is 0.
  879. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess.";
  880. // Create a TextFile dataset
  881. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  882. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  883. EXPECT_NE(ds, nullptr);
  884. // Create white_tokenizer operation on ds
  885. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  886. EXPECT_NE(white_tokenizer, nullptr);
  887. // Create sliding_window operation on ds
  888. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(3, 0);
  889. EXPECT_NE(sliding_window, nullptr);
  890. // Create Map operation on ds
  891. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  892. EXPECT_NE(ds, nullptr);
  893. // Create an iterator over the result of the above dataset
  894. // This will trigger the creation of the Execution Tree and launch it.
  895. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  896. EXPECT_NE(iter, nullptr);
  897. // Iterate the dataset and get each row
  898. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  899. iter->GetNextRow(&row);
  900. std::vector<std::vector<std::string>> expected = {{"This", "is", "a", "is", "a", "text", "a", "text", "file."},
  901. {"Be", "happy", "every", "happy", "every", "day."},
  902. {"Good", "luck", "to", "luck", "to", "everyone."}};
  903. uint64_t i = 0;
  904. while (row.size() != 0) {
  905. auto ind = row["text"];
  906. std::shared_ptr<Tensor> expected_tensor;
  907. int x = expected[i].size() / 3;
  908. Tensor::CreateFromVector(expected[i], TensorShape({x, 3}), &expected_tensor);
  909. EXPECT_EQ(*ind, *expected_tensor);
  910. iter->GetNextRow(&row);
  911. i++;
  912. }
  913. EXPECT_EQ(i, 3);
  914. // Manually terminate the pipeline
  915. iter->Stop();
  916. }
  917. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess1) {
  918. // Testing the parameter of SlidingWindow interface when the axis is -1.
  919. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess1.";
  920. // Create a TextFile dataset
  921. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  922. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  923. EXPECT_NE(ds, nullptr);
  924. // Create white_tokenizer operation on ds
  925. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  926. EXPECT_NE(white_tokenizer, nullptr);
  927. // Create sliding_window operation on ds
  928. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(2, -1);
  929. EXPECT_NE(sliding_window, nullptr);
  930. // Create Map operation on ds
  931. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  932. EXPECT_NE(ds, nullptr);
  933. // Create an iterator over the result of the above dataset
  934. // This will trigger the creation of the Execution Tree and launch it.
  935. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  936. EXPECT_NE(iter, nullptr);
  937. // Iterate the dataset and get each row
  938. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  939. iter->GetNextRow(&row);
  940. std::vector<std::vector<std::string>> expected = {{"This", "is", "is", "a", "a", "text", "text", "file."},
  941. {"Be", "happy", "happy", "every", "every", "day."},
  942. {"Good", "luck", "luck", "to", "to", "everyone."}};
  943. uint64_t i = 0;
  944. while (row.size() != 0) {
  945. auto ind = row["text"];
  946. std::shared_ptr<Tensor> expected_tensor;
  947. int x = expected[i].size() / 2;
  948. Tensor::CreateFromVector(expected[i], TensorShape({x, 2}), &expected_tensor);
  949. EXPECT_EQ(*ind, *expected_tensor);
  950. iter->GetNextRow(&row);
  951. i++;
  952. }
  953. EXPECT_EQ(i, 3);
  954. // Manually terminate the pipeline
  955. iter->Stop();
  956. }
  957. TEST_F(MindDataTestPipeline, TestSlidingWindowFail) {
  958. // Testing the incorrect parameter of SlidingWindow interface.
  959. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowFail.";
  960. // Create a TextFile dataset
  961. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  962. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  963. EXPECT_NE(ds, nullptr);
  964. // Create sliding_window operation on ds
  965. // Testing the parameter width less than or equal to 0
  966. // The parameter axis support 0 or -1 only for now
  967. std::shared_ptr<TensorOperation> sliding_window = text::SlidingWindow(0, 0);
  968. EXPECT_EQ(sliding_window, nullptr);
  969. // Testing the parameter width less than or equal to 0
  970. // The parameter axis support 0 or -1 only for now
  971. std::shared_ptr<TensorOperation> sliding_window1 = text::SlidingWindow(-2, 0);
  972. EXPECT_EQ(sliding_window1, nullptr);
  973. }
  974. TEST_F(MindDataTestPipeline, TestToNumberSuccess1) {
  975. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess1.";
  976. // Test ToNumber with integer numbers
  977. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  978. // Create a TextFile dataset
  979. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  980. EXPECT_NE(ds, nullptr);
  981. // Create a Take operation on ds
  982. ds = ds->Take(8);
  983. EXPECT_NE(ds, nullptr);
  984. // Create ToNumber operation on ds
  985. std::shared_ptr<TensorOperation> to_number = text::ToNumber("int64");
  986. EXPECT_NE(to_number, nullptr);
  987. // Create a Map operation on ds
  988. ds = ds->Map({to_number}, {"text"});
  989. EXPECT_NE(ds, nullptr);
  990. // Create an iterator over the result of the above dataset
  991. // This will trigger the creation of the Execution Tree and launch it.
  992. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  993. EXPECT_NE(iter, nullptr);
  994. // Iterate the dataset and get each row
  995. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  996. iter->GetNextRow(&row);
  997. std::vector<int64_t> expected = {-121, 14, -2219, 7623, -8162536, 162371864, -1726483716, 98921728421};
  998. uint64_t i = 0;
  999. while (row.size() != 0) {
  1000. auto ind = row["text"];
  1001. std::shared_ptr<Tensor> expected_tensor;
  1002. Tensor::CreateScalar(expected[i], &expected_tensor);
  1003. EXPECT_EQ(*ind, *expected_tensor);
  1004. iter->GetNextRow(&row);
  1005. i++;
  1006. }
  1007. EXPECT_EQ(i, 8);
  1008. // Manually terminate the pipeline
  1009. iter->Stop();
  1010. }
  1011. TEST_F(MindDataTestPipeline, TestToNumberSuccess2) {
  1012. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess2.";
  1013. // Test ToNumber with float numbers
  1014. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1015. // Create a TextFile dataset
  1016. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1017. EXPECT_NE(ds, nullptr);
  1018. // Create a Skip operation on ds
  1019. ds = ds->Skip(8);
  1020. EXPECT_NE(ds, nullptr);
  1021. // Create a Take operation on ds
  1022. ds = ds->Take(6);
  1023. EXPECT_NE(ds, nullptr);
  1024. // Create ToNumber operation on ds
  1025. std::shared_ptr<TensorOperation> to_number = text::ToNumber("float64");
  1026. EXPECT_NE(to_number, nullptr);
  1027. // Create a Map operation on ds
  1028. ds = ds->Map({to_number}, {"text"});
  1029. EXPECT_NE(ds, nullptr);
  1030. // Create an iterator over the result of the above dataset
  1031. // This will trigger the creation of the Execution Tree and launch it.
  1032. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1033. EXPECT_NE(iter, nullptr);
  1034. // Iterate the dataset and get each row
  1035. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1036. iter->GetNextRow(&row);
  1037. std::vector<double_t> expected = {-1.1, 1.4, -2219.321, 7623.453, -816256.234282, 162371864.243243};
  1038. uint64_t i = 0;
  1039. while (row.size() != 0) {
  1040. auto ind = row["text"];
  1041. std::shared_ptr<Tensor> expected_tensor;
  1042. Tensor::CreateScalar(expected[i], &expected_tensor);
  1043. EXPECT_EQ(*ind, *expected_tensor);
  1044. iter->GetNextRow(&row);
  1045. i++;
  1046. }
  1047. EXPECT_EQ(i, 6);
  1048. // Manually terminate the pipeline
  1049. iter->Stop();
  1050. }
  1051. TEST_F(MindDataTestPipeline, TestToNumberFail1) {
  1052. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail1.";
  1053. // Test ToNumber with overflow integer numbers
  1054. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1055. // Create a TextFile dataset
  1056. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1057. EXPECT_NE(ds, nullptr);
  1058. // Create a Skip operation on ds
  1059. ds = ds->Skip(2);
  1060. EXPECT_NE(ds, nullptr);
  1061. // Create a Take operation on ds
  1062. ds = ds->Take(6);
  1063. EXPECT_NE(ds, nullptr);
  1064. // Create ToNumber operation on ds
  1065. std::shared_ptr<TensorOperation> to_number = text::ToNumber("int8");
  1066. EXPECT_NE(to_number, nullptr);
  1067. // Create a Map operation on ds
  1068. ds = ds->Map({to_number}, {"text"});
  1069. EXPECT_NE(ds, nullptr);
  1070. // Create an iterator over the result of the above dataset
  1071. // This will trigger the creation of the Execution Tree and launch it.
  1072. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1073. EXPECT_NE(iter, nullptr);
  1074. // Iterate the dataset and get each row
  1075. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1076. // Expect error: input out of bounds of int8
  1077. iter->GetNextRow(&row);
  1078. uint64_t i = 0;
  1079. while (row.size() != 0) {
  1080. iter->GetNextRow(&row);
  1081. i++;
  1082. }
  1083. // Expect failure: GetNextRow fail and return nothing
  1084. EXPECT_EQ(i, 0);
  1085. // Manually terminate the pipeline
  1086. iter->Stop();
  1087. }
  1088. TEST_F(MindDataTestPipeline, TestToNumberFail2) {
  1089. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail2.";
  1090. // Test ToNumber with overflow float numbers
  1091. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1092. // Create a TextFile dataset
  1093. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1094. EXPECT_NE(ds, nullptr);
  1095. // Create a Skip operation on ds
  1096. ds = ds->Skip(12);
  1097. EXPECT_NE(ds, nullptr);
  1098. // Create a Take operation on ds
  1099. ds = ds->Take(2);
  1100. EXPECT_NE(ds, nullptr);
  1101. // Create ToNumber operation on ds
  1102. std::shared_ptr<TensorOperation> to_number = text::ToNumber("float16");
  1103. EXPECT_NE(to_number, nullptr);
  1104. // Create a Map operation on ds
  1105. ds = ds->Map({to_number}, {"text"});
  1106. EXPECT_NE(ds, nullptr);
  1107. // Create an iterator over the result of the above dataset
  1108. // This will trigger the creation of the Execution Tree and launch it.
  1109. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1110. EXPECT_NE(iter, nullptr);
  1111. // Iterate the dataset and get each row
  1112. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1113. // Expect error: input out of bounds of float16
  1114. iter->GetNextRow(&row);
  1115. uint64_t i = 0;
  1116. while (row.size() != 0) {
  1117. iter->GetNextRow(&row);
  1118. i++;
  1119. }
  1120. // Expect failure: GetNextRow fail and return nothing
  1121. EXPECT_EQ(i, 0);
  1122. // Manually terminate the pipeline
  1123. iter->Stop();
  1124. }
  1125. TEST_F(MindDataTestPipeline, TestToNumberFail3) {
  1126. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail3.";
  1127. // Test ToNumber with non numerical input
  1128. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1129. // Create a TextFile dataset
  1130. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1131. EXPECT_NE(ds, nullptr);
  1132. // Create a Skip operation on ds
  1133. ds = ds->Skip(14);
  1134. EXPECT_NE(ds, nullptr);
  1135. // Create ToNumber operation on ds
  1136. std::shared_ptr<TensorOperation> to_number = text::ToNumber("int64");
  1137. EXPECT_NE(to_number, nullptr);
  1138. // Create a Map operation on ds
  1139. ds = ds->Map({to_number}, {"text"});
  1140. EXPECT_NE(ds, nullptr);
  1141. // Create an iterator over the result of the above dataset
  1142. // This will trigger the creation of the Execution Tree and launch it.
  1143. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1144. EXPECT_NE(iter, nullptr);
  1145. // Iterate the dataset and get each row
  1146. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1147. // Expect error: invalid input which is non numerical
  1148. iter->GetNextRow(&row);
  1149. uint64_t i = 0;
  1150. while (row.size() != 0) {
  1151. iter->GetNextRow(&row);
  1152. i++;
  1153. }
  1154. // Expect failure: GetNextRow fail and return nothing
  1155. EXPECT_EQ(i, 0);
  1156. // Manually terminate the pipeline
  1157. iter->Stop();
  1158. }
  1159. TEST_F(MindDataTestPipeline, TestToNumberFail4) {
  1160. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail4.";
  1161. // Test ToNumber with non numerical data type
  1162. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1163. // Create a TextFile dataset
  1164. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1165. EXPECT_NE(ds, nullptr);
  1166. // Create ToNumber operation on ds
  1167. std::shared_ptr<TensorOperation> to_number1 = text::ToNumber("string");
  1168. // Expect failure: invalid parameter with non numerical data type
  1169. EXPECT_EQ(to_number1, nullptr);
  1170. // Create ToNumber operation on ds
  1171. std::shared_ptr<TensorOperation> to_number2 = text::ToNumber("bool");
  1172. // Expect failure: invalid parameter with non numerical data type
  1173. EXPECT_EQ(to_number2, nullptr);
  1174. }
  1175. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess1) {
  1176. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess1.";
  1177. // Testing basic TruncateSequencePair
  1178. // Set seed for RandomDataset
  1179. auto original_seed = config::get_seed();
  1180. bool status_set_seed = config::set_seed(0);
  1181. EXPECT_EQ(status_set_seed, true);
  1182. // Set num_parallel_workers for RandomDataset
  1183. auto original_worker = config::get_num_parallel_workers();
  1184. bool status_set_worker = config::set_num_parallel_workers(1);
  1185. EXPECT_EQ(status_set_worker, true);
  1186. // Create a RandomDataset which has column names "col1" and "col2"
  1187. std::shared_ptr<SchemaObj> schema = Schema();
  1188. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt16, {5});
  1189. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt32, {3});
  1190. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1191. EXPECT_NE(ds, nullptr);
  1192. // Create a truncate_sequence_pair operation on ds
  1193. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(4);
  1194. EXPECT_NE(truncate_sequence_pair, nullptr);
  1195. // Create Map operation on ds
  1196. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1197. EXPECT_NE(ds, nullptr);
  1198. // Create an iterator over the result of the above dataset
  1199. // This will trigger the creation of the Execution Tree and launch it.
  1200. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1201. EXPECT_NE(iter, nullptr);
  1202. // Iterate the dataset and get each row
  1203. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1204. iter->GetNextRow(&row);
  1205. std::vector<std::vector<int16_t>> expected1 = {{-29556, -29556}, {-18505, -18505}, {-25958, -25958}};
  1206. std::vector<std::vector<int32_t>> expected2 = {
  1207. {-1751672937, -1751672937}, {-656877352, -656877352}, {-606348325, -606348325}};
  1208. uint64_t i = 0;
  1209. while (row.size() != 0) {
  1210. auto ind1 = row["col1"];
  1211. auto ind2 = row["col2"];
  1212. std::shared_ptr<Tensor> expected_tensor1;
  1213. std::shared_ptr<Tensor> expected_tensor2;
  1214. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1215. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1216. EXPECT_EQ(*ind1, *expected_tensor1);
  1217. EXPECT_EQ(*ind2, *expected_tensor2);
  1218. iter->GetNextRow(&row);
  1219. i++;
  1220. }
  1221. EXPECT_EQ(i, 3);
  1222. // Manually terminate the pipeline
  1223. iter->Stop();
  1224. // Restore original seed and num_parallel_workers
  1225. status_set_seed = config::set_seed(original_seed);
  1226. EXPECT_EQ(status_set_seed, true);
  1227. status_set_worker = config::set_num_parallel_workers(original_worker);
  1228. EXPECT_EQ(status_set_worker, true);
  1229. }
  1230. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess2) {
  1231. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess2.";
  1232. // Testing basic TruncateSequencePair with odd max_length
  1233. // Set seed for RandomDataset
  1234. auto original_seed = config::get_seed();
  1235. bool status_set_seed = config::set_seed(1);
  1236. EXPECT_EQ(status_set_seed, true);
  1237. // Set num_parallel_workers for RandomDataset
  1238. auto original_worker = config::get_num_parallel_workers();
  1239. bool status_set_worker = config::set_num_parallel_workers(1);
  1240. EXPECT_EQ(status_set_worker, true);
  1241. // Create a RandomDataset which has column names "col1" and "col2"
  1242. std::shared_ptr<SchemaObj> schema = Schema();
  1243. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt32, {4});
  1244. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt64, {4});
  1245. std::shared_ptr<Dataset> ds = RandomData(4, schema);
  1246. EXPECT_NE(ds, nullptr);
  1247. // Create a truncate_sequence_pair operation on ds
  1248. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(5);
  1249. EXPECT_NE(truncate_sequence_pair, nullptr);
  1250. // Create Map operation on ds
  1251. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1252. EXPECT_NE(ds, nullptr);
  1253. // Create an iterator over the result of the above dataset
  1254. // This will trigger the creation of the Execution Tree and launch it.
  1255. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1256. EXPECT_NE(iter, nullptr);
  1257. // Iterate the dataset and get each row
  1258. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1259. iter->GetNextRow(&row);
  1260. std::vector<std::vector<int32_t>> expected1 = {{1785358954, 1785358954, 1785358954},
  1261. {-1195853640, -1195853640, -1195853640},
  1262. {0, 0, 0},
  1263. {1296911693, 1296911693, 1296911693}};
  1264. std::vector<std::vector<int64_t>> expected2 = {
  1265. {-1, -1}, {-1229782938247303442, -1229782938247303442}, {2314885530818453536, 2314885530818453536}, {-1, -1}};
  1266. uint64_t i = 0;
  1267. while (row.size() != 0) {
  1268. auto ind1 = row["col1"];
  1269. auto ind2 = row["col2"];
  1270. std::shared_ptr<Tensor> expected_tensor1;
  1271. std::shared_ptr<Tensor> expected_tensor2;
  1272. Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1273. Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1274. EXPECT_EQ(*ind1, *expected_tensor1);
  1275. EXPECT_EQ(*ind2, *expected_tensor2);
  1276. iter->GetNextRow(&row);
  1277. i++;
  1278. }
  1279. EXPECT_EQ(i, 4);
  1280. // Manually terminate the pipeline
  1281. iter->Stop();
  1282. // Restore original seed and num_parallel_workers
  1283. status_set_seed = config::set_seed(original_seed);
  1284. EXPECT_EQ(status_set_seed, true);
  1285. status_set_worker = config::set_num_parallel_workers(original_worker);
  1286. EXPECT_EQ(status_set_worker, true);
  1287. }
  1288. TEST_F(MindDataTestPipeline, TestTruncateSequencePairFail) {
  1289. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairFail.";
  1290. // Testing TruncateSequencePair with negative max_length
  1291. // Create a RandomDataset which has column names "col1" and "col2"
  1292. std::shared_ptr<SchemaObj> schema = Schema();
  1293. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt8, {3});
  1294. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt8, {3});
  1295. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1296. EXPECT_NE(ds, nullptr);
  1297. // Create a truncate_sequence_pair operation on ds
  1298. std::shared_ptr<TensorOperation> truncate_sequence_pair = text::TruncateSequencePair(-1);
  1299. // Expect failure: invalid parameter with negative max_length
  1300. EXPECT_EQ(truncate_sequence_pair, nullptr);
  1301. }
  1302. TEST_F(MindDataTestPipeline, TestNgramSuccess) {
  1303. // Testing the parameter of Ngram interface.
  1304. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess.";
  1305. // Create a TextFile dataset
  1306. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1307. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1308. EXPECT_NE(ds, nullptr);
  1309. // Create white_tokenizer operation on ds
  1310. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1311. EXPECT_NE(white_tokenizer, nullptr);
  1312. // Create sliding_window operation on ds
  1313. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2}, {"_", 1}, {"_", 1}, " ");
  1314. EXPECT_NE(ngram_op, nullptr);
  1315. // Create Map operation on ds
  1316. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1317. EXPECT_NE(ds, nullptr);
  1318. // Create an iterator over the result of the above dataset
  1319. // This will trigger the creation of the Execution Tree and launch it.
  1320. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1321. EXPECT_NE(iter, nullptr);
  1322. // Iterate the dataset and get each row
  1323. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1324. iter->GetNextRow(&row);
  1325. std::vector<std::vector<std::string>> expected = {{"_ This", "This is", "is a", "a text", "text file.", "file. _"},
  1326. {"_ Be", "Be happy", "happy every", "every day.", "day. _"},
  1327. {"_ Good", "Good luck", "luck to", "to everyone.", "everyone. _"}};
  1328. uint64_t i = 0;
  1329. while (row.size() != 0) {
  1330. auto ind = row["text"];
  1331. std::shared_ptr<Tensor> expected_tensor;
  1332. int x = expected[i].size();
  1333. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1334. EXPECT_EQ(*ind, *expected_tensor);
  1335. iter->GetNextRow(&row);
  1336. i++;
  1337. }
  1338. EXPECT_EQ(i, 3);
  1339. // Manually terminate the pipeline
  1340. iter->Stop();
  1341. }
  1342. TEST_F(MindDataTestPipeline, TestNgramSuccess1) {
  1343. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess1.";
  1344. // Create a TextFile dataset
  1345. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1346. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1347. EXPECT_NE(ds, nullptr);
  1348. // Create white_tokenizer operation on ds
  1349. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1350. EXPECT_NE(white_tokenizer, nullptr);
  1351. // Create sliding_window operation on ds
  1352. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2, 3}, {"&", 2}, {"&", 2}, "-");
  1353. EXPECT_NE(ngram_op, nullptr);
  1354. // Create Map operation on ds
  1355. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1356. EXPECT_NE(ds, nullptr);
  1357. // Create an iterator over the result of the above dataset
  1358. // This will trigger the creation of the Execution Tree and launch it.
  1359. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1360. EXPECT_NE(iter, nullptr);
  1361. // Iterate the dataset and get each row
  1362. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1363. iter->GetNextRow(&row);
  1364. std::vector<std::vector<std::string>> expected = {
  1365. {"&-This", "This-is", "is-a", "a-text", "text-file.", "file.-&", "&-&-This", "&-This-is", "This-is-a", "is-a-text",
  1366. "a-text-file.", "text-file.-&", "file.-&-&"},
  1367. {"&-Be", "Be-happy", "happy-every", "every-day.", "day.-&", "&-&-Be", "&-Be-happy", "Be-happy-every",
  1368. "happy-every-day.", "every-day.-&", "day.-&-&"},
  1369. {"&-Good", "Good-luck", "luck-to", "to-everyone.", "everyone.-&", "&-&-Good", "&-Good-luck", "Good-luck-to",
  1370. "luck-to-everyone.", "to-everyone.-&", "everyone.-&-&"}};
  1371. uint64_t i = 0;
  1372. while (row.size() != 0) {
  1373. auto ind = row["text"];
  1374. std::shared_ptr<Tensor> expected_tensor;
  1375. int x = expected[i].size();
  1376. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1377. EXPECT_EQ(*ind, *expected_tensor);
  1378. iter->GetNextRow(&row);
  1379. i++;
  1380. }
  1381. EXPECT_EQ(i, 3);
  1382. // Manually terminate the pipeline
  1383. iter->Stop();
  1384. }
  1385. TEST_F(MindDataTestPipeline, TestNgramFail) {
  1386. // Testing the incorrect parameter of Ngram interface.
  1387. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail.";
  1388. // Create a TextFile dataset
  1389. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1390. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1391. EXPECT_NE(ds, nullptr);
  1392. // Create sliding_window operation on ds
  1393. // Testing the vector of ngram is empty
  1394. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({});
  1395. EXPECT_EQ(ngram_op, nullptr);
  1396. // Testing the value of ngrams vector less than and equal to 0
  1397. std::shared_ptr<TensorOperation> ngram_op1 = text::Ngram({0});
  1398. EXPECT_EQ(ngram_op1, nullptr);
  1399. // Testing the value of ngrams vector less than and equal to 0
  1400. std::shared_ptr<TensorOperation> ngram_op2 = text::Ngram({-2});
  1401. EXPECT_EQ(ngram_op2, nullptr);
  1402. // Testing the second parameter pad_width in left_pad vector less than 0
  1403. std::shared_ptr<TensorOperation> ngram_op3 = text::Ngram({2}, {"", -1});
  1404. EXPECT_EQ(ngram_op3, nullptr);
  1405. // Testing the second parameter pad_width in right_pad vector less than 0
  1406. std::shared_ptr<TensorOperation> ngram_op4 = text::Ngram({2}, {"", 1}, {"", -1});
  1407. EXPECT_EQ(ngram_op4, nullptr);
  1408. }
  1409. TEST_F(MindDataTestPipeline, TestTextOperationName) {
  1410. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTextOperationName.";
  1411. // Create object for the tensor op, and check the name
  1412. std::string data_file = datasets_root_path_ + "/testVocab/words.txt";
  1413. std::shared_ptr<TensorOperation> sentence_piece_tokenizer_op =
  1414. text::SentencePieceTokenizer(data_file, SPieceTokenizerOutType::kString);
  1415. std::string correct_name = "SentencepieceTokenizer";
  1416. EXPECT_EQ(correct_name, sentence_piece_tokenizer_op->Name());
  1417. }
  1418. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success) {
  1419. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkc.
  1420. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success.";
  1421. // Create a TextFile dataset
  1422. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1423. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1424. EXPECT_NE(ds, nullptr);
  1425. // Create normalizeutf8 operation on ds
  1426. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkc);
  1427. EXPECT_NE(normalizeutf8, nullptr);
  1428. // Create Map operation on ds
  1429. ds = ds->Map({normalizeutf8}, {"text"});
  1430. EXPECT_NE(ds, nullptr);
  1431. // Create an iterator over the result of the above dataset
  1432. // This will trigger the creation of the Execution Tree and launch it.
  1433. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1434. EXPECT_NE(iter, nullptr);
  1435. // Iterate the dataset and get each row
  1436. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1437. iter->GetNextRow(&row);
  1438. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1439. uint64_t i = 0;
  1440. while (row.size() != 0) {
  1441. auto ind = row["text"];
  1442. std::shared_ptr<Tensor> expected_tensor;
  1443. Tensor::CreateScalar(expected[i], &expected_tensor);
  1444. EXPECT_EQ(*ind, *expected_tensor);
  1445. iter->GetNextRow(&row);
  1446. i++;
  1447. }
  1448. EXPECT_EQ(i, 6);
  1449. // Manually terminate the pipeline
  1450. iter->Stop();
  1451. }
  1452. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success1) {
  1453. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfc.
  1454. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success1.";
  1455. // Create a TextFile dataset
  1456. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1457. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1458. EXPECT_NE(ds, nullptr);
  1459. // Create normalizeutf8 operation on ds
  1460. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfc);
  1461. EXPECT_NE(normalizeutf8, nullptr);
  1462. // Create Map operation on ds
  1463. ds = ds->Map({normalizeutf8}, {"text"});
  1464. EXPECT_NE(ds, nullptr);
  1465. // Create an iterator over the result of the above dataset
  1466. // This will trigger the creation of the Execution Tree and launch it.
  1467. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1468. EXPECT_NE(iter, nullptr);
  1469. // Iterate the dataset and get each row
  1470. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1471. iter->GetNextRow(&row);
  1472. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1473. uint64_t i = 0;
  1474. while (row.size() != 0) {
  1475. auto ind = row["text"];
  1476. std::shared_ptr<Tensor> expected_tensor;
  1477. Tensor::CreateScalar(expected[i], &expected_tensor);
  1478. EXPECT_EQ(*ind, *expected_tensor);
  1479. iter->GetNextRow(&row);
  1480. i++;
  1481. }
  1482. EXPECT_EQ(i, 6);
  1483. // Manually terminate the pipeline
  1484. iter->Stop();
  1485. }
  1486. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success2) {
  1487. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfd.
  1488. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success2.";
  1489. // Create a TextFile dataset
  1490. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1491. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1492. EXPECT_NE(ds, nullptr);
  1493. // Create normalizeutf8 operation on ds
  1494. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfd);
  1495. EXPECT_NE(normalizeutf8, nullptr);
  1496. // Create Map operation on ds
  1497. ds = ds->Map({normalizeutf8}, {"text"});
  1498. EXPECT_NE(ds, nullptr);
  1499. // Create an iterator over the result of the above dataset
  1500. // This will trigger the creation of the Execution Tree and launch it.
  1501. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1502. EXPECT_NE(iter, nullptr);
  1503. // Iterate the dataset and get each row
  1504. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1505. iter->GetNextRow(&row);
  1506. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1507. uint64_t i = 0;
  1508. while (row.size() != 0) {
  1509. auto ind = row["text"];
  1510. std::shared_ptr<Tensor> expected_tensor;
  1511. Tensor::CreateScalar(expected[i], &expected_tensor);
  1512. EXPECT_EQ(*ind, *expected_tensor);
  1513. iter->GetNextRow(&row);
  1514. i++;
  1515. }
  1516. EXPECT_EQ(i, 6);
  1517. // Manually terminate the pipeline
  1518. iter->Stop();
  1519. }
  1520. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success3) {
  1521. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkd.
  1522. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success3.";
  1523. // Create a TextFile dataset
  1524. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1525. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1526. EXPECT_NE(ds, nullptr);
  1527. // Create normalizeutf8 operation on ds
  1528. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkd);
  1529. EXPECT_NE(normalizeutf8, nullptr);
  1530. // Create Map operation on ds
  1531. ds = ds->Map({normalizeutf8}, {"text"});
  1532. EXPECT_NE(ds, nullptr);
  1533. // Create an iterator over the result of the above dataset
  1534. // This will trigger the creation of the Execution Tree and launch it.
  1535. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1536. EXPECT_NE(iter, nullptr);
  1537. // Iterate the dataset and get each row
  1538. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1539. iter->GetNextRow(&row);
  1540. std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1541. uint64_t i = 0;
  1542. while (row.size() != 0) {
  1543. auto ind = row["text"];
  1544. std::shared_ptr<Tensor> expected_tensor;
  1545. Tensor::CreateScalar(expected[i], &expected_tensor);
  1546. EXPECT_EQ(*ind, *expected_tensor);
  1547. iter->GetNextRow(&row);
  1548. i++;
  1549. }
  1550. EXPECT_EQ(i, 6);
  1551. // Manually terminate the pipeline
  1552. iter->Stop();
  1553. }
  1554. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess) {
  1555. // Testing the parameter of RegexReplace interface when the replace_all is true.
  1556. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess.";
  1557. // Create a TextFile dataset
  1558. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1559. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1560. EXPECT_NE(ds, nullptr);
  1561. // Create regex_replace operation on ds
  1562. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", true);
  1563. EXPECT_NE(regex_replace, nullptr);
  1564. // Create Map operation on ds
  1565. ds = ds->Map({regex_replace}, {"text"});
  1566. EXPECT_NE(ds, nullptr);
  1567. // Create an iterator over the result of the above dataset
  1568. // This will trigger the creation of the Execution Tree and launch it.
  1569. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1570. EXPECT_NE(iter, nullptr);
  1571. // Iterate the dataset and get each row
  1572. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1573. iter->GetNextRow(&row);
  1574. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1575. "31:beijing", "Welcome_to_China!", "_我_不想_长大_", "Welcome_to_Shenzhen!"};
  1576. uint64_t i = 0;
  1577. while (row.size() != 0) {
  1578. auto ind = row["text"];
  1579. std::shared_ptr<Tensor> expected_tensor;
  1580. Tensor::CreateScalar(expected[i], &expected_tensor);
  1581. EXPECT_EQ(*ind, *expected_tensor);
  1582. iter->GetNextRow(&row);
  1583. i++;
  1584. }
  1585. EXPECT_EQ(i, 8);
  1586. // Manually terminate the pipeline
  1587. iter->Stop();
  1588. }
  1589. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess1) {
  1590. // Testing the parameter of RegexReplace interface when the replace_all is false.
  1591. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess1.";
  1592. // Create a TextFile dataset
  1593. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1594. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1595. EXPECT_NE(ds, nullptr);
  1596. // Create regex_replace operation on ds
  1597. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", false);
  1598. EXPECT_NE(regex_replace, nullptr);
  1599. // Create Map operation on ds
  1600. ds = ds->Map({regex_replace}, {"text"});
  1601. EXPECT_NE(ds, nullptr);
  1602. // Create an iterator over the result of the above dataset
  1603. // This will trigger the creation of the Execution Tree and launch it.
  1604. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1605. EXPECT_NE(iter, nullptr);
  1606. // Iterate the dataset and get each row
  1607. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1608. iter->GetNextRow(&row);
  1609. std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1610. "31:beijing", "Welcome_to China!", "_我 不想 长大 ", "Welcome_to Shenzhen!"};
  1611. uint64_t i = 0;
  1612. while (row.size() != 0) {
  1613. auto ind = row["text"];
  1614. std::shared_ptr<Tensor> expected_tensor;
  1615. Tensor::CreateScalar(expected[i], &expected_tensor);
  1616. EXPECT_EQ(*ind, *expected_tensor);
  1617. iter->GetNextRow(&row);
  1618. i++;
  1619. }
  1620. EXPECT_EQ(i, 8);
  1621. // Manually terminate the pipeline
  1622. iter->Stop();
  1623. }
  1624. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess) {
  1625. // Testing the parameter of RegexTokenizer interface when the with_offsets is false.
  1626. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess.";
  1627. // Create a TextFile dataset
  1628. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1629. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1630. EXPECT_NE(ds, nullptr);
  1631. // Create regex_tokenizer operation on ds
  1632. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", false);
  1633. EXPECT_NE(regex_tokenizer, nullptr);
  1634. // Create Map operation on ds
  1635. ds = ds->Map({regex_tokenizer}, {"text"});
  1636. EXPECT_NE(ds, nullptr);
  1637. // Create an iterator over the result of the above dataset
  1638. // This will trigger the creation of the Execution Tree and launch it.
  1639. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1640. EXPECT_NE(iter, nullptr);
  1641. // Iterate the dataset and get each row
  1642. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1643. iter->GetNextRow(&row);
  1644. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1645. {"Let's", " ", "Go"},
  1646. {"1:hello"},
  1647. {"2:world"},
  1648. {"31:beijing"},
  1649. {"Welcome", " ", "to", " ", "China!"},
  1650. {" ", "我", " ", "不想", " ", "长大", " "},
  1651. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1652. uint64_t i = 0;
  1653. while (row.size() != 0) {
  1654. auto ind = row["text"];
  1655. std::shared_ptr<Tensor> expected_tensor;
  1656. int x = expected[i].size();
  1657. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1658. EXPECT_EQ(*ind, *expected_tensor);
  1659. iter->GetNextRow(&row);
  1660. i++;
  1661. }
  1662. EXPECT_EQ(i, 8);
  1663. // Manually terminate the pipeline
  1664. iter->Stop();
  1665. }
  1666. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess1) {
  1667. // Testing the parameter of RegexTokenizer interface when the with_offsets is true.
  1668. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess1.";
  1669. // Create a TextFile dataset
  1670. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1671. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1672. EXPECT_NE(ds, nullptr);
  1673. // Create regex_tokenizer operation on ds
  1674. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", true);
  1675. EXPECT_NE(regex_tokenizer, nullptr);
  1676. // Create Map operation on ds
  1677. ds = ds->Map({regex_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1678. {"token", "offsets_start", "offsets_limit"});
  1679. EXPECT_NE(ds, nullptr);
  1680. // Create an iterator over the result of the above dataset
  1681. // This will trigger the creation of the Execution Tree and launch it.
  1682. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1683. EXPECT_NE(iter, nullptr);
  1684. // Iterate the dataset and get each row
  1685. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1686. iter->GetNextRow(&row);
  1687. std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1688. {"Let's", " ", "Go"},
  1689. {"1:hello"},
  1690. {"2:world"},
  1691. {"31:beijing"},
  1692. {"Welcome", " ", "to", " ", "China!"},
  1693. {" ", "我", " ", "不想", " ", "长大", " "},
  1694. {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1695. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1696. {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}};
  1697. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1698. {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}};
  1699. uint64_t i = 0;
  1700. while (row.size() != 0) {
  1701. auto ind = row["offsets_start"];
  1702. auto ind1 = row["offsets_limit"];
  1703. auto token = row["token"];
  1704. std::shared_ptr<Tensor> expected_tensor;
  1705. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1706. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1707. int x = expected[i].size();
  1708. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1709. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1710. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1711. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1712. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1713. EXPECT_EQ(*token, *expected_tensor);
  1714. iter->GetNextRow(&row);
  1715. i++;
  1716. }
  1717. EXPECT_EQ(i, 8);
  1718. // Manually terminate the pipeline
  1719. iter->Stop();
  1720. }
  1721. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess) {
  1722. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is default.
  1723. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess.";
  1724. // Create a TextFile dataset
  1725. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1726. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1727. EXPECT_NE(ds, nullptr);
  1728. // Create unicodechar_tokenizer operation on ds
  1729. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer();
  1730. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1731. // Create Map operation on ds
  1732. ds = ds->Map({unicodechar_tokenizer}, {"text"});
  1733. EXPECT_NE(ds, nullptr);
  1734. // Create an iterator over the result of the above dataset
  1735. // This will trigger the creation of the Execution Tree and launch it.
  1736. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1737. EXPECT_NE(iter, nullptr);
  1738. // Iterate the dataset and get each row
  1739. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1740. iter->GetNextRow(&row);
  1741. std::vector<std::vector<std::string>> expected = {
  1742. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1743. {"北", "京", "欢", "迎", "您", "!"},
  1744. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1745. {" ", " "}};
  1746. uint64_t i = 0;
  1747. while (row.size() != 0) {
  1748. auto ind = row["text"];
  1749. std::shared_ptr<Tensor> expected_tensor;
  1750. int x = expected[i].size();
  1751. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1752. EXPECT_EQ(*ind, *expected_tensor);
  1753. iter->GetNextRow(&row);
  1754. i++;
  1755. }
  1756. EXPECT_EQ(i, 4);
  1757. // Manually terminate the pipeline
  1758. iter->Stop();
  1759. }
  1760. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess1) {
  1761. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is true.
  1762. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess1.";
  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 unicodechar_tokenizer operation on ds
  1768. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer(true);
  1769. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1770. // Create Map operation on ds
  1771. ds = ds->Map({unicodechar_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. {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1783. {"北", "京", "欢", "迎", "您", "!"},
  1784. {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1785. {" ", " "}};
  1786. std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1787. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
  1788. {0, 3, 6, 9, 12, 15},
  1789. {0, 3, 6, 9, 10, 11, 12, 13, 14, 15, 16},
  1790. {0, 1}};
  1791. std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1792. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  1793. {3, 6, 9, 12, 15, 18},
  1794. {3, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  1795. {1, 2}};
  1796. uint64_t i = 0;
  1797. while (row.size() != 0) {
  1798. auto ind = row["offsets_start"];
  1799. auto ind1 = row["offsets_limit"];
  1800. auto token = row["token"];
  1801. std::shared_ptr<Tensor> expected_tensor;
  1802. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1803. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1804. int x = expected[i].size();
  1805. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1806. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1807. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1808. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1809. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1810. EXPECT_EQ(*token, *expected_tensor);
  1811. iter->GetNextRow(&row);
  1812. i++;
  1813. }
  1814. EXPECT_EQ(i, 4);
  1815. // Manually terminate the pipeline
  1816. iter->Stop();
  1817. }
  1818. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess) {
  1819. // Testing the parameter of UnicodeScriptTokenizer interface when the with_offsets and the keep_whitespace is default.
  1820. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess.";
  1821. // Create a TextFile dataset
  1822. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1823. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1824. EXPECT_NE(ds, nullptr);
  1825. // Create unicodescript_tokenizer operation on ds
  1826. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer();
  1827. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1828. // Create Map operation on ds
  1829. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1830. EXPECT_NE(ds, nullptr);
  1831. // Create an iterator over the result of the above dataset
  1832. // This will trigger the creation of the Execution Tree and launch it.
  1833. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1834. EXPECT_NE(iter, nullptr);
  1835. // Iterate the dataset and get each row
  1836. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1837. iter->GetNextRow(&row);
  1838. std::vector<std::vector<std::string>> expected = {
  1839. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1840. uint64_t i = 0;
  1841. while (row.size() != 0) {
  1842. auto ind = row["text"];
  1843. std::shared_ptr<Tensor> expected_tensor;
  1844. int x = expected[i].size();
  1845. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1846. EXPECT_EQ(*ind, *expected_tensor);
  1847. iter->GetNextRow(&row);
  1848. i++;
  1849. }
  1850. EXPECT_EQ(i, 4);
  1851. // Manually terminate the pipeline
  1852. iter->Stop();
  1853. }
  1854. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess1) {
  1855. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1856. // false.
  1857. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess1.";
  1858. // Create a TextFile dataset
  1859. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1860. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1861. EXPECT_NE(ds, nullptr);
  1862. // Create unicodescript_tokenizer operation on ds
  1863. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true);
  1864. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1865. // Create Map operation on ds
  1866. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1867. EXPECT_NE(ds, nullptr);
  1868. // Create an iterator over the result of the above dataset
  1869. // This will trigger the creation of the Execution Tree and launch it.
  1870. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1871. EXPECT_NE(iter, nullptr);
  1872. // Iterate the dataset and get each row
  1873. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1874. iter->GetNextRow(&row);
  1875. std::vector<std::vector<std::string>> expected = {
  1876. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1877. uint64_t i = 0;
  1878. while (row.size() != 0) {
  1879. auto ind = row["text"];
  1880. std::shared_ptr<Tensor> expected_tensor;
  1881. int x = expected[i].size();
  1882. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1883. EXPECT_EQ(*ind, *expected_tensor);
  1884. iter->GetNextRow(&row);
  1885. i++;
  1886. }
  1887. EXPECT_EQ(i, 4);
  1888. // Manually terminate the pipeline
  1889. iter->Stop();
  1890. }
  1891. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess2) {
  1892. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is false and the with_offsets is
  1893. // true.
  1894. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess2.";
  1895. // Create a TextFile dataset
  1896. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1897. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1898. EXPECT_NE(ds, nullptr);
  1899. // Create unicodescript_tokenizer operation on ds
  1900. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(false, true);
  1901. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1902. // Create Map operation on ds
  1903. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1904. {"token", "offsets_start", "offsets_limit"});
  1905. EXPECT_NE(ds, nullptr);
  1906. // Create an iterator over the result of the above dataset
  1907. // This will trigger the creation of the Execution Tree and launch it.
  1908. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1909. EXPECT_NE(iter, nullptr);
  1910. // Iterate the dataset and get each row
  1911. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1912. iter->GetNextRow(&row);
  1913. std::vector<std::vector<std::string>> expected = {
  1914. {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1915. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1916. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 18, 19}, {15, 18}, {9, 16, 17}, {0}};
  1917. uint64_t i = 0;
  1918. while (row.size() != 0) {
  1919. auto ind = row["offsets_start"];
  1920. auto ind1 = row["offsets_limit"];
  1921. auto token = row["token"];
  1922. std::shared_ptr<Tensor> expected_tensor;
  1923. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1924. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1925. int x = expected[i].size();
  1926. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1927. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1928. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1929. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1930. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1931. EXPECT_EQ(*token, *expected_tensor);
  1932. iter->GetNextRow(&row);
  1933. i++;
  1934. }
  1935. EXPECT_EQ(i, 4);
  1936. // Manually terminate the pipeline
  1937. iter->Stop();
  1938. }
  1939. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess3) {
  1940. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1941. // true.
  1942. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess3.";
  1943. // Create a TextFile dataset
  1944. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1945. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1946. EXPECT_NE(ds, nullptr);
  1947. // Create unicodescript_tokenizer operation on ds
  1948. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true, true);
  1949. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1950. // Create Map operation on ds
  1951. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1952. {"token", "offsets_start", "offsets_limit"});
  1953. EXPECT_NE(ds, nullptr);
  1954. // Create an iterator over the result of the above dataset
  1955. // This will trigger the creation of the Execution Tree and launch it.
  1956. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1957. EXPECT_NE(iter, nullptr);
  1958. // Iterate the dataset and get each row
  1959. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1960. iter->GetNextRow(&row);
  1961. std::vector<std::vector<std::string>> expected = {
  1962. {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1963. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 7, 8, 10, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1964. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 8, 10, 11, 18, 19}, {15, 18}, {9, 16, 17}, {2}};
  1965. uint64_t i = 0;
  1966. while (row.size() != 0) {
  1967. auto ind = row["offsets_start"];
  1968. auto ind1 = row["offsets_limit"];
  1969. auto token = row["token"];
  1970. std::shared_ptr<Tensor> expected_tensor;
  1971. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  1972. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  1973. int x = expected[i].size();
  1974. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1975. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1976. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1977. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1978. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1979. EXPECT_EQ(*token, *expected_tensor);
  1980. iter->GetNextRow(&row);
  1981. i++;
  1982. }
  1983. EXPECT_EQ(i, 4);
  1984. // Manually terminate the pipeline
  1985. iter->Stop();
  1986. }
  1987. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess) {
  1988. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is default.
  1989. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess.";
  1990. // Create a TextFile dataset
  1991. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1992. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1993. EXPECT_NE(ds, nullptr);
  1994. // Create white_tokenizer operation on ds
  1995. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1996. EXPECT_NE(white_tokenizer, nullptr);
  1997. // Create Map operation on ds
  1998. ds = ds->Map({white_tokenizer}, {"text"});
  1999. EXPECT_NE(ds, nullptr);
  2000. // Create an iterator over the result of the above dataset
  2001. // This will trigger the creation of the Execution Tree and launch it.
  2002. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2003. EXPECT_NE(iter, nullptr);
  2004. // Iterate the dataset and get each row
  2005. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  2006. iter->GetNextRow(&row);
  2007. std::vector<std::vector<std::string>> expected = {
  2008. {"This", "is", "a", "text", "file."}, {"Be", "happy", "every", "day."}, {"Good", "luck", "to", "everyone."}};
  2009. uint64_t i = 0;
  2010. while (row.size() != 0) {
  2011. auto ind = row["text"];
  2012. std::shared_ptr<Tensor> expected_tensor;
  2013. int x = expected[i].size();
  2014. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2015. EXPECT_EQ(*ind, *expected_tensor);
  2016. iter->GetNextRow(&row);
  2017. i++;
  2018. }
  2019. EXPECT_EQ(i, 3);
  2020. // Manually terminate the pipeline
  2021. iter->Stop();
  2022. }
  2023. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess1) {
  2024. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is true.
  2025. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess1.";
  2026. // Create a TextFile dataset
  2027. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2028. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2029. EXPECT_NE(ds, nullptr);
  2030. // Create white_tokenizer operation on ds
  2031. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer(true);
  2032. EXPECT_NE(white_tokenizer, nullptr);
  2033. // Create Map operation on ds
  2034. ds = ds->Map({white_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  2035. {"token", "offsets_start", "offsets_limit"});
  2036. EXPECT_NE(ds, nullptr);
  2037. // Create an iterator over the result of the above dataset
  2038. // This will trigger the creation of the Execution Tree and launch it.
  2039. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2040. EXPECT_NE(iter, nullptr);
  2041. // Iterate the dataset and get each row
  2042. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  2043. iter->GetNextRow(&row);
  2044. std::vector<std::vector<std::string>> expected = {
  2045. {"Welcome", "to", "Beijing!"}, {"北京欢迎您!"}, {"我喜欢English!"}, {""}};
  2046. std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11}, {0}, {0}, {0}};
  2047. std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 19}, {18}, {17}, {0}};
  2048. uint64_t i = 0;
  2049. while (row.size() != 0) {
  2050. auto ind = row["offsets_start"];
  2051. auto ind1 = row["offsets_limit"];
  2052. auto token = row["token"];
  2053. std::shared_ptr<Tensor> expected_tensor;
  2054. std::shared_ptr<Tensor> expected_tensor_offsets_start;
  2055. std::shared_ptr<Tensor> expected_tensor_offsets_limit;
  2056. int x = expected[i].size();
  2057. Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2058. Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  2059. Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  2060. EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  2061. EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  2062. EXPECT_EQ(*token, *expected_tensor);
  2063. iter->GetNextRow(&row);
  2064. i++;
  2065. }
  2066. EXPECT_EQ(i, 4);
  2067. // Manually terminate the pipeline
  2068. iter->Stop();
  2069. }