#!/usr/bin/python
# coding: utf-8
#
# Matthew 25:30, copyright (c) 2015, 2018 Nick Montfort <nickm@nickm.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and
# this notice are preserved. This file is offered as-is, without any warranty.
#
# Updated 31 May 2018, changed "print" for Python 2 & 3 compatibility
# Updated 26 November 2018, substituted a shorter all-permissive license
# Written 29 November 2015

"""_Matthew 25:30 is the title of both a computer program, the source code
to which you may be reading, and the output of this program, which in
many ways is like a standard novel or poem and which you may instead be
reading. This note appears at the beginning of both.

To produce markdown output, run matthew.py (Python 2) with TextBlob.

    % python matthew.py > matthew.text

To produce PDF and epub documents, use pandoc:

    % pandoc -V geometry:paperwidth=5.5in \
        -V geometry:paperheight=8.25in \
        -V geometry:margin=.75in -o matthew.pdf \
        matthew.text
    % echo '% Matthew 25:30' > info.txt
    % echo '% Nick Montfort' >> info.txt
    % pandoc -o matthew.epub info.txt matthew.text

_Matthew 25:30_ was written/generated for the third NaNoGenMo (National
Novel Generation Month) in November 2015, and is free software."""

__author__ = 'Nick Montfort'
__license__ = 'ISC'
__version__ = '1.0'

from textblob.wordnet import Synset
from random import choice, shuffle

def nodes(top):
    if len(top) == 0:
        return []
    return [top[0]] + nodes(top[0].hyponyms()) + nodes(top[1:])

def leaves(top):
    if len(top) == 0:
        return []
    elif len(top[0].hyponyms()) == 0:
        return [top[0]] + leaves(top[1:])
    return leaves(top[0].hyponyms()) + leaves(top[1:])

goods, cnames, items, catalog = [Synset('consumer_goods.n.01')], [], [], []
colors = [Synset('achromatic_color.n.01'), Synset('chromatic_color.n.01')]
adjs = ['an elegant', 'a modern', 'a contemporary', 'a rustic',
        'a distressed', 'a chic', 'a timeless', 'a fashionable', 'a classy',
        'a cute', 'a subtle', 'an understated', 'a dapper']
enticement = ['exclusively available here', 'new this season', 'now 10% off',
              '25% off', 'on sale today only', 'significantly reduced in price',
              'available with free shipping', 'as seen on television']
for c in leaves(colors):
    for name in c.lemma_names():
        if not name[-4:] == 'ness':
            cnames += [name]
for n in nodes(goods):
    items += n.lemma_names()
for i in range(5):
    shuffle(items)
    catalog += items
out = u"""# Matthew 25:30\n## Nick Montfort\n\n
The last stair of 34th Street, Herald Square; at my feet
a clamor of trains that weave iron mazes.
Steam and shrieks ascend the morning,
which, at once, becomes the final reckoning,
the ultimate sale, the blackest Friday. From the obscured
horizon and from the center of my being, an infinite voice
said these things (these actual stock-keeping units,
not the words that follow, which are but my poor time-bound
translation of what was a single word):
“Playing-cards, chessboards, a nice hardback copy of the Grettir Saga,  \n"""
for item in catalog:
    if item[-4:] == 'jean':
        item += 's'
    if item == 'laundry':
        item += ' hamper'
    if item == 'suit of clothes':
        item = 'suit'
    product, start = item.replace('_', ' ') + ' that is ', choice(adjs)
    if (item[-1] == 's' and not item[-2] in 'us') or item[-3:] in ['ear', 'ire']:
        start = start.split()[1]
    if item[-1] == 's' and not item[-2] in 'us':
        product = product[:-3] + 'are '
    out += (start + ' ' + choice(cnames).replace('_', ' ') +
            ' ' + product + choice(enticement) + ',  \n')
out = out[:-4] + '.  \n'
out += u"""In vain oceans have been squandered on you,
in vain New York’s retail operations, marveled at by Whitman.
You have consumed the years and they have consumed you,
and still ... you have not generated the novel.”"""
print(out.encode('utf-8'))
