« Steganography | The "Little Planet" Effect » |
Twitter is an... interesting way to spend one's time. It can be quite a challenge to cram a thought into 140 characters. However, I have found there are many ways to be entertained by this micro-blogging site. One of them includes interacting and learning from a wide variety of people. During an interaction with a creative-writer, I was a bit challenged by a poem.
This is a brief entry that includes my implementation of that "Poem Anagram Generator."
The Original Poem
The first thing that is required, is the poem. You can find the original at what_adri_writes[^].
Here is an excerpt:
Stanley the fishmonger
told me how to know it
when I saw it
without a poet
And we fished out drowned Phlebas,
Patron saint of Unconsidered Phoenicians
and failed Changers of Minds.
The Highlander Art double-checked the veil
of Reichenbach Falls
three days later
and found
A beekeeper.
Strategy
When I thought about it, I realized it wouldn't take much to actually write a program to reorganize the words of the poem. Especially if I were to use the algorithms
that are available in the C++ Standard Library. That is, of course, creating the naïve implementation that ignores such possibly desirable features such as sentence structure or proper grammar.
Then again, this is poetry!
So let's go ahead and throw those concerns out the window. This will be a post-modern avant-garde cyber-experience.
Tokenize
My original goal was to tokenize all of the words from the poem, then randomize with something like next_permutation
. However, when I really started to look at the text I saw all of the punctuation. Did I want to remove the punctuation and just live with the words? Well then there are also the new-lines that give the text form and clues the reader in that "pssst, this is probably a poem"
So I decided that I would include both the punctuation and new-lines as tokens to be generated for the poem generator.
To do this I put a space between ever word, punctuation mark, and new-line in the poem; like so:
C++
const std::string poem( "Oh but I too want to be a poet ! \n " | |
"and yet \n " | |
"Tell me where is meaning bred \n " | |
"In my heart or in your head ? \n " | |
| |
// omitted for brevity | |
| |
"And so the poem \n " | |
"was not to be . \n"); |
Here is a simple function to add each token into a vector
for future modification:
C++
typedef vector<string> words_t; | |
| |
// ************************************************************ | |
words_t tokenize(const string &poem) | |
{ | |
words_t words; | |
size_t next = 0; | |
| |
do | |
{ | |
size_t cur = next; | |
next = poem.find(' ', cur); | |
| |
size_t count = next == string::npos | |
? string::npos | |
: next - cur; | |
words.push_back(poem.substr(cur, count)); | |
| |
if (next != string::npos) | |
next += 1; | |
| |
} while (next != string::npos); | |
| |
return words; | |
} |
If I missed a potential algorithm from the standard library that would perform this task I would be interested to learn how this function could be simplified.
The Generator
The generator code is found below. It contains three algorithms from the standard library; A random number generator, shuffle
and copy
. Then of course the call to tokenize
.
You can run the code below to generate a new poem each time.
C++
// ************************************************************ | |
int main() | |
{ | |
// Tokenize the poem. | |
words_t words(tokenize(poem)); | |
| |
// Jumble the words. | |
random_device rdev; | |
mt19937 rng(rdev()); | |
| |
shuffle(words.begin(), words.end(), rng); | |
| |
// Print the results. | |
copy(words.begin(), words.end(), ostream_iterator<string>(cout, " ")); | |
| |
cout << "\n"; | |
| |
return 0; | |
} |
Output:
Twitter and Poetry...
Instant art!
Maybe to improve upon this I could pre-tokenize based on particular phrases.
Summary
Twitter is fun!
C++ is fun!
Combining Twitter and C++ makes poetry fun even for a left-brained analytic like myself.
If you end up generating an interesting poem, post it in the comments.
Recent Comments