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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /**
  2. * Copyright 2020-2021 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 "include/api/status.h"
  21. #include "minddata/dataset/include/config.h"
  22. #include "minddata/dataset/include/datasets.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::Status;
  28. using mindspore::dataset::ShuffleMode;
  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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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. // mindspore::MSTensor 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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. // mindspore::MSTensor 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor expected_tensor;
  661. // mindspore::MSTensor expected_tensor_offsets_start;
  662. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor 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, mindspore::MSTensor> 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, mindspore::MSTensor> 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, mindspore::MSTensor> 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, mindspore::MSTensor> 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. // mindspore::MSTensor expected_tensor1;
  1213. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // mindspore::MSTensor expected_tensor1;
  1271. // mindspore::MSTensor 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, mindspore::MSTensor> 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. // _"}};
  1329. uint64_t i = 0;
  1330. while (row.size() != 0) {
  1331. // auto ind = row["text"];
  1332. // mindspore::MSTensor expected_tensor;
  1333. // int x = expected[i].size();
  1334. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1335. // EXPECT_EQ(*ind, *expected_tensor);
  1336. iter->GetNextRow(&row);
  1337. i++;
  1338. }
  1339. EXPECT_EQ(i, 3);
  1340. // Manually terminate the pipeline
  1341. iter->Stop();
  1342. }
  1343. TEST_F(MindDataTestPipeline, TestNgramSuccess1) {
  1344. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess1.";
  1345. // Create a TextFile dataset
  1346. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1347. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1348. EXPECT_NE(ds, nullptr);
  1349. // Create white_tokenizer operation on ds
  1350. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1351. EXPECT_NE(white_tokenizer, nullptr);
  1352. // Create sliding_window operation on ds
  1353. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({2, 3}, {"&", 2}, {"&", 2}, "-");
  1354. EXPECT_NE(ngram_op, nullptr);
  1355. // Create Map operation on ds
  1356. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1357. EXPECT_NE(ds, nullptr);
  1358. // Create an iterator over the result of the above dataset
  1359. // This will trigger the creation of the Execution Tree and launch it.
  1360. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1361. EXPECT_NE(iter, nullptr);
  1362. // Iterate the dataset and get each row
  1363. std::unordered_map<std::string, mindspore::MSTensor> row;
  1364. iter->GetNextRow(&row);
  1365. // std::vector<std::vector<std::string>> expected = {
  1366. // {"&-This", "This-is", "is-a", "a-text", "text-file.", "file.-&", "&-&-This", "&-This-is", "This-is-a",
  1367. // "is-a-text",
  1368. // "a-text-file.", "text-file.-&", "file.-&-&"},
  1369. // {"&-Be", "Be-happy", "happy-every", "every-day.", "day.-&", "&-&-Be", "&-Be-happy", "Be-happy-every",
  1370. // "happy-every-day.", "every-day.-&", "day.-&-&"},
  1371. // {"&-Good", "Good-luck", "luck-to", "to-everyone.", "everyone.-&", "&-&-Good", "&-Good-luck", "Good-luck-to",
  1372. // "luck-to-everyone.", "to-everyone.-&", "everyone.-&-&"}};
  1373. uint64_t i = 0;
  1374. while (row.size() != 0) {
  1375. // auto ind = row["text"];
  1376. // mindspore::MSTensor expected_tensor;
  1377. // int x = expected[i].size();
  1378. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1379. // EXPECT_EQ(*ind, *expected_tensor);
  1380. iter->GetNextRow(&row);
  1381. i++;
  1382. }
  1383. EXPECT_EQ(i, 3);
  1384. // Manually terminate the pipeline
  1385. iter->Stop();
  1386. }
  1387. TEST_F(MindDataTestPipeline, TestNgramFail) {
  1388. // Testing the incorrect parameter of Ngram interface.
  1389. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail.";
  1390. // Create a TextFile dataset
  1391. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1392. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1393. EXPECT_NE(ds, nullptr);
  1394. // Create sliding_window operation on ds
  1395. // Testing the vector of ngram is empty
  1396. std::shared_ptr<TensorOperation> ngram_op = text::Ngram({});
  1397. EXPECT_EQ(ngram_op, nullptr);
  1398. // Testing the value of ngrams vector less than and equal to 0
  1399. std::shared_ptr<TensorOperation> ngram_op1 = text::Ngram({0});
  1400. EXPECT_EQ(ngram_op1, nullptr);
  1401. // Testing the value of ngrams vector less than and equal to 0
  1402. std::shared_ptr<TensorOperation> ngram_op2 = text::Ngram({-2});
  1403. EXPECT_EQ(ngram_op2, nullptr);
  1404. // Testing the second parameter pad_width in left_pad vector less than 0
  1405. std::shared_ptr<TensorOperation> ngram_op3 = text::Ngram({2}, {"", -1});
  1406. EXPECT_EQ(ngram_op3, nullptr);
  1407. // Testing the second parameter pad_width in right_pad vector less than 0
  1408. std::shared_ptr<TensorOperation> ngram_op4 = text::Ngram({2}, {"", 1}, {"", -1});
  1409. EXPECT_EQ(ngram_op4, nullptr);
  1410. }
  1411. TEST_F(MindDataTestPipeline, TestTextOperationName) {
  1412. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTextOperationName.";
  1413. // Create object for the tensor op, and check the name
  1414. std::string data_file = datasets_root_path_ + "/testVocab/words.txt";
  1415. std::shared_ptr<TensorOperation> sentence_piece_tokenizer_op =
  1416. text::SentencePieceTokenizer(data_file, SPieceTokenizerOutType::kString);
  1417. std::string correct_name = "SentencepieceTokenizer";
  1418. EXPECT_EQ(correct_name, sentence_piece_tokenizer_op->Name());
  1419. }
  1420. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success) {
  1421. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkc.
  1422. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success.";
  1423. // Create a TextFile dataset
  1424. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1425. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1426. EXPECT_NE(ds, nullptr);
  1427. // Create normalizeutf8 operation on ds
  1428. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkc);
  1429. EXPECT_NE(normalizeutf8, nullptr);
  1430. // Create Map operation on ds
  1431. ds = ds->Map({normalizeutf8}, {"text"});
  1432. EXPECT_NE(ds, nullptr);
  1433. // Create an iterator over the result of the above dataset
  1434. // This will trigger the creation of the Execution Tree and launch it.
  1435. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1436. EXPECT_NE(iter, nullptr);
  1437. // Iterate the dataset and get each row
  1438. std::unordered_map<std::string, mindspore::MSTensor> row;
  1439. iter->GetNextRow(&row);
  1440. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1441. uint64_t i = 0;
  1442. while (row.size() != 0) {
  1443. // auto ind = row["text"];
  1444. // mindspore::MSTensor expected_tensor;
  1445. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1446. // EXPECT_EQ(*ind, *expected_tensor);
  1447. iter->GetNextRow(&row);
  1448. i++;
  1449. }
  1450. EXPECT_EQ(i, 6);
  1451. // Manually terminate the pipeline
  1452. iter->Stop();
  1453. }
  1454. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success1) {
  1455. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfc.
  1456. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success1.";
  1457. // Create a TextFile dataset
  1458. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1459. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1460. EXPECT_NE(ds, nullptr);
  1461. // Create normalizeutf8 operation on ds
  1462. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfc);
  1463. EXPECT_NE(normalizeutf8, nullptr);
  1464. // Create Map operation on ds
  1465. ds = ds->Map({normalizeutf8}, {"text"});
  1466. EXPECT_NE(ds, nullptr);
  1467. // Create an iterator over the result of the above dataset
  1468. // This will trigger the creation of the Execution Tree and launch it.
  1469. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1470. EXPECT_NE(iter, nullptr);
  1471. // Iterate the dataset and get each row
  1472. std::unordered_map<std::string, mindspore::MSTensor> row;
  1473. iter->GetNextRow(&row);
  1474. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1475. uint64_t i = 0;
  1476. while (row.size() != 0) {
  1477. // auto ind = row["text"];
  1478. // mindspore::MSTensor expected_tensor;
  1479. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1480. // EXPECT_EQ(*ind, *expected_tensor);
  1481. iter->GetNextRow(&row);
  1482. i++;
  1483. }
  1484. EXPECT_EQ(i, 6);
  1485. // Manually terminate the pipeline
  1486. iter->Stop();
  1487. }
  1488. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success2) {
  1489. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfd.
  1490. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success2.";
  1491. // Create a TextFile dataset
  1492. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1493. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1494. EXPECT_NE(ds, nullptr);
  1495. // Create normalizeutf8 operation on ds
  1496. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfd);
  1497. EXPECT_NE(normalizeutf8, nullptr);
  1498. // Create Map operation on ds
  1499. ds = ds->Map({normalizeutf8}, {"text"});
  1500. EXPECT_NE(ds, nullptr);
  1501. // Create an iterator over the result of the above dataset
  1502. // This will trigger the creation of the Execution Tree and launch it.
  1503. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1504. EXPECT_NE(iter, nullptr);
  1505. // Iterate the dataset and get each row
  1506. std::unordered_map<std::string, mindspore::MSTensor> row;
  1507. iter->GetNextRow(&row);
  1508. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1509. uint64_t i = 0;
  1510. while (row.size() != 0) {
  1511. // auto ind = row["text"];
  1512. // mindspore::MSTensor expected_tensor;
  1513. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1514. // EXPECT_EQ(*ind, *expected_tensor);
  1515. iter->GetNextRow(&row);
  1516. i++;
  1517. }
  1518. EXPECT_EQ(i, 6);
  1519. // Manually terminate the pipeline
  1520. iter->Stop();
  1521. }
  1522. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success3) {
  1523. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkd.
  1524. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success3.";
  1525. // Create a TextFile dataset
  1526. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1527. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1528. EXPECT_NE(ds, nullptr);
  1529. // Create normalizeutf8 operation on ds
  1530. std::shared_ptr<TensorOperation> normalizeutf8 = text::NormalizeUTF8(NormalizeForm::kNfkd);
  1531. EXPECT_NE(normalizeutf8, nullptr);
  1532. // Create Map operation on ds
  1533. ds = ds->Map({normalizeutf8}, {"text"});
  1534. EXPECT_NE(ds, nullptr);
  1535. // Create an iterator over the result of the above dataset
  1536. // This will trigger the creation of the Execution Tree and launch it.
  1537. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1538. EXPECT_NE(iter, nullptr);
  1539. // Iterate the dataset and get each row
  1540. std::unordered_map<std::string, mindspore::MSTensor> row;
  1541. iter->GetNextRow(&row);
  1542. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1543. uint64_t i = 0;
  1544. while (row.size() != 0) {
  1545. // auto ind = row["text"];
  1546. // mindspore::MSTensor expected_tensor;
  1547. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1548. // EXPECT_EQ(*ind, *expected_tensor);
  1549. iter->GetNextRow(&row);
  1550. i++;
  1551. }
  1552. EXPECT_EQ(i, 6);
  1553. // Manually terminate the pipeline
  1554. iter->Stop();
  1555. }
  1556. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess) {
  1557. // Testing the parameter of RegexReplace interface when the replace_all is true.
  1558. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess.";
  1559. // Create a TextFile dataset
  1560. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1561. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1562. EXPECT_NE(ds, nullptr);
  1563. // Create regex_replace operation on ds
  1564. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", true);
  1565. EXPECT_NE(regex_replace, nullptr);
  1566. // Create Map operation on ds
  1567. ds = ds->Map({regex_replace}, {"text"});
  1568. EXPECT_NE(ds, nullptr);
  1569. // Create an iterator over the result of the above dataset
  1570. // This will trigger the creation of the Execution Tree and launch it.
  1571. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1572. EXPECT_NE(iter, nullptr);
  1573. // Iterate the dataset and get each row
  1574. std::unordered_map<std::string, mindspore::MSTensor> row;
  1575. iter->GetNextRow(&row);
  1576. // std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1577. // "31:beijing", "Welcome_to_China!", "_我_不想_长大_", "Welcome_to_Shenzhen!"};
  1578. uint64_t i = 0;
  1579. while (row.size() != 0) {
  1580. // auto ind = row["text"];
  1581. // mindspore::MSTensor expected_tensor;
  1582. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1583. // EXPECT_EQ(*ind, *expected_tensor);
  1584. iter->GetNextRow(&row);
  1585. i++;
  1586. }
  1587. EXPECT_EQ(i, 8);
  1588. // Manually terminate the pipeline
  1589. iter->Stop();
  1590. }
  1591. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess1) {
  1592. // Testing the parameter of RegexReplace interface when the replace_all is false.
  1593. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess1.";
  1594. // Create a TextFile dataset
  1595. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1596. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1597. EXPECT_NE(ds, nullptr);
  1598. // Create regex_replace operation on ds
  1599. std::shared_ptr<TensorOperation> regex_replace = text::RegexReplace("\\s+", "_", false);
  1600. EXPECT_NE(regex_replace, nullptr);
  1601. // Create Map operation on ds
  1602. ds = ds->Map({regex_replace}, {"text"});
  1603. EXPECT_NE(ds, nullptr);
  1604. // Create an iterator over the result of the above dataset
  1605. // This will trigger the creation of the Execution Tree and launch it.
  1606. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1607. EXPECT_NE(iter, nullptr);
  1608. // Iterate the dataset and get each row
  1609. std::unordered_map<std::string, mindspore::MSTensor> row;
  1610. iter->GetNextRow(&row);
  1611. // std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1612. // "31:beijing", "Welcome_to China!", "_我 不想 长大 ", "Welcome_to
  1613. // Shenzhen!"};
  1614. uint64_t i = 0;
  1615. while (row.size() != 0) {
  1616. // auto ind = row["text"];
  1617. // mindspore::MSTensor expected_tensor;
  1618. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1619. // EXPECT_EQ(*ind, *expected_tensor);
  1620. iter->GetNextRow(&row);
  1621. i++;
  1622. }
  1623. EXPECT_EQ(i, 8);
  1624. // Manually terminate the pipeline
  1625. iter->Stop();
  1626. }
  1627. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess) {
  1628. // Testing the parameter of RegexTokenizer interface when the with_offsets is false.
  1629. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess.";
  1630. // Create a TextFile dataset
  1631. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1632. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1633. EXPECT_NE(ds, nullptr);
  1634. // Create regex_tokenizer operation on ds
  1635. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", false);
  1636. EXPECT_NE(regex_tokenizer, nullptr);
  1637. // Create Map operation on ds
  1638. ds = ds->Map({regex_tokenizer}, {"text"});
  1639. EXPECT_NE(ds, nullptr);
  1640. // Create an iterator over the result of the above dataset
  1641. // This will trigger the creation of the Execution Tree and launch it.
  1642. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1643. EXPECT_NE(iter, nullptr);
  1644. // Iterate the dataset and get each row
  1645. std::unordered_map<std::string, mindspore::MSTensor> row;
  1646. iter->GetNextRow(&row);
  1647. // std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1648. // {"Let's", " ", "Go"},
  1649. // {"1:hello"},
  1650. // {"2:world"},
  1651. // {"31:beijing"},
  1652. // {"Welcome", " ", "to", " ", "China!"},
  1653. // {" ", "我", " ", "不想", " ", "长大", " "},
  1654. // {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1655. uint64_t i = 0;
  1656. while (row.size() != 0) {
  1657. // auto ind = row["text"];
  1658. // mindspore::MSTensor expected_tensor;
  1659. // int x = expected[i].size();
  1660. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1661. // EXPECT_EQ(*ind, *expected_tensor);
  1662. iter->GetNextRow(&row);
  1663. i++;
  1664. }
  1665. EXPECT_EQ(i, 8);
  1666. // Manually terminate the pipeline
  1667. iter->Stop();
  1668. }
  1669. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess1) {
  1670. // Testing the parameter of RegexTokenizer interface when the with_offsets is true.
  1671. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess1.";
  1672. // Create a TextFile dataset
  1673. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1674. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1675. EXPECT_NE(ds, nullptr);
  1676. // Create regex_tokenizer operation on ds
  1677. std::shared_ptr<TensorOperation> regex_tokenizer = text::RegexTokenizer("\\s+", "\\s+", true);
  1678. EXPECT_NE(regex_tokenizer, nullptr);
  1679. // Create Map operation on ds
  1680. ds = ds->Map({regex_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1681. {"token", "offsets_start", "offsets_limit"});
  1682. EXPECT_NE(ds, nullptr);
  1683. // Create an iterator over the result of the above dataset
  1684. // This will trigger the creation of the Execution Tree and launch it.
  1685. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1686. EXPECT_NE(iter, nullptr);
  1687. // Iterate the dataset and get each row
  1688. std::unordered_map<std::string, mindspore::MSTensor> row;
  1689. iter->GetNextRow(&row);
  1690. // std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1691. // {"Let's", " ", "Go"},
  1692. // {"1:hello"},
  1693. // {"2:world"},
  1694. // {"31:beijing"},
  1695. // {"Welcome", " ", "to", " ", "China!"},
  1696. // {" ", "我", " ", "不想", " ", "长大", " "},
  1697. // {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1698. // std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1699. // {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}};
  1700. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1701. // {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}};
  1702. uint64_t i = 0;
  1703. while (row.size() != 0) {
  1704. // auto ind = row["offsets_start"];
  1705. // auto ind1 = row["offsets_limit"];
  1706. // auto token = row["token"];
  1707. // mindspore::MSTensor expected_tensor;
  1708. // mindspore::MSTensor expected_tensor_offsets_start;
  1709. // mindspore::MSTensor expected_tensor_offsets_limit;
  1710. // int x = expected[i].size();
  1711. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1712. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1713. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1714. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1715. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1716. // EXPECT_EQ(*token, *expected_tensor);
  1717. iter->GetNextRow(&row);
  1718. i++;
  1719. }
  1720. EXPECT_EQ(i, 8);
  1721. // Manually terminate the pipeline
  1722. iter->Stop();
  1723. }
  1724. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess) {
  1725. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is default.
  1726. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess.";
  1727. // Create a TextFile dataset
  1728. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1729. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1730. EXPECT_NE(ds, nullptr);
  1731. // Create unicodechar_tokenizer operation on ds
  1732. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer();
  1733. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1734. // Create Map operation on ds
  1735. ds = ds->Map({unicodechar_tokenizer}, {"text"});
  1736. EXPECT_NE(ds, nullptr);
  1737. // Create an iterator over the result of the above dataset
  1738. // This will trigger the creation of the Execution Tree and launch it.
  1739. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1740. EXPECT_NE(iter, nullptr);
  1741. // Iterate the dataset and get each row
  1742. std::unordered_map<std::string, mindspore::MSTensor> row;
  1743. iter->GetNextRow(&row);
  1744. // std::vector<std::vector<std::string>> expected = {
  1745. // {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1746. // {"北", "京", "欢", "迎", "您", "!"},
  1747. // {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1748. // {" ", " "}};
  1749. uint64_t i = 0;
  1750. while (row.size() != 0) {
  1751. // auto ind = row["text"];
  1752. // mindspore::MSTensor expected_tensor;
  1753. // int x = expected[i].size();
  1754. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1755. // EXPECT_EQ(*ind, *expected_tensor);
  1756. iter->GetNextRow(&row);
  1757. i++;
  1758. }
  1759. EXPECT_EQ(i, 4);
  1760. // Manually terminate the pipeline
  1761. iter->Stop();
  1762. }
  1763. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess1) {
  1764. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is true.
  1765. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess1.";
  1766. // Create a TextFile dataset
  1767. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1768. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1769. EXPECT_NE(ds, nullptr);
  1770. // Create unicodechar_tokenizer operation on ds
  1771. std::shared_ptr<TensorOperation> unicodechar_tokenizer = text::UnicodeCharTokenizer(true);
  1772. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1773. // Create Map operation on ds
  1774. ds = ds->Map({unicodechar_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1775. {"token", "offsets_start", "offsets_limit"});
  1776. EXPECT_NE(ds, nullptr);
  1777. // Create an iterator over the result of the above dataset
  1778. // This will trigger the creation of the Execution Tree and launch it.
  1779. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1780. EXPECT_NE(iter, nullptr);
  1781. // Iterate the dataset and get each row
  1782. std::unordered_map<std::string, mindspore::MSTensor> row;
  1783. iter->GetNextRow(&row);
  1784. // std::vector<std::vector<std::string>> expected = {
  1785. // {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1786. // {"北", "京", "欢", "迎", "您", "!"},
  1787. // {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1788. // {" ", " "}};
  1789. // std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1790. // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
  1791. // {0, 3, 6, 9, 12, 15},
  1792. // {0, 3, 6, 9, 10, 11, 12, 13, 14, 15, 16},
  1793. // {0, 1}};
  1794. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1795. // {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  1796. // {3, 6, 9, 12, 15, 18},
  1797. // {3, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  1798. // {1, 2}};
  1799. uint64_t i = 0;
  1800. while (row.size() != 0) {
  1801. // auto ind = row["offsets_start"];
  1802. // auto ind1 = row["offsets_limit"];
  1803. // auto token = row["token"];
  1804. // mindspore::MSTensor expected_tensor;
  1805. // mindspore::MSTensor expected_tensor_offsets_start;
  1806. // mindspore::MSTensor expected_tensor_offsets_limit;
  1807. // int x = expected[i].size();
  1808. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1809. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1810. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1811. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1812. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1813. // EXPECT_EQ(*token, *expected_tensor);
  1814. iter->GetNextRow(&row);
  1815. i++;
  1816. }
  1817. EXPECT_EQ(i, 4);
  1818. // Manually terminate the pipeline
  1819. iter->Stop();
  1820. }
  1821. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess) {
  1822. // Testing the parameter of UnicodeScriptTokenizer interface when the with_offsets and the keep_whitespace is default.
  1823. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess.";
  1824. // Create a TextFile dataset
  1825. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1826. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1827. EXPECT_NE(ds, nullptr);
  1828. // Create unicodescript_tokenizer operation on ds
  1829. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer();
  1830. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1831. // Create Map operation on ds
  1832. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1833. EXPECT_NE(ds, nullptr);
  1834. // Create an iterator over the result of the above dataset
  1835. // This will trigger the creation of the Execution Tree and launch it.
  1836. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1837. EXPECT_NE(iter, nullptr);
  1838. // Iterate the dataset and get each row
  1839. std::unordered_map<std::string, mindspore::MSTensor> row;
  1840. iter->GetNextRow(&row);
  1841. // std::vector<std::vector<std::string>> expected = {
  1842. // {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1843. uint64_t i = 0;
  1844. while (row.size() != 0) {
  1845. // auto ind = row["text"];
  1846. // mindspore::MSTensor expected_tensor;
  1847. // int x = expected[i].size();
  1848. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1849. // EXPECT_EQ(*ind, *expected_tensor);
  1850. iter->GetNextRow(&row);
  1851. i++;
  1852. }
  1853. EXPECT_EQ(i, 4);
  1854. // Manually terminate the pipeline
  1855. iter->Stop();
  1856. }
  1857. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess1) {
  1858. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1859. // false.
  1860. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess1.";
  1861. // Create a TextFile dataset
  1862. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1863. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1864. EXPECT_NE(ds, nullptr);
  1865. // Create unicodescript_tokenizer operation on ds
  1866. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true);
  1867. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1868. // Create Map operation on ds
  1869. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  1870. EXPECT_NE(ds, nullptr);
  1871. // Create an iterator over the result of the above dataset
  1872. // This will trigger the creation of the Execution Tree and launch it.
  1873. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1874. EXPECT_NE(iter, nullptr);
  1875. // Iterate the dataset and get each row
  1876. std::unordered_map<std::string, mindspore::MSTensor> row;
  1877. iter->GetNextRow(&row);
  1878. // std::vector<std::vector<std::string>> expected = {
  1879. // {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1880. uint64_t i = 0;
  1881. while (row.size() != 0) {
  1882. // auto ind = row["text"];
  1883. // mindspore::MSTensor expected_tensor;
  1884. // int x = expected[i].size();
  1885. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1886. // EXPECT_EQ(*ind, *expected_tensor);
  1887. iter->GetNextRow(&row);
  1888. i++;
  1889. }
  1890. EXPECT_EQ(i, 4);
  1891. // Manually terminate the pipeline
  1892. iter->Stop();
  1893. }
  1894. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess2) {
  1895. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is false and the with_offsets is
  1896. // true.
  1897. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess2.";
  1898. // Create a TextFile dataset
  1899. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1900. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1901. EXPECT_NE(ds, nullptr);
  1902. // Create unicodescript_tokenizer operation on ds
  1903. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(false, true);
  1904. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1905. // Create Map operation on ds
  1906. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1907. {"token", "offsets_start", "offsets_limit"});
  1908. EXPECT_NE(ds, nullptr);
  1909. // Create an iterator over the result of the above dataset
  1910. // This will trigger the creation of the Execution Tree and launch it.
  1911. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1912. EXPECT_NE(iter, nullptr);
  1913. // Iterate the dataset and get each row
  1914. std::unordered_map<std::string, mindspore::MSTensor> row;
  1915. iter->GetNextRow(&row);
  1916. // std::vector<std::vector<std::string>> expected = {
  1917. // {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  1918. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1919. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 18, 19}, {15, 18}, {9, 16, 17}, {0}};
  1920. uint64_t i = 0;
  1921. while (row.size() != 0) {
  1922. // auto ind = row["offsets_start"];
  1923. // auto ind1 = row["offsets_limit"];
  1924. // auto token = row["token"];
  1925. // mindspore::MSTensor expected_tensor;
  1926. // mindspore::MSTensor expected_tensor_offsets_start;
  1927. // mindspore::MSTensor expected_tensor_offsets_limit;
  1928. // int x = expected[i].size();
  1929. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1930. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1931. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1932. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1933. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1934. // EXPECT_EQ(*token, *expected_tensor);
  1935. iter->GetNextRow(&row);
  1936. i++;
  1937. }
  1938. EXPECT_EQ(i, 4);
  1939. // Manually terminate the pipeline
  1940. iter->Stop();
  1941. }
  1942. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess3) {
  1943. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  1944. // true.
  1945. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess3.";
  1946. // Create a TextFile dataset
  1947. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1948. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1949. EXPECT_NE(ds, nullptr);
  1950. // Create unicodescript_tokenizer operation on ds
  1951. std::shared_ptr<TensorOperation> unicodescript_tokenizer = text::UnicodeScriptTokenizer(true, true);
  1952. EXPECT_NE(unicodescript_tokenizer, nullptr);
  1953. // Create Map operation on ds
  1954. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1955. {"token", "offsets_start", "offsets_limit"});
  1956. EXPECT_NE(ds, nullptr);
  1957. // Create an iterator over the result of the above dataset
  1958. // This will trigger the creation of the Execution Tree and launch it.
  1959. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1960. EXPECT_NE(iter, nullptr);
  1961. // Iterate the dataset and get each row
  1962. std::unordered_map<std::string, mindspore::MSTensor> row;
  1963. iter->GetNextRow(&row);
  1964. // std::vector<std::vector<std::string>> expected = {
  1965. // {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  1966. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 7, 8, 10, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  1967. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 8, 10, 11, 18, 19}, {15, 18}, {9, 16, 17}, {2}};
  1968. uint64_t i = 0;
  1969. while (row.size() != 0) {
  1970. // auto ind = row["offsets_start"];
  1971. // auto ind1 = row["offsets_limit"];
  1972. // auto token = row["token"];
  1973. // mindspore::MSTensor expected_tensor;
  1974. // mindspore::MSTensor expected_tensor_offsets_start;
  1975. // mindspore::MSTensor expected_tensor_offsets_limit;
  1976. // int x = expected[i].size();
  1977. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1978. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1979. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1980. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1981. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1982. // EXPECT_EQ(*token, *expected_tensor);
  1983. iter->GetNextRow(&row);
  1984. i++;
  1985. }
  1986. EXPECT_EQ(i, 4);
  1987. // Manually terminate the pipeline
  1988. iter->Stop();
  1989. }
  1990. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess) {
  1991. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is default.
  1992. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess.";
  1993. // Create a TextFile dataset
  1994. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1995. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1996. EXPECT_NE(ds, nullptr);
  1997. // Create white_tokenizer operation on ds
  1998. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer();
  1999. EXPECT_NE(white_tokenizer, nullptr);
  2000. // Create Map operation on ds
  2001. ds = ds->Map({white_tokenizer}, {"text"});
  2002. EXPECT_NE(ds, nullptr);
  2003. // Create an iterator over the result of the above dataset
  2004. // This will trigger the creation of the Execution Tree and launch it.
  2005. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2006. EXPECT_NE(iter, nullptr);
  2007. // Iterate the dataset and get each row
  2008. std::unordered_map<std::string, mindspore::MSTensor> row;
  2009. iter->GetNextRow(&row);
  2010. // std::vector<std::vector<std::string>> expected = {
  2011. // {"This", "is", "a", "text", "file."}, {"Be", "happy", "every", "day."}, {"Good", "luck", "to", "everyone."}};
  2012. uint64_t i = 0;
  2013. while (row.size() != 0) {
  2014. // auto ind = row["text"];
  2015. // mindspore::MSTensor expected_tensor;
  2016. // int x = expected[i].size();
  2017. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2018. // EXPECT_EQ(*ind, *expected_tensor);
  2019. iter->GetNextRow(&row);
  2020. i++;
  2021. }
  2022. EXPECT_EQ(i, 3);
  2023. // Manually terminate the pipeline
  2024. iter->Stop();
  2025. }
  2026. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess1) {
  2027. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is true.
  2028. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess1.";
  2029. // Create a TextFile dataset
  2030. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2031. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2032. EXPECT_NE(ds, nullptr);
  2033. // Create white_tokenizer operation on ds
  2034. std::shared_ptr<TensorOperation> white_tokenizer = text::WhitespaceTokenizer(true);
  2035. EXPECT_NE(white_tokenizer, nullptr);
  2036. // Create Map operation on ds
  2037. ds = ds->Map({white_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  2038. {"token", "offsets_start", "offsets_limit"});
  2039. EXPECT_NE(ds, nullptr);
  2040. // Create an iterator over the result of the above dataset
  2041. // This will trigger the creation of the Execution Tree and launch it.
  2042. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2043. EXPECT_NE(iter, nullptr);
  2044. // Iterate the dataset and get each row
  2045. std::unordered_map<std::string, mindspore::MSTensor> row;
  2046. iter->GetNextRow(&row);
  2047. // std::vector<std::vector<std::string>> expected = {
  2048. // {"Welcome", "to", "Beijing!"}, {"北京欢迎您!"}, {"我喜欢English!"}, {""}};
  2049. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11}, {0}, {0}, {0}};
  2050. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 19}, {18}, {17}, {0}};
  2051. uint64_t i = 0;
  2052. while (row.size() != 0) {
  2053. // auto ind = row["offsets_start"];
  2054. // auto ind1 = row["offsets_limit"];
  2055. // auto token = row["token"];
  2056. // mindspore::MSTensor expected_tensor;
  2057. // mindspore::MSTensor expected_tensor_offsets_start;
  2058. // mindspore::MSTensor expected_tensor_offsets_limit;
  2059. // int x = expected[i].size();
  2060. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2061. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  2062. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  2063. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  2064. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  2065. // EXPECT_EQ(*token, *expected_tensor);
  2066. iter->GetNextRow(&row);
  2067. i++;
  2068. }
  2069. EXPECT_EQ(i, 4);
  2070. // Manually terminate the pipeline
  2071. iter->Stop();
  2072. }