-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_song_titles_app.rb
executable file
·123 lines (95 loc) · 3.22 KB
/
fake_song_titles_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env ruby
# fake_song_title_app.rb
# Copyright 2011 Robert Jones ([email protected]) Craic Computing LLC
# This code and associated data files are distributed freely under the terms of the MIT license
# Initially this is a Fake Captain Beefheart Song Title Generator
# Based on a comment of Gideon Coe on BBC 6 Music in November 2011 regarding the odd song titles
# of Captain Beefheart (Don Van Vliet). Gideon commented that there should be a Beefheart song title
# generator ... and so now there is ...
require 'erb'
$:.unshift File.join(File.dirname(__FILE__))
#-----------------------------------------------------------------
def load_words(filename)
words = Hash.new
open(filename, 'rb').each_line do |line|
next if line =~ /^#/ or line =~ /^\s*$/
line.chomp!
words[line.downcase] = 1
end
words.keys.sort
end
def pick_two_nouns_and_linkage(nouns, linkages)
word0 = nouns[rand(nouns.length)]
word1 = nouns[rand(nouns.length)]
link = linkages[rand(linkages.length)]
"#{word0} #{link} #{word1}"
end
def pick_two_nouns(nouns)
word0 = nouns[rand(nouns.length)]
word1 = nouns[rand(nouns.length)]
"#{word0} #{word1}"
end
def pick_adjective_and_noun(nouns, adjectives)
word0 = nouns[rand(nouns.length)]
adj0 = adjectives[rand(adjectives.length)]
"#{adj0} #{word0}"
end
def pick_two_nouns_adjective_and_linkage(nouns, adjectives, linkages)
word0 = nouns[rand(nouns.length)]
word1 = nouns[rand(nouns.length)]
adj0 = adjectives[rand(adjectives.length)]
link = linkages[rand(linkages.length)]
"#{adj0} #{word0} #{link} #{word1}"
end
def pick_two_nouns_and_verb(nouns, verbs)
word0 = nouns[rand(nouns.length)]
word1 = nouns[rand(nouns.length)]
verb = verbs[rand(verbs.length)]
"#{word0} #{verb} #{word1}"
end
def generate_titles(n_titles, nouns, verbs, adjectives, linkages)
titles = Array.new
(0...n_titles).each do |i|
j = rand(5)
if j == 0
titles << pick_two_nouns_and_linkage(nouns, linkages)
elsif j == 1
titles << pick_two_nouns(nouns)
elsif j == 2
titles << pick_adjective_and_noun(nouns, adjectives)
elsif j == 3
titles << pick_two_nouns_adjective_and_linkage(nouns, adjectives, linkages)
else
titles << pick_two_nouns_and_verb(nouns, verbs)
end
end
titles
end
# MAIN -----------------------------------------------------------------
class FakeSongTitlesApp < Sinatra::Base
set :root, File.dirname(__FILE__)
set :static, true
nouns_file = File.join(File.dirname(__FILE__), 'data', 'nouns.txt')
verbs_file = File.join(File.dirname(__FILE__), 'data', 'verbs.txt')
adjectives_file = File.join(File.dirname(__FILE__), 'data', 'adjectives.txt')
linkages_file = File.join(File.dirname(__FILE__), 'data', 'linkages.txt')
nouns = load_words(nouns_file)
verbs = load_words(verbs_file)
adjectives = load_words(adjectives_file)
linkages = load_words(linkages_file)
get '/' do
n_titles = 5
@titles = generate_titles(n_titles, nouns, verbs, adjectives, linkages)
erb :index
end
get '/about' do
erb :about
end
get '/update' do
n_titles = 1
titles = generate_titles(n_titles, nouns, verbs, adjectives, linkages)
@title = titles[0]
# content_type 'text/plain'
@title
end
end