Created
January 20, 2022 17:43
-
-
Save BartMassey/d9fa93ab5750eed8889cda80b2bb32df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Transform text to one-sentence-per-line format. | |
# Bart Massey 2021-01-20 | |
import re, sys | |
blank = re.compile("^[ \t]*$") | |
dot = re.compile("[.] ") | |
text = open(sys.argv[1], "r").read().splitlines() | |
paragraphs = [] | |
paragraph = "" | |
for line in text: | |
if blank.match(line): | |
paragraphs.append(paragraph) | |
paragraph = "" | |
elif paragraph: | |
paragraph += " " + line | |
else: | |
paragraph += line | |
if paragraph: | |
paragraphs.append(paragraph) | |
text = [] | |
for paragraph in paragraphs: | |
if not paragraph: | |
continue | |
text.append(dot.sub(".\n", paragraph)) | |
print("\n\n".join(text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment