Skip to content

Instantly share code, notes, and snippets.

@bdnbdy
Created December 14, 2015 20:01
Show Gist options
  • Save bdnbdy/6d456b67192e8dceb5e7 to your computer and use it in GitHub Desktop.
Save bdnbdy/6d456b67192e8dceb5e7 to your computer and use it in GitHub Desktop.
A short story based on the experience of going down a wikihole.
require 'httparty'
class Algorithmia
private
def self.post_http(endpoint, params = {})
params = params.to_s unless params.is_a?(Hash)
result = HTTParty.post(endpoint,
body: params.to_json,
headers: { "Authorization" => 'YOUR_API_KEY',
"Content-Type" => "application/json" })
return result.parsed_response["result"]
end
end
class Article
attr_accessor :title, :url, :text
def initialize(article)
@title = article['title']
@text = article['text']
@url = article['url']
end
def title
# remove the "- Wikipedia..." part of the page title
title = @title[/[^-]+/]
end
def text
# split the page summary into sentences and only return the first one
sentences = Algorithmia.post_http('https://api.algorithmia.com/v1/algo/StanfordNLP/SentenceSplit/0.1.0', @text)
sentences.first
end
end
class BookBuilder
def self.navigation_commentary(title, url)
# makes a markdown link to article
@navigation_commentary.sample + "[#{title}](#{url})"
end
def self.create_paragraph(summary)
# chooses commentary & adds text from article
commentary = [self.pos_commentary, self.neg_commentary].sample
self.format_summary(summary) + commentary
end
private
def self.format_summary(summary)
# markdown formatting
"> _" + summary + "_" + "\n\n"
end
def self.neg_commentary
@negative_thought_phrases.sample + @negative_adjs.sample + @punctuation.sample
end
def self.pos_commentary
@positive_thought_phrases.sample + @positive_adjs.sample + @punctuation.sample
end
@positive_adjs = [
"interesting",
"absorbing",
"animating",
"appealing",
"captivating",
"challenging",
"consuming",
"curious",
"engaging",
"entertaining",
"enthralling",
"enticing",
"exciting",
"fascinating",
"intriguing",
"inviting",
"lively",
"moving",
"piquant",
"provocative",
"spellbinding"]
@negative_adjs = [
"boring",
"arid",
"banal",
"characterless",
"colorless",
"commonplace",
"drab",
"drudging",
"flat",
"humdrum",
"insipid",
"interminable",
"lame",
"lifeless",
"monotonous",
"mundane",
"repetitious",
"routine",
"spiritless",
"stale",
"stodgy",
"tame",
"tedious",
"tiresome",
"tiring",
"trite",
"unexciting",
"uninteresting",
"vapid",
"wearisome"]
@negative_thought_phrases = [
"Hmm, ",
"Well that was ",
"You know, I think that article was a little ",
"That was too ",
"I can't believe I just read that thing--",
"How ",
"Ugh, ",
"I guess that was okay, but a little ",
"Wow, ",
"Yeah, I guess that was ",
"Not worth the time it took to read: "]
@positive_thought_phrases = [
"Whoa, ",
"OMG, ",
"That was crazy ",
"How ",
"Hmm, ",
"Wow, ",
"That was ",
"I thought that was so ",
"Yeah, ",
"Boy-howdy, that was ",
"Woo-boy, ",
"So ",
"Quite "]
@navigation_commentary = [
"Oooh, lemme click on this link: ",
"Wait, what's this? ",
"Guess I should check out ",
"Never heard of ",
"Hmm, better click on this link: ",
"Where to next? Probably ",
"What to read next... ",
"Yeah! I wanna learn about ",
"I could read this article on ",
"Better waste a few more minutes... ",
"Where to next? ",
"This looks good: "]
@punctuation = [
"!",
"?",
"??",
"!!",
"...",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
".",
"."]
end
#!/usr/bin/env ruby
require_relative 'wikihole'
print "Give me a wikipedia url: "
url = gets.chomp.to_s
wikihole = WikiHole.new(url)
wikihole.write_book!
# Run `ruby wikihole_script.rb` on the commandline.
# You will be prompted to input a url to a wiki article.
# When you see "Done!", you can find the story in the same directory and give it a read!
# See the main class, WikiHole to understand the guts of this program.

You know what I should look up?

Jock Jams

Jock Jams is a series of compilation albums released by Tommy Boy Records.

How trite.

This looks good: Dance music

Dance music is music composed specifically to facilitate or accompany dancing.

That was too monotonous.

Wait, what's this? Latin dance

Latin dance is a general label, and a term in partner dance competition jargon.

How unexciting.

