Spelling Correction using JamSpell#

This tutorial is available as an IPython notebook at Malaya/example/spelling-correction-jamspell.

[1]:
import logging

logging.basicConfig(level=logging.INFO)
[2]:
import malaya
INFO:torch.distributed.nn.jit.instantiator:Created a temporary directory at /tmp/tmp0bb_h21m
INFO:torch.distributed.nn.jit.instantiator:Writing /tmp/tmp0bb_h21m/_remote_module_non_scriptable.py
/home/husein/dev/malaya/malaya/tokenizer.py:214: FutureWarning: Possible nested set at position 3397
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
/home/husein/dev/malaya/malaya/tokenizer.py:214: FutureWarning: Possible nested set at position 3927
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
[3]:
# some text examples copied from Twitter

string1 = 'krajaan patut bagi pencen awal skt kpd warga emas supaya emosi'
string2 = 'Husein ska mkn aym dkat kampng Jawa'
string3 = 'Melayu malas ni narration dia sama je macam men are trash. True to some, false to some.'
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.'
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'
string6 = 'blh bntg dlm kls nlp sy, nnti intch'
string7 = 'mulakn slh org boleh ,bila geng tuh kena slhkn jgk xboleh trima .. pelik'

List available models#

[4]:
malaya.spelling_correction.jamspell.available_model
[4]:
{'wiki+news': {'Size (MB)': 337},
 'wiki': {'Size (MB)': 148},
 'news': {'Size (MB)': 215}}

Load JamSpell speller#

JamSpell use Norvig + Ngram words.

Before you able to use this spelling correction, you need to install jamspell,

For mac,

wget http://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz
tar -zxf swig-3.0.12.tar.gz
./swig-3.0.12/configure && make && make install
pip3 install jamspell

For debian / ubuntu,

apt install swig3.0 -y
pip3 install jamspell
def jamspell(model: str = 'wiki+news', **kwargs):
    """
    Load a jamspell Spell Corrector for Malay.

    Parameters
    ----------
    model: str, optional (default='wiki+news')
        Supported models. Allowed values:

        * ``'wiki+news'`` - Wikipedia + News, 337MB.
        * ``'wiki'`` - Wikipedia, 148MB.
        * ``'news'`` - Wikipedia, 215MB.

    Returns
    -------
    result: malaya.spell.JamSpell class
    """
[23]:
model = malaya.spelling_correction.jamspell.load(model = 'wiki')

List possible generated pool of words#

def edit_candidates(self, word: str, string: List[str], index: int = -1):
    """
    Generate candidates given a word.

    Parameters
    ----------
    word: str
    string: str
        Entire string, `word` must a word inside `string`.
    index: int, optional(default=-1)
        index of word in the string, if -1, will try to use `string.index(word)`.

    Returns
    -------
    result: List[str]
    """
[10]:
model.edit_candidates('suke', 'saya suke makan ayem'.split())
[10]:
('suka',
 'suke',
 'suku',
 'duke',
 'luke',
 'suk',
 'sue',
 'suki',
 'sake',
 'ske',
 'soke',
 'sure',
 'yuke',
 'suko',
 'puke')

To correct a word#

def correct(self, word: str, string: List[str], index: int = -1):
    """
    Correct a word within a text, returning the corrected word.

    Parameters
    ----------
    word: str
    string: List[str]
        Tokenized string, `word` must a word inside `string`.
    index: int, optional(default=-1)
        index of word in the string, if -1, will try to use `string.index(word)`.

    Returns
    -------
    result: str
    """
[11]:
model.correct('suke', 'saya suke makan ayem'.split())
[11]:
'suka'
[12]:
splitted = string1.split()
model.correct('kpd', splitted)
[12]:
'kpd'
[13]:
model.correct('krajaan', splitted)
[13]:
'krajaan'

To correct a sentence#

def correct_text(self, text: str):
    """
    Correct all the words within a text, returning the corrected text.

    Parameters
    ----------
    text: str

    Returns
    -------
    result: str
    """
[14]:
model.correct_text(string1)
[14]:
'krajaan patut bagi pencen awal set kpd warga emas supaya emosi'
[15]:
tokenizer = malaya.tokenizer.Tokenizer()
[16]:
string2
[16]:
'Husein ska mkn aym dkat kampng Jawa'
[17]:
tokenized = tokenizer.tokenize(string2)
model.correct_text(' '.join(tokenized))
[17]:
'Husein ska mkn aym dkat kampng Jawa'
[18]:
tokenized = tokenizer.tokenize(string3)
model.correct_text(' '.join(tokenized))
[18]:
'Melayu malas ni narration dia sama je macam men are trash . True to some , false to some .'
[19]:
tokenized = tokenizer.tokenize(string5)
model.correct_text(' '.join(tokenized))
[19]:
'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'
[20]:
tokenized = tokenizer.tokenize(string6)
model.correct_text(' '.join(tokenized))
[20]:
'blh yang dlm kes alp sy , nnti inch'
[21]:
tokenized = tokenizer.tokenize(string7)
model.correct_text(' '.join(tokenized))
[21]:
'mulan slh org boleh , bila geng tuh kena salah jgk boleh trima . . pelik'
[22]:
string7
[22]:
'mulakn slh org boleh ,bila geng tuh kena slhkn jgk xboleh trima .. pelik'