Skip to content

Instantly share code, notes, and snippets.

@gbluma
Last active April 17, 2022 18:11
Show Gist options
  • Save gbluma/8544361 to your computer and use it in GitHub Desktop.
Save gbluma/8544361 to your computer and use it in GitHub Desktop.
Writing tool that inserts newlines after every prepositional phrase.

I like to use a technique from Richard Lanham's book "Revising Prose" to help myself write better English sentences. In the book, Lanham suggests that we break apart sentences at each predicate. Doing this helps us visually see the structure of our words. Errors and issues with flow stand out immediately. Likewise, sentences that contain many predicates don't flow nicely.

Splitting these sentences manually is a bit of work. Fortunately a computer can do this work trivially. It is not a problem to repetitively split apart sentences based on simple rules. Below is a program I wrote to do the splitting and un-splitting, named pred and unpred respectively.

For example, we can take the quote from C.A.R. Hoare and pred it:

Original

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.

Pred form

There are two ways 
of constructing a software design:
One way 
is 
to make it so simple 
that there are obviously no deficiencies 
and the other way 
is 
to make it so complicated 
that there are no obvious deficiencies.

Unpred form

(same as the original)

Usage

You can use these scripts from the command line, but it is not exactly handy.

$ pred < input.txt
... (text) ...

A better way to use it is from a program like Vim.

Vim instructions

Visually select a block of text and then type :!pred. This will replace the text you selecte with the split version. Reselecting and typing :!unpred will reverse the operation.

Because I use these so much, I like to have them mapped to shortcuts:

map ,1 :!pred<cr>
map ,2 :!unpred<cr>
#!/usr/bin/env python
import sys
# my personal list
words = ["is", "it's", "as", "that", "and", "or", "of", "for", "from",
"in", "around", "into", "how", "through", "but", "which", "with",
"without"]
# additions courtesy of
# http://donnayoung.org/english/grammar/prepositions.htm
words += ["about", "below", "in spite of", "regarding",
"above", "beneath", "instead of", "since",
"according", "to", "beside", "into", "through",
"across", "between", "like", "throughout",
"after", "beyond", "near", "to",
"against", "but", "of", "toward",
"along", "by", "off", "under",
"amid", "concerning", "on", "underneath",
"among", "down", "on account of", "until",
"around", "during", "onto", "up",
"at", "except", "out", "upon",
"atop", "for", "out", "of", "with",
"because of", "from", "outside", "within",
"before", "in", "over", "without",
"behind", "inside", "past" ]
# clean up any extraneous newlines
output = sys.stdin.read().replace("\n", "")
# add newlines before predicates
for word in words:
output = output.replace(" " + word + " ", " \n" + word + " ")
# do some more cleanup with sentance boundaries
output = (output
.replace(": ", ":\n")
.replace(". ", ".")
.replace(". ", ".")
.replace(" .", ".")
.replace(".", ".\n\n")
)
# print
print output,
#!/usr/bin/env python
import sys
print (sys.stdin.read()
.replace('\n','')
.replace('.', '. ')
.replace(':', ': ')
.strip(' ')),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment