def lemmatize(ambiguous_word: str, pos: str = None, neverstem=False,
              lemmatizer=wnl, stemmer=porter) -> str:
    """
    Tries to convert a surface word into lemma, and if lemmatize word is not in
    wordnet then try and convert surface word into its stem.

    This is to handle the case where users input a surface word as an ambiguous
    word and the surface word is a not a lemma.
    """

    # Try to be a little smarter and use most frequent POS.
    pos = pos if pos else penn2morphy(pos_tag([ambiguous_word])[0][1],
                                     default_to_noun=True)
    lemma = lemmatizer.lemmatize(ambiguous_word, pos=pos)
    stem = stemmer.stem(ambiguous_word)
    # Ensure that ambiguous word is a lemma.
    if not wn.synsets(lemma):
        if neverstem:
            return ambiguous_word
        if not wn.synsets(stem):
            return ambiguous_word
        else:
            return stem
    else:
        return lemma
def lemmatize ( ambiguous_word : str , pos : str = None , neverstem = False , lemmatizer = wnl , stemmer = porter ) -> str : pos = pos if pos else penn2morphy ( pos_tag ( [ ambiguous_word ] ) [ 0 ] [ 1 ] , default_to_noun = True ) lemma = lemmatizer . lemmatize ( ambiguous_word , pos = pos ) stem = stemmer . stem ( ambiguous_word ) if not wn . synsets ( lemma ) : if neverstem : return ambiguous_word if not wn . synsets ( stem ) : return ambiguous_word else : return stem else : return lemma
Tries to convert a surface word into lemma, and if lemmatize word is not in
    wordnet then try and convert surface word into its stem.

    This is to handle the case where users input a surface word as an ambiguous
    word and the surface word is a not a lemma.