strickvl commited on
Commit
b1d540a
·
1 Parent(s): e8868b3

add spacy trainer

Browse files
Files changed (1) hide show
  1. src/train_tokenizer.ipynb +263 -7
src/train_tokenizer.ipynb CHANGED
@@ -47,6 +47,7 @@
47
  "source": [
48
  "import os\n",
49
  "\n",
 
50
  "def get_txt_file_paths(directory):\n",
51
  " txt_file_paths = []\n",
52
  " for root, dirs, files in os.walk(directory):\n",
@@ -56,11 +57,12 @@
56
  " txt_file_paths.append(file_path)\n",
57
  " return txt_file_paths\n",
58
  "\n",
 
59
  "# Replace \"directory_path\" with the actual path of the directory you want to search\n",
60
  "directory_path = \"../data/raw_text\"\n",
61
  "txt_paths = get_txt_file_paths(directory_path)\n",
62
  "\n",
63
- "len(txt_paths)\n"
64
  ]
65
  },
66
  {
@@ -71,16 +73,17 @@
71
  "source": [
72
  "import re\n",
73
  "\n",
 
74
  "def clean_text(file_path):\n",
75
  " # Open the file and read it into memory\n",
76
- " with open(file_path, 'r', encoding='utf-8') as file:\n",
77
  " text = file.read()\n",
78
  "\n",
79
  " # Remove English-language characters and numbers\n",
80
- " text = re.sub(r'[a-zA-Z0-9]', '', text)\n",
81
  "\n",
82
  " # Remove any excess whitespace\n",
83
- " text = re.sub(r'[^\\S\\n]+', ' ', text)\n",
84
  "\n",
85
  " return text"
86
  ]
@@ -96,8 +99,18 @@
96
  "\n",
97
  " # write the cleaned text to a new file with an incremented filename\n",
98
  " # write the files all into the '../data/processed_text' directory\n",
99
- " with open(f'../data/processed_text/{path.split(\"/\")[-1]}', 'w', encoding='utf-8') as file:\n",
100
- " file.write(cleaned_text)\n"
 
 
 
 
 
 
 
 
 
 
101
  ]
102
  },
103
  {
@@ -119,6 +132,7 @@
119
  "outputs": [],
120
  "source": [
121
  "from tokenizers.pre_tokenizers import Whitespace\n",
 
122
  "tokenizer.pre_tokenizer = Whitespace()"
123
  ]
124
  },
@@ -266,7 +280,9 @@
266
  }
267
  ],
268
  "source": [
269
- "sample_text = \"      آیک  جناورے اَت۔  لھتے گشیت آ سکیں کارزوالے ات کہ اگاں آزاتی دیگ بہ بیت، بازارءَ، لوگے ءَ، جاگاہ یے  ءَ،دپتر ء ُ کارگس یے  ءَ یا ھر ھما جاگاہ ءَ کہ شُت کنت مزنیں کارزوالی کنت۔گوں ھر کس ءَ جنگ ء ُ مڑ بیت۔گدء ُ پچاں  چنڈ چنڈ ء ُ راڑ راڑ کنت،کاگد ء ُ وانگیاں وارت ء ُ آدراہ کنت۔ورگی چیزاں اگاں وارت نکنت آھاں گٹ پاچیت ھراب کنت۔ایندگہ جناور چہ بندات ء َ ایشی ءِ کازوالیاں چہ وتا دیر دارگ ءِ کوشست کن اَنت۔ چیا کہ آ بازیں دگہ ھرابی ء ُ کارزوالی ھم کنت،پمیشکا کسانیں جناور  بالی مُرگ،کوہ پاچن،آسک ء ُ ایندگہ کسان کسانیں جناورچر آئی ءِ کارزوالیانی سوب ءَ آئی ءَ چہ سک باز شزار اَنت ۔\".replace(\"\\xa0\", \"\")\n",
 
 
270
  "sample_sentence = sample_text.split(\"۔\")[2]\n",
271
  "sample_sentence"
272
  ]
@@ -291,6 +307,246 @@
291
  "tokenizer.encode(sample_sentence).tokens"
292
  ]
