Created
February 8, 2021 18:54
-
-
Save jeffbass/4cde8fa8abf3b1dcb2d7c56391ee951e to your computer and use it in GitHub Desktop.
Python program to generate text files of various sizes
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/env python3 | |
""" Generate text files for testing Evernote v10.x | |
Generates text files of different sizes. The text files have increasing numbers | |
of lines. The number of words per line is random, between min_words and | |
max_words. The text that provides the pool of words is The Gettysburg Address. | |
Text files are named ENtest500.txt, ENtest1000, ENtest1500, ENtest2000, ... | |
where the number portion represents the number of text lines in the file. | |
Author: Jeff Bass, https://github.com/jeffbass | |
License: Public Domain per CC0 Creative Commmons License | |
""" | |
gettysburg_address = """ | |
Four score and seven years ago our fathers brought forth on this continent, a | |
new nation, conceived in Liberty, and dedicated to the proposition that all men | |
are created equal. | |
Now we are engaged in a great civil war, testing whether that nation, or any | |
nation so conceived and so dedicated, can long endure. We are met on a great | |
battle-field of that war. We have come to dedicate a portion of that field, as a | |
final resting place for those who here gave their lives that that nation might | |
live. It is altogether fitting and proper that we should do this. | |
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can | |
not hallow -- this ground. The brave men, living and dead, who struggled here, | |
have consecrated it, far above our poor power to add or detract. The world will | |
little note, nor long remember what we say here, but it can never forget what | |
they did here. It is for us the living, rather, to be dedicated here to the | |
unfinished work which they who fought here have thus far so nobly advanced. It | |
is rather for us to be here dedicated to the great task remaining before us -- | |
that from these honored dead we take increased devotion to that cause for which | |
they gave the last full measure of devotion -- that we here highly resolve that | |
these dead shall not have died in vain -- that this nation, under God, shall | |
have a new birth of freedom -- and that government of the people, by the people, | |
for the people, shall not perish from the earth. | |
Abraham Lincoln, November 19, 1863 | |
""" | |
import re | |
import random | |
words = re.sub("[^\w]", " ", gettysburg_address).split() # make a list of words | |
lines = [500, 1000, 1500, 2000, 3000, 4000, 5000] # num of lines per file | |
min_words = 6 # minimum words per line | |
max_words = 20 # maximum words per line | |
for file_size in lines: | |
filename = 'ENtest' + str(file_size).strip() + '.txt' | |
with open(filename, 'w') as text_file: | |
for n_lines in range(file_size): | |
n_words = random.randint(min_words, max_words) | |
text_line = ' '.join(random.sample(words, n_words)) + '\n' | |
text_file.write(text_line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here here are snippets of the generated files that illustrate the output of running the above program. The text snippets are generated from the output files via the tail command:
tail *txt