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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  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<TensorTransform> basic_tokenizer = std::make_shared<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<TensorTransform> basic_tokenizer = std::make_shared<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<TensorTransform> basic_tokenizer =
  130. std::make_shared<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<TensorTransform> bert_tokenizer = std::make_shared<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<TensorTransform> bert_tokenizer =
  235. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "[UNK]", true);
  236. EXPECT_NE(bert_tokenizer, nullptr);
  237. // Create Map operation on ds
  238. ds = ds->Map({bert_tokenizer}, {"text"});
  239. EXPECT_NE(ds, nullptr);
  240. // Create an iterator over the result of the above dataset
  241. // This will trigger the creation of the Execution Tree and launch it.
  242. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  243. EXPECT_NE(iter, nullptr);
  244. // Iterate the dataset and get each row
  245. std::unordered_map<std::string, mindspore::MSTensor> row;
  246. iter->GetNextRow(&row);
  247. // std::vector<std::string> expected = {"i", "am", "mak", "##ing", "small", "mistake",
  248. // "##s", "during", "work", "##ing", "hour", "##s"};
  249. uint64_t i = 0;
  250. while (row.size() != 0) {
  251. // auto ind = row["text"];
  252. // mindspore::MSTensor expected_tensor;
  253. // Tensor::CreateFromVector(expected, &expected_tensor);
  254. // EXPECT_EQ(*ind, *expected_tensor);
  255. iter->GetNextRow(&row);
  256. i++;
  257. }
  258. EXPECT_EQ(i, 1);
  259. // Manually terminate the pipeline
  260. iter->Stop();
  261. }
  262. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess3) {
  263. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess3.";
  264. // Test BertTokenizer with normalization_form NFKC
  265. // Create a TextFile dataset
  266. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  267. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  268. EXPECT_NE(ds, nullptr);
  269. // Create Skip operation on ds
  270. ds = ds->Skip(5);
  271. EXPECT_NE(ds, nullptr);
  272. // Create Take operation on ds
  273. ds = ds->Take(2);
  274. EXPECT_NE(ds, nullptr);
  275. // Create a vocab from vector
  276. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  277. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  278. EXPECT_EQ(s, Status::OK());
  279. // Create BertTokenizer operation on ds
  280. std::shared_ptr<TensorTransform> bert_tokenizer =
  281. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "[UNK]", false, false, NormalizeForm::kNfc);
  282. EXPECT_NE(bert_tokenizer, nullptr);
  283. // Create Map operation on ds
  284. ds = ds->Map({bert_tokenizer}, {"text"});
  285. EXPECT_NE(ds, nullptr);
  286. // Create an iterator over the result of the above dataset
  287. // This will trigger the creation of the Execution Tree and launch it.
  288. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  289. EXPECT_NE(iter, nullptr);
  290. // Iterate the dataset and get each row
  291. std::unordered_map<std::string, mindspore::MSTensor> row;
  292. iter->GetNextRow(&row);
  293. // std::vector<std::vector<std::string>> expected = {
  294. // {"😀", "嘿", "嘿", "😃", "哈", "哈", "😄", "大", "笑", "😁", "嘻", "嘻"}, {"繁", "體", "字"}};
  295. uint64_t i = 0;
  296. while (row.size() != 0) {
  297. // auto ind = row["text"];
  298. // mindspore::MSTensor expected_tensor;
  299. // Tensor::CreateFromVector(expected[i], &expected_tensor);
  300. // EXPECT_EQ(*ind, *expected_tensor);
  301. iter->GetNextRow(&row);
  302. i++;
  303. }
  304. EXPECT_EQ(i, 2);
  305. // Manually terminate the pipeline
  306. iter->Stop();
  307. }
  308. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess4) {
  309. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess4.";
  310. // Test BertTokenizer with keep_whitespace true
  311. // Create a TextFile dataset
  312. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  313. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  314. EXPECT_NE(ds, nullptr);
  315. // Create Skip operation on ds
  316. ds = ds->Skip(7);
  317. EXPECT_NE(ds, nullptr);
  318. // Create Take operation on ds
  319. ds = ds->Take(1);
  320. EXPECT_NE(ds, nullptr);
  321. // Create a vocab from vector
  322. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  323. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  324. EXPECT_EQ(s, Status::OK());
  325. // Create BertTokenizer operation on ds
  326. std::shared_ptr<TensorTransform> bert_tokenizer =
  327. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "[UNK]", false, true);
  328. EXPECT_NE(bert_tokenizer, nullptr);
  329. // Create Map operation on ds
  330. ds = ds->Map({bert_tokenizer}, {"text"});
  331. EXPECT_NE(ds, nullptr);
  332. // Create an iterator over the result of the above dataset
  333. // This will trigger the creation of the Execution Tree and launch it.
  334. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  335. EXPECT_NE(iter, nullptr);
  336. // Iterate the dataset and get each row
  337. std::unordered_map<std::string, mindspore::MSTensor> row;
  338. iter->GetNextRow(&row);
  339. // std::vector<std::string> expected = {"[UNK]", " ", "[CLS]"};
  340. uint64_t i = 0;
  341. while (row.size() != 0) {
  342. // auto ind = row["text"];
  343. // mindspore::MSTensor expected_tensor;
  344. // Tensor::CreateFromVector(expected, &expected_tensor);
  345. // EXPECT_EQ(*ind, *expected_tensor);
  346. iter->GetNextRow(&row);
  347. i++;
  348. }
  349. EXPECT_EQ(i, 1);
  350. // Manually terminate the pipeline
  351. iter->Stop();
  352. }
  353. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess5) {
  354. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess5.";
  355. // Test BertTokenizer with unknown_token empty and keep_whitespace true
  356. // Create a TextFile dataset
  357. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  358. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  359. EXPECT_NE(ds, nullptr);
  360. // Create Skip operation on ds
  361. ds = ds->Skip(7);
  362. EXPECT_NE(ds, nullptr);
  363. // Create Take operation on ds
  364. ds = ds->Take(1);
  365. EXPECT_NE(ds, nullptr);
  366. // Create a vocab from vector
  367. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  368. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  369. EXPECT_EQ(s, Status::OK());
  370. // Create BertTokenizer operation on ds
  371. std::shared_ptr<TensorTransform> bert_tokenizer =
  372. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "", false, true);
  373. EXPECT_NE(bert_tokenizer, nullptr);
  374. // Create Map operation on ds
  375. ds = ds->Map({bert_tokenizer}, {"text"});
  376. EXPECT_NE(ds, nullptr);
  377. // Create an iterator over the result of the above dataset
  378. // This will trigger the creation of the Execution Tree and launch it.
  379. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  380. EXPECT_NE(iter, nullptr);
  381. // Iterate the dataset and get each row
  382. std::unordered_map<std::string, mindspore::MSTensor> row;
  383. iter->GetNextRow(&row);
  384. // std::vector<std::string> expected = {"unused", " ", "[CLS]"};
  385. uint64_t i = 0;
  386. while (row.size() != 0) {
  387. // auto ind = row["text"];
  388. // mindspore::MSTensor expected_tensor;
  389. // Tensor::CreateFromVector(expected, &expected_tensor);
  390. // EXPECT_EQ(*ind, *expected_tensor);
  391. iter->GetNextRow(&row);
  392. i++;
  393. }
  394. EXPECT_EQ(i, 1);
  395. // Manually terminate the pipeline
  396. iter->Stop();
  397. }
  398. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess6) {
  399. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess6.";
  400. // Test BertTokenizer with preserve_unused_token false, unknown_token empty and keep_whitespace true
  401. // Create a TextFile dataset
  402. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  403. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  404. EXPECT_NE(ds, nullptr);
  405. // Create Skip operation on ds
  406. ds = ds->Skip(7);
  407. EXPECT_NE(ds, nullptr);
  408. // Create Take operation on ds
  409. ds = ds->Take(1);
  410. EXPECT_NE(ds, nullptr);
  411. // Create a vocab from vector
  412. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  413. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  414. EXPECT_EQ(s, Status::OK());
  415. // Create BertTokenizer operation on ds
  416. std::shared_ptr<TensorTransform> bert_tokenizer =
  417. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "", false, true, NormalizeForm::kNone, false);
  418. EXPECT_NE(bert_tokenizer, nullptr);
  419. // Create Map operation on ds
  420. ds = ds->Map({bert_tokenizer}, {"text"});
  421. EXPECT_NE(ds, nullptr);
  422. // Create an iterator over the result of the above dataset
  423. // This will trigger the creation of the Execution Tree and launch it.
  424. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  425. EXPECT_NE(iter, nullptr);
  426. // Iterate the dataset and get each row
  427. std::unordered_map<std::string, mindspore::MSTensor> row;
  428. iter->GetNextRow(&row);
  429. // std::vector<std::string> expected = {"unused", " ", "[", "CLS", "]"};
  430. uint64_t i = 0;
  431. while (row.size() != 0) {
  432. // auto ind = row["text"];
  433. // mindspore::MSTensor expected_tensor;
  434. // Tensor::CreateFromVector(expected, &expected_tensor);
  435. // EXPECT_EQ(*ind, *expected_tensor);
  436. iter->GetNextRow(&row);
  437. i++;
  438. }
  439. EXPECT_EQ(i, 1);
  440. // Manually terminate the pipeline
  441. iter->Stop();
  442. }
  443. TEST_F(MindDataTestPipeline, TestBertTokenizerSuccess7) {
  444. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerSuccess7.";
  445. // Test BertTokenizer with with_offsets true and lower_case true
  446. // Create a TextFile dataset
  447. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  448. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  449. EXPECT_NE(ds, nullptr);
  450. // Create Skip operation on ds
  451. ds = ds->Skip(4);
  452. EXPECT_NE(ds, nullptr);
  453. // Create Take operation on ds
  454. ds = ds->Take(1);
  455. EXPECT_NE(ds, nullptr);
  456. // Create a vocab from vector
  457. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  458. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  459. EXPECT_EQ(s, Status::OK());
  460. // Create BertTokenizer operation on ds
  461. std::shared_ptr<TensorTransform> bert_tokenizer =
  462. std::make_shared<text::BertTokenizer>(vocab, "##", 100, "[UNK]", true, false, NormalizeForm::kNone, true, true);
  463. EXPECT_NE(bert_tokenizer, nullptr);
  464. // Create Map operation on ds
  465. ds = ds->Map({bert_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"});
  466. EXPECT_NE(ds, nullptr);
  467. // Create an iterator over the result of the above dataset
  468. // This will trigger the creation of the Execution Tree and launch it.
  469. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  470. EXPECT_NE(iter, nullptr);
  471. // Iterate the dataset and get each row
  472. std::unordered_map<std::string, mindspore::MSTensor> row;
  473. iter->GetNextRow(&row);
  474. // std::vector<std::string> expected_tokens = {"i", "am", "mak", "##ing", "small", "mistake",
  475. // "##s", "during", "work", "##ing", "hour", "##s"};
  476. // std::vector<uint32_t> expected_offsets_start = {0, 2, 5, 8, 12, 18, 25, 27, 34, 38, 42, 46};
  477. // std::vector<uint32_t> expected_offsets_limit = {1, 4, 8, 11, 17, 25, 26, 33, 38, 41, 46, 47};
  478. uint64_t i = 0;
  479. while (row.size() != 0) {
  480. // auto ind = row["token"];
  481. // mindspore::MSTensor expected_token_tensor;
  482. // Tensor::CreateFromVector(expected_tokens, &expected_token_tensor);
  483. // EXPECT_EQ(*ind, *expected_token_tensor);
  484. // auto start = row["offsets_start"];
  485. // mindspore::MSTensor expected_start_tensor;
  486. // Tensor::CreateFromVector(expected_offsets_start, &expected_start_tensor);
  487. // EXPECT_EQ(*start, *expected_start_tensor);
  488. // auto limit = row["offsets_limit"];
  489. // mindspore::MSTensor expected_limit_tensor;
  490. // Tensor::CreateFromVector(expected_offsets_limit, &expected_limit_tensor);
  491. // EXPECT_EQ(*limit, *expected_limit_tensor);
  492. iter->GetNextRow(&row);
  493. i++;
  494. }
  495. EXPECT_EQ(i, 1);
  496. // Manually terminate the pipeline
  497. iter->Stop();
  498. }
  499. TEST_F(MindDataTestPipeline, TestBertTokenizerFail1) {
  500. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerFail1.";
  501. // Test BertTokenizer with nullptr vocab
  502. // Create a TextFile dataset
  503. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  504. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  505. EXPECT_NE(ds, nullptr);
  506. // Create BertTokenizer operation on ds
  507. std::shared_ptr<TensorTransform> bert_tokenizer = std::make_shared<text::BertTokenizer>(nullptr);
  508. EXPECT_NE(bert_tokenizer, nullptr);
  509. // Create a Map operation on ds
  510. ds = ds->Map({bert_tokenizer});
  511. EXPECT_NE(ds, nullptr);
  512. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  513. // Expect failure: invalid BertTokenizer input with nullptr vocab
  514. EXPECT_EQ(iter, nullptr);
  515. }
  516. TEST_F(MindDataTestPipeline, TestBertTokenizerFail2) {
  517. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBertTokenizerFail2.";
  518. // Test BertTokenizer with negative max_bytes_per_token
  519. // Create a TextFile dataset
  520. std::string data_file = datasets_root_path_ + "/testTokenizerData/bert_tokenizer.txt";
  521. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  522. EXPECT_NE(ds, nullptr);
  523. // Create a vocab from vector
  524. std::shared_ptr<Vocab> vocab = std::make_shared<Vocab>();
  525. Status s = Vocab::BuildFromVector(list, {}, true, &vocab);
  526. EXPECT_EQ(s, Status::OK());
  527. // Create BertTokenizer operation on ds
  528. std::shared_ptr<TensorTransform> bert_tokenizer = std::make_shared<text::BertTokenizer>(vocab, "##", -1);
  529. EXPECT_NE(bert_tokenizer, nullptr);
  530. // Create a Map operation on ds
  531. ds = ds->Map({bert_tokenizer});
  532. EXPECT_NE(ds, nullptr);
  533. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  534. // Expect failure: invalid BertTokenizer input with nullptr vocab
  535. EXPECT_EQ(iter, nullptr);
  536. }
  537. TEST_F(MindDataTestPipeline, TestCaseFoldSuccess) {
  538. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCaseFoldSuccess.";
  539. // Create a TextFile dataset
  540. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  541. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  542. EXPECT_NE(ds, nullptr);
  543. // Create casefold operation on ds
  544. std::shared_ptr<TensorTransform> casefold = std::make_shared<text::CaseFold>();
  545. EXPECT_NE(casefold, nullptr);
  546. // Create Map operation on ds
  547. ds = ds->Map({casefold}, {"text"});
  548. EXPECT_NE(ds, nullptr);
  549. // Create an iterator over the result of the above dataset
  550. // This will trigger the creation of the Execution Tree and launch it.
  551. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  552. EXPECT_NE(iter, nullptr);
  553. // Iterate the dataset and get each row
  554. std::unordered_map<std::string, mindspore::MSTensor> row;
  555. iter->GetNextRow(&row);
  556. // std::vector<std::string> expected = {"welcome to beijing!", "北京欢迎您!", "我喜欢english!", " "};
  557. uint64_t i = 0;
  558. while (row.size() != 0) {
  559. // auto ind = row["text"];
  560. // mindspore::MSTensor expected_tensor;
  561. // Tensor::CreateScalar(expected[i], &expected_tensor);
  562. // EXPECT_EQ(*ind, *expected_tensor);
  563. iter->GetNextRow(&row);
  564. i++;
  565. }
  566. EXPECT_EQ(i, 4);
  567. // Manually terminate the pipeline
  568. iter->Stop();
  569. }
  570. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess) {
  571. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kMp and the with_offsets is false.
  572. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess.";
  573. // Create a TextFile dataset
  574. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  575. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  576. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  577. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  578. EXPECT_NE(ds, nullptr);
  579. // Create jieba_tokenizer operation on ds
  580. std::shared_ptr<TensorTransform> jieba_tokenizer =
  581. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  582. EXPECT_NE(jieba_tokenizer, nullptr);
  583. // Create Map operation on ds
  584. ds = ds->Map({jieba_tokenizer}, {"text"});
  585. EXPECT_NE(ds, nullptr);
  586. // Create an iterator over the result of the above dataset
  587. // This will trigger the creation of the Execution Tree and launch it.
  588. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  589. EXPECT_NE(iter, nullptr);
  590. // Iterate the dataset and get each row
  591. std::unordered_map<std::string, mindspore::MSTensor> row;
  592. iter->GetNextRow(&row);
  593. // std::vector<std::string> expected = {"今天天气", "太好了", "我们", "一起", "去", "外面", "玩吧"};
  594. uint64_t i = 0;
  595. while (row.size() != 0) {
  596. // auto ind = row["text"];
  597. // mindspore::MSTensor expected_tensor;
  598. // Tensor::CreateFromVector(expected, &expected_tensor);
  599. // EXPECT_EQ(*ind, *expected_tensor);
  600. iter->GetNextRow(&row);
  601. i++;
  602. }
  603. EXPECT_EQ(i, 1);
  604. // Manually terminate the pipeline
  605. iter->Stop();
  606. }
  607. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess1) {
  608. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kHmm and the with_offsets is false.
  609. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess1.";
  610. // Create a TextFile dataset
  611. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  612. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  613. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  614. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  615. EXPECT_NE(ds, nullptr);
  616. // Create jieba_tokenizer operation on ds
  617. std::shared_ptr<TensorTransform> jieba_tokenizer =
  618. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kHmm);
  619. EXPECT_NE(jieba_tokenizer, nullptr);
  620. // Create Map operation on ds
  621. ds = ds->Map({jieba_tokenizer}, {"text"});
  622. EXPECT_NE(ds, nullptr);
  623. // Create an iterator over the result of the above dataset
  624. // This will trigger the creation of the Execution Tree and launch it.
  625. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  626. EXPECT_NE(iter, nullptr);
  627. // Iterate the dataset and get each row
  628. std::unordered_map<std::string, mindspore::MSTensor> row;
  629. iter->GetNextRow(&row);
  630. // std::vector<std::string> expected = {"今天", "天气", "太", "好", "了", "我们", "一起", "去", "外面", "玩", "吧"};
  631. uint64_t i = 0;
  632. while (row.size() != 0) {
  633. // auto ind = row["text"];
  634. // mindspore::MSTensor expected_tensor;
  635. // Tensor::CreateFromVector(expected, &expected_tensor);
  636. // EXPECT_EQ(*ind, *expected_tensor);
  637. iter->GetNextRow(&row);
  638. i++;
  639. }
  640. EXPECT_EQ(i, 1);
  641. // Manually terminate the pipeline
  642. iter->Stop();
  643. }
  644. TEST_F(MindDataTestPipeline, TestJiebaTokenizerSuccess2) {
  645. // Testing the parameter of JiebaTokenizer interface when the mode is JiebaMode::kMp and the with_offsets is true.
  646. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerSuccess2.";
  647. // Create a TextFile dataset
  648. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  649. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  650. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  651. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  652. EXPECT_NE(ds, nullptr);
  653. // Create jieba_tokenizer operation on ds
  654. std::shared_ptr<TensorTransform> jieba_tokenizer =
  655. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp, true);
  656. EXPECT_NE(jieba_tokenizer, nullptr);
  657. // Create Map operation on ds
  658. ds = ds->Map({jieba_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  659. {"token", "offsets_start", "offsets_limit"});
  660. EXPECT_NE(ds, nullptr);
  661. // Create an iterator over the result of the above dataset
  662. // This will trigger the creation of the Execution Tree and launch it.
  663. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  664. EXPECT_NE(iter, nullptr);
  665. // Iterate the dataset and get each row
  666. std::unordered_map<std::string, mindspore::MSTensor> row;
  667. iter->GetNextRow(&row);
  668. // std::vector<std::string> expected = {"今天天气", "太好了", "我们", "一起", "去", "外面", "玩吧"};
  669. // std::vector<uint32_t> expected_offsets_start = {0, 12, 21, 27, 33, 36, 42};
  670. // std::vector<uint32_t> expected_offsets_limit = {12, 21, 27, 33, 36, 42, 48};
  671. uint64_t i = 0;
  672. while (row.size() != 0) {
  673. // auto ind = row["offsets_start"];
  674. // auto ind1 = row["offsets_limit"];
  675. // auto token = row["token"];
  676. // mindspore::MSTensor expected_tensor;
  677. // mindspore::MSTensor expected_tensor_offsets_start;
  678. // mindspore::MSTensor expected_tensor_offsets_limit;
  679. // Tensor::CreateFromVector(expected, &expected_tensor);
  680. // Tensor::CreateFromVector(expected_offsets_start, &expected_tensor_offsets_start);
  681. // Tensor::CreateFromVector(expected_offsets_limit, &expected_tensor_offsets_limit);
  682. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  683. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  684. // EXPECT_EQ(*token, *expected_tensor);
  685. iter->GetNextRow(&row);
  686. i++;
  687. }
  688. EXPECT_EQ(i, 1);
  689. // Manually terminate the pipeline
  690. iter->Stop();
  691. }
  692. TEST_F(MindDataTestPipeline, TestJiebaTokenizerFail1) {
  693. // Testing the incorrect parameter of JiebaTokenizer interface.
  694. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerFail1.";
  695. // Create a TextFile dataset
  696. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  697. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  698. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  699. EXPECT_NE(ds, nullptr);
  700. // Create jieba_tokenizer operation on ds
  701. // Testing the parameter hmm_path is empty
  702. std::shared_ptr<TensorTransform> jieba_tokenizer =
  703. std::make_shared<text::JiebaTokenizer>("", mp_path, JiebaMode::kMp);
  704. EXPECT_NE(jieba_tokenizer, nullptr);
  705. // Create a Map operation on ds
  706. ds = ds->Map({jieba_tokenizer});
  707. EXPECT_NE(ds, nullptr);
  708. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  709. // Expect failure: invalid JiebaTokenizer input (parameter hmm_path is empty)
  710. EXPECT_EQ(iter, nullptr);
  711. }
  712. TEST_F(MindDataTestPipeline, TestJiebaTokenizerFail2) {
  713. // Testing the incorrect parameter of JiebaTokenizer interface.
  714. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerFail2.";
  715. // Create a TextFile dataset
  716. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  717. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  718. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  719. EXPECT_NE(ds, nullptr);
  720. // Create jieba_tokenizer operation on ds
  721. // Testing the parameter mp_path is empty
  722. std::shared_ptr<TensorTransform> jieba_tokenizer =
  723. std::make_shared<text::JiebaTokenizer>(hmm_path, "", JiebaMode::kMp);
  724. EXPECT_NE(jieba_tokenizer, nullptr);
  725. // Create a Map operation on ds
  726. ds = ds->Map({jieba_tokenizer});
  727. EXPECT_NE(ds, nullptr);
  728. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  729. // Expect failure: invalid JiebaTokenizer input (parameter mp_path is empty)
  730. EXPECT_EQ(iter, nullptr);
  731. }
  732. TEST_F(MindDataTestPipeline, TestJiebaTokenizerFail3) {
  733. // Testing the incorrect parameter of JiebaTokenizer interface.
  734. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerFail3.";
  735. // Create a TextFile dataset
  736. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  737. std::string hmm_path_invalid = datasets_root_path_ + "/jiebadict/1.txt";
  738. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  739. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  740. EXPECT_NE(ds, nullptr);
  741. // Create jieba_tokenizer operation on ds
  742. // Testing the parameter hmm_path is invalid path
  743. std::shared_ptr<TensorTransform> jieba_tokenizer =
  744. std::make_shared<text::JiebaTokenizer>(hmm_path_invalid, mp_path, JiebaMode::kMp);
  745. EXPECT_NE(jieba_tokenizer, nullptr);
  746. // Create a Map operation on ds
  747. ds = ds->Map({jieba_tokenizer});
  748. EXPECT_NE(ds, nullptr);
  749. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  750. // Expect failure: invalid JiebaTokenizer input (parameter hmm_path is invalid path)
  751. EXPECT_EQ(iter, nullptr);
  752. }
  753. TEST_F(MindDataTestPipeline, TestJiebaTokenizerFail4) {
  754. // Testing the incorrect parameter of JiebaTokenizer interface.
  755. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerFail4.";
  756. // Create a TextFile dataset
  757. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  758. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  759. std::string mp_path_invalid = datasets_root_path_ + "/jiebadict/1.txt";
  760. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  761. EXPECT_NE(ds, nullptr);
  762. // Create jieba_tokenizer operation on ds
  763. // Testing the parameter mp_path is invalid path
  764. std::shared_ptr<TensorTransform> jieba_tokenizer =
  765. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path_invalid, JiebaMode::kMp);
  766. EXPECT_NE(jieba_tokenizer, nullptr);
  767. // Create a Map operation on ds
  768. ds = ds->Map({jieba_tokenizer});
  769. EXPECT_NE(ds, nullptr);
  770. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  771. // Expect failure: invalid JiebaTokenizer input (parameter mp_path is invalid path)
  772. EXPECT_EQ(iter, nullptr);
  773. }
  774. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord) {
  775. // Testing the parameter AddWord of JiebaTokenizer when the freq is not provided (default 0).
  776. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord.";
  777. // Create a TextFile dataset
  778. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  779. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  780. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  781. std::shared_ptr<Dataset> ds = TextFile({data_file});
  782. EXPECT_NE(ds, nullptr);
  783. // Create jieba_tokenizer operation on ds
  784. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer =
  785. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  786. EXPECT_NE(jieba_tokenizer, nullptr);
  787. // Add word with freq not provided (default 0)
  788. jieba_tokenizer->AddWord("男默女泪");
  789. // Create Map operation on ds
  790. ds = ds->Map({jieba_tokenizer}, {"text"});
  791. EXPECT_NE(ds, nullptr);
  792. // Create an iterator over the result of the above dataset
  793. // This will trigger the creation of the Execution Tree and launch it.
  794. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  795. EXPECT_NE(iter, nullptr);
  796. // Iterate the dataset and get each row
  797. std::unordered_map<std::string, mindspore::MSTensor> row;
  798. iter->GetNextRow(&row);
  799. // std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  800. uint64_t i = 0;
  801. while (row.size() != 0) {
  802. // auto ind = row["text"];
  803. // mindspore::MSTensor expected_tensor;
  804. // Tensor::CreateFromVector(expected, &expected_tensor);
  805. // EXPECT_EQ(*ind, *expected_tensor);
  806. iter->GetNextRow(&row);
  807. i++;
  808. }
  809. EXPECT_EQ(i, 1);
  810. // Manually terminate the pipeline
  811. iter->Stop();
  812. }
  813. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord1) {
  814. // Testing the parameter AddWord of JiebaTokenizer when the freq is set explicitly to 0.
  815. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord1.";
  816. // Create a TextFile dataset
  817. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  818. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  819. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  820. std::shared_ptr<Dataset> ds = TextFile({data_file});
  821. EXPECT_NE(ds, nullptr);
  822. // Create jieba_tokenizer operation on ds
  823. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer =
  824. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  825. EXPECT_NE(jieba_tokenizer, nullptr);
  826. // Add word with freq is set explicitly to 0
  827. jieba_tokenizer->AddWord("男默女泪", 0);
  828. // Create Map operation on ds
  829. ds = ds->Map({jieba_tokenizer}, {"text"});
  830. EXPECT_NE(ds, nullptr);
  831. // Create an iterator over the result of the above dataset
  832. // This will trigger the creation of the Execution Tree and launch it.
  833. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  834. EXPECT_NE(iter, nullptr);
  835. // Iterate the dataset and get each row
  836. std::unordered_map<std::string, mindspore::MSTensor> row;
  837. iter->GetNextRow(&row);
  838. // std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  839. uint64_t i = 0;
  840. while (row.size() != 0) {
  841. // auto ind = row["text"];
  842. // mindspore::MSTensor expected_tensor;
  843. // Tensor::CreateFromVector(expected, &expected_tensor);
  844. // EXPECT_EQ(*ind, *expected_tensor);
  845. iter->GetNextRow(&row);
  846. i++;
  847. }
  848. EXPECT_EQ(i, 1);
  849. // Manually terminate the pipeline
  850. iter->Stop();
  851. }
  852. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord2) {
  853. // Testing the parameter AddWord of JiebaTokenizer when the freq is 10.
  854. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord2.";
  855. // Create a TextFile dataset
  856. std::string data_file = datasets_root_path_ + "/testJiebaDataset/4.txt";
  857. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  858. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  859. std::shared_ptr<Dataset> ds = TextFile({data_file});
  860. EXPECT_NE(ds, nullptr);
  861. // Create jieba_tokenizer operation on ds
  862. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer =
  863. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  864. EXPECT_NE(jieba_tokenizer, nullptr);
  865. // Add word with freq 10
  866. jieba_tokenizer->AddWord("男默女泪", 10);
  867. // Create Map operation on ds
  868. ds = ds->Map({jieba_tokenizer}, {"text"});
  869. EXPECT_NE(ds, nullptr);
  870. // Create an iterator over the result of the above dataset
  871. // This will trigger the creation of the Execution Tree and launch it.
  872. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  873. EXPECT_NE(iter, nullptr);
  874. // Iterate the dataset and get each row
  875. std::unordered_map<std::string, mindspore::MSTensor> row;
  876. iter->GetNextRow(&row);
  877. // std::vector<std::string> expected = {"男默女泪", "市", "长江大桥"};
  878. uint64_t i = 0;
  879. while (row.size() != 0) {
  880. // auto ind = row["text"];
  881. // mindspore::MSTensor expected_tensor;
  882. // Tensor::CreateFromVector(expected, &expected_tensor);
  883. // EXPECT_EQ(*ind, *expected_tensor);
  884. iter->GetNextRow(&row);
  885. i++;
  886. }
  887. EXPECT_EQ(i, 1);
  888. // Manually terminate the pipeline
  889. iter->Stop();
  890. }
  891. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWord3) {
  892. // Testing the parameter AddWord of JiebaTokenizer when the freq is 20000 which affects the result of segmentation.
  893. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWord3.";
  894. // Create a TextFile dataset
  895. std::string data_file = datasets_root_path_ + "/testJiebaDataset/6.txt";
  896. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  897. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  898. std::shared_ptr<Dataset> ds = TextFile({data_file});
  899. EXPECT_NE(ds, nullptr);
  900. // Create jieba_tokenizer operation on ds
  901. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer =
  902. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  903. EXPECT_NE(jieba_tokenizer, nullptr);
  904. // Add word with freq 20000
  905. jieba_tokenizer->AddWord("江大桥", 20000);
  906. // Create Map operation on ds
  907. ds = ds->Map({jieba_tokenizer}, {"text"});
  908. EXPECT_NE(ds, nullptr);
  909. // Create an iterator over the result of the above dataset
  910. // This will trigger the creation of the Execution Tree and launch it.
  911. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  912. EXPECT_NE(iter, nullptr);
  913. // Iterate the dataset and get each row
  914. std::unordered_map<std::string, mindspore::MSTensor> row;
  915. iter->GetNextRow(&row);
  916. // std::vector<std::string> expected = {"江州", "市长", "江大桥", "参加", "了", "长江大桥", "的", "通车", "仪式"};
  917. uint64_t i = 0;
  918. while (row.size() != 0) {
  919. // auto ind = row["text"];
  920. // mindspore::MSTensor expected_tensor;
  921. // Tensor::CreateFromVector(expected, &expected_tensor);
  922. // EXPECT_EQ(*ind, *expected_tensor);
  923. iter->GetNextRow(&row);
  924. i++;
  925. }
  926. EXPECT_EQ(i, 1);
  927. // Manually terminate the pipeline
  928. iter->Stop();
  929. }
  930. TEST_F(MindDataTestPipeline, TestJiebaTokenizerAddWordFail) {
  931. // Testing the incorrect parameter of AddWord in JiebaTokenizer.
  932. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestJiebaTokenizerAddWordFail.";
  933. // Create a TextFile dataset
  934. std::string data_file = datasets_root_path_ + "/testJiebaDataset/3.txt";
  935. std::string hmm_path = datasets_root_path_ + "/jiebadict/hmm_model.utf8";
  936. std::string mp_path = datasets_root_path_ + "/jiebadict/jieba.dict.utf8";
  937. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  938. EXPECT_NE(ds, nullptr);
  939. // Testing the parameter word of AddWord is empty
  940. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer =
  941. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  942. EXPECT_NE(jieba_tokenizer, nullptr);
  943. EXPECT_NE(jieba_tokenizer->AddWord("", 10), Status::OK());
  944. // Testing the parameter freq of AddWord is negative
  945. std::shared_ptr<text::JiebaTokenizer> jieba_tokenizer1 =
  946. std::make_shared<text::JiebaTokenizer>(hmm_path, mp_path, JiebaMode::kMp);
  947. EXPECT_NE(jieba_tokenizer1, nullptr);
  948. EXPECT_NE(jieba_tokenizer1->AddWord("我们", -1), Status::OK());
  949. }
  950. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess) {
  951. // Testing the parameter of SlidingWindow interface when the axis is 0.
  952. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess.";
  953. // Create a TextFile dataset
  954. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  955. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  956. EXPECT_NE(ds, nullptr);
  957. // Create white_tokenizer operation on ds
  958. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>();
  959. EXPECT_NE(white_tokenizer, nullptr);
  960. // Create sliding_window operation on ds
  961. std::shared_ptr<TensorTransform> sliding_window = std::make_shared<text::SlidingWindow>(3, 0);
  962. EXPECT_NE(sliding_window, nullptr);
  963. // Create Map operation on ds
  964. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  965. EXPECT_NE(ds, nullptr);
  966. // Create an iterator over the result of the above dataset
  967. // This will trigger the creation of the Execution Tree and launch it.
  968. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  969. EXPECT_NE(iter, nullptr);
  970. // Iterate the dataset and get each row
  971. std::unordered_map<std::string, mindspore::MSTensor> row;
  972. iter->GetNextRow(&row);
  973. // std::vector<std::vector<std::string>> expected = {{"This", "is", "a", "is", "a", "text", "a", "text", "file."},
  974. // {"Be", "happy", "every", "happy", "every", "day."},
  975. // {"Good", "luck", "to", "luck", "to", "everyone."}};
  976. uint64_t i = 0;
  977. while (row.size() != 0) {
  978. // auto ind = row["text"];
  979. // mindspore::MSTensor expected_tensor;
  980. // int x = expected[i].size() / 3;
  981. // Tensor::CreateFromVector(expected[i], TensorShape({x, 3}), &expected_tensor);
  982. // EXPECT_EQ(*ind, *expected_tensor);
  983. iter->GetNextRow(&row);
  984. i++;
  985. }
  986. EXPECT_EQ(i, 3);
  987. // Manually terminate the pipeline
  988. iter->Stop();
  989. }
  990. TEST_F(MindDataTestPipeline, TestSlidingWindowSuccess1) {
  991. // Testing the parameter of SlidingWindow interface when the axis is -1.
  992. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowSuccess1.";
  993. // Create a TextFile dataset
  994. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  995. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  996. EXPECT_NE(ds, nullptr);
  997. // Create white_tokenizer operation on ds
  998. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>();
  999. EXPECT_NE(white_tokenizer, nullptr);
  1000. // Create sliding_window operation on ds
  1001. std::shared_ptr<TensorTransform> sliding_window = std::make_shared<text::SlidingWindow>(2, -1);
  1002. EXPECT_NE(sliding_window, nullptr);
  1003. // Create Map operation on ds
  1004. ds = ds->Map({white_tokenizer, sliding_window}, {"text"});
  1005. EXPECT_NE(ds, nullptr);
  1006. // Create an iterator over the result of the above dataset
  1007. // This will trigger the creation of the Execution Tree and launch it.
  1008. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1009. EXPECT_NE(iter, nullptr);
  1010. // Iterate the dataset and get each row
  1011. std::unordered_map<std::string, mindspore::MSTensor> row;
  1012. iter->GetNextRow(&row);
  1013. // std::vector<std::vector<std::string>> expected = {{"This", "is", "is", "a", "a", "text", "text", "file."},
  1014. // {"Be", "happy", "happy", "every", "every", "day."},
  1015. // {"Good", "luck", "luck", "to", "to", "everyone."}};
  1016. uint64_t i = 0;
  1017. while (row.size() != 0) {
  1018. // auto ind = row["text"];
  1019. // mindspore::MSTensor expected_tensor;
  1020. // int x = expected[i].size() / 2;
  1021. // Tensor::CreateFromVector(expected[i], TensorShape({x, 2}), &expected_tensor);
  1022. // EXPECT_EQ(*ind, *expected_tensor);
  1023. iter->GetNextRow(&row);
  1024. i++;
  1025. }
  1026. EXPECT_EQ(i, 3);
  1027. // Manually terminate the pipeline
  1028. iter->Stop();
  1029. }
  1030. TEST_F(MindDataTestPipeline, TestSlidingWindowFail1) {
  1031. // Testing the incorrect parameter of SlidingWindow interface.
  1032. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowFail1.";
  1033. // Create a TextFile dataset
  1034. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1035. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1036. EXPECT_NE(ds, nullptr);
  1037. // Create sliding_window operation on ds
  1038. // Testing the parameter width less than or equal to 0
  1039. // The parameter axis support 0 or -1 only for now
  1040. std::shared_ptr<TensorTransform> sliding_window = std::make_shared<text::SlidingWindow>(0, 0);
  1041. EXPECT_NE(sliding_window, nullptr);
  1042. // Create a Map operation on ds
  1043. ds = ds->Map({sliding_window});
  1044. EXPECT_NE(ds, nullptr);
  1045. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1046. // Expect failure: invalid SlidingWindow input (width less than or equal to 0)
  1047. EXPECT_EQ(iter, nullptr);
  1048. }
  1049. TEST_F(MindDataTestPipeline, TestSlidingWindowFail2) {
  1050. // Testing the incorrect parameter of SlidingWindow interface.
  1051. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSlidingWindowFail2.";
  1052. // Create a TextFile dataset
  1053. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1054. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1055. EXPECT_NE(ds, nullptr);
  1056. // Create sliding_window operation on ds
  1057. // Testing the parameter width less than or equal to 0
  1058. // The parameter axis support 0 or -1 only for now
  1059. std::shared_ptr<TensorTransform> sliding_window = std::make_shared<text::SlidingWindow>(-2, 0);
  1060. EXPECT_NE(sliding_window, nullptr);
  1061. // Create a Map operation on ds
  1062. ds = ds->Map({sliding_window});
  1063. EXPECT_NE(ds, nullptr);
  1064. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1065. // Expect failure: invalid SlidingWindow input (width less than or equal to 0)
  1066. EXPECT_EQ(iter, nullptr);
  1067. }
  1068. TEST_F(MindDataTestPipeline, TestToNumberSuccess1) {
  1069. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess1.";
  1070. // Test ToNumber with integer numbers
  1071. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1072. // Create a TextFile dataset
  1073. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1074. EXPECT_NE(ds, nullptr);
  1075. // Create a Take operation on ds
  1076. ds = ds->Take(8);
  1077. EXPECT_NE(ds, nullptr);
  1078. // Create ToNumber operation on ds
  1079. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("int64");
  1080. EXPECT_NE(to_number, nullptr);
  1081. // Create a Map operation on ds
  1082. ds = ds->Map({to_number}, {"text"});
  1083. EXPECT_NE(ds, nullptr);
  1084. // Create an iterator over the result of the above dataset
  1085. // This will trigger the creation of the Execution Tree and launch it.
  1086. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1087. EXPECT_NE(iter, nullptr);
  1088. // Iterate the dataset and get each row
  1089. std::unordered_map<std::string, mindspore::MSTensor> row;
  1090. iter->GetNextRow(&row);
  1091. // std::vector<int64_t> expected = {-121, 14, -2219, 7623, -8162536, 162371864, -1726483716, 98921728421};
  1092. uint64_t i = 0;
  1093. while (row.size() != 0) {
  1094. // auto ind = row["text"];
  1095. // mindspore::MSTensor expected_tensor;
  1096. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1097. // EXPECT_EQ(*ind, *expected_tensor);
  1098. iter->GetNextRow(&row);
  1099. i++;
  1100. }
  1101. EXPECT_EQ(i, 8);
  1102. // Manually terminate the pipeline
  1103. iter->Stop();
  1104. }
  1105. TEST_F(MindDataTestPipeline, TestToNumberSuccess2) {
  1106. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberSuccess2.";
  1107. // Test ToNumber with float numbers
  1108. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1109. // Create a TextFile dataset
  1110. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1111. EXPECT_NE(ds, nullptr);
  1112. // Create a Skip operation on ds
  1113. ds = ds->Skip(8);
  1114. EXPECT_NE(ds, nullptr);
  1115. // Create a Take operation on ds
  1116. ds = ds->Take(6);
  1117. EXPECT_NE(ds, nullptr);
  1118. // Create ToNumber operation on ds
  1119. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("float64");
  1120. EXPECT_NE(to_number, nullptr);
  1121. // Create a Map operation on ds
  1122. ds = ds->Map({to_number}, {"text"});
  1123. EXPECT_NE(ds, nullptr);
  1124. // Create an iterator over the result of the above dataset
  1125. // This will trigger the creation of the Execution Tree and launch it.
  1126. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1127. EXPECT_NE(iter, nullptr);
  1128. // Iterate the dataset and get each row
  1129. std::unordered_map<std::string, mindspore::MSTensor> row;
  1130. iter->GetNextRow(&row);
  1131. // std::vector<double_t> expected = {-1.1, 1.4, -2219.321, 7623.453, -816256.234282, 162371864.243243};
  1132. uint64_t i = 0;
  1133. while (row.size() != 0) {
  1134. // auto ind = row["text"];
  1135. // mindspore::MSTensor expected_tensor;
  1136. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1137. // EXPECT_EQ(*ind, *expected_tensor);
  1138. iter->GetNextRow(&row);
  1139. i++;
  1140. }
  1141. EXPECT_EQ(i, 6);
  1142. // Manually terminate the pipeline
  1143. iter->Stop();
  1144. }
  1145. TEST_F(MindDataTestPipeline, TestToNumberFail1) {
  1146. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail1.";
  1147. // Test ToNumber with overflow integer numbers
  1148. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1149. // Create a TextFile dataset
  1150. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1151. EXPECT_NE(ds, nullptr);
  1152. // Create a Skip operation on ds
  1153. ds = ds->Skip(2);
  1154. EXPECT_NE(ds, nullptr);
  1155. // Create a Take operation on ds
  1156. ds = ds->Take(6);
  1157. EXPECT_NE(ds, nullptr);
  1158. // Create ToNumber operation on ds
  1159. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("int8");
  1160. EXPECT_NE(to_number, nullptr);
  1161. // Create a Map operation on ds
  1162. ds = ds->Map({to_number}, {"text"});
  1163. EXPECT_NE(ds, nullptr);
  1164. // Create an iterator over the result of the above dataset
  1165. // This will trigger the creation of the Execution Tree and launch it.
  1166. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1167. EXPECT_NE(iter, nullptr);
  1168. // Iterate the dataset and get each row
  1169. std::unordered_map<std::string, mindspore::MSTensor> row;
  1170. // Expect error: input out of bounds of int8
  1171. iter->GetNextRow(&row);
  1172. uint64_t i = 0;
  1173. while (row.size() != 0) {
  1174. iter->GetNextRow(&row);
  1175. i++;
  1176. }
  1177. // Expect failure: GetNextRow fail and return nothing
  1178. EXPECT_EQ(i, 0);
  1179. // Manually terminate the pipeline
  1180. iter->Stop();
  1181. }
  1182. TEST_F(MindDataTestPipeline, TestToNumberFail2) {
  1183. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail2.";
  1184. // Test ToNumber with overflow float numbers
  1185. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1186. // Create a TextFile dataset
  1187. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1188. EXPECT_NE(ds, nullptr);
  1189. // Create a Skip operation on ds
  1190. ds = ds->Skip(12);
  1191. EXPECT_NE(ds, nullptr);
  1192. // Create a Take operation on ds
  1193. ds = ds->Take(2);
  1194. EXPECT_NE(ds, nullptr);
  1195. // Create ToNumber operation on ds
  1196. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("float16");
  1197. EXPECT_NE(to_number, nullptr);
  1198. // Create a Map operation on ds
  1199. ds = ds->Map({to_number}, {"text"});
  1200. EXPECT_NE(ds, nullptr);
  1201. // Create an iterator over the result of the above dataset
  1202. // This will trigger the creation of the Execution Tree and launch it.
  1203. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1204. EXPECT_NE(iter, nullptr);
  1205. // Iterate the dataset and get each row
  1206. std::unordered_map<std::string, mindspore::MSTensor> row;
  1207. // Expect error: input out of bounds of float16
  1208. iter->GetNextRow(&row);
  1209. uint64_t i = 0;
  1210. while (row.size() != 0) {
  1211. iter->GetNextRow(&row);
  1212. i++;
  1213. }
  1214. // Expect failure: GetNextRow fail and return nothing
  1215. EXPECT_EQ(i, 0);
  1216. // Manually terminate the pipeline
  1217. iter->Stop();
  1218. }
  1219. TEST_F(MindDataTestPipeline, TestToNumberFail3) {
  1220. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail3.";
  1221. // Test ToNumber with non numerical input
  1222. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1223. // Create a TextFile dataset
  1224. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1225. EXPECT_NE(ds, nullptr);
  1226. // Create a Skip operation on ds
  1227. ds = ds->Skip(14);
  1228. EXPECT_NE(ds, nullptr);
  1229. // Create ToNumber operation on ds
  1230. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("int64");
  1231. EXPECT_NE(to_number, nullptr);
  1232. // Create a Map operation on ds
  1233. ds = ds->Map({to_number}, {"text"});
  1234. EXPECT_NE(ds, nullptr);
  1235. // Create an iterator over the result of the above dataset
  1236. // This will trigger the creation of the Execution Tree and launch it.
  1237. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1238. EXPECT_NE(iter, nullptr);
  1239. // Iterate the dataset and get each row
  1240. std::unordered_map<std::string, mindspore::MSTensor> row;
  1241. // Expect error: invalid input which is non numerical
  1242. iter->GetNextRow(&row);
  1243. uint64_t i = 0;
  1244. while (row.size() != 0) {
  1245. iter->GetNextRow(&row);
  1246. i++;
  1247. }
  1248. // Expect failure: GetNextRow fail and return nothing
  1249. EXPECT_EQ(i, 0);
  1250. // Manually terminate the pipeline
  1251. iter->Stop();
  1252. }
  1253. TEST_F(MindDataTestPipeline, TestToNumberFail4) {
  1254. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail4.";
  1255. // Test ToNumber with non numerical data type
  1256. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1257. // Create a TextFile dataset
  1258. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1259. EXPECT_NE(ds, nullptr);
  1260. // Create ToNumber operation on ds
  1261. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("string");
  1262. EXPECT_NE(to_number, nullptr);
  1263. // Create a Map operation on ds
  1264. ds = ds->Map({to_number}, {"text"});
  1265. EXPECT_NE(ds, nullptr);
  1266. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1267. // Expect failure: invalid parameter with non numerical data type
  1268. EXPECT_EQ(iter, nullptr);
  1269. }
  1270. TEST_F(MindDataTestPipeline, TestToNumberFail5) {
  1271. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestToNumberFail5.";
  1272. // Test ToNumber with non numerical data type
  1273. std::string data_file = datasets_root_path_ + "/testTokenizerData/to_number.txt";
  1274. // Create a TextFile dataset
  1275. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1276. EXPECT_NE(ds, nullptr);
  1277. // Create ToNumber operation on ds
  1278. std::shared_ptr<TensorTransform> to_number = std::make_shared<text::ToNumber>("bool");
  1279. EXPECT_NE(to_number, nullptr);
  1280. // Create a Map operation on ds
  1281. ds = ds->Map({to_number}, {"text"});
  1282. EXPECT_NE(ds, nullptr);
  1283. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1284. // Expect failure: invalid parameter with non numerical data type
  1285. EXPECT_EQ(iter, nullptr);
  1286. }
  1287. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess1) {
  1288. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess1.";
  1289. // Testing basic TruncateSequencePair
  1290. // Set seed for RandomDataset
  1291. auto original_seed = config::get_seed();
  1292. bool status_set_seed = config::set_seed(0);
  1293. EXPECT_EQ(status_set_seed, true);
  1294. // Set num_parallel_workers for RandomDataset
  1295. auto original_worker = config::get_num_parallel_workers();
  1296. bool status_set_worker = config::set_num_parallel_workers(1);
  1297. EXPECT_EQ(status_set_worker, true);
  1298. // Create a RandomDataset which has column names "col1" and "col2"
  1299. std::shared_ptr<SchemaObj> schema = Schema();
  1300. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt16, {5});
  1301. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt32, {3});
  1302. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1303. EXPECT_NE(ds, nullptr);
  1304. // Create a truncate_sequence_pair operation on ds
  1305. std::shared_ptr<TensorTransform> truncate_sequence_pair = std::make_shared<text::TruncateSequencePair>(4);
  1306. EXPECT_NE(truncate_sequence_pair, nullptr);
  1307. // Create Map operation on ds
  1308. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1309. EXPECT_NE(ds, nullptr);
  1310. // Create an iterator over the result of the above dataset
  1311. // This will trigger the creation of the Execution Tree and launch it.
  1312. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1313. EXPECT_NE(iter, nullptr);
  1314. // Iterate the dataset and get each row
  1315. std::unordered_map<std::string, mindspore::MSTensor> row;
  1316. iter->GetNextRow(&row);
  1317. // std::vector<std::vector<int16_t>> expected1 = {{-29556, -29556}, {-18505, -18505}, {-25958, -25958}};
  1318. // std::vector<std::vector<int32_t>> expected2 = {
  1319. // {-1751672937, -1751672937}, {-656877352, -656877352}, {-606348325, -606348325}};
  1320. uint64_t i = 0;
  1321. while (row.size() != 0) {
  1322. // auto ind1 = row["col1"];
  1323. // auto ind2 = row["col2"];
  1324. // mindspore::MSTensor expected_tensor1;
  1325. // mindspore::MSTensor expected_tensor2;
  1326. // Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1327. // Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1328. // EXPECT_EQ(*ind1, *expected_tensor1);
  1329. // EXPECT_EQ(*ind2, *expected_tensor2);
  1330. iter->GetNextRow(&row);
  1331. i++;
  1332. }
  1333. EXPECT_EQ(i, 3);
  1334. // Manually terminate the pipeline
  1335. iter->Stop();
  1336. // Restore original seed and num_parallel_workers
  1337. status_set_seed = config::set_seed(original_seed);
  1338. EXPECT_EQ(status_set_seed, true);
  1339. status_set_worker = config::set_num_parallel_workers(original_worker);
  1340. EXPECT_EQ(status_set_worker, true);
  1341. }
  1342. TEST_F(MindDataTestPipeline, TestTruncateSequencePairSuccess2) {
  1343. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairSuccess2.";
  1344. // Testing basic TruncateSequencePair with odd max_length
  1345. // Set seed for RandomDataset
  1346. auto original_seed = config::get_seed();
  1347. bool status_set_seed = config::set_seed(1);
  1348. EXPECT_EQ(status_set_seed, true);
  1349. // Set num_parallel_workers for RandomDataset
  1350. auto original_worker = config::get_num_parallel_workers();
  1351. bool status_set_worker = config::set_num_parallel_workers(1);
  1352. EXPECT_EQ(status_set_worker, true);
  1353. // Create a RandomDataset which has column names "col1" and "col2"
  1354. std::shared_ptr<SchemaObj> schema = Schema();
  1355. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt32, {4});
  1356. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt64, {4});
  1357. std::shared_ptr<Dataset> ds = RandomData(4, schema);
  1358. EXPECT_NE(ds, nullptr);
  1359. // Create a truncate_sequence_pair operation on ds
  1360. std::shared_ptr<TensorTransform> truncate_sequence_pair = std::make_shared<text::TruncateSequencePair>(5);
  1361. EXPECT_NE(truncate_sequence_pair, nullptr);
  1362. // Create Map operation on ds
  1363. ds = ds->Map({truncate_sequence_pair}, {"col1", "col2"});
  1364. EXPECT_NE(ds, nullptr);
  1365. // Create an iterator over the result of the above dataset
  1366. // This will trigger the creation of the Execution Tree and launch it.
  1367. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1368. EXPECT_NE(iter, nullptr);
  1369. // Iterate the dataset and get each row
  1370. std::unordered_map<std::string, mindspore::MSTensor> row;
  1371. iter->GetNextRow(&row);
  1372. // std::vector<std::vector<int32_t>> expected1 = {{1785358954, 1785358954, 1785358954},
  1373. // {-1195853640, -1195853640, -1195853640},
  1374. // {0, 0, 0},
  1375. // {1296911693, 1296911693, 1296911693}};
  1376. // std::vector<std::vector<int64_t>> expected2 = {
  1377. // {-1, -1}, {-1229782938247303442, -1229782938247303442}, {2314885530818453536, 2314885530818453536}, {-1, -1}};
  1378. uint64_t i = 0;
  1379. while (row.size() != 0) {
  1380. // auto ind1 = row["col1"];
  1381. // auto ind2 = row["col2"];
  1382. // mindspore::MSTensor expected_tensor1;
  1383. // mindspore::MSTensor expected_tensor2;
  1384. // Tensor::CreateFromVector(expected1[i], &expected_tensor1);
  1385. // Tensor::CreateFromVector(expected2[i], &expected_tensor2);
  1386. // EXPECT_EQ(*ind1, *expected_tensor1);
  1387. // EXPECT_EQ(*ind2, *expected_tensor2);
  1388. iter->GetNextRow(&row);
  1389. i++;
  1390. }
  1391. EXPECT_EQ(i, 4);
  1392. // Manually terminate the pipeline
  1393. iter->Stop();
  1394. // Restore original seed and num_parallel_workers
  1395. status_set_seed = config::set_seed(original_seed);
  1396. EXPECT_EQ(status_set_seed, true);
  1397. status_set_worker = config::set_num_parallel_workers(original_worker);
  1398. EXPECT_EQ(status_set_worker, true);
  1399. }
  1400. TEST_F(MindDataTestPipeline, TestTruncateSequencePairFail) {
  1401. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTruncateSequencePairFail.";
  1402. // Testing TruncateSequencePair with negative max_length
  1403. // Create a RandomDataset which has column names "col1" and "col2"
  1404. std::shared_ptr<SchemaObj> schema = Schema();
  1405. schema->add_column("col1", mindspore::TypeId::kNumberTypeInt8, {3});
  1406. schema->add_column("col2", mindspore::TypeId::kNumberTypeInt8, {3});
  1407. std::shared_ptr<Dataset> ds = RandomData(3, schema);
  1408. EXPECT_NE(ds, nullptr);
  1409. // Create a truncate_sequence_pair operation on ds
  1410. std::shared_ptr<TensorTransform> truncate_sequence_pair = std::make_shared<text::TruncateSequencePair>(-1);
  1411. EXPECT_NE(truncate_sequence_pair, nullptr);
  1412. // Create a Map operation on ds
  1413. ds = ds->Map({truncate_sequence_pair});
  1414. EXPECT_NE(ds, nullptr);
  1415. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1416. // Expect failure: invalid TruncateSequencePair input (invalid parameter with negative max_length)
  1417. EXPECT_EQ(iter, nullptr);
  1418. }
  1419. TEST_F(MindDataTestPipeline, TestNgramSuccess) {
  1420. // Testing the parameter of Ngram interface.
  1421. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess.";
  1422. // Create a TextFile dataset
  1423. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1424. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1425. EXPECT_NE(ds, nullptr);
  1426. // Create white_tokenizer operation on ds
  1427. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>();
  1428. EXPECT_NE(white_tokenizer, nullptr);
  1429. // Create sliding_window operation on ds
  1430. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({2}, {"_", 1}, {"_", 1}, " "));
  1431. EXPECT_NE(ngram_op, nullptr);
  1432. // Create Map operation on ds
  1433. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1434. EXPECT_NE(ds, nullptr);
  1435. // Create an iterator over the result of the above dataset
  1436. // This will trigger the creation of the Execution Tree and launch it.
  1437. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1438. EXPECT_NE(iter, nullptr);
  1439. // Iterate the dataset and get each row
  1440. std::unordered_map<std::string, mindspore::MSTensor> row;
  1441. iter->GetNextRow(&row);
  1442. // std::vector<std::vector<std::string>> expected = {{"_ This", "This is", "is a", "a text", "text file.", "file. _"},
  1443. // {"_ Be", "Be happy", "happy every", "every day.", "day. _"},
  1444. // {"_ Good", "Good luck", "luck to", "to everyone.", "everyone.
  1445. // _"}};
  1446. uint64_t i = 0;
  1447. while (row.size() != 0) {
  1448. // auto ind = row["text"];
  1449. // mindspore::MSTensor expected_tensor;
  1450. // int x = expected[i].size();
  1451. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1452. // EXPECT_EQ(*ind, *expected_tensor);
  1453. iter->GetNextRow(&row);
  1454. i++;
  1455. }
  1456. EXPECT_EQ(i, 3);
  1457. // Manually terminate the pipeline
  1458. iter->Stop();
  1459. }
  1460. TEST_F(MindDataTestPipeline, TestNgramSuccess1) {
  1461. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramSuccess1.";
  1462. // Create a TextFile dataset
  1463. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1464. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1465. EXPECT_NE(ds, nullptr);
  1466. // Create white_tokenizer operation on ds
  1467. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>();
  1468. EXPECT_NE(white_tokenizer, nullptr);
  1469. // Create sliding_window operation on ds
  1470. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({2, 3}, {"&", 2}, {"&", 2}, "-"));
  1471. EXPECT_NE(ngram_op, nullptr);
  1472. // Create Map operation on ds
  1473. ds = ds->Map({white_tokenizer, ngram_op}, {"text"});
  1474. EXPECT_NE(ds, nullptr);
  1475. // Create an iterator over the result of the above dataset
  1476. // This will trigger the creation of the Execution Tree and launch it.
  1477. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1478. EXPECT_NE(iter, nullptr);
  1479. // Iterate the dataset and get each row
  1480. std::unordered_map<std::string, mindspore::MSTensor> row;
  1481. iter->GetNextRow(&row);
  1482. // std::vector<std::vector<std::string>> expected = {
  1483. // {"&-This", "This-is", "is-a", "a-text", "text-file.", "file.-&", "&-&-This", "&-This-is", "This-is-a",
  1484. // "is-a-text",
  1485. // "a-text-file.", "text-file.-&", "file.-&-&"},
  1486. // {"&-Be", "Be-happy", "happy-every", "every-day.", "day.-&", "&-&-Be", "&-Be-happy", "Be-happy-every",
  1487. // "happy-every-day.", "every-day.-&", "day.-&-&"},
  1488. // {"&-Good", "Good-luck", "luck-to", "to-everyone.", "everyone.-&", "&-&-Good", "&-Good-luck", "Good-luck-to",
  1489. // "luck-to-everyone.", "to-everyone.-&", "everyone.-&-&"}};
  1490. uint64_t i = 0;
  1491. while (row.size() != 0) {
  1492. // auto ind = row["text"];
  1493. // mindspore::MSTensor expected_tensor;
  1494. // int x = expected[i].size();
  1495. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1496. // EXPECT_EQ(*ind, *expected_tensor);
  1497. iter->GetNextRow(&row);
  1498. i++;
  1499. }
  1500. EXPECT_EQ(i, 3);
  1501. // Manually terminate the pipeline
  1502. iter->Stop();
  1503. }
  1504. TEST_F(MindDataTestPipeline, TestNgramFail1) {
  1505. // Testing the incorrect parameter of Ngram interface.
  1506. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail1.";
  1507. // Create a TextFile dataset
  1508. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1509. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1510. EXPECT_NE(ds, nullptr);
  1511. // Create sliding_window operation on ds
  1512. // Testing the vector of ngram is empty
  1513. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({}));
  1514. EXPECT_NE(ngram_op, nullptr);
  1515. // Create a Map operation on ds
  1516. ds = ds->Map({ngram_op});
  1517. EXPECT_NE(ds, nullptr);
  1518. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1519. // Expect failure: invalid Ngram input (the vector of ngram is empty)
  1520. EXPECT_EQ(iter, nullptr);
  1521. }
  1522. TEST_F(MindDataTestPipeline, TestNgramFail2) {
  1523. // Testing the incorrect parameter of Ngram interface.
  1524. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail2.";
  1525. // Create a TextFile dataset
  1526. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1527. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1528. EXPECT_NE(ds, nullptr);
  1529. // Create sliding_window operation on ds
  1530. // Testing the value of ngrams vector less than and equal to 0
  1531. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({0}));
  1532. EXPECT_NE(ngram_op, nullptr);
  1533. // Create a Map operation on ds
  1534. ds = ds->Map({ngram_op});
  1535. EXPECT_NE(ds, nullptr);
  1536. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1537. // Expect failure: invalid Ngram input (the value of ngrams vector less than and equal to 0)
  1538. EXPECT_EQ(iter, nullptr);
  1539. }
  1540. TEST_F(MindDataTestPipeline, TestNgramFail3) {
  1541. // Testing the incorrect parameter of Ngram interface.
  1542. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail3.";
  1543. // Create a TextFile dataset
  1544. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1545. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1546. EXPECT_NE(ds, nullptr);
  1547. // Create sliding_window operation on ds
  1548. // Testing the value of ngrams vector less than and equal to 0
  1549. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({-2}));
  1550. EXPECT_NE(ngram_op, nullptr);
  1551. // Create a Map operation on ds
  1552. ds = ds->Map({ngram_op});
  1553. EXPECT_NE(ds, nullptr);
  1554. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1555. // Expect failure: invalid Ngram input (the value of ngrams vector less than and equal to 0)
  1556. EXPECT_EQ(iter, nullptr);
  1557. }
  1558. TEST_F(MindDataTestPipeline, TestNgramFail4) {
  1559. // Testing the incorrect parameter of Ngram interface.
  1560. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail4.";
  1561. // Create a TextFile dataset
  1562. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1563. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1564. EXPECT_NE(ds, nullptr);
  1565. // Create sliding_window operation on ds
  1566. // Testing the second parameter pad_width in left_pad vector less than 0
  1567. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({2}, {"", -1}));
  1568. EXPECT_NE(ngram_op, nullptr);
  1569. // Create a Map operation on ds
  1570. ds = ds->Map({ngram_op});
  1571. EXPECT_NE(ds, nullptr);
  1572. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1573. // Expect failure: invalid Ngram input (the second parameter pad_width in left_pad vector less than 0)
  1574. EXPECT_EQ(iter, nullptr);
  1575. }
  1576. TEST_F(MindDataTestPipeline, TestNgramFail5) {
  1577. // Testing the incorrect parameter of Ngram interface.
  1578. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNgramFail5.";
  1579. // Create a TextFile dataset
  1580. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  1581. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1582. EXPECT_NE(ds, nullptr);
  1583. // Create sliding_window operation on ds
  1584. // Testing the second parameter pad_width in right_pad vector less than 0
  1585. std::shared_ptr<TensorTransform> ngram_op(new text::Ngram({2}, {"", 1}, {"", -1}));
  1586. EXPECT_NE(ngram_op, nullptr);
  1587. // Create a Map operation on ds
  1588. ds = ds->Map({ngram_op});
  1589. EXPECT_NE(ds, nullptr);
  1590. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1591. // Expect failure: invalid Ngram input (the second parameter pad_width in left_pad vector less than 0)
  1592. EXPECT_EQ(iter, nullptr);
  1593. }
  1594. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success) {
  1595. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkc.
  1596. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success.";
  1597. // Create a TextFile dataset
  1598. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1599. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1600. EXPECT_NE(ds, nullptr);
  1601. // Create normalizeutf8 operation on ds
  1602. std::shared_ptr<TensorTransform> normalizeutf8 = std::make_shared<text::NormalizeUTF8>(NormalizeForm::kNfkc);
  1603. EXPECT_NE(normalizeutf8, nullptr);
  1604. // Create Map operation on ds
  1605. ds = ds->Map({normalizeutf8}, {"text"});
  1606. EXPECT_NE(ds, nullptr);
  1607. // Create an iterator over the result of the above dataset
  1608. // This will trigger the creation of the Execution Tree and launch it.
  1609. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1610. EXPECT_NE(iter, nullptr);
  1611. // Iterate the dataset and get each row
  1612. std::unordered_map<std::string, mindspore::MSTensor> row;
  1613. iter->GetNextRow(&row);
  1614. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1615. uint64_t i = 0;
  1616. while (row.size() != 0) {
  1617. // auto ind = row["text"];
  1618. // mindspore::MSTensor expected_tensor;
  1619. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1620. // EXPECT_EQ(*ind, *expected_tensor);
  1621. iter->GetNextRow(&row);
  1622. i++;
  1623. }
  1624. EXPECT_EQ(i, 6);
  1625. // Manually terminate the pipeline
  1626. iter->Stop();
  1627. }
  1628. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success1) {
  1629. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfc.
  1630. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success1.";
  1631. // Create a TextFile dataset
  1632. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1633. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1634. EXPECT_NE(ds, nullptr);
  1635. // Create normalizeutf8 operation on ds
  1636. std::shared_ptr<TensorTransform> normalizeutf8 = std::make_shared<text::NormalizeUTF8>(NormalizeForm::kNfc);
  1637. EXPECT_NE(normalizeutf8, nullptr);
  1638. // Create Map operation on ds
  1639. ds = ds->Map({normalizeutf8}, {"text"});
  1640. EXPECT_NE(ds, nullptr);
  1641. // Create an iterator over the result of the above dataset
  1642. // This will trigger the creation of the Execution Tree and launch it.
  1643. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1644. EXPECT_NE(iter, nullptr);
  1645. // Iterate the dataset and get each row
  1646. std::unordered_map<std::string, mindspore::MSTensor> row;
  1647. iter->GetNextRow(&row);
  1648. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1649. uint64_t i = 0;
  1650. while (row.size() != 0) {
  1651. // auto ind = row["text"];
  1652. // mindspore::MSTensor expected_tensor;
  1653. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1654. // EXPECT_EQ(*ind, *expected_tensor);
  1655. iter->GetNextRow(&row);
  1656. i++;
  1657. }
  1658. EXPECT_EQ(i, 6);
  1659. // Manually terminate the pipeline
  1660. iter->Stop();
  1661. }
  1662. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success2) {
  1663. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfd.
  1664. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success2.";
  1665. // Create a TextFile dataset
  1666. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1667. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1668. EXPECT_NE(ds, nullptr);
  1669. // Create normalizeutf8 operation on ds
  1670. std::shared_ptr<TensorTransform> normalizeutf8 = std::make_shared<text::NormalizeUTF8>(NormalizeForm::kNfd);
  1671. EXPECT_NE(normalizeutf8, nullptr);
  1672. // Create Map operation on ds
  1673. ds = ds->Map({normalizeutf8}, {"text"});
  1674. EXPECT_NE(ds, nullptr);
  1675. // Create an iterator over the result of the above dataset
  1676. // This will trigger the creation of the Execution Tree and launch it.
  1677. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1678. EXPECT_NE(iter, nullptr);
  1679. // Iterate the dataset and get each row
  1680. std::unordered_map<std::string, mindspore::MSTensor> row;
  1681. iter->GetNextRow(&row);
  1682. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "2⁵", "ẛ̣"};
  1683. uint64_t i = 0;
  1684. while (row.size() != 0) {
  1685. // auto ind = row["text"];
  1686. // mindspore::MSTensor expected_tensor;
  1687. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1688. // EXPECT_EQ(*ind, *expected_tensor);
  1689. iter->GetNextRow(&row);
  1690. i++;
  1691. }
  1692. EXPECT_EQ(i, 6);
  1693. // Manually terminate the pipeline
  1694. iter->Stop();
  1695. }
  1696. TEST_F(MindDataTestPipeline, TestNormalizeUTF8Success3) {
  1697. // Testing the parameter of NormalizeUTF8 interface when the normalize_form is NormalizeForm::kNfkd.
  1698. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeUTF8Success3.";
  1699. // Create a TextFile dataset
  1700. std::string data_file = datasets_root_path_ + "/testTokenizerData/normalize.txt";
  1701. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1702. EXPECT_NE(ds, nullptr);
  1703. // Create normalizeutf8 operation on ds
  1704. std::shared_ptr<TensorTransform> normalizeutf8 = std::make_shared<text::NormalizeUTF8>(NormalizeForm::kNfkd);
  1705. EXPECT_NE(normalizeutf8, nullptr);
  1706. // Create Map operation on ds
  1707. ds = ds->Map({normalizeutf8}, {"text"});
  1708. EXPECT_NE(ds, nullptr);
  1709. // Create an iterator over the result of the above dataset
  1710. // This will trigger the creation of the Execution Tree and launch it.
  1711. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1712. EXPECT_NE(iter, nullptr);
  1713. // Iterate the dataset and get each row
  1714. std::unordered_map<std::string, mindspore::MSTensor> row;
  1715. iter->GetNextRow(&row);
  1716. // std::vector<std::string> expected = {"ṩ", "ḍ̇", "q̣̇", "fi", "25", "ṩ"};
  1717. uint64_t i = 0;
  1718. while (row.size() != 0) {
  1719. // auto ind = row["text"];
  1720. // mindspore::MSTensor expected_tensor;
  1721. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1722. // EXPECT_EQ(*ind, *expected_tensor);
  1723. iter->GetNextRow(&row);
  1724. i++;
  1725. }
  1726. EXPECT_EQ(i, 6);
  1727. // Manually terminate the pipeline
  1728. iter->Stop();
  1729. }
  1730. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess) {
  1731. // Testing the parameter of RegexReplace interface when the replace_all is true.
  1732. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess.";
  1733. // Create a TextFile dataset
  1734. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1735. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1736. EXPECT_NE(ds, nullptr);
  1737. // Create regex_replace operation on ds
  1738. std::shared_ptr<TensorTransform> regex_replace = std::make_shared<text::RegexReplace>("\\s+", "_", true);
  1739. EXPECT_NE(regex_replace, nullptr);
  1740. // Create Map operation on ds
  1741. ds = ds->Map({regex_replace}, {"text"});
  1742. EXPECT_NE(ds, nullptr);
  1743. // Create an iterator over the result of the above dataset
  1744. // This will trigger the creation of the Execution Tree and launch it.
  1745. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1746. EXPECT_NE(iter, nullptr);
  1747. // Iterate the dataset and get each row
  1748. std::unordered_map<std::string, mindspore::MSTensor> row;
  1749. iter->GetNextRow(&row);
  1750. // std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1751. // "31:beijing", "Welcome_to_China!", "_我_不想_长大_", "Welcome_to_Shenzhen!"};
  1752. uint64_t i = 0;
  1753. while (row.size() != 0) {
  1754. // auto ind = row["text"];
  1755. // mindspore::MSTensor expected_tensor;
  1756. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1757. // EXPECT_EQ(*ind, *expected_tensor);
  1758. iter->GetNextRow(&row);
  1759. i++;
  1760. }
  1761. EXPECT_EQ(i, 8);
  1762. // Manually terminate the pipeline
  1763. iter->Stop();
  1764. }
  1765. TEST_F(MindDataTestPipeline, TestRegexReplaceSuccess1) {
  1766. // Testing the parameter of RegexReplace interface when the replace_all is false.
  1767. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexReplaceSuccess1.";
  1768. // Create a TextFile dataset
  1769. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1770. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1771. EXPECT_NE(ds, nullptr);
  1772. // Create regex_replace operation on ds
  1773. std::shared_ptr<TensorTransform> regex_replace = std::make_shared<text::RegexReplace>("\\s+", "_", false);
  1774. EXPECT_NE(regex_replace, nullptr);
  1775. // Create Map operation on ds
  1776. ds = ds->Map({regex_replace}, {"text"});
  1777. EXPECT_NE(ds, nullptr);
  1778. // Create an iterator over the result of the above dataset
  1779. // This will trigger the creation of the Execution Tree and launch it.
  1780. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1781. EXPECT_NE(iter, nullptr);
  1782. // Iterate the dataset and get each row
  1783. std::unordered_map<std::string, mindspore::MSTensor> row;
  1784. iter->GetNextRow(&row);
  1785. // std::vector<std::string> expected = {"Hello_World", "Let's_Go", "1:hello", "2:world",
  1786. // "31:beijing", "Welcome_to China!", "_我 不想 长大 ", "Welcome_to
  1787. // Shenzhen!"};
  1788. uint64_t i = 0;
  1789. while (row.size() != 0) {
  1790. // auto ind = row["text"];
  1791. // mindspore::MSTensor expected_tensor;
  1792. // Tensor::CreateScalar(expected[i], &expected_tensor);
  1793. // EXPECT_EQ(*ind, *expected_tensor);
  1794. iter->GetNextRow(&row);
  1795. i++;
  1796. }
  1797. EXPECT_EQ(i, 8);
  1798. // Manually terminate the pipeline
  1799. iter->Stop();
  1800. }
  1801. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess) {
  1802. // Testing the parameter of RegexTokenizer interface when the with_offsets is false.
  1803. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess.";
  1804. // Create a TextFile dataset
  1805. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1806. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1807. EXPECT_NE(ds, nullptr);
  1808. // Create regex_tokenizer operation on ds
  1809. std::shared_ptr<TensorTransform> regex_tokenizer = std::make_shared<text::RegexTokenizer>("\\s+", "\\s+", false);
  1810. EXPECT_NE(regex_tokenizer, nullptr);
  1811. // Create Map operation on ds
  1812. ds = ds->Map({regex_tokenizer}, {"text"});
  1813. EXPECT_NE(ds, nullptr);
  1814. // Create an iterator over the result of the above dataset
  1815. // This will trigger the creation of the Execution Tree and launch it.
  1816. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1817. EXPECT_NE(iter, nullptr);
  1818. // Iterate the dataset and get each row
  1819. std::unordered_map<std::string, mindspore::MSTensor> row;
  1820. iter->GetNextRow(&row);
  1821. // std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1822. // {"Let's", " ", "Go"},
  1823. // {"1:hello"},
  1824. // {"2:world"},
  1825. // {"31:beijing"},
  1826. // {"Welcome", " ", "to", " ", "China!"},
  1827. // {" ", "我", " ", "不想", " ", "长大", " "},
  1828. // {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1829. uint64_t i = 0;
  1830. while (row.size() != 0) {
  1831. // auto ind = row["text"];
  1832. // mindspore::MSTensor expected_tensor;
  1833. // int x = expected[i].size();
  1834. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1835. // EXPECT_EQ(*ind, *expected_tensor);
  1836. iter->GetNextRow(&row);
  1837. i++;
  1838. }
  1839. EXPECT_EQ(i, 8);
  1840. // Manually terminate the pipeline
  1841. iter->Stop();
  1842. }
  1843. TEST_F(MindDataTestPipeline, TestRegexTokenizerSuccess1) {
  1844. // Testing the parameter of RegexTokenizer interface when the with_offsets is true.
  1845. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRegexTokenizerSuccess1.";
  1846. // Create a TextFile dataset
  1847. std::string data_file = datasets_root_path_ + "/testTokenizerData/regex_replace.txt";
  1848. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1849. EXPECT_NE(ds, nullptr);
  1850. // Create regex_tokenizer operation on ds
  1851. std::shared_ptr<TensorTransform> regex_tokenizer = std::make_shared<text::RegexTokenizer>("\\s+", "\\s+", true);
  1852. EXPECT_NE(regex_tokenizer, nullptr);
  1853. // Create Map operation on ds
  1854. ds = ds->Map({regex_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1855. {"token", "offsets_start", "offsets_limit"});
  1856. EXPECT_NE(ds, nullptr);
  1857. // Create an iterator over the result of the above dataset
  1858. // This will trigger the creation of the Execution Tree and launch it.
  1859. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1860. EXPECT_NE(iter, nullptr);
  1861. // Iterate the dataset and get each row
  1862. std::unordered_map<std::string, mindspore::MSTensor> row;
  1863. iter->GetNextRow(&row);
  1864. // std::vector<std::vector<std::string>> expected = {{"Hello", " ", "World"},
  1865. // {"Let's", " ", "Go"},
  1866. // {"1:hello"},
  1867. // {"2:world"},
  1868. // {"31:beijing"},
  1869. // {"Welcome", " ", "to", " ", "China!"},
  1870. // {" ", "我", " ", "不想", " ", "长大", " "},
  1871. // {"Welcome", " ", "to", " ", "Shenzhen!"}};
  1872. // std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1873. // {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}};
  1874. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1875. // {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}};
  1876. uint64_t i = 0;
  1877. while (row.size() != 0) {
  1878. // auto ind = row["offsets_start"];
  1879. // auto ind1 = row["offsets_limit"];
  1880. // auto token = row["token"];
  1881. // mindspore::MSTensor expected_tensor;
  1882. // mindspore::MSTensor expected_tensor_offsets_start;
  1883. // mindspore::MSTensor expected_tensor_offsets_limit;
  1884. // int x = expected[i].size();
  1885. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1886. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1887. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1888. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1889. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1890. // EXPECT_EQ(*token, *expected_tensor);
  1891. iter->GetNextRow(&row);
  1892. i++;
  1893. }
  1894. EXPECT_EQ(i, 8);
  1895. // Manually terminate the pipeline
  1896. iter->Stop();
  1897. }
  1898. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess) {
  1899. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is default.
  1900. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess.";
  1901. // Create a TextFile dataset
  1902. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1903. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1904. EXPECT_NE(ds, nullptr);
  1905. // Create unicodechar_tokenizer operation on ds
  1906. std::shared_ptr<TensorTransform> unicodechar_tokenizer = std::make_shared<text::UnicodeCharTokenizer>();
  1907. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1908. // Create Map operation on ds
  1909. ds = ds->Map({unicodechar_tokenizer}, {"text"});
  1910. EXPECT_NE(ds, nullptr);
  1911. // Create an iterator over the result of the above dataset
  1912. // This will trigger the creation of the Execution Tree and launch it.
  1913. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1914. EXPECT_NE(iter, nullptr);
  1915. // Iterate the dataset and get each row
  1916. std::unordered_map<std::string, mindspore::MSTensor> row;
  1917. iter->GetNextRow(&row);
  1918. // std::vector<std::vector<std::string>> expected = {
  1919. // {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1920. // {"北", "京", "欢", "迎", "您", "!"},
  1921. // {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1922. // {" ", " "}};
  1923. uint64_t i = 0;
  1924. while (row.size() != 0) {
  1925. // auto ind = row["text"];
  1926. // mindspore::MSTensor expected_tensor;
  1927. // int x = expected[i].size();
  1928. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1929. // EXPECT_EQ(*ind, *expected_tensor);
  1930. iter->GetNextRow(&row);
  1931. i++;
  1932. }
  1933. EXPECT_EQ(i, 4);
  1934. // Manually terminate the pipeline
  1935. iter->Stop();
  1936. }
  1937. TEST_F(MindDataTestPipeline, TestUnicodeCharTokenizerSuccess1) {
  1938. // Testing the parameter of UnicodeCharTokenizer interface when the with_offsets is true.
  1939. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeCharTokenizerSuccess1.";
  1940. // Create a TextFile dataset
  1941. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  1942. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  1943. EXPECT_NE(ds, nullptr);
  1944. // Create unicodechar_tokenizer operation on ds
  1945. std::shared_ptr<TensorTransform> unicodechar_tokenizer = std::make_shared<text::UnicodeCharTokenizer>(true);
  1946. EXPECT_NE(unicodechar_tokenizer, nullptr);
  1947. // Create Map operation on ds
  1948. ds = ds->Map({unicodechar_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  1949. {"token", "offsets_start", "offsets_limit"});
  1950. EXPECT_NE(ds, nullptr);
  1951. // Create an iterator over the result of the above dataset
  1952. // This will trigger the creation of the Execution Tree and launch it.
  1953. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1954. EXPECT_NE(iter, nullptr);
  1955. // Iterate the dataset and get each row
  1956. std::unordered_map<std::string, mindspore::MSTensor> row;
  1957. iter->GetNextRow(&row);
  1958. // std::vector<std::vector<std::string>> expected = {
  1959. // {"W", "e", "l", "c", "o", "m", "e", " ", "t", "o", " ", "B", "e", "i", "j", "i", "n", "g", "!"},
  1960. // {"北", "京", "欢", "迎", "您", "!"},
  1961. // {"我", "喜", "欢", "E", "n", "g", "l", "i", "s", "h", "!"},
  1962. // {" ", " "}};
  1963. // std::vector<std::vector<uint32_t>> expected_offsets_start = {
  1964. // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
  1965. // {0, 3, 6, 9, 12, 15},
  1966. // {0, 3, 6, 9, 10, 11, 12, 13, 14, 15, 16},
  1967. // {0, 1}};
  1968. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {
  1969. // {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  1970. // {3, 6, 9, 12, 15, 18},
  1971. // {3, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  1972. // {1, 2}};
  1973. uint64_t i = 0;
  1974. while (row.size() != 0) {
  1975. // auto ind = row["offsets_start"];
  1976. // auto ind1 = row["offsets_limit"];
  1977. // auto token = row["token"];
  1978. // mindspore::MSTensor expected_tensor;
  1979. // mindspore::MSTensor expected_tensor_offsets_start;
  1980. // mindspore::MSTensor expected_tensor_offsets_limit;
  1981. // int x = expected[i].size();
  1982. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  1983. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  1984. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  1985. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  1986. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  1987. // EXPECT_EQ(*token, *expected_tensor);
  1988. iter->GetNextRow(&row);
  1989. i++;
  1990. }
  1991. EXPECT_EQ(i, 4);
  1992. // Manually terminate the pipeline
  1993. iter->Stop();
  1994. }
  1995. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess) {
  1996. // Testing the parameter of UnicodeScriptTokenizer interface when the with_offsets and the keep_whitespace is default.
  1997. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess.";
  1998. // Create a TextFile dataset
  1999. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2000. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2001. EXPECT_NE(ds, nullptr);
  2002. // Create unicodescript_tokenizer operation on ds
  2003. std::shared_ptr<TensorTransform> unicodescript_tokenizer = std::make_shared<text::UnicodeScriptTokenizer>();
  2004. EXPECT_NE(unicodescript_tokenizer, nullptr);
  2005. // Create Map operation on ds
  2006. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  2007. EXPECT_NE(ds, nullptr);
  2008. // Create an iterator over the result of the above dataset
  2009. // This will trigger the creation of the Execution Tree and launch it.
  2010. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2011. EXPECT_NE(iter, nullptr);
  2012. // Iterate the dataset and get each row
  2013. std::unordered_map<std::string, mindspore::MSTensor> row;
  2014. iter->GetNextRow(&row);
  2015. // std::vector<std::vector<std::string>> expected = {
  2016. // {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  2017. uint64_t i = 0;
  2018. while (row.size() != 0) {
  2019. // auto ind = row["text"];
  2020. // mindspore::MSTensor expected_tensor;
  2021. // int x = expected[i].size();
  2022. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2023. // EXPECT_EQ(*ind, *expected_tensor);
  2024. iter->GetNextRow(&row);
  2025. i++;
  2026. }
  2027. EXPECT_EQ(i, 4);
  2028. // Manually terminate the pipeline
  2029. iter->Stop();
  2030. }
  2031. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess1) {
  2032. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  2033. // false.
  2034. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess1.";
  2035. // Create a TextFile dataset
  2036. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2037. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2038. EXPECT_NE(ds, nullptr);
  2039. // Create unicodescript_tokenizer operation on ds
  2040. std::shared_ptr<TensorTransform> unicodescript_tokenizer = std::make_shared<text::UnicodeScriptTokenizer>(true);
  2041. EXPECT_NE(unicodescript_tokenizer, nullptr);
  2042. // Create Map operation on ds
  2043. ds = ds->Map({unicodescript_tokenizer}, {"text"});
  2044. EXPECT_NE(ds, nullptr);
  2045. // Create an iterator over the result of the above dataset
  2046. // This will trigger the creation of the Execution Tree and launch it.
  2047. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2048. EXPECT_NE(iter, nullptr);
  2049. // Iterate the dataset and get each row
  2050. std::unordered_map<std::string, mindspore::MSTensor> row;
  2051. iter->GetNextRow(&row);
  2052. // std::vector<std::vector<std::string>> expected = {
  2053. // {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  2054. uint64_t i = 0;
  2055. while (row.size() != 0) {
  2056. // auto ind = row["text"];
  2057. // mindspore::MSTensor expected_tensor;
  2058. // int x = expected[i].size();
  2059. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2060. // EXPECT_EQ(*ind, *expected_tensor);
  2061. iter->GetNextRow(&row);
  2062. i++;
  2063. }
  2064. EXPECT_EQ(i, 4);
  2065. // Manually terminate the pipeline
  2066. iter->Stop();
  2067. }
  2068. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess2) {
  2069. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is false and the with_offsets is
  2070. // true.
  2071. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess2.";
  2072. // Create a TextFile dataset
  2073. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2074. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2075. EXPECT_NE(ds, nullptr);
  2076. // Create unicodescript_tokenizer operation on ds
  2077. std::shared_ptr<TensorTransform> unicodescript_tokenizer =
  2078. std::make_shared<text::UnicodeScriptTokenizer>(false, true);
  2079. EXPECT_NE(unicodescript_tokenizer, nullptr);
  2080. // Create Map operation on ds
  2081. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  2082. {"token", "offsets_start", "offsets_limit"});
  2083. EXPECT_NE(ds, nullptr);
  2084. // Create an iterator over the result of the above dataset
  2085. // This will trigger the creation of the Execution Tree and launch it.
  2086. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2087. EXPECT_NE(iter, nullptr);
  2088. // Iterate the dataset and get each row
  2089. std::unordered_map<std::string, mindspore::MSTensor> row;
  2090. iter->GetNextRow(&row);
  2091. // std::vector<std::vector<std::string>> expected = {
  2092. // {"Welcome", "to", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {""}};
  2093. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  2094. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 18, 19}, {15, 18}, {9, 16, 17}, {0}};
  2095. uint64_t i = 0;
  2096. while (row.size() != 0) {
  2097. // auto ind = row["offsets_start"];
  2098. // auto ind1 = row["offsets_limit"];
  2099. // auto token = row["token"];
  2100. // mindspore::MSTensor expected_tensor;
  2101. // mindspore::MSTensor expected_tensor_offsets_start;
  2102. // mindspore::MSTensor expected_tensor_offsets_limit;
  2103. // int x = expected[i].size();
  2104. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2105. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  2106. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  2107. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  2108. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  2109. // EXPECT_EQ(*token, *expected_tensor);
  2110. iter->GetNextRow(&row);
  2111. i++;
  2112. }
  2113. EXPECT_EQ(i, 4);
  2114. // Manually terminate the pipeline
  2115. iter->Stop();
  2116. }
  2117. TEST_F(MindDataTestPipeline, TestUnicodeScriptTokenizerSuccess3) {
  2118. // Testing the parameter of UnicodeScriptTokenizer interface when the keep_whitespace is true and the with_offsets is
  2119. // true.
  2120. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUnicodeScriptTokenizerSuccess3.";
  2121. // Create a TextFile dataset
  2122. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2123. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2124. EXPECT_NE(ds, nullptr);
  2125. // Create unicodescript_tokenizer operation on ds
  2126. std::shared_ptr<TensorTransform> unicodescript_tokenizer = std::make_shared<text::UnicodeScriptTokenizer>(true, true);
  2127. EXPECT_NE(unicodescript_tokenizer, nullptr);
  2128. // Create Map operation on ds
  2129. ds = ds->Map({unicodescript_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  2130. {"token", "offsets_start", "offsets_limit"});
  2131. EXPECT_NE(ds, nullptr);
  2132. // Create an iterator over the result of the above dataset
  2133. // This will trigger the creation of the Execution Tree and launch it.
  2134. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2135. EXPECT_NE(iter, nullptr);
  2136. // Iterate the dataset and get each row
  2137. std::unordered_map<std::string, mindspore::MSTensor> row;
  2138. iter->GetNextRow(&row);
  2139. // std::vector<std::vector<std::string>> expected = {
  2140. // {"Welcome", " ", "to", " ", "Beijing", "!"}, {"北京欢迎您", "!"}, {"我喜欢", "English", "!"}, {" "}};
  2141. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 7, 8, 10, 11, 18}, {0, 15}, {0, 9, 16}, {0}};
  2142. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 8, 10, 11, 18, 19}, {15, 18}, {9, 16, 17}, {2}};
  2143. uint64_t i = 0;
  2144. while (row.size() != 0) {
  2145. // auto ind = row["offsets_start"];
  2146. // auto ind1 = row["offsets_limit"];
  2147. // auto token = row["token"];
  2148. // mindspore::MSTensor expected_tensor;
  2149. // mindspore::MSTensor expected_tensor_offsets_start;
  2150. // mindspore::MSTensor expected_tensor_offsets_limit;
  2151. // int x = expected[i].size();
  2152. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2153. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  2154. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  2155. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  2156. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  2157. // EXPECT_EQ(*token, *expected_tensor);
  2158. iter->GetNextRow(&row);
  2159. i++;
  2160. }
  2161. EXPECT_EQ(i, 4);
  2162. // Manually terminate the pipeline
  2163. iter->Stop();
  2164. }
  2165. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess) {
  2166. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is default.
  2167. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess.";
  2168. // Create a TextFile dataset
  2169. std::string data_file = datasets_root_path_ + "/testTextFileDataset/1.txt";
  2170. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2171. EXPECT_NE(ds, nullptr);
  2172. // Create white_tokenizer operation on ds
  2173. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>();
  2174. EXPECT_NE(white_tokenizer, nullptr);
  2175. // Create Map operation on ds
  2176. ds = ds->Map({white_tokenizer}, {"text"});
  2177. EXPECT_NE(ds, nullptr);
  2178. // Create an iterator over the result of the above dataset
  2179. // This will trigger the creation of the Execution Tree and launch it.
  2180. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2181. EXPECT_NE(iter, nullptr);
  2182. // Iterate the dataset and get each row
  2183. std::unordered_map<std::string, mindspore::MSTensor> row;
  2184. iter->GetNextRow(&row);
  2185. // std::vector<std::vector<std::string>> expected = {
  2186. // {"This", "is", "a", "text", "file."}, {"Be", "happy", "every", "day."}, {"Good", "luck", "to", "everyone."}};
  2187. uint64_t i = 0;
  2188. while (row.size() != 0) {
  2189. // auto ind = row["text"];
  2190. // mindspore::MSTensor expected_tensor;
  2191. // int x = expected[i].size();
  2192. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2193. // EXPECT_EQ(*ind, *expected_tensor);
  2194. iter->GetNextRow(&row);
  2195. i++;
  2196. }
  2197. EXPECT_EQ(i, 3);
  2198. // Manually terminate the pipeline
  2199. iter->Stop();
  2200. }
  2201. TEST_F(MindDataTestPipeline, TestWhitespaceTokenizerSuccess1) {
  2202. // Testing the parameter of WhitespaceTokenizer interface when the with_offsets is true.
  2203. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestWhitespaceTokenizerSuccess1.";
  2204. // Create a TextFile dataset
  2205. std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
  2206. std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
  2207. EXPECT_NE(ds, nullptr);
  2208. // Create white_tokenizer operation on ds
  2209. std::shared_ptr<TensorTransform> white_tokenizer = std::make_shared<text::WhitespaceTokenizer>(true);
  2210. EXPECT_NE(white_tokenizer, nullptr);
  2211. // Create Map operation on ds
  2212. ds = ds->Map({white_tokenizer}, {"text"}, {"token", "offsets_start", "offsets_limit"},
  2213. {"token", "offsets_start", "offsets_limit"});
  2214. EXPECT_NE(ds, nullptr);
  2215. // Create an iterator over the result of the above dataset
  2216. // This will trigger the creation of the Execution Tree and launch it.
  2217. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2218. EXPECT_NE(iter, nullptr);
  2219. // Iterate the dataset and get each row
  2220. std::unordered_map<std::string, mindspore::MSTensor> row;
  2221. iter->GetNextRow(&row);
  2222. // std::vector<std::vector<std::string>> expected = {
  2223. // {"Welcome", "to", "Beijing!"}, {"北京欢迎您!"}, {"我喜欢English!"}, {""}};
  2224. // std::vector<std::vector<uint32_t>> expected_offsets_start = {{0, 8, 11}, {0}, {0}, {0}};
  2225. // std::vector<std::vector<uint32_t>> expected_offsets_limit = {{7, 10, 19}, {18}, {17}, {0}};
  2226. uint64_t i = 0;
  2227. while (row.size() != 0) {
  2228. // auto ind = row["offsets_start"];
  2229. // auto ind1 = row["offsets_limit"];
  2230. // auto token = row["token"];
  2231. // mindspore::MSTensor expected_tensor;
  2232. // mindspore::MSTensor expected_tensor_offsets_start;
  2233. // mindspore::MSTensor expected_tensor_offsets_limit;
  2234. // int x = expected[i].size();
  2235. // Tensor::CreateFromVector(expected[i], TensorShape({x}), &expected_tensor);
  2236. // Tensor::CreateFromVector(expected_offsets_start[i], TensorShape({x}), &expected_tensor_offsets_start);
  2237. // Tensor::CreateFromVector(expected_offsets_limit[i], TensorShape({x}), &expected_tensor_offsets_limit);
  2238. // EXPECT_EQ(*ind, *expected_tensor_offsets_start);
  2239. // EXPECT_EQ(*ind1, *expected_tensor_offsets_limit);
  2240. // EXPECT_EQ(*token, *expected_tensor);
  2241. iter->GetNextRow(&row);
  2242. i++;
  2243. }
  2244. EXPECT_EQ(i, 4);
  2245. // Manually terminate the pipeline
  2246. iter->Stop();
  2247. }