293
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  {
295
  "cell_type": "code",
296
  "execution_count": null,
 
47
  "source": [
48
  "import os\n",
49
  "\n",
50
+ "\n",
51
  "def get_txt_file_paths(directory):\n",
52
  " txt_file_paths = []\n",
53
  " for root, dirs, files in os.walk(directory):\n",
 
57
  " txt_file_paths.append(file_path)\n",
58
  " return txt_file_paths\n",
59
  "\n",
60
+ "\n",
61
  "# Replace \"directory_path\" with the actual path of the directory you want to search\n",
62
  "directory_path = \"../data/raw_text\"\n",
63
  "txt_paths = get_txt_file_paths(directory_path)\n",
64
  "\n",
65
+ "len(txt_paths)"
66
  ]
67
  },
68
  {
 
73
  "source": [
74
  "import re\n",
75
  "\n",
76
+ "\n",
77
  "def clean_text(file_path):\n",
78
  " # Open the file and read it into memory\n",
79
+ " with open(file_path, \"r\", encoding=\"utf-8\") as file:\n",
80
  " text = file.read()\n",
81
  "\n",
82
  " # Remove English-language characters and numbers\n",
83
+ " text = re.sub(r\"[a-zA-Z0-9]\", \"\", text)\n",
84
  "\n",
85
  " # Remove any excess whitespace\n",
86
+ " text = re.sub(r\"[^\\S\\n]+\", \" \", text)\n",
87
  "\n",
88
  " return text"
89
  ]
 
99
  "\n",
100
  " # write the cleaned text to a new file with an incremented filename\n",
101
  " # write the files all into the '../data/processed_text' directory\n",
102
+ " with open(\n",
103
+ " f'../data/processed_text/{path.split(\"/\")[-1]}', \"w\", encoding=\"utf-8\"\n",
104
+ " ) as file:\n",
105
+ " file.write(cleaned_text)"
106
+ ]
107
+ },
108
+ {
109
+ "attachments": {},
110
+ "cell_type": "markdown",
111
+ "metadata": {},
112
+ "source": [
113
+ "# Training a Tokenizer using 🤗 Tokenizers"
114
  ]
115
  },
116
  {
 
132
  "outputs": [],
133
  "source": [
134
  "from tokenizers.pre_tokenizers import Whitespace\n",
135
+ "\n",
136
  "tokenizer.pre_tokenizer = Whitespace()"
137
  ]
138
  },
 
280
  }
281
  ],
282
  "source": [
283
+ "sample_text = \"      آیک  جناورے اَت۔  لھتے گشیت آ سکیں کارزوالے ات کہ اگاں آزاتی دیگ بہ بیت، بازارءَ، لوگے ءَ، جاگاہ یے  ءَ،دپتر ء ُ کارگس یے  ءَ یا ھر ھما جاگاہ ءَ کہ شُت کنت مزنیں کارزوالی کنت۔گوں ھر کس ءَ جنگ ء ُ مڑ بیت۔گدء ُ پچاں  چنڈ چنڈ ء ُ راڑ راڑ کنت،کاگد ء ُ وانگیاں وارت ء ُ آدراہ کنت۔ورگی چیزاں اگاں وارت نکنت آھاں گٹ پاچیت ھراب کنت۔ایندگہ جناور چہ بندات ء َ ایشی ءِ کازوالیاں چہ وتا دیر دارگ ءِ کوشست کن اَنت۔ چیا کہ آ بازیں دگہ ھرابی ء ُ کارزوالی ھم کنت،پمیشکا کسانیں جناور  بالی مُرگ،کوہ پاچن،آسک ء ُ ایندگہ کسان کسانیں جناورچر آئی ءِ کارزوالیانی سوب ءَ آئی ءَ چہ سک باز شزار اَنت ۔\".replace(\n",
284
+ " \"\\xa0\", \"\"\n",
285
+ ")\n",
286
  "sample_sentence = sample_text.split(\"۔\")[2]\n",
287
  "sample_sentence"
288
  ]
 
307
  "tokenizer.encode(sample_sentence).tokens"
308
  ]
309
  },
