{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BsRfx3DyAHC5"
},
"source": [
"# Exploring Unicode categories\n",
"\n",
"Anyone working with data will sooner or later come across Unicode. In this notebook we're going to download Unicode data directly from the official source and explore the various categories of Unicode characters using interactive Bokeh tables"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mRDzwN2HAHC6"
},
"source": [
"The file `UnicodeData.txt`, available from [unicode.org](https://www.unicode.org), the home of Unicode, is the core of the Unicode Character Database (UCD). It is an ASCII file that serves as the foundational data source for Unicode-related functionality, including Python's [unicodedata](https://docs.python.org/3/library/unicodedata.html) module.\n",
"\n",
"The file contains detailed information about each Unicode character, including its properties and categorizations. The structure of `UnicodeData.txt` (i.e., its columns) and descriptions of the Unicode general categories are documented in the [Unicode® Standard Annex #44](https://www.unicode.org/reports/tr44). For direct reference, you can find information about the [columns of UnicodeData.txt](https://www.unicode.org/reports/tr44/#UnicodeData.txt) and the [list of categories](https://www.unicode.org/reports/tr44/#GC_Values_Table) in the annex."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cX1_7jw6AHC6"
},
"source": [
"## Download the data\n",
"\n",
"Download the file `UnicodeData.txt` and save it to the dataframe `df`. Download categories and save them to the dictionary `categories`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"execution": {
"iopub.execute_input": "2024-10-13T09:45:19.959122Z",
"iopub.status.busy": "2024-10-13T09:45:19.958567Z",
"iopub.status.idle": "2024-10-13T09:45:20.809813Z",
"shell.execute_reply": "2024-10-13T09:45:20.809119Z"
},
"id": "UVJYpfi7AHC7",
"outputId": "30716ba4-572a-4342-c08a-0086acc70fa5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of Unicode data dataframe: 40116\n"
]
}
],
"source": [
"import urllib.request\n",
"import pandas as pd\n",
"from collections import defaultdict\n",
"import re\n",
"\n",
"url = 'https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt' # unicode data\n",
"url_cat = 'https://unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt' # ctegories\n",
"\n",
"df = pd.read_csv(url, sep=\";\", header=None)\n",
"df.columns = [str(c) for c in df.columns]\n",
"\n",
"categories = defaultdict(str)\n",
"for line in urllib.request.urlopen(url_cat).readlines():\n",
" line = line.decode('utf-8')\n",
" if line.startswith('gc'):\n",
" name, desc = re.split(r'\\s*;\\s*', line.strip())[1:3]\n",
" desc = desc.split()[0]\n",
" if len(name)>1:\n",
" categories[name] = desc\n",
"\n",
"\n",
"print(\"Length of Unicode data dataframe: {}\".format(len(df.index)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9I72cVMQAHC7"
},
"source": [
"## List categories\n",
"\n",
"The Unicode standard has 31 different categories."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 445
},
"execution": {
"iopub.execute_input": "2024-10-13T09:45:20.841426Z",
"iopub.status.busy": "2024-10-13T09:45:20.840951Z",
"iopub.status.idle": "2024-10-13T09:45:21.224090Z",
"shell.execute_reply": "2024-10-13T09:45:21.223507Z"
},
"id": "kR4pw-7tAHC8",
"outputId": "5266f1c2-78d7-400f-bfb1-ca9375505d84"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"
\\n\"+\n",
" \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"
\\n\"+\n \"\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"
\\n\"+\n",
" \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"
\\n\"+\n \"