Build a nltk.stem.StemmerI instance from regex, named stemmer ('Lancaster', 'Porter', None), or function

    Arguments:
      min (int): Dont stem anything short than this. e.g. for min=4 don't stem token "I'm" to "I"

    >>> make_stemmer()
    <Stemmer object at ...>
    >>> make_stemmer(str_lower)
    <function str_lower at ...>
    >>> make_stemmer('str_lower')
    <function str_lower at ...>
    >>> make_stemmer('Lancaster')
    <Stemmer object at ...>
    >>> make_stemmer('WordNet')
    <Stemmer object at ...>
    >>> make_stemmer('ing$|s$')
    <Stemmer object at ...>

def make_stemmer(stem=None, min_len=3):
    """Build a nltk.stem.StemmerI instance from regex, named stemmer ('Lancaster', 'Porter', None), or function

    Arguments:
      min (int): Dont stem anything short than this. e.g. for min=4 don't stem token "I'm" to "I"

    >>> make_stemmer()
    <Stemmer object at ...>
    >>> make_stemmer(str_lower)
    <function str_lower at ...>
    >>> make_stemmer('str_lower')
    <function str_lower at ...>
    >>> make_stemmer('Lancaster')
    <Stemmer object at ...>
    >>> make_stemmer('WordNet')
    <Stemmer object at ...>
    >>> make_stemmer('ing$|s$')
    <Stemmer object at ...>
    """
    if not stem or stem == 'passthrough':
        stem = Stemmer()
        # FIXME: this is unnecessary?! and will make the object less picklable?
        stem.stem = passthrough
        return stem
    if isinstance(stem, basestring):
        stem = globals().get(stem, None) or locals().get(stem, stem)
    # in case stem is a compiled stemmer regex, make it a string so it can be compiled by the nltk.RegexStemmer
    if hasattr(stem, 'pattern'):
        stem = Stemmer(nltk.stem.RegexpStemmer(stem, min=min_len))
    if isinstance(stem, basestring):
        # strip nonascii and whitespace, and only consider first letter, case-insentively
        name = stringify(stem).lower().strip() or 'porter'
        if name in STEMMER_TYPES:
            dataset = STEMMER_DATASETS.get(name, None)
            if dataset is not None:
                nltk_download(dataset)
            stem = STEMMER_TYPES.get(name, stem)
        else:
            stem = Stemmer(nltk.stem.RegexpStemmer(stem, min=min_len))
    if isinstance(stem, type):
        stem = stem()
    if (hasattr(stem, 'stem') and callable(stem.stem)) or (hasattr(stem, 'lemmatize') and callable(stem.lemmatize) or stem is True):
        return Stemmer(stem)
    elif callable(stem):
        return stem
    raise(ValueError("Unable to make {} into a stemmer. ".format(stem) +
                     "Try 'porter', 'lancaster', None, a regular expression, a callable function, " +
                     "or an object with a stem method."))