Install PySastrawi via pip
masterYou can install the PySastrawi library using pip by running the following command in your terminal or command prompt:
pip install PySastrawirepository·master·Indexed 18 days ago
https://github.com/har07/pysastrawiA 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.
You can install the PySastrawi library using pip by running the following command in your terminal or command prompt:
pip install PySastrawiTo 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:
StemmerFactory from Sastrawi.Stemmer.StemmerFactory.StemmerFactory()..create_stemmer() on the factory to get a stemmer object..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 tiruThe 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").stem() method is called on a stemmer object created by StemmerFactory. It accepts a string representing an Indonesian sentence or word and returns the stemmed version (the base form of the words).StemmerFactory class is used to instantiate stemmer objects. It serves as the entry point for the stemming functionality in PySastrawi.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)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 ...