Oooh, lemme click on this link: Bomba (Puerto Rico)

Bomba is one of the traditional musical styles of Puerto Rico.

Not worth the time it took to read: stodgy.

This looks good: Cueca

Cueca (Spanish pronunciation: [ˈkweka]) is a family of musical styles and associated dances from Argentina, Chile and Bolivia.

Woo-boy, exciting.

Guess I should check out Joropo

The Joropo is a musical style resembling the fandango, and an accompanying dance.

I guess that was okay, but a little characterless.

Never heard of Diablada

in Bolivia: Carnaval de Oruro, Fiesta del Gran Poder, Fiesta de Urkupiña, Fiesta de Ch'utillos, Entrada de la Virgen de Guadalupe, Fiesta de San Miguel Arcangel in Peru: Fiesta de la Candelaria, Carnaval de Juliaca, 28 de Julio, Tata Pancho, Festividad de la Octava del Niño Jesús, Fiesta de San Miguel Arcangel.

Not worth the time it took to read: drudging.

Better waste a few more minutes... Altiplano

The Altiplano (Spanish for "high plain"), Andean Plateau or Bolivian Plateau, in west-central South America, where the Andes are at their widest, is the most extensive area of high plateau on Earth outside of Tibet.

You know, I think that article was a little boring.

Hmm, better click on this link: Ventifact

Ventifacts are rocks that have been abraded, pitted, etched, grooved, or polished by wind-driven sand or ice crystals.

I guess that was okay, but a little drudging.

Where to next? Atacama Desert

Coordinates: 24°30′S 69°15′W / 24.500°S 69.250°W / -24.500; -69.250 The Atacama Desert (Spanish: Desierto de Atacama) is a plateau in South America, covering a 1,000-kilometre (600 mi) strip of land on the Pacific coast, west of the Andes mountains.

That was crazy moving.

Never heard of Chihuahuan Desert

The Chihuahuan Desert is a desert, and an ecoregion designation, that straddles the U.S.-Mexico border in the central and northern portions of the Mexican Plateau.

You know, I think that article was a little flat.

I could read this article on West Texas

West Texas is a vernacular term applied to a region in the southwestern quadrant of the United States that primarily encompasses the arid and semiarid lands in the western portion of the state of Texas.

Well that was trite.

Guess I should check out Brazos Wind Farm

The Brazos Wind Farm, also known as the Green Mountain Energy Wind Farm at Brazos, is located in Borden and Scurry counties in Texas.

Wow, animating.

Yeah! I wanna learn about Wind power in California

As of October 31, 2015, California has 6,022 megawatts (MW) of wind powered electricity generating capacity.

That was exciting!!

Where to next? Probably Rocky Mountain Institute

Rocky Mountain Institute (RMI) is an organization in the United States dedicated to research, publication, consulting, and lecturing in the general field of sustainability, with a special focus on profitable innovations for energy and resource efficiency.

You know, I think that article was a little interminable.

Where to next? Probably Alexander S. Onassis Foundation

Alexander S. Onassis Foundation: Wishing to honor the memory of his son Alexander, who died in 1973, in an airplane crash at the age of 24, Aristotle Onassis directed in his will that half of his estate should be transferred upon his own death to a foundation to be established in Alexander’s name.

Well that was trite?

This looks good: International Standard Book Number

The International Standard Book Number (ISBN) is a unique[a][b] numeric commercial book identifier.

Hmm, intriguing.

Yeah! I wanna learn about TIFF/EP

Tag Image File Format/Electronic Photography (TIFF/EP) is a digital image file format standard – ISO 12234-2, titled "Electronic still-picture imaging – Removable memory – Part 2: TIFF/EP image data format".

Hmm, insipid.

Oooh, lemme click on this link: OpenEXR

OpenEXR is a high dynamic range imaging image file format, released as an open standard along with a set of software tools created by Industrial Light and Magic (ILM), under a free software license similar to the BSD license.

Ugh, unexciting.

Where to next? Wavelet

A wavelet is a wave-like oscillation with an amplitude that begins at zero, increases, and then decreases back to zero.

Wow, lively!

Never heard of Time–frequency representation

A time–frequency representation (TFR) is a view of a signal (taken to be a function of time) represented over both time and frequency.

That was crazy moving.

Better waste a few more minutes... Fractional Fourier transform

In mathematics, in the area of harmonic analysis, the fractional Fourier transform (FRFT) is a family of linear transformations generalizing the Fourier transform.

Well that was lame.

Where to next? Wikipedia, the free encyclopedia

The Toledo War (1835–36) was a nearly bloodless sovereignty dispute between the US state of Ohio and the Michigan Territory over a small strip of land along their border, fueled by conflicting legislation, a desire to control the then-prosperous city of Toledo, and poor maps.

