forked from fastai/fastai
/
text.transform.html
877 lines (611 loc) · 52.3 KB
/
text.transform.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
---
title: text.transform
keywords: fastai
sidebar: home_sidebar
summary: "NLP data processing; tokenizes text and creates vocab indexes"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/text.transform.ipynb
# instructions: https://docs.fast.ai/gen_doc_main.html
-->
<div class="container" id="notebook-container">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="NLP-Preprocessing">NLP Preprocessing<a class="anchor-link" href="#NLP-Preprocessing">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><code>text.tranform</code> contains the functions that deal behind the scenes with the two main tasks when preparing texts for modelling: <em>tokenization</em> and <em>numericalization</em>.</p>
<p><em>Tokenization</em> splits the raw texts into tokens (which can be words, or punctuation signs...). The most basic way to do this would be to separate according to spaces, but it's possible to be more subtle; for instance, the contractions like "isn't" or "don't" should be split in ["is","n't"] or ["do","n't"]. By default fastai will use the powerful <a href="https://spacy.io/api/tokenizer">spacy tokenizer</a>.</p>
<p><em>Numericalization</em> is easier as it just consists in attributing a unique id to each token and mapping each of those tokens to their respective ids.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Tokenization">Tokenization<a class="anchor-link" href="#Tokenization">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Introduction">Introduction<a class="anchor-link" href="#Introduction">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This step is actually divided in two phases: first, we apply a certain list of <code>rules</code> to the raw texts as preprocessing, then we use the tokenizer to split them in lists of tokens. Combining together those <code>rules</code>, the <code>tok_func</code>and the <code>lang</code> to process the texts is the role of the <a href="/text.transform.html#Tokenizer"><code>Tokenizer</code></a> class.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="Tokenizer" class="doc_header"><code>class</code> <code>Tokenizer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L87" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Tokenizer-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>Tokenizer</code>(<strong><code>tok_func</code></strong>:<code>Callable</code>=<strong><em><code>'SpacyTokenizer'</code></em></strong>, <strong><code>lang</code></strong>:<code>str</code>=<strong><em><code>'en'</code></em></strong>, <strong><code>pre_rules</code></strong>:<code>ListRules</code>=<strong><em><code>None</code></em></strong>, <strong><code>post_rules</code></strong>:<code>ListRules</code>=<strong><em><code>None</code></em></strong>, <strong><code>special_cases</code></strong>:<code>StrList</code>=<strong><em><code>None</code></em></strong>, <strong><code>n_cpus</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="Tokenizer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Tokenizer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>Tokenizer</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_tokenize</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L15" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L24" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L32" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Put together rules and a tokenizer function to tokenize text with multiprocessing.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This class will process texts by applying them the <code>pre_rules</code>, tokenizing them with <code>tok_func(lang)</code> and then applying the <code>post_rules</code>. <code>special_cases</code> are a list of tokens passed as special to the tokenizer and <code>n_cpus</code> is the number of cpus to use for multi-processing (by default, half the cpus available). We don't directly pass a tokenizer for multi-processing purposes: each process needs to initiate a tokenizer of its own. The rules and special_cases default to:</p>
<p><code>defaults.text_pre_rules = [fix_html, replace_rep, replace_wrep, spec_add_spaces, rm_useless_spaces]</code> <div style="text-align: right"><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L80">[source]</a></div></p>
<p><code>defaults.text_post_rules = [replace_all_caps, deal_caps]</code> <div style="text-align: right"><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L81">[source]</a></div></p>
<p>and</p>
<p><code>defaults.text_spec_tok = [UNK,PAD,BOS,FLD,TK_MAJ,TK_UP,TK_REP,TK_WREP]</code> <div style="text-align: right"><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L10">[source]</a></div></p>
<p>The rules are all listed below, here is the meaning of the special tokens:</p>
<ul>
<li><code>UNK</code> (xxunk) is for an unknown word (one that isn't present in the current vocabulary)</li>
<li><code>PAD</code> (xxpad) is the token used for padding, if we need to regroup several texts of different lengths in a batch</li>
<li><code>BOS</code> (xxbos) represents the beginning of a text in your dataset</li>
<li><code>FLD</code> (xxfld) is used if you set <code>mark_fields=True</code> in your <a href="/text.data.html#TokenizeProcessor"><code>TokenizeProcessor</code></a> to separate the different fields of texts (if your texts are loaded from several columns in a dataframe)</li>
<li><code>TK_MAJ</code> (xxmaj) is used to indicate the next word begins with a capital in the original text</li>
<li><code>TK_UP</code> (xxup) is used to indicate the next word is written in all caps in the original text</li>
<li><code>TK_REP</code> (xxrep) is used to indicate the next character is repeated n times in the original text (usage xxrep n {char})</li>
<li><code>TK_WREP</code>(xxwrep) is used to indicate the next word is repeated n times in the original text (usage xxwrep n {word})</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="Tokenizer.process_text" class="doc_header"><code>process_text</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L103" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Tokenizer-process_text-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>process_text</code>(<strong><code>t</code></strong>:<code>str</code>, <strong><code>tok</code></strong>:<a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a>) → <code>List</code>[<code>str</code>]</p>
</blockquote>
<div class="collapse" id="Tokenizer-process_text-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Tokenizer-process_text-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>process_text</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Process one text <code>t</code> with tokenizer <code>tok</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="Tokenizer.process_all" class="doc_header"><code>process_all</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L116" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Tokenizer-process_all-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>process_all</code>(<strong><code>texts</code></strong>:<code>StrList</code>) → <code>List</code>[<code>List</code>[<code>str</code>]]</p>
</blockquote>
<div class="collapse" id="Tokenizer-process_all-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Tokenizer-process_all-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>process_all</code>:</p><p>Some other tests where <code>process_all</code> is used:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_tokenize</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L15" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L24" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L32" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Process a list of <code>texts</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For an example, we're going to grab some IMDB reviews.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">path</span> <span class="o">=</span> <span class="n">untar_data</span><span class="p">(</span><span class="n">URLs</span><span class="o">.</span><span class="n">IMDB_SAMPLE</span><span class="p">)</span>
<span class="n">path</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>PosixPath('/home/ubuntu/.fastai/data/imdb_sample')</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="o">/</span><span class="s1">'texts.csv'</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">example_text</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span> <span class="n">example_text</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'This is a extremely well-made film. The acting, script and camera-work are all first-rate. The music is good, too, though it is mostly early in the film, when things are still relatively cheery. There are no really superstars in the cast, though several faces will be familiar. The entire cast does an excellent job with the script.<br /><br />But it is hard to watch, because there is no good end to a situation like the one presented. It is now fashionable to blame the British for setting Hindus and Muslims against each other, and then cruelly separating them into two countries. There is some merit in this view, but it\'s also true that no one forced Hindus and Muslims in the region to mistreat each other as they did around the time of partition. It seems more likely that the British simply saw the tensions between the religions and were clever enough to exploit them to their own ends.<br /><br />The result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen. But it is never painted as a black-and-white case. There is baseness and nobility on both sides, and also the hope for change in the younger generation.<br /><br />There is redemption of a sort, in the end, when Puro has to make a hard choice between a man who has ruined her life, but also truly loved her, and her family which has disowned her, then later come looking for her. But by that point, she has no option that is without great pain for her.<br /><br />This film carries the message that both Muslims and Hindus have their grave faults, and also that both can be dignified and caring people. The reality of partition makes that realisation all the more wrenching, since there can never be real reconciliation across the India/Pakistan border. In that sense, it is similar to "Mr & Mrs Iyer".<br /><br />In the end, we were glad to have seen the film, even though the resolution was heartbreaking. If the UK and US could deal with their own histories of racism with this kind of frankness, they would certainly be better off.'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">tokenizer</span> <span class="o">=</span> <span class="n">Tokenizer</span><span class="p">()</span>
<span class="n">tok</span> <span class="o">=</span> <span class="n">SpacyTokenizer</span><span class="p">(</span><span class="s1">'en'</span><span class="p">)</span>
<span class="s1">' '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">tokenizer</span><span class="o">.</span><span class="n">process_text</span><span class="p">(</span><span class="n">example_text</span><span class="p">,</span> <span class="n">tok</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'xxmaj this is a extremely well - made film . xxmaj the acting , script and camera - work are all first - rate . xxmaj the music is good , too , though it is mostly early in the film , when things are still relatively cheery . xxmaj there are no really superstars in the cast , though several faces will be familiar . xxmaj the entire cast does an excellent job with the script . \n\n xxmaj but it is hard to watch , because there is no good end to a situation like the one presented . xxmaj it is now fashionable to blame the xxmaj british for setting xxmaj hindus and xxmaj muslims against each other , and then cruelly separating them into two countries . xxmaj there is some merit in this view , but it \'s also true that no one forced xxmaj hindus and xxmaj muslims in the region to mistreat each other as they did around the time of partition . xxmaj it seems more likely that the xxmaj british simply saw the tensions between the religions and were clever enough to exploit them to their own ends . \n\n xxmaj the result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen . xxmaj but it is never painted as a black - and - white case . xxmaj there is baseness and nobility on both sides , and also the hope for change in the younger generation . \n\n xxmaj there is redemption of a sort , in the end , when xxmaj puro has to make a hard choice between a man who has ruined her life , but also truly loved her , and her family which has disowned her , then later come looking for her . xxmaj but by that point , she has no option that is without great pain for her . \n\n xxmaj this film carries the message that both xxmaj muslims and xxmaj hindus have their grave faults , and also that both can be dignified and caring people . xxmaj the reality of partition makes that realisation all the more wrenching , since there can never be real reconciliation across the xxmaj india / xxmaj pakistan border . xxmaj in that sense , it is similar to " xxmaj mr & xxmaj mrs xxmaj iyer " . \n\n xxmaj in the end , we were glad to have seen the film , even though the resolution was heartbreaking . xxmaj if the xxup uk and xxup us could deal with their own histories of racism with this kind of frankness , they would certainly be better off .'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>As explained before, the tokenizer split the text according to words/punctuations signs but in a smart manner. The rules (see below) also have modified the text a little bit. We can tokenize a list of texts directly at the same time:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="o">/</span><span class="s1">'texts.csv'</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">texts</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">tokenizer</span> <span class="o">=</span> <span class="n">Tokenizer</span><span class="p">()</span>
<span class="n">tokens</span> <span class="o">=</span> <span class="n">tokenizer</span><span class="o">.</span><span class="n">process_all</span><span class="p">(</span><span class="n">texts</span><span class="p">)</span>
<span class="s1">' '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">tokens</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'xxmaj this is a extremely well - made film . xxmaj the acting , script and camera - work are all first - rate . xxmaj the music is good , too , though it is mostly early in the film , when things are still relatively cheery . xxmaj there are no really superstars in the cast , though several faces will be familiar . xxmaj the entire cast does an excellent job with the script . \n\n xxmaj but it is hard to watch , because there is no good end to a situation like the one presented . xxmaj it is now fashionable to blame the xxmaj british for setting xxmaj hindus and xxmaj muslims against each other , and then cruelly separating them into two countries . xxmaj there is some merit in this view , but it \'s also true that no one forced xxmaj hindus and xxmaj muslims in the region to mistreat each other as they did around the time of partition . xxmaj it seems more likely that the xxmaj british simply saw the tensions between the religions and were clever enough to exploit them to their own ends . \n\n xxmaj the result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen . xxmaj but it is never painted as a black - and - white case . xxmaj there is baseness and nobility on both sides , and also the hope for change in the younger generation . \n\n xxmaj there is redemption of a sort , in the end , when xxmaj puro has to make a hard choice between a man who has ruined her life , but also truly loved her , and her family which has disowned her , then later come looking for her . xxmaj but by that point , she has no option that is without great pain for her . \n\n xxmaj this film carries the message that both xxmaj muslims and xxmaj hindus have their grave faults , and also that both can be dignified and caring people . xxmaj the reality of partition makes that realisation all the more wrenching , since there can never be real reconciliation across the xxmaj india / xxmaj pakistan border . xxmaj in that sense , it is similar to " xxmaj mr & xxmaj mrs xxmaj iyer " . \n\n xxmaj in the end , we were glad to have seen the film , even though the resolution was heartbreaking . xxmaj if the xxup uk and xxup us could deal with their own histories of racism with this kind of frankness , they would certainly be better off .'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Customize-the-tokenizer">Customize the tokenizer<a class="anchor-link" href="#Customize-the-tokenizer">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The <code>tok_func</code> must return an instance of <a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a>:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="BaseTokenizer" class="doc_header"><code>class</code> <code>BaseTokenizer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L16" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#BaseTokenizer-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>BaseTokenizer</code>(<strong><code>lang</code></strong>:<code>str</code>)</p>
</blockquote>
<div class="collapse" id="BaseTokenizer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#BaseTokenizer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>BaseTokenizer</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Basic class for a tokenizer function.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="BaseTokenizer.tokenizer" class="doc_header"><code>tokenizer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L19" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#BaseTokenizer-tokenizer-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>tokenizer</code>(<strong><code>t</code></strong>:<code>str</code>) → <code>List</code>[<code>str</code>]</p>
</blockquote>
<div class="collapse" id="BaseTokenizer-tokenizer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#BaseTokenizer-tokenizer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>tokenizer</code>:</p><p>Some other tests where <code>tokenizer</code> is used:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_tokenize</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L15" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L24" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L32" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Take a text <code>t</code> and returns the list of its tokens.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="BaseTokenizer.add_special_cases" class="doc_header"><code>add_special_cases</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L20" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#BaseTokenizer-add_special_cases-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>add_special_cases</code>(<strong><code>toks</code></strong>:<code>StrList</code>)</p>
</blockquote>
<div class="collapse" id="BaseTokenizer-add_special_cases-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#BaseTokenizer-add_special_cases-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>add_special_cases</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Record a list of special tokens <code>toks</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The fastai library uses <a href="https://spacy.io/">spacy</a> tokenizers as its default. The following class wraps it as <a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="SpacyTokenizer" class="doc_header"><code>class</code> <code>SpacyTokenizer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L22" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#SpacyTokenizer-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>SpacyTokenizer</code>(<strong><code>lang</code></strong>:<code>str</code>) :: <a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a></p>
</blockquote>
<div class="collapse" id="SpacyTokenizer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#SpacyTokenizer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>SpacyTokenizer</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Wrapper around a spacy tokenizer to make it a <a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If you want to use your custom tokenizer, just subclass the <a href="/text.transform.html#BaseTokenizer"><code>BaseTokenizer</code></a> and override its <code>tokenizer</code> and <code>add_spec_cases</code> functions.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Rules">Rules<a class="anchor-link" href="#Rules">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Rules are just functions that take a string and return the modified string. This allows you to customize the list of <code>default_pre_rules</code> or <code>default_post_rules</code> as you please. Those are:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="deal_caps" class="doc_header"><code>deal_caps</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L75" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#deal_caps-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>deal_caps</code>(<strong><code>x</code></strong>:<code>StrList</code>) → <code>StrList</code></p>
</blockquote>
<div class="collapse" id="deal_caps-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#deal_caps-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>deal_caps</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In <code>t</code>, every word is lower-case. If a word begins with a capital, we put a token <code>TK_MAJ</code> in front of it.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="fix_html" class="doc_header"><code>fix_html</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L58" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#fix_html-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>fix_html</code>(<strong><code>x</code></strong>:<code>str</code>) → <code>str</code></p>
</blockquote>
<div class="collapse" id="fix_html-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#fix_html-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>fix_html</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This rules replaces a bunch of HTML characters or norms in plain text ones. For instance <code><br /></code> are replaced by <code>\n</code>, <code>&nbsp;</code> by spaces etc...</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">fix_html</span><span class="p">(</span><span class="s2">"Some HTML&nbsp;text<br />"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'Some HTML& text\n'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="replace_all_caps" class="doc_header"><code>replace_all_caps</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L67" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#replace_all_caps-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>replace_all_caps</code>(<strong><code>x</code></strong>:<code>StrList</code>) → <code>StrList</code></p>
</blockquote>
<div class="collapse" id="replace_all_caps-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#replace_all_caps-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>replace_all_caps</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Replace tokens in ALL CAPS in <code>x</code> by their lower version and add <code>TK_UP</code> before.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="replace_rep" class="doc_header"><code>replace_rep</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L42" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#replace_rep-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>replace_rep</code>(<strong><code>t</code></strong>:<code>str</code>) → <code>str</code></p>
</blockquote>
<div class="collapse" id="replace_rep-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#replace_rep-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>replace_rep</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Whenever a character is repeated more than three times in <code>t</code>, we replace the whole thing by 'TK_REP n char' where n is the number of occurrences and char the character.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">replace_rep</span><span class="p">(</span><span class="s2">"I'm so excited!!!!!!!!"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>"I'm so excited xxrep 8 ! "</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="replace_wrep" class="doc_header"><code>replace_wrep</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L50" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#replace_wrep-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>replace_wrep</code>(<strong><code>t</code></strong>:<code>str</code>) → <code>str</code></p>
</blockquote>
<div class="collapse" id="replace_wrep-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#replace_wrep-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>replace_wrep</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Whenever a word is repeated more than four times in <code>t</code>, we replace the whole thing by 'TK_WREP n w' where n is the number of occurrences and w the word repeated.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">replace_wrep</span><span class="p">(</span><span class="s2">"I've never ever ever ever ever ever ever ever done this."</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>"I've never xxwrep 7 ever done this."</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="rm_useless_spaces" class="doc_header"><code>rm_useless_spaces</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L38" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#rm_useless_spaces-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>rm_useless_spaces</code>(<strong><code>t</code></strong>:<code>str</code>) → <code>str</code></p>
</blockquote>
<div class="collapse" id="rm_useless_spaces-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#rm_useless_spaces-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>rm_useless_spaces</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Remove multiple spaces in <code>t</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">rm_useless_spaces</span><span class="p">(</span><span class="s2">"Inconsistent use of spaces."</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'Inconsistent use of spaces.'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="spec_add_spaces" class="doc_header"><code>spec_add_spaces</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L34" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#spec_add_spaces-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>spec_add_spaces</code>(<strong><code>t</code></strong>:<code>str</code>) → <code>str</code></p>
</blockquote>
<div class="collapse" id="spec_add_spaces-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#spec_add_spaces-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>spec_add_spaces</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_rules</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L5" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Add spaces around / and # in <code>t</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">spec_add_spaces</span><span class="p">(</span><span class="s1">'I #like to #put #hashtags #everywhere!'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'I # like to # put # hashtags # everywhere!'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Numericalization">Numericalization<a class="anchor-link" href="#Numericalization">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To convert our set of tokens to unique ids (and be able to have them go through embeddings), we use the following class:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="Vocab" class="doc_header"><code>class</code> <code>Vocab</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L122" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Vocab-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>Vocab</code>(<strong><code>itos</code></strong>:<code>StrList</code>)</p>
</blockquote>
<div class="collapse" id="Vocab-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Vocab-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>Vocab</code>:</p><p>Some other tests where <code>Vocab</code> is used:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_numericalize_and_textify</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L39" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Contain the correspondence between numbers and tokens and numericalize.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><code>itos</code> contains the id to token correspondence.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="Vocab.create" class="doc_header"><code>create</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L148" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Vocab-create-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>create</code>(<strong><code>tokens</code></strong>:<code>Tokens</code>, <strong><code>max_vocab</code></strong>:<code>int</code>, <strong><code>min_freq</code></strong>:<code>int</code>) → <code>Vocab</code></p>
</blockquote>
<div class="collapse" id="Vocab-create-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Vocab-create-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>create</code>:</p><p>Some other tests where <code>create</code> is used:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_numericalize_and_textify</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L39" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Create a vocabulary from a set of <code>tokens</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Only keeps <code>max_vocab</code> tokens, and only if they appear at least <code>min_freq</code> times, set the rest to <code>UNK</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="Vocab.numericalize" class="doc_header"><code>numericalize</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L128" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Vocab-numericalize-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>numericalize</code>(<strong><code>t</code></strong>:<code>StrList</code>) → <code>List</code>[<code>int</code>]</p>
</blockquote>
<div class="collapse" id="Vocab-numericalize-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Vocab-numericalize-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>numericalize</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_numericalize_and_textify</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L39" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Convert a list of tokens <code>t</code> to their ids.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="Vocab.textify" class="doc_header"><code>textify</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/transform.py#L132" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Vocab-textify-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>textify</code>(<strong><code>nums</code></strong>:<code>Collection</code>[<code>int</code>], <strong><code>sep</code></strong>=<strong><em><code>' '</code></em></strong>) → <code>List</code>[<code>str</code>]</p>
</blockquote>
<div class="collapse" id="Vocab-textify-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Vocab-textify-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>textify</code>:</p><ul><li><code>pytest -sv tests/test_text_transform.py::test_numericalize_and_textify</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_transform.py#L39" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Convert a list of <code>nums</code> to their tokens.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">vocab</span> <span class="o">=</span> <span class="n">Vocab</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">tokens</span><span class="p">,</span> <span class="n">max_vocab</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">min_freq</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">vocab</span><span class="o">.</span><span class="n">numericalize</span><span class="p">(</span><span class="n">tokens</span><span class="p">[</span><span class="mi">2</span><span class="p">])[:</span><span class="mi">10</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>[4, 20, 15, 12, 623, 89, 23, 115, 31, 10]</pre>
</div>
</div>
</div>
</div>
</div>
</div>