Lemmatization in Python for NLP
How lemmatization works in Python with NLTK and spaCy, how it differs from stemming, what it changes in your text, and when raw text is the better choice.
Lemmatization reduces each inflected word to its dictionary form, or lemma, so that "churches" becomes "church" and, with the right part-of-speech information, "running" becomes "run". In Python NLP work it is usually done with NLTK or spaCy as a preprocessing step after tokenization. This review explains what the transformation changes, what it costs, and when you are better off leaving text as it is.
Is lemmatization suitable for your use?
Lemmatization is a good fit when you need different inflections of a word to count as the same item. Typical cases are word-frequency analysis, keyword extraction, topic modeling, bag-of-words or TF-IDF features, and search where a query for "mouse" should also match "mice". The main benefit is that vocabulary is consolidated without the truncated, non-word forms a stemmer can produce.
The decisive tradeoff is that lemmatization throws information away. Tense, number, and comparative degree disappear, and the result depends on how accurately each word's part of speech is identified. The output also stops reading as natural language, so it works as an internal representation, not as text for people.
Plain, unlemmatized text is preferable in the following cases:
- You are feeding text to a transformer model with its own subword tokenizer, which is designed to work with raw inflected text.
- The task depends on grammatical detail, such as tense, plurality, or agreement.
- The text will be displayed, quoted, exported, or read aloud to people.
- You need to reconstruct or edit the original wording later and have not stored it separately.
What lemmatization actually changes
Lemmatization changes the stored characters of individual word tokens. Each token's string is replaced with a different string, its lemma. Nothing about visual presentation is involved: unlike reversed text, mirrored glyphs, or Unicode "font" styling, the character order within a word is not flipped and no look-alike characters are substituted. The transformation is linguistic. It changes which word form is stored, using a dictionary, morphological rules, and part-of-speech information.
Because it works on words, lemmatization needs tokenization first. Sentence order and punctuation are normally left as the tokenizer produced them. Only the word forms change.
Lemmatization versus stemming
Stemming is often confused with lemmatization. A stemmer strips affixes using rules and does not check whether the result is a real word. For example, NLTK's Porter stemmer turns "studies" into "studi". A lemmatizer aims to return a valid dictionary form and can handle irregular forms that suffix rules cannot.
| Aspect | Stemming | Lemmatization |
| Method | Rule-based suffix stripping | Dictionary lookup and morphological rules, usually guided by part of speech |
| Output | May not be a real word | Intended to be a valid dictionary form |
| Irregular forms | Generally not resolved | Can be resolved when the part of speech is known |
| Context needed | None | Part-of-speech tags improve results |
| Readability of output | Low | Higher per word, but sentences still read unnaturally |
Lemmatization with NLTK
NLTK's WordNetLemmatizer looks words up in WordNet. Its most important behavior is that it assumes a word is a noun unless you pass a part of speech. That is why "running" comes back unchanged by default but becomes "run" when tagged as a verb.
import nltk
from nltk.stem import WordNetLemmatizer, PorterStemmer
nltk.download("wordnet")
lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize("dogs")) # dog
print(lemmatizer.lemmatize("churches")) # church
print(lemmatizer.lemmatize("running")) # running (treated as a noun)
print(lemmatizer.lemmatize("running", pos="v")) # run
print(lemmatizer.lemmatize("better", pos="a")) # good
stemmer = PorterStemmer()
print(stemmer.stem("studies")) # studi
For real sentences, tag the tokens first and map NLTK's Penn Treebank tags to WordNet's categories. The exact names of the tokenizer and tagger data packages differ between NLTK versions. If a resource is missing, NLTK's error message names the package to download.
import nltk
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
def to_wordnet_pos(tag):
if tag.startswith("J"):
return wordnet.ADJ
if tag.startswith("V"):
return wordnet.VERB
if tag.startswith("R"):
return wordnet.ADV
return wordnet.NOUN
lemmatizer = WordNetLemmatizer()
tokens = nltk.word_tokenize("The striped bats were hanging on their feet")
lemmas = [
lemmatizer.lemmatize(word, to_wordnet_pos(tag))
for word, tag in nltk.pos_tag(tokens)
]
print(lemmas)
Lemmatization with spaCy
spaCy lemmatizes as part of its processing pipeline, so tokenization, tagging, and lemmatization happen in one pass. Each token exposes its lemma as token.lemma_. You need a trained pipeline installed, for example with: python -m spacy download en_core_web_sm. Lemmas can differ between pipeline versions, so record the version you use if results must be reproducible.
import spacy
nlp = spacy.load("en_core_web_sm", disable=["parser", "ner"])
doc = nlp("The striped bats were hanging on their feet")
for token in doc:
print(token.text, token.pos_, token.lemma_, token.idx)
If you only need lemmas, disabling the parser and named-entity recognizer avoids work you will not use. Keep the components the lemmatizer relies on for part-of-speech information. The token.idx value is the character offset in the original text, which is useful for mapping lemmas back to the source.
Checking output against the mechanism
When you review sample output, check that each change can be explained by a lemma lookup for the assigned part of speech. Common surprises include:
- Words left unchanged because they were tagged as nouns.
- Ambiguous forms resolved the wrong way, such as "saw" or "meeting".
- Proper nouns being altered or left alone inconsistently.
- Irregular forms such as "better" only becoming "good" when tagged as adjectives.
Character-level issues matter too. A word with an accented letter can be stored as a single precomposed character or as a base letter plus a combining mark. The two look identical but are different strings, and a dictionary lookup may match only one of them. Normalizing text to a consistent Unicode form, such as NFC with Python's unicodedata module, before tokenizing avoids this mismatch. Emoji built from several code points should pass through lemmatization unchanged, but tokenizers can split them differently, so check how yours handles them if they appear in your data.
Readability and usability after copying or sharing
Lemmatized output contains only ordinary characters, with no styling or look-alike substitutions. Copying, pasting, storing, and indexing it behave like any other plain text. The usability problem is meaning, not encoding. A lemmatized sentence reads like broken English, and a screen reader will read the lemmas literally. Do not show lemmatized text in place of the original to readers or assistive technology.
Search is where lemmatization helps most and also where it needs care. Apply the same lemmatization pipeline to both the indexed documents and the incoming queries, or matches will be missed. Expect some loss of precision when distinct senses collapse to one lemma.
Editing is the other limit. The transformation is lossy, so the original wording cannot be rebuilt from the lemmas. Store the original text, or the tokens with their character offsets, alongside the lemmas whenever anything may need to be displayed, quoted, corrected, or audited later. That plain-text original is the version to share with people.
Evidence gaps
No benchmark data was available for this review. There are no verified measurements comparing the accuracy or speed of NLTK and spaCy, no tested results for specific pipeline versions, and no evaluation for languages other than English. The example outputs in the NLTK snippet reflect documented WordNet lemmatizer and Porter stemmer behavior. spaCy output was deliberately not stated because it depends on the installed pipeline. Before committing to a tool, run both options on a representative sample of your own text and inspect the lemmas by hand.