Giter VIP home page Giter VIP logo

poetrybot's Introduction

PoetryBot

A poetry writing bot! It reads websites, learns how to write based on those websites, and then uses that knowledge to generate unique poems! I created this bot for the June 2019 Discord Hack Week. I am no longer hosting this bot, so see below if you wish to run this bot.

image of a poem

Bot Commands

poem [url]

Generate a unique poem based on the text on a webpage. Urban Dictionary pages or websites with lots of text work well for this. Example: poem https://wikipedia.org/wiki/Discord_(software)

poemsearch [space separated keywords]

Searches Google for keywords, selects a URL, and then generates a poem based on that URL. Example: poemsearch discord hack week

invite

Get an invite link to invite me to your server

help

Show this list of commands in the chat.

ping

Get the bot's API latency.

stats

Get bot statistics

How it works

The gist of PoetryBot's algorithm:

  1. Load a webpage into a DOM. Then use Mozilla's Readbility article extraction code to get a plain text representation of the content of the webpage.
  2. Create a Markov Chain of the writing on the page. The model is a dictionary, which contains words mapped to a Counted Set representing the words that follow that word, and their frequency. This allows PoetryBot to generate sentences that are more natural than those generated by treating all words equally.
  3. Generate lines (strings of words of random length). These lines seed the generation of those that follow, to keep the poem on topic (for the most part)
  4. Combine the lines and post to the chat for your enjoyment!

Ok, but how can I play with it?

You can use this bot two ways:

  1. Run the bot locally on your computer (information about how to do that below)
  2. Invite the version I'm hosting, by clicking here. Hosted version has been shut down.

How do I host this on my computer locally?

  1. Download this repository as a zip (or clone with a shallow depth)
  2. Create env.json with the following contents:
{
  "TOKEN":"your discord bot token here",
  "CREATOR_ID":"your user ID here"
}
  1. run npm install to install dependencies.
  2. run node server.js to start the bot and the webserver. In the console the bot will print the webserver's port. In your web browser, navigate to localhost://PORT_HERE to see the webpage.

Wow, this poem sucks!

Run the commands a few times! Not all websites are favorable for the algorithm, and sometimes the bot chooses a bad starting word on good websites. The bot caches where it can so subsequent runs should be faster.

The poemsearch command doesn't work!

If the poemsearch command is failing, perform the following:

  1. Follow the local hosting instructions
  2. Set a breakpoint in the resolve() call inside the urlToDOM() function in util.js
  3. Run the bot with a debugger attached. Have it run poemsearch. The breakpoint will trigger.
  4. Use the debugger to get the contents of data. Copy all of it to your clipboard.
  5. Paste into CodeVisualizer. The render view should populate with the google search results page in your clipboard.
  6. In CodeVisualizer, use your browser's find and replace to locate a link. Make sure you are clicked inside the code view.
  7. Find the CSS class name of the a containing element to the anchor tag. For example, if the find-replace found
...
<div class="kCrYT"><a href="/url?q=https://www.merriam-webster.com/dictionary"></a>
...

you would copy kCrYT to your clipboard.

  1. Scroll to getUrls() in util.js. Replace the assignment to classname with the value you just copied.
  2. Restart the bot in the debugger.
  3. Set a breakpoint under the getElementsByClassName() call to ensure that this class query is working.
  4. Run another poemsearch query. Make sure that the resulting HTMLCollection on the breakpoint has elements.
  5. Using the debugger, step to ensure url contains a correct URL.
  6. Submit a pull request or open an Issue. Only open an Issue if you have completed the above!

poetrybot's People

Contributors

ravbug avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.