Code Fragment: Highlighting Typos

Another of those occasional examples of why I think it can be handy folk know how to code… a simple code fragment to style the output of a spell checker:

# Spell checking and grammar checking using:
# -
# -

# %pip install --upgrade language_tool_python

import language_tool_python
tool = language_tool_python.LanguageTool('en-US')

text = 'This sentence is fine. A sentence with a error in the Hitchhiker’s Guide tot he Galaxy'
matches = tool.check(text)

def styler(error):
    html = error.context
    from_ = error.offsetInContext
    to_ = from_ + error.errorLength
    txt = html[from_:to_]
    html =  html[:from_] + '<span style="color:red">' + txt +"</span>"+ html[to_:]
    return html

from IPython.display import HTML


I did think I’d find a simple Jupyter extension to add languagetool support (using something like language_tool_python) to check notebook markdown cells, but from a quick search, I couldn’t find one offhand…?

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: