The Fluther Blog

Back to Fluther

What is Fluther?

Fluther is a free Q&A collective that specializes in getting
fast answers from the right people. Check it out!

“How’d they do that?” Correcting punctuation

7:48 am

In my last post, I announced a new feature that automatically corrects the punctuation of questions using “fancy computer logic”. I’d like to reveal some of that computer logic, in the first in a series of blog posts where we explore the technology that drives Fluther.

Fluther uses the Django web framework, so our application code is written in Python. I just started learning Python and really like a lot of the features of the language.

One of those features is the re module, which allows you to search through text for patterns.

To automatically correct question punctuation, I first needed to detect two patterns:

  1. Questions that end in multiple question marks or exclaimation points
  2. Questions that end in no punctuation

Here is the regular expression I used to detect the first pattern:

MULTIPLE_PUNCTUATION_END = re.compile("[?|!]+$")

In English, this regular expression would read “a question mark or exclamation point, repeated one or more times, at the end of the string”. Note that I used re.compile, which compiles a regular expression for efficiency.

Detecting this pattern and swapping it out for a single punctuation mark can be done with just one line of code:

question = MULTIPLE_PUNCTUATION_END.sub(question[-1], question)

That was easy. My second task was to detect questions with no ending punctuation:

NO_PUNCTUATION_END = re.compile("[^(?|.|!)]$")

You would read this regular expression as “anything other than, a question mark, period, or exclaimation point, at the end of the string”.

When no punctuation is detected at the end of a question, I simply append a question mark.

if NO_PUNCTUATION_END.search(question):
    question += "?"

With just a few lines of code, I was able to pretty-up the punctuation on Fluther.

Please leave comments to let me know what you think of this new series. I wanted to start with something relatively simple, but I’d like to dig even deeper into the Fluther codebase. What are some features on Fluther where you’ve asked “How’d they do that”?