added fns.py, which includes URL fn.

This commit is contained in:
mousebot 2020-04-22 18:49:24 -03:00
parent 6fbed12eb4
commit 2152bdb8b0
1 changed files with 49 additions and 16 deletions

View File

@ -19,18 +19,19 @@
"""
"""
a (very basic) script to markovify a text file and
output a user-specified number of sentences to a text file. see --help for other options.
a (very basic) script to markovify local and/or remote text files and
output a user-specified number of sentences to a local text file. see --help for other options.
"""
import requests
import markovify
import sys
import argparse
import fns
def main():
# argparse for cmd line args
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(prog="mkv-this", description="markovify a local or remote text file and output the results to local text file.", epilog="may you have many delightful éoncés!")
# positional args:
parser.add_argument('infile', help="the text file to process, with path. NB: file cannot be empty.")
@ -39,6 +40,7 @@ def main():
# optional args:
parser.add_argument('-s', '--state-size', help="the number of preceeding words used to calculate the probability of the next word. defaults to 2, 1 makes it more random, 3 less so. must be an integer. anything more than 4 will likely have little effect.", type=int, default=2)
# if i use --state-size (w a dash), type=int doesn't work.
parser.add_argument('-u', '--URL', help="infile is a URL. NB: for this to work best it should be the location of a text file.", action='store_true')
parser.add_argument('-n', '--sentences', help="the number of 'sentences' to output. defaults to 5. must be an integer.", type=int, default=5)
parser.add_argument('-l', '--length', help="set maximum number of characters per sentence. must be an integer.", type=int)
parser.add_argument('-o', '--overlap', help="the amount of overlap allowed between original text and the output, expressed as a ratio between 0 and 1. defaults to 0.5", type=float, default=0.5)
@ -46,23 +48,44 @@ def main():
parser.add_argument('--newline', help="sentences in input file end with newlines rather than with full stops.", action='store_true')
# store_true = default to False, become True if flagged.
parser.add_argument('-c', '--combine', help="provide an another input text file with path to be combined with the first.")
parser.add_argument('-C', '--combine-URL', help="provide an additional URL to be combined with the first")
parser.add_argument('-w', '--weight', help="specify the weight to be given to the second text provided with --combine. defaults to 1, and the weight of the initial text is also 1. setting this to 1.5 will place 50 percent more weight on the second text, while setting it to 0.5 will place less.", type=float, default=1)
args = parser.parse_args()
fnf = 'error: file not found. please provide a path to a really-existing file!'
# if a combine file is provided, combine it w/ input file:
# if a combine file is provided, combine it w infile/URL:
if args.combine :
# get raw text as a string for both files:
try:
with open(args.infile, encoding="latin-1") as f:
text = f.read()
with open(args.combine, encoding="latin-1") as cf:
ctext = cf.read()
# infile can be a URL:
if args.URL:
text = fns.URL(args.infile)
# or normal file:
else:
with open(args.infile, encoding="latin-1") as f:
text = f.read()
except FileNotFoundError:
print(fnf)
sys.exit()
with open(args.combine, encoding="latin-1") as cf:
ctext = cf.read()
# if combine URL is provided, combine it w infile/URL:
elif args.combine_URL :
try:
# infile can still be a URL:
if args.URL:
text = fns.URL(args.infile)
# or normal file:
else:
with open(args.infile, encoding="latin-1") as f:
text = f.read()
except FileNotFoundError:
print(fnf)
sys.exit()
# now combine as URL:
ctext = fns.URL(args.combine_URL)
# build the models and build a combined model:
# NB: attempting to implement Newline option here (and below):
@ -96,12 +119,19 @@ def main():
# if no combo file, just do normal:
else:
# Get raw text as string.
try:
with open(args.infile, encoding="latin-1") as f:
text = f.read()
except FileNotFoundError:
print(fnf)
sys.exit()
# either from a URL:
if args.URL:
text = fns.URL(args.infile)
# text is what fn returns, ie = req.text.
# or a normal local file:
else:
try:
with open(args.infile, encoding="latin-1") as f:
text = f.read()
except FileNotFoundError:
print(fnf)
sys.exit()
# Build the model:
# NB: this errors if infile is EMPTY:
@ -128,6 +158,9 @@ def main():
# add a star between each appended set.
output.close()
print('\n The options you used were as follows:\n')
print(vars(args))
print('\n: literary genius has been written to the file ' + args.outfile + '. thanks for playing!')
sys.exit()
# sys.exit()
main()