natural language toolkit stemmers c 20012023 nltk project trevor cohn tacohncs mu oz au edward loper edlopergmail com steven bird stevenbird1gmail com url https www nltk org for license information see license txt a stemmer that uses regular expressions to identify morphological affixes any substrings that match the regular expressions will be removed from nltk stem import regexpstemmer st regexpstemmer ingseable min4 st stem cars car st stem mass mas st stem was was st stem bee bee st stem compute comput st stem advisable advis type regexp str or regexp param regexp the regular expression that should be used to identify morphological affixes type min int param min the minimum length of string to stem natural language toolkit stemmers c 2001 2023 nltk project trevor cohn tacohn cs mu oz au edward loper edloper gmail com steven bird stevenbird1 gmail com url https www nltk org for license information see license txt a stemmer that uses regular expressions to identify morphological affixes any substrings that match the regular expressions will be removed from nltk stem import regexpstemmer st regexpstemmer ing s e able min 4 st stem cars car st stem mass mas st stem was was st stem bee bee st stem compute comput st stem advisable advis type regexp str or regexp param regexp the regular expression that should be used to identify morphological affixes type min int param min the minimum length of string to stem
import re

from nltk.stem.api import StemmerI


class RegexpStemmer(StemmerI):
    

    def __init__(self, regexp, min=0):

        if not hasattr(regexp, "pattern"):
            regexp = re.compile(regexp)
        self._regexp = regexp
        self._min = min

    def stem(self, word):
        if len(word) < self._min:
            return word
        else:
            return self._regexp.sub("", word)

    def __repr__(self):
        return f"<RegexpStemmer: {self._regexp.pattern!r}>"