{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Spelling Correction using Symspeller" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [Malaya/example/spelling-correction-symspell](https://github.com/huseinzol05/Malaya/tree/master/example/spelling-correction-symspell).\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dependencies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This spelling correction is an improvement version for https://github.com/mammothb/symspellpy to adapt with our local shortform / typos. Before you able to use this spelling correction, you need to install,\n", "\n", "```bash\n", "pip install symspellpy\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:torch.distributed.nn.jit.instantiator:Created a temporary directory at /tmp/tmp8er24wly\n", "INFO:torch.distributed.nn.jit.instantiator:Writing /tmp/tmp8er24wly/_remote_module_non_scriptable.py\n", "/home/husein/dev/malaya/malaya/tokenizer.py:214: FutureWarning: Possible nested set at position 3397\n", " self.tok = re.compile(r'({})'.format('|'.join(pipeline)))\n", "/home/husein/dev/malaya/malaya/tokenizer.py:214: FutureWarning: Possible nested set at position 3927\n", " self.tok = re.compile(r'({})'.format('|'.join(pipeline)))\n" ] } ], "source": [ "import malaya" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# some text examples copied from Twitter\n", "\n", "string1 = 'krajaan patut bagi pencen awal skt kpd warga emas supaya emosi'\n", "string2 = 'Husein ska mkn aym dkat kampng Jawa'\n", "string3 = 'Melayu malas ni narration dia sama je macam men are trash. True to some, false to some.'\n", "string4 = 'Tapi tak pikir ke bahaya perpetuate myths camtu. Nanti kalau ada hiring discrimination despite your good qualifications because of your race tau pulak marah. Your kids will be victims of that too.'\n", "string5 = 'DrM cerita Melayu malas semenjak saya kat University (early 1980s) and now as i am edging towards retirement in 4-5 years time after a career of being an Engineer, Project Manager, General Manager'\n", "string6 = 'blh bntg dlm kls nlp sy, nnti intch'\n", "string7 = 'mulakn slh org boleh ,bila geng tuh kena slhkn jgk xboleh trima .. pelik'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load symspeller model\n", "\n", "```python\n", "def load(\n", " max_edit_distance_dictionary: int = 2,\n", " prefix_length: int = 7,\n", " term_index: int = 0,\n", " count_index: int = 1,\n", " top_k: int = 10,\n", " **kwargs\n", "):\n", " \"\"\"\n", " Load a symspell Spell Corrector for Malay.\n", "\n", " Returns\n", " -------\n", " result: malaya.spelling_correction.symspell.Symspell class\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:malaya_boilerplate.huggingface:downloading frozen huseinzol05/v27-preprocessing/bm_1grams.txt\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8653a1e4372349439a1040da7a9f56ab", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading (…)e/main/bm_1grams.txt: 0%| | 0.00/2.41M [00:00