Whoa, fascinating.

Oooh, lemme click on this link: Free content

Free content, libre content, or free information, is any kind of functional work, artwork, or other creative content that meets the definition of a free cultural work.

Yeah, I guess that was interminable.

Yeah! I wanna learn about UKOLN

Formerly known as The United Kingdom Office for Library and Information Networking, UKOLN was a centre of expertise in digital information management, providing advice and services to the library, information, education and cultural heritage communities.

How interminable.

This looks good: European Union

The European Union (EU) is a politico-economic union of 28 member states that are located primarily in Europe.

Well that was stale.

Hmm, better click on this link: Welsh language

All speakers: 740,000 (2004–2014) Welsh (Cymraeg or y Gymraeg, pronounced [kəmˈrɑːɨɡ, ə ɡəmˈrɑːɨɡ]) is a member of the Brittonic branch of the Celtic languages spoken natively in Wales, by some in England, and in Y Wladfa (the Welsh colony in Chubut Province, Argentina).

Wow, mundane.

Yeah! I wanna learn about Welsh English

Welsh English, Anglo-Welsh, or Wenglish refers to the dialects of English spoken in Wales by Welsh people.

Whoa, engaging.

Guess I should check out Welsh

Welsh-language literature has been produced continuously since the emergence of Welsh from Brythonic as a distinct language c. 5th century AD.

You know, I think that article was a little colorless.

Better waste a few more minutes... Cynghanedd

In Welsh language poetry, cynghanedd (Welsh pronunciation: [kəŋ̊ˈhaneð], literally "harmony") is the basic concept of sound-arrangement within one line, using stress, alliteration and rhyme.

I guess that was okay, but a little vapid.

This looks good: Cape Breton fiddling

Cape Breton fiddling is a regional violin style which falls within the Celtic music idiom.

Whoa, appealing.

Never heard of Traditional square dance

Traditional square dance is a generic American term for any style of American square dance other than modern Western.

I guess that was okay, but a little trite.

Uhhh....where did the last 2 hours of my life go?

END.

require_relative "algorithmia"
require_relative "book_builder"
require_relative "article"
class WikiHole
def initialize(starting_url)
@starting_url = starting_url
@analyze_url = 'https://api.algorithmia.com/v1/algo/web/AnalyzeURL/0.2.14'
@get_links = 'https://api.algorithmia.com/v1/algo/brejnko/UrlLinkList/0.1.0'
@paragraphs = []
@word_count = 0
# change book_length for total wordcount of short story
@book_length = 1000
end
def write_book!
puts "Starting book"
@referring_url = start_book
while @word_count < @book_length
puts "Still writing..."
puts @word_count.to_s + " words written"
keep_writing(@referring_url)
end
print_book
end
def start_book
article = create_new_article(@starting_url)
markdown_link = "[#{article.title}](#{article.url})"
@paragraphs << "You know what I should look up?\n\n" + markdown_link
create_paragraph(article.text)
return sample_links(article.url)
end
def keep_writing(url)
article = create_new_article(url)
navigate_wiki(article.title, article.url)
create_paragraph(article.text)
@referring_url = sample_links(article.url)
end
def create_new_article(url)
Article.new(fetch_article(url))
end
def fetch_article(url)
Algorithmia.post_http(@analyze_url, url)
end
def sample_links(url)
links = Algorithmia.post_http(@get_links, url)
reject_bad_links(links)
links.sample
end
def reject_bad_links(links)
# remove links to other language wikis
links.delete_if {|link| !link.include? "https://en.wikipedia.org/wiki/" }
wiki_pages = [
"wiki/Category",
"wiki/Portal",
"wiki/Special",
"wiki/File",
"wiki/Wikipedia",
"wiki/Talk",
"wiki/Template",
"wiki/Help",
"#"
]
# remove wiki related pages
wiki_pages.each do |wiki_link|
links.delete_if {|link| link.include? wiki_link }
end
end
def create_paragraph(summary)
new_paragraph = BookBuilder.create_paragraph(summary)
@paragraphs << new_paragraph
update_word_count(new_paragraph)
end
def navigate_wiki(title, url)
navigation_commentary = BookBuilder.navigation_commentary(title, url)
@paragraphs << navigation_commentary
end
def update_word_count(string)
@word_count += string.split.length
end
def print_book
book = open('WikiHole.md', 'w')
@paragraphs.each do |paragraph|
book.write(paragraph)
book.write("\n\n")
end
book.write("Uhhh....where did the last 2 hours of my life go?\n\nEND.")
book.close
puts "Done!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment