Stemmer and Lemmatization#

This tutorial is available as an IPython notebook at Malaya/example/stemmer.

This module only trained on standard language structure, so it is not save to use it for local language structure.

[1]:
import os

os.environ['CUDA_VISIBLE_DEVICES'] = ''
[2]:
%%time
import malaya
/home/husein/.local/lib/python3.8/site-packages/bitsandbytes/cextension.py:34: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.
  warn("The installed version of bitsandbytes was compiled without GPU support. "
/home/husein/.local/lib/python3.8/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cadam32bit_grad_fp32
CPU times: user 3.11 s, sys: 2.59 s, total: 5.7 s
Wall time: 2.96 s
/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]:
string = 'Benda yg SALAH ni, jgn lah didebatkan. Yg SALAH xkan jadi betul. Ingat tu. Mcm mana kesat sekalipun org sampaikan mesej, dan memang benda tu salah, diam je. Xyah nk tunjuk kau open sangat nk tegur cara org lain berdakwah'
another_string = 'melayu bodoh, dah la gay, sokong lgbt lagi, memang tak guna, http://twitter.com @kesedihan rm15'

Use Naive stemmer#

Simply use regex pattern to do stemming. This method not able to lemmatize.

def naive():
    """
    Load stemming model using startswith and endswith naively using regex patterns.

    Returns
    -------
    result : malaya.stem.NAIVE class
    """
[4]:
naive = malaya.stem.naive()

Stem and lemmatization#

def stem(self, string: str):
    """
    Stem a string using Regex pattern.

    Parameters
    ----------
    string : str

    Returns
    -------
    result: str
    """
[5]:
naive.stem('saya menyerukanlah')
/home/husein/dev/malaya/malaya/model/stem.py:28: FutureWarning: Possible nested set at position 3
  or re.findall(_expressions['ic'], word.lower())
[5]:
'saya yerukan'
[6]:
naive.stem('menarik')
[6]:
'arik'
[7]:
naive.stem('slhlah')
[7]:
'slh'
[8]:
naive.stem(string)
[8]:
'Benda yg SALAH ni , jgn lah debat . Yg SALAH x jadi betul . Ingat tu . Mcm mana sat kalipun org sampai sej , dan ang benda tu sa , am je . Xyah nk tunjuk kau open sangat nk tegur cara org lain dakwah'
[9]:
naive.stem(another_string)
[9]:
'layu bodoh , dah la gay , sokong lgbt lagi , ang tak guna , http://twitter.com @kesedihan rm15'

Use Sastrawi stemmer#

Malaya also included interface for https://pypi.org/project/PySastrawi/. We use it for internal purpose. To use it, simply,

def sastrawi():
    """
    Load stemming model using Sastrawi, this also include lemmatization.

    Returns
    -------
    result: malaya.stem.SASTRAWI class
    """
[10]:
sastrawi = malaya.stem.sastrawi()

Stem and lemmatization#

def stem(self, string: str):
    """
    Stem a string using Sastrawi, this also include lemmatization.

    Parameters
    ----------
    string : str

    Returns
    -------
    result: str
    """
[11]:
sastrawi.stem('saya menyerukanlah')
[11]:
'saya seru'
[12]:
sastrawi.stem('slhlah')
[12]:
'slhlah'
[13]:
sastrawi.stem(string)
[13]:
'Benda yg SALAH ni , jgn lah debat . Yg SALAH xkan jadi betul . Ingat tu . Mcm mana kesat sekalipun org sampai mesej , dan memang benda tu salah , diam je . Xyah nk tunjuk kau open sangat nk tegur cara org lain dakwah'
[14]:
sastrawi.stem(another_string)
[14]:
'melayu bodoh , dah la gay , sokong lgbt lagi , memang tak guna , http://twitter.com @kesedihan rm15'

List available HuggingFace models#

[15]:
malaya.stem.available_huggingface
[15]:
{'mesolitica/stem-lstm-512': {'Size (MB)': 35.2,
  'hidden size': 512,
  'CER': 0.02549779186652238,
  'WER': 0.05448552235248484},
 'mesolitica/stem-gru-bahdanau-1024': {'Size (MB)': 83.7,
  'vocab size': 1000,
  'hidden size': 1024,
  'CER': 0.07082863511793107,
  'WER': 0.11684768403456935}}
[16]:
print(malaya.stem.info)
Trained on train set and tested on test set, https://github.com/huseinzol05/malay-dataset/tree/master/normalization/stemmer

Use HuggingFace model#

