quotefix

Insert matching punctuation in strings


License
MIT
Install
pip install quotefix==1.0.1

Documentation

quotefix

This Python module inserts punctuation to balance quotation marks, parentheses, and brackets that are unmatched in a Unicode string.

It allows you to use text synthesis techniques that are unaware of these internal clauses and still clean things up nicely for presentation.

Like this:

>>> quotefix('She said, "I ran out (to the store')
'She said, "I ran out (to the store)"'

>>> quotefix("it works in reverse too)")
'(it works in reverse too)'

You came here because you want to...

Assumptions

The main assumption made by quotefix is that punctuation ending a nested clause (a parenthetic aside, a quotation, etc) should also end any other open clauses. Likewise, punctuation opening a clause should open any unopened clauses in the remainder of the string.

A secondary assumption concerns the direction of the double quotation mark: ". If this character is closer to the beginning of a word, it's assumed to be an open quote.

History

quotefix started as a function in Fate, a fast & scalable trigram chat framework: https://github.com/pteichman/fate