forked from schollz/poetry-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoem.rb
146 lines (139 loc) · 4.47 KB
/
poem.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/ruby
#====================================================
#
# Usage: ruby poem.rb [<bnf file>] [<sub-type>]
# Eg. ruby poem.rb shakespeare.bnf rough
class BNF_Dictionary
def initialize(bnf_file)
@grammar = {}
File.open(bnf_file,"r") do |f|
f.each do |line|
line_split = line.split('=')
@grammar[line_split[0]] = []
line_split[1].split('|').each do |syntax|
@grammar[line_split[0]].push(syntax)
end
end
end
end
def generate(key,num)
key
gram = @grammar[key]
i = rand(gram.length - 1)
string = ""
if !gram[i].include?"<"
string = gram[i]
else
gram[i].split().each do |word|
if !word.include?"<"
string = string.concat(word).concat(" ")
else
string = string.concat(generate(word,1).concat(" "))
end
end
end
return string.gsub('newline','')
end
def generate_pretty(key)
poem = generate(key,1)
capitalize = true
is_title = true
is_paragraph = false
new_poem = ""
lines = poem.split('\n')
lines.push("\n")
lines.each do |line|
line = line.rstrip.lstrip
line = line.gsub(' ,',',')
line = line.gsub(' ?','?')
line = line.gsub(' !','!')
line = line.gsub(' .','.')
line = line.gsub(' :',':')
line = line.gsub(' \'','\'')
if capitalize
line = line.capitalize
capitalize = false
end
if is_title
mydate = Time.now
new_poem = new_poem.concat("<h1>").concat(line).concat("</h1>\n<h2>by The Poet's Computer, ").concat(mydate.strftime("%d %B %Y")).concat("</h2>\n")
is_title = false
else
if line.length < 1
if is_paragraph
new_poem = new_poem.concat("</p>")
end
is_paragraph = false
else
if !is_paragraph
new_poem = new_poem.concat("\n\n<p>")
line = line.capitalize
end
is_paragraph = true
new_poem = new_poem.concat(line).concat("<break>")
end
if line.include?"." or line.include?"!" or line.include?"?" or line.include?"!"
capitalize = true
end
end
end
new_poem = new_poem.gsub('<break><break>','<break>')
new_poem = new_poem.gsub('<break></p>','</p>')
new_poem = new_poem.gsub('<break>','<br />\n')
lines = new_poem.split("\n")
new_poem = ""
is_paragraph = false
capitalize = false
lines.each do |line|
if capitalize
line = line.capitalize()
capitalize = false
end
if line.include?"</p>"
is_paragraph = false
last_char = line.rsplit('</p>',1)[0][-1]
if last_char.include?"." or last_char.include?"-" or last_char.include?"," or last_char.include?"!" or last_char.include?"?" or last_char.include?"!"
new_poem = new_poem.concat(line).concat("\n")
else
new_poem = new_poem.concat(line.gsub('</p>','.</p>\n'))
end
elsif line.include?"<p>"
is_paragraph = true
new_poem = new_poem.concat(line).concat("\n")
else
if is_paragraph
last_char = line.reverse.split('<br />',2)[0][-1].collect(&:reverse).reverse
if last_char.include? "." or last_char.include? "-" or last_char.include? "," or last_char.include? "!" or last_char.include? "?" or last_char.include? "!"
#do nothing
else
if rand(20) <2
new_poem = new_poem.concat(line.gsub('<br />','?<br />\n'))
capitalize = true
elsif (0 + rand(20))<2
new_poem = new_poem.concat(line.gsub('<br />','.<br />\n'))
capitalize = true
elsif (0 + rand(20))<2
new_poem = new_poem.concat(line.gsub('<br />','!<br />\n'))
capitalize = true
elsif (0 + rand(20))<2
new_poem = new_poem.concat(line.gsub('<br />',',<br />\n'))
else
new_poem = new_poem.concat(line).concat("\n")
end
end
else
new_poem = new_poem.concat(line).concat("\n")
end
end
end
new_poem = new_poem.gsub('..','.')
new_poem = new_poem.gsub('?.','!')
new_poem = new_poem.gsub('!.','?')
new_poem = new_poem.gsub(' i ',' I ')
return new_poem
end
end
bnf_file = ARGV[0].nil? ? "shakespeare.bnf" : ARGV[0]
sub_type = ARGV[1].nil? ? "<poem>" : "<#{ARGV[1]}>"
bnf = BNF_Dictionary.new(bnf_file)
puts(bnf.generate_pretty("#{sub_type}"))