Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Text processing for search

After this lesson: I can turn raw text into good tokens.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Why is text cleaned before searching?

Text processing for search turns raw text into clean tokens: lower case, no punctuation, no stop words and words cut back to a stem. Without it, "Bus", "bus." and "buses" would be three different words to a search system.

By the end of this lesson you will write a tokeniser that makes the same token from every form of a word.

By the end of this lesson

Note

I can turn raw text into good tokens.

Key words

WordWhat it meansExample
Tokenone piece of text that is looked up in the indexbus
Normalisationmaking different spellings of a word the sameBus and bus
Stop worda very common word that is left outthe
Stemmingcutting a word back to its stem
Stemthe part of a word that stays the samedelay

The cells students fill in are left blank in this preview.

What is wrong with raw text?

A computer compares strings exactly. "Bus" and "bus" are different, and so are "late," and "late". If the index holds one and the query uses the other, the document is not found.

So the same steps are applied to every document and every query. Then equal words become equal tokens.

Lower case and remove punctuation

Code: python
text = "The Bus is LATE, again!"
text = text.lower()
for mark in [",", "!", ".", "?"]:
    text = text.replace(mark, " ")
print(text.split())
# Put "bus-stop" in the text. Add "-" to the list of marks.

Look at the first line of a file

Command
docker exec jupyter head -n 1 hires.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Clean a line of text in one step

Command
docker exec jupyter python3 -c "print('The Bus is LATE, again!'.lower().replace(',', ' ').replace('!', ' ').split())"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: what each step does

StepText after the step
Raw textThe Bus-stop is BUSY!
Lower casethe bus-stop is busy!
Split at punctuationthe, bus, stop, is, busy
Drop stop wordsbus, stop, busy

The cells students fill in are left blank in this preview.

What are stop words?

Stop words are very common words, such as "the", "is" and "of". They are in nearly every document, so they do not help tell one from another. The idf of such a word is close to 0.

Dropping them makes the index smaller and the ranking cleaner. The list is a choice. A phrase such as "to be or not to be" would vanish.

What is stemming?

Stemming cuts a word back to a stem, so that "delay", "delays", "delayed" and "delaying" are one token. A search for one finds the others.

A simple stemmer only strips endings by rules, such as "ies" to "y", then "ing", then "ed", then a final "s". It does not know any English.

Worked example: a simple stemmer at work

WordRule usedStem
ticketsremove the final sticket
delayedremove eddelay
delayingremove ingdelay
studiesies becomes ystudy
bustoo short to changebus
newsremove the final snew

The cells students fill in are left blank in this preview.

A simple stemmer in Python

Code: python
def stem(word):
    if word.endswith("ies") and len(word) > 4:
        return word[:-3] + "y"
    if word.endswith("ing") and len(word) > 5:
        return word[:-3]
    if word.endswith("ed") and len(word) > 4:
        return word[:-2]
    if word.endswith("s") and not word.endswith("ss") and len(word) > 3:
        return word[:-1]
    return word

for word in ["tickets", "delayed", "delaying", "studies", "bus", "news"]:
    print(word, stem(word))
# Try "running". Is the result a real word?

A common mistake

Warning

Many people think a stem is always a real word. A crude stemmer turns "news" into "new" and "running" into "runn". That is acceptable, because the stem only has to be the same for the words that should match, in the index and in the query.

Going further

Write one sentence about a bus journey in three forms: with capitals and a comma, then plain, then with the verb in another tense. Tokenise all three by hand and check that the tokens match.

Then find a word where the simple stemmer joins two words that should stay apart, and say what a search for one would wrongly find.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.