Spelling Correction using Spylls#

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

Dependencies#

Spylls is Hunspell ported to Python.

Before you able to use this spelling correction, you need to install https://github.com/zverok/spylls,

pip3 install Spylls
[1]:
import logging

logging.basicConfig(level=logging.INFO)
[2]:
import malaya
INFO:numexpr.utils:NumExpr defaulting to 8 threads.
[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'

Load Spylls model#

def load(model: str = 'libreoffice-pejam', **kwargs):
    """
    Load a spylls Spell Corrector for Malay.

    Parameters
    ----------
    model : str, optional (default='libreoffice-pejam')
        Model spelling correction supported. Allowed values:

        * ``'libreoffice-pejam'`` - from LibreOffice pEJAm, https://extensions.libreoffice.org/en/extensions/show/3868

    Returns
    -------
    result: malaya.spelling_correction.spylls.Spylls class
    """
[4]:
model = malaya.spelling_correction.spylls.load()
INFO:malaya_boilerplate.huggingface:downloading frozen huseinzol05/v46-preprocessing/pEJAm21z.oxt

List possible generated pool of words#

def edit_candidates(self, word):
    """
    Generate candidates given a word.

    Parameters
    ----------
    word: str

    Returns
    -------
    result: List[str]
    """
[5]:
model.edit_candidates('mhthir')
[5]:
['Mahathir']
[6]:
model.edit_candidates('smbng')
[6]:
['sbng', 'smbang', 'jmbng', 'cmbng']

To correct a word#

def correct(self, word: str, **kwargs):
    """
    Most probable spelling correction for word.

    Parameters
    ----------
    word: str

    Returns
    -------
    result: str
    """
[7]:
model.correct('suke')
[7]:
'suka'
[8]:
model.correct('kpd')
[8]:
'kepada'
[9]:
model.correct('krajaan')
[9]:
'kerajaan'

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
    """
[10]:
model.correct_text(string1)
[10]:
'kerajaan patut bagi pencen awal tks kepada warga emas supaya emosi'
[11]:
tokenizer = malaya.tokenizer.Tokenizer()
[12]:
string2
[12]:
'Husein ska mkn aym dkat kampng Jawa'
[13]:
tokenized = tokenizer.tokenize(string2)
model.correct_text(' '.join(tokenized))
[13]:
'Hussein suka mkn aum tkad kampang Jawa'
[14]:
tokenized = tokenizer.tokenize(string3)
model.correct_text(' '.join(tokenized))
[14]:
'Melayu malas ini narration dia sama sahaja macam men are trash . True to some , false to some .'
[15]:
tokenized = tokenizer.tokenize(string5)
model.correct_text(' '.join(tokenized))
[15]:
'DrM cerita Melayu malas semenjak saya kat University ( early 1980s ) and now as i am edging towards retirement in 4 - 5 ye ars time after a career of being an Engineer , Project Manager , General Manager'
[16]:
tokenized = tokenizer.tokenize(string6)
model.correct_text(' '.join(tokenized))
[16]:
'boleh bnth dalam slk nap st , nanti intiha'
[17]:
tokenized = tokenizer.tokenize(string7)
model.correct_text(' '.join(tokenized))
[17]:
'mulakan salah orang boleh , bila geng itu kena knsl juga boleh terima . . pelik'