Created
March 30, 2025 05:45
-
-
Save smourier/9bd1059412dc74f23af76460f4fbd468 to your computer and use it in GitHub Desktop.
Creates a numbered, unwrapped versions of .txt books (for the ones like in project gutenberg https://www.gutenberg.org/)
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
public static void Main(string[] args) | |
{ | |
var path = @"C:\Users\bob\Downloads\AliceInWonderland.txt"; | |
path = @"C:\Users\bob\Downloads\Frankenstein.txt"; | |
path = @"C:\Users\bob\Downloads\MobyDick.txt"; | |
var text = File.ReadAllText(path); | |
// add numbers in front of each line | |
var lines = text.Split(["\r\n", "\n"], StringSplitOptions.None); | |
var digits = Math.Floor(Math.Log10(lines.Length) + 1); | |
var format = "{0:D" + digits + "}"; | |
var lineNum = 1; | |
var newLines = new string[lines.Length]; | |
foreach (var line in lines) | |
{ | |
newLines[lineNum - 1] = $"{string.Format(format, lineNum)}: {line}"; | |
lineNum++; | |
} | |
var newPath = Path.Combine(Path.GetDirectoryName(path)!, Path.GetFileNameWithoutExtension(path) + "Numbered" + Path.GetExtension(path)); | |
File.WriteAllLines(newPath, newLines); | |
// create a no wrap version | |
var linesNoWrap = new List<string>(); | |
var paragraph = new StringBuilder(); | |
var prevEmpty = false; | |
foreach (var line in lines) | |
{ | |
if (string.IsNullOrWhiteSpace(line)) | |
{ | |
linesNoWrap.Add(paragraph.ToString()); | |
if (!prevEmpty) | |
{ | |
linesNoWrap.Add(string.Empty); | |
} | |
paragraph.Clear(); | |
prevEmpty = true; | |
} | |
else | |
{ | |
if (paragraph.Length > 0) | |
{ | |
paragraph.Append(' '); | |
} | |
paragraph.Append(line); | |
prevEmpty = false; | |
} | |
} | |
if (paragraph.Length > 0) | |
{ | |
linesNoWrap.Add(paragraph.ToString()); | |
} | |
// create a no wrap numbered version | |
var linesNoWrapNumbered = new string[linesNoWrap.Count]; | |
digits = Math.Floor(Math.Log10(linesNoWrap.Count) + 1); | |
format = "{0:D" + digits + "}"; | |
lineNum = 1; | |
foreach (var line in linesNoWrap) | |
{ | |
linesNoWrapNumbered[lineNum - 1] = $"{string.Format(format, lineNum)}: {line}"; | |
lineNum++; | |
} | |
var newPathNoWrap = Path.Combine(Path.GetDirectoryName(path)!, Path.GetFileNameWithoutExtension(path) + "NoWrap" + Path.GetExtension(path)); | |
File.WriteAllLines(newPathNoWrap, linesNoWrap); | |
var newPathNoWrapNumbered = Path.Combine(Path.GetDirectoryName(path)!, Path.GetFileNameWithoutExtension(path) + "NoWrapNumbered" + Path.GetExtension(path)); | |
File.WriteAllLines(newPathNoWrapNumbered, linesNoWrapNumbered); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment