{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2020-12-04 09:25:00-- https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt\n", "Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'\n", "Resolving s3.amazonaws.com (s3.amazonaws.com)... 52.216.104.253\n", "Connecting to s3.amazonaws.com (s3.amazonaws.com)|52.216.104.253|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 231508 (226K) [text/plain]\n", "Saving to: ‘/tmp/bert-base-uncased-vocab.txt’\n", "\n", "/tmp/bert-base-unca 100%[===================>] 226.08K --.-KB/s in 0.06s \n", "\n", "2020-12-04 09:25:00 (3.87 MB/s) - ‘/tmp/bert-base-uncased-vocab.txt’ saved [231508/231508]\n", "\n" ] } ], "source": [ "!wget https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt -O /tmp/bert-base-uncased-vocab.txt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from tokenizers import BertWordPieceTokenizer\n", "from tokenizers.tools import EncodingVisualizer\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "EncodingVisualizer.unk_token_regex.search(\"aaa[udsnk]aaa\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "text = \"\"\"Mathias Bynens 'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞': Whenever you’re working on a piece of JavaScript code that deals with strings or regular expressions in some way, just add a unit test that contains a pile of poo (💩) in a string, 💩💩💩💩💩💩💩💩💩💩💩💩 and see if anything breaks. It’s a quick, fun, and easy way to see if your code supports astral symbols. Once you’ve found a Unicode-related bug in your code, all you need to do is apply the techniques discussed in this post to fix it.\"\"\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "tokenizer = BertWordPieceTokenizer(\"/tmp/bert-base-uncased-vocab.txt\", lowercase=True)\n", "visualizer = EncodingVisualizer(tokenizer=tokenizer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing Tokens With No Annotations" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n", " Mathias Bynens 'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞': Whenever youre working on a piece of JavaScript code that deals with strings or regular expressions in some way, just add a unit test that contains a pile of poo (💩) in a string, 💩💩💩💩💩💩💩💩💩💩💩💩 and see if anything breaks. Its a quick, fun, and easy way to see if your code supports astral symbols. Once youve found a Unicode-related bug in your code, all you need to do is apply the techniques discussed in this post to fix it.\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "visualizer(text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing Tokens With Aligned Annotations\n", "First we make some annotations with the Annotation class" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from tokenizers.tools import Annotation" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "anno1 = Annotation(start=0, end=2, label=\"foo\")\n", "anno2 = Annotation(start=2, end=4, label=\"bar\")\n", "anno3 = Annotation(start=6, end=8, label=\"poo\")\n", "anno4 = Annotation(start=9, end=12, label=\"shoe\")\n", "annotations=[\n", " anno1,\n", " anno2,\n", " anno3,\n", " anno4,\n", " Annotation(start=23, end=30, label=\"random tandem bandem sandem landem fandom\"),\n", " Annotation(start=63, end=70, label=\"foo\"),\n", " Annotation(start=80, end=95, label=\"bar\"),\n", " Annotation(start=120, end=128, label=\"bar\"),\n", " Annotation(start=152, end=155, label=\"poo\"),\n", "]\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n", " Mathias Bynens 'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞': Whenever youre working on a piece of JavaScript code that deals with strings or regular expressions in some way, just add a unit test that contains a pile of poo (💩) in a string, 💩💩💩💩💩💩💩💩💩💩💩💩 and see if anything breaks. Its a quick, fun, and easy way to see if your code supports astral symbols. Once youve found a Unicode-related bug in your code, all you need to do is apply the techniques discussed in this post to fix it.\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "visualizer(text,annotations=annotations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using A Custom Annotation Format\n", "Every system has its own representation of annotations. That's why we can instantiate the EncodingVisualizer with a convertion function." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'startPlace': 0, 'endPlace': 3, 'theTag': '0'},\n", " {'startPlace': 4, 'endPlace': 7, 'theTag': '4'},\n", " {'startPlace': 8, 'endPlace': 11, 'theTag': '8'},\n", " {'startPlace': 12, 'endPlace': 15, 'theTag': '12'},\n", " {'startPlace': 16, 'endPlace': 19, 'theTag': '16'}]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "funnyAnnotations = [dict(startPlace=i,endPlace=i+3,theTag=str(i)) for i in range(0,20,4)]\n", "funnyAnnotations" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "converter = lambda funny: Annotation(start=funny['startPlace'], end=funny['endPlace'], label=funny['theTag'])\n", "visualizer = EncodingVisualizer(tokenizer=tokenizer, default_to_notebook=True, annotation_converter=converter)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n", " Mathias Bynens 'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞': Whenever youre working on a piece of JavaScript code that deals with strings or regular expressions in some way, just add a unit test that contains a pile of poo (💩) in a string, 💩💩💩💩💩💩💩💩💩💩💩💩 and see if anything breaks. Its a quick, fun, and easy way to see if your code supports astral symbols. Once youve found a Unicode-related bug in your code, all you need to do is apply the techniques discussed in this post to fix it.\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "visualizer(text, annotations=funnyAnnotations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trying with Roberta\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2020-12-04 09:25:00-- https://s3.amazonaws.com/models.huggingface.co/bert/roberta-base-vocab.json\n", "Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'\n", "Resolving s3.amazonaws.com (s3.amazonaws.com)... 52.216.226.19\n", "Connecting to s3.amazonaws.com (s3.amazonaws.com)|52.216.226.19|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 898823 (878K) [application/json]\n", "Saving to: ‘/tmp/roberta-base-vocab.json’\n", "\n", "/tmp/roberta-base-v 100%[===================>] 877.76K 4.35MB/s in 0.2s \n", "\n", "2020-12-04 09:25:00 (4.35 MB/s) - ‘/tmp/roberta-base-vocab.json’ saved [898823/898823]\n", "\n", "--2020-12-04 09:25:00-- https://s3.amazonaws.com/models.huggingface.co/bert/roberta-base-merges.txt\n", "Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'\n", "Resolving s3.amazonaws.com (s3.amazonaws.com)... 52.216.104.253\n", "Connecting to s3.amazonaws.com (s3.amazonaws.com)|52.216.104.253|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 456318 (446K) [text/plain]\n", "Saving to: ‘/tmp/roberta-base-merges.txt’\n", "\n", "/tmp/roberta-base-m 100%[===================>] 445.62K --.-KB/s in 0.1s \n", "\n", "2020-12-04 09:25:01 (4.04 MB/s) - ‘/tmp/roberta-base-merges.txt’ saved [456318/456318]\n", "\n" ] } ], "source": [ "!wget \"https://s3.amazonaws.com/models.huggingface.co/bert/roberta-base-vocab.json\" -O /tmp/roberta-base-vocab.json\n", "!wget \"https://s3.amazonaws.com/models.huggingface.co/bert/roberta-base-merges.txt\" -O /tmp/roberta-base-merges.txt\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n", " Mathias Bynens 'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞': Whenever youre working on a piece of JavaScript code that deals with strings or regular expressions in some way, just add a unit test that contains a pile of poo (💩) in a string, 💩💩💩💩💩💩💩💩💩💩💩💩 and see if anything breaks. Its a quick, fun, and easy way to see if your code supports astral symbols. Once youve found a Unicode-related bug in your code, all you need to do is apply the techniques discussed in this post to fix it.\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from tokenizers import ByteLevelBPETokenizer\n", "roberta_tokenizer = ByteLevelBPETokenizer.from_file('/tmp/roberta-base-vocab.json', '/tmp/roberta-base-merges.txt')\n", "roberta_visualizer = EncodingVisualizer(tokenizer=roberta_tokenizer, default_to_notebook=True)\n", "roberta_visualizer(text, annotations=annotations)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }