7
\$\begingroup\$

Pretty fun and simple challenge here

given any input, create an acrostic poem for that input. you may use any word list of your choosing, and include any code you want to help "smarten" up your acrostic.

EXAMPLE

Every
Xylophone
Always
Makes
Powerfully
Low
Emissions

the winner will be the code chosen by most up-votes, please up-vote for creativity, problem solving in making the acrostics make sense, and humor.

most important of all, have fun!

(please test your program using the following 3 words, and any other word or 2 you want to add - laughable ; codegolf ; loremipsum)

\$\endgroup\$
  • 1
    \$\begingroup\$ add more details. Should we be reading input from standard in? from the command line? or can this just be a function that takes something and returns something? You might also linking "acrostic poem" to its wikipedia page because people are lazy. \$\endgroup\$ – Wug Aug 28 '12 at 18:03
  • 2
    \$\begingroup\$ You are going to get panned for your subjective winning criteria (I wonder how the admin's think contests like the IOCCC are judged...). I'd recommend you add some guidelines that people should use to judge on whether or not to give their upvotes or not. \$\endgroup\$ – Griffin Aug 28 '12 at 18:56
  • \$\begingroup\$ "given any input" "please up-vote for creativity, problem solving in making the acrostics make sense, and humor." \$\endgroup\$ – NRGdallas Aug 29 '12 at 17:46
  • \$\begingroup\$ Nice word play: if you shuffle the letters of “acrostic”, you will get “costirca”, which is a possible spelling of my surname... \$\endgroup\$ – Andreï Kostyrka Dec 3 '16 at 12:37
5
\$\begingroup\$

Python 2.7

import random
import string
import sys
import re

class Markov(object):
    def __init__(self,filename):
        with open(filename,'r') as f:
            self._words = f.read().split()
            self._wordCount = len(self._words)

    def getWordList(self,seedWord=None,pattern=r'^.*$'):
        if seedWord is not None:
            chainWords = [self._words[i+1] for i in range(self._wordCount-1)\
                if seedWord == self._words[i].lower()]
        else:
            chainWords = self._words
        p = re.compile(pattern,re.IGNORECASE)
        return filter(p.match,chainWords)

    def getRandWord(self,seedWord=None,pattern=r'^.*$'):
        wordList = self.getWordList(seedWord,pattern)
        if len(wordList) == 0:
            return None
        return random.choice(self.getWordList(seedWord,pattern))

def main():
    list = Markov('AStudyInScarlet.txt')
    for word in sys.argv[1:]:
        poem = []
        for letter in word:
            if len(poem) == 0:
                nextWord = list.getRandWord(pattern=r'^[{0}.*$]'.format(letter))
            else:
                nextWord = list.getRandWord(poem[-1],r'^{0}.*$'.format(letter))
                if nextWord is None:
                    nextWord = list.getRandWord(pattern=r'^{0}.*$'.format(letter))
            nextWord = ''.join(ch for ch in nextWord if ch not in string.punctuation)
            poem.append(nextWord)

        print word
        print '='*len(word)
        for line in poem:
            print line
        print ''

if __name__ == '__main__':
    main()

Loads a text file called 'AStudyInScarlet.txt'. Only words used in that document will be candidates for incorporation into the final poem. (currently there are no words starting with x in this document, I will add some soon)

This program tries to find words in the text file that follow the previous word in the poem. For example, if the acrostic word is 'pita' and so far the poem is "Private," a possible next word is "inquiry" as the two word phrase "private inquiry" is found in the text document. If no matching word pairs are found, any random word starting with the right letter is chosen.

A word is chosen from a list of words that meet the desired qualifications. If a word shows up multiple times in the list of acceptable words, it is more likely to be chosen than one that does not.

Usage

Words to create poems from should be specified as command line arguments. One poem will be printed for each word specified on the command line.

Example:

$ acrostic.py laughable codegolf loremipsum
laughable
=========
Looking
a
Union
grievous
his
agitation
be
likely
enough

codegolf
========
couple
of
discoloured
evident
goes
or
late
from

loremipsum
==========
learn
of
revenge
engine
me
it
proved
smartest
up
my
\$\endgroup\$
  • \$\begingroup\$ Perhaps add a comma or period when you don't find a "good" word? I think it can make the poem feel more natural. \$\endgroup\$ – ugoren Aug 29 '12 at 15:10
  • \$\begingroup\$ @ugoren Yeah, I was thinking about doing something like that. I'm not sure how much that would help, but it's probably better than nothing. \$\endgroup\$ – Matt Aug 29 '12 at 15:39
  • \$\begingroup\$ another idea might be to limit the individual word length to like 4 letters or something, not sure how it might affect it, but it could help? \$\endgroup\$ – NRGdallas Sep 6 '12 at 17:12
4
\$\begingroup\$

PHP

<?php
$dict = array(
    'a' => 'and',
    'i' => 'if',
    'o' => 'or',
    'n' => 'not',
    'l' => 'list'
);
$funcs = get_defined_functions();
$word = rtrim(fgets(STDIN), "\n");
echo "\n";
foreach(str_split($word) as $char)
    foreach($dict + $funcs['internal'] as $func)
        if($func[0] == $char){
            echo $func . "\n";
            break;
        }

I admit it does not always make sense, but I am too lazy to look for a real word list, so I used PHP function names plus a couple of PHP reserved words.

Examples:

laughable

list
and
user_error
get_class
hash
and
bzopen
list
each

codegolf

class_exists
or
define
each
get_class
or
list
func_num_args

loremipsum

list
or
restore_error_handler
each
method_exists
if
property_exists
strlen
user_error
method_exists

neurotic

not
each
user_error
restore_error_handler
or
trigger_error
if
class_exists

diplomat

define
if
property_exists
list
or
method_exists
and
trigger_error
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.