310
+ {
311
+ "attachments": {},
312
+ "cell_type": "markdown",
313
+ "metadata": {},
314
+ "source": [
315
+ "# Training a custom tokenizer using Spacy and FastAI"
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "code",
320
+ "execution_count": 68,
321
+ "metadata": {},
322
+ "outputs": [],
323
+ "source": [
324
+ "from fastai.text.all import *\n",
325
+ "files = get_text_files(\"../data/processed_text\")"
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "code",
330
+ "execution_count": 69,
331
+ "metadata": {},
332
+ "outputs": [
333
+ {
334
+ "data": {
335
+ "text/plain": [
336
+ "4294"
337
+ ]
338
+ },
339
+ "execution_count": 69,
340
+ "metadata": {},
341
+ "output_type": "execute_result"
342
+ }
343
+ ],
344
+ "source": [
345
+ "len(files)"
346
+ ]
347
+ },
348
+ {
349
+ "cell_type": "code",
350
+ "execution_count": 70,
351
+ "metadata": {},
352
+ "outputs": [
353
+ {
354
+ "data": {
355
+ "text/plain": [
356
+ "'*آمیتگءِ جُستءَمکن* لچّہ: *آمیتگءِ جُستءَمکن* آ میتگءَکہ من وتی شوکیں کسانی'"
357
+ ]
358
+ },
359
+ "execution_count": 70,
360
+ "metadata": {},
361
+ "output_type": "execute_result"
362
+ }
363
+ ],
364
+ "source": [
365
+ "txt = files[0].open().read(); txt[:75]"
366
+ ]
367
+ },
368
+ {
369
+ "cell_type": "code",
370
+ "execution_count": 71,
371
+ "metadata": {},
372
+ "outputs": [
373
+ {
374
+ "name": "stdout",
375
+ "output_type": "stream",
376
+ "text": [
377
+ "(#146) ['*','آمیتگءِ','جُستءَمکن','*','لچّہ',':','*','آمیتگءِ','جُستءَمکن','*','آ','میتگءَکہ','من','وتی','شوکیں','کسانی','پیر','کُت','آ','میتگءِ','جسُتءَمکن','آ','میتگءِ','گیراں','مبو','بے','اوستیں','تاهیراں','مبو','آ'...]\n"
378
+ ]
379
+ }
380
+ ],
381
+ "source": [
382
+ "spacy = WordTokenizer()\n",
383
+ "toks = first(spacy([txt]))\n",
384
+ "print(coll_repr(toks, 30))"
385
+ ]
386
+ },
387
+ {
388
+ "cell_type": "code",
389
+ "execution_count": 72,
390
+ "metadata": {},
391
+ "outputs": [
392
+ {
393
+ "name": "stdout",
394
+ "output_type": "stream",
395
+ "text": [
396
+ "(#147) ['xxbos','*','آمیتگءِ','جُستءَمکن','*','لچّہ',':','*','آمیتگءِ','جُستءَمکن','*','آ','میتگءَکہ','من','وتی','شوکیں','کسانی','پیر','کُت','آ','میتگءِ','جسُتءَمکن','آ','میتگءِ','گیراں','مبو','بے','اوستیں','تاهیراں','مبو','آ'...]\n"
397
+ ]
398
+ }
399
+ ],
400
+ "source": [
401
+ "tkn = Tokenizer(spacy)\n",
402
+ "print(coll_repr(tkn(txt), 31))"
403
+ ]
404
+ },
405
+ {
406
+ "cell_type": "code",
407
+ "execution_count": 81,
408
+ "metadata": {},
409
+ "outputs": [],
410
+ "source": [
411
+ "txts = L(o.open().read() for o in files)"
412
+ ]
413
+ },
414
+ {
415
+ "cell_type": "code",
416
+ "execution_count": 82,
417
+ "metadata": {},
418
+ "outputs": [],
419
+ "source": [
420
+ "def subword(size: int):\n",
421
+ " sp = SubwordTokenizer(vocab_sz=size)\n",
422
+ " sp.setup(txts)\n",
423
+ " return \" \".join(first(sp([txt]))[:40])\n"
424
+ ]
425
+ },
426
+ {
427
+ "cell_type": "code",
428
+ "execution_count": 83,
429
+ "metadata": {},
430
+ "outputs": [
431
+ {
432
+ "data": {
433
+ "text/plain": [
434
+ "'▁* آ می تگ ءِ ▁جُست ءَ م ک ن * ▁لچّہ : ▁* آ می تگ ءِ ▁جُست ءَ م ک ن * ▁آ ▁میتگ ءَ کہ ▁من ▁وتی ▁ش وکیں ▁کس انی ▁پیر ▁کُت ▁آ ▁میتگ ءِ ▁ج'"
435
+ ]
436
+ },
437
+ "execution_count": 83,
438
+ "metadata": {},
439
+ "output_type": "execute_result"
440
+ }
441
+ ],
442
+ "source": [
443
+ "subword(1000)"
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "code",
448
+ "execution_count": 86,
449
+ "metadata": {},
450
+ "outputs": [
451
+ {
452
+ "data": {
453
+ "text/plain": [
454
+ "'▁ * آ م ی ت گ ء ِ ▁ ج ُ س ت ء َ م ک ن * ▁ ل چ ّ ہ : ▁ * آ م ی ت گ ء ِ ▁ ج ُ س ت'"
455
+ ]
456
+ },
457
+ "execution_count": 86,
458
+ "metadata": {},
459
+ "output_type": "execute_result"
460
+ }
461
+ ],
462
+ "source": [
463
+ "subword(275)"
464
+ ]
465
+ },
466
+ {
467
+ "cell_type": "code",
468
+ "execution_count": 87,
469
+ "metadata": {},
470
+ "outputs": [
471
+ {
472
+ "data": {
473
+ "text/plain": [
474
+ "(#147) ['xxbos','*','آمیتگءِ','جُستءَمکن','*','لچّہ',':','*','آمیتگءِ','جُستءَمکن'...]"
475
+ ]
476
+ },
477
+ "execution_count": 87,
478
+ "metadata": {},
479
+ "output_type": "execute_result"
480
+ }
481
+ ],
482
+ "source": [
483
+ "toks200 = txts[:200].map(tkn)\n",
484
+ "toks200[0]"
485
+ ]
486
+ },
487
+ {
488
+ "cell_type": "code",
489
+ "execution_count": 89,
490
+ "metadata": {},
491
+ "outputs": [
492
+ {
493
+ "data": {
494
+ "text/plain": [
495
+ "\"(#4096) ['xxunk','xxpad','xxbos','xxeos','xxfld','xxrep','xxwrep','xxup','xxmaj','ءَ','ءِ','ءُ','۔','کہ','،','انت','من','اے','نہ','وتی','بیت','”','ات','چہ','گوں','اَنت','اِنت','پہ','بہ','‘','یک','آئی','.','آ','منی','ھم',')','کنت','بلوچی','3','تو','بلے','ئے',':','کنگ','(','بوتگ','آں','کن','؟'...]\""
496
+ ]
497
+ },
498
+ "execution_count": 89,
499
+ "metadata": {},
500
+ "output_type": "execute_result"
501
+ }
502
+ ],
503
+ "source": [
504
+ "num = Numericalize()\n",
505
+ "num.setup(toks200)\n",
506
+ "coll_repr(num.vocab,50)"
507
+ ]
508
+ },
509
+ {
510
+ "cell_type": "code",
511
+ "execution_count": 90,
512
+ "metadata": {},
513
+ "outputs": [
514
+ {
515
+ "data": {
516
+ "text/plain": [
517
+ "TensorText([ 156, 2340, 0, 156, 563, 43, 156, 2340, 0, 156, 33,\n",
518
+ " 0, 16, 19, 1490, 831, 457, 102, 33, 1031])"
519
+ ]
520
+ },
521
+ "execution_count": 90,
522
+ "metadata": {},
523
+ "output_type": "execute_result"
524
+ }
525
+ ],
526
+ "source": [
527
+ "nums = num(toks)[:20]; nums"
528
+ ]
529
+ },
530
+ {
531
+ "cell_type": "code",
532
+ "execution_count": 91,
533
+ "metadata": {},
534
+ "outputs": [
535
+ {
536
+ "data": {
537
+ "text/plain": [
538
+ "'* آمیتگءِ xxunk * لچّہ : * آمیتگءِ xxunk * آ xxunk من وتی شوکیں کسانی پیر کُت آ میتگءِ'"
539
+ ]
540
+ },
541
+ "execution_count": 91,
542
+ "metadata": {},
543
+ "output_type": "execute_result"
544
+ }
545
+ ],
546
+ "source": [
547
+ "' '.join(num.vocab[o] for o in nums)"
548
+ ]
549
+ },
550
  {
551
  "cell_type": "code",
552
  "execution_count": null,