def huggingface(
    model: str = 'mesolitica/stem-lstm-512',
    force_check: bool = True,
    **kwargs,
):
    """
    Load HuggingFace model to stem and lemmatization.

    Parameters
    ----------
    model: str, optional (default='mesolitica/stem-lstm-512')
        Check available models at `malaya.stem.available_huggingface`.
    force_check: bool, optional (default=True)
        Force check model one of malaya model.
        Set to False if you have your own huggingface model.

    Returns
    -------
    result: malaya.torch_model.rnn.Stem
    """
[17]:
model = malaya.stem.huggingface()
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.

Stem and lemmatization#

def stem(self, string: str, beam_search: bool = True):
    """
    Stem a string, this also include lemmatization.

    Parameters
    ----------
    string : str
    beam_search : bool, (optional=True)
        If True, use beam search decoder, else use greedy decoder.

    Returns
    -------
    result: str
    """

If want to speed up the inference, set beam_search = False.

[18]:
%%time

model.stem(string)
spaces_between_special_tokens is deprecated and will be removed in transformers v5. It was adding spaces between `added_tokens`, not special tokens, and does not exist in our fast implementation. Future tokenizers will handle the decoding process on a per-model rule.
CPU times: user 7.48 s, sys: 98 ms, total: 7.58 s
Wall time: 712 ms
[18]:
'Benda yg SALAH ni , jgn lah debat . Yg SALAH xkan jadi betul . Ingat tu . Mcm mana sat sekal ogr sampai mesej , dan memang benda tu salah , diam je . Xyah nk tunjuk kau open sangat nk tegur cara org lain dakwah'
[19]:
model.stem(another_string)
[19]:
'melayu bodoh , dah la gay , sokong lgbt lagi , memang tak guna , http://twitter.com @kesedihan rm15'
[20]:
model.stem('saya menyerukanlah')
[20]:
'saya seru'

Sensitive towards local language structure#

Let us compare stemming results using Facebook comments.

[21]:
string1 = 'mulakn slh org boleh ,bila geng tuh kena slhkn jgk xboleh trima .. pelik , dia slhkn org bole hri2 crta sakau then bila kna bls balik xdpt jwb ,kata mcm biasa slh (parti sampah) 🤣🤣🤣 jgn mulakn dlu slhkn org kalau xboleh trima bila kna bls balik 🤣🤣🤣'
[22]:
string2 = 'berehatlh najib.. sudah2 lh tu.. jgn buat rakyat hilang kepercyaan tu pda system kehakiman negara.. klu btl x slh kenapa x dibuktikan semasa sblm rayuan.. sudah lah tu kami dh letih dengan drama korang. ok'
[23]:
model.stem(string1)
[23]:
'mula slh org boleh , bila geng tuh kena slh jgk xboleh trima . . pelik , dia slh org bole hri crta sakau then bila kna bls balik xdpt jwb , kata mcm biasa slh ( parti sampah ) 🤣 🤣 🤣 jgn mula dlu slh org kalau xboleh trima bila kna bls balik 🤣 🤣 🤣'
[24]:
sastrawi.stem(string1)
[24]:
'mulakn slh org boleh , bila geng tuh kena slhkn jgk xboleh trima . . pelik , dia slhkn org bole hri2 crta sakau then bila kna bls balik xdpt jwb , kata mcm biasa slh ( parti sampah ) 🤣 🤣 🤣 jgn mulakn dlu slhkn org kalau xboleh trima bila kna bls balik 🤣 🤣 🤣'
[25]:
naive.stem(string1)
[25]:
'mul slh org boleh , bila geng tuh na sl jgk xboleh trima . . lik , a sl org bole hri2 crta sakau then bila kna bls balik xdpt jwb , kata mcm biasa slh ( parti sampah ) 🤣 🤣 🤣 jgn mul dlu sl org kalau xboleh trima bila kna bls balik 🤣 🤣 🤣'
[26]:
model.stem(string2)
[26]:
'rehat najib . . sudah lh tu . . jgn buat rakyat hilang percya tu pda system hakim negara . . klu btl xd slh napa x bukti semasa sblm rayu . . sudah lah tu kami dh letih dengan drama korang . ok'
[27]:
sastrawi.stem(string2)
[27]:
'berehatlh najib . . sudah2 lh tu . . jgn buat rakyat hilang kepercyaan tu pda system hakim negara . . klu btl x slh kenapa x bukti masa sblm rayu . . sudah lah tu kami dh letih dengan drama korang . ok'
[28]:
naive.stem(string2)
[28]:
'eha najib . . sudah2 lh tu . . jgn buat rakyat hilang percya tu pda system hakim negara . . klu btl x slh napa x bukti masa sblm rayu . . sudah lah tu kami dh letih deng drama korang . ok'