PySastrawi Documentation

repository·master·Indexed 18 days ago

https://github.com/har07/pysastrawi

A Python port of the Sastrawi PHP project used for stemming Indonesian language text. It provides the StemmerFactory and Stemmer classes to reduce inflected words to their base forms using algorithms such as Nazief & Adriani, CS Stemmer, ECS Stemmer, and Improved ECS.

Tokens
997
Snippets
5
Records
7
Agent score
13%

What's inside PySastrawi

  1. Perform Indonesian word stemming with PySastrawi

    master

    To reduce inflected Indonesian words to their base form, use the StemmerFactory to create a stemmer instance, then call the .stem() method on your target text.

    Workflow:

    1. Import StemmerFactory from Sastrawi.Stemmer.StemmerFactory.
    2. Instantiate StemmerFactory().
    3. Call .create_stemmer() on the factory to get a stemmer object.
    4. Use .stem(text) to process strings.
    from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
    
    # Initialize the factory and create the stemmer
    factory = StemmerFactory()
    stemmer = factory.create_stemmer()
    
    # Stem a sentence
    sentence = 'Perekonomian Indonesia sedang dalam pertumbuhan yang membanggakan'
    output = stemmer.stem(sentence)
    print(output)
    # Output: ekonomi indonesia sedang dalam tumbuh yang bangga
    
    # Stem a single word/phrase
    print(stemmer.stem('Mereka meniru-nirukannya'))
    # Output: mereka tiru
  2. Stem Indonesian text with the Stemmer class

    master

    The Stemmer class provides methods to reduce Indonesian words or full text strings to their base (root) form. It uses a dictionary-based approach and supports various stemming algorithms (Nazief & Adriani, CS Stemmer, ECS Stemmer, Improved ECS).

    To use it, you must instantiate the class with a dictionary object. The primary method for processing full sentences or paragraphs is stem(text), while stem_word(word) can be used for individual words.

    # Note: You must provide a valid dictionary instance to the constructor
    stemmer = Stemmer(dictionary)
    
    # Stem a full string
    result = stemmer.stem("teks yang ingin di-stem")
    
    # Stem a single word
    word_root = stemmer.stem_word("kata")
  3. Stem a single word with stem_word()

    master

    The stem_word(word) method reduces a single word to its base form. It internally determines if a word is plural or singular using is_plural() and routes the processing to either stem_plural_word() or stem_singular_word() accordingly.

    def stem_word(self, word):
        """Stem a word to its common stem form."""
        if self.is_plural(word):
            return self.stem_plural_word(word)
        else:
            return self.stem_singular_word(word)
  4. Stem a text string with stem()

    master

    The stem(text) method takes a string, normalizes the text using TextNormalizer, splits it into individual words, and returns a single string where each word has been replaced by its stem, joined by spaces.

    def stem(self, text):
        """Stem a text string to its common stem form."""
        # ... implementation ...