-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequel_generator.rb
189 lines (143 loc) · 4.9 KB
/
sequel_generator.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require './string'
require './imdb'
CONJUNCTIONS = ['after', 'although', 'and', 'as', 'as far', 'as if', 'as long', 'as soon', 'as though', 'as well', 'because', 'before', 'both', 'but', 'either', 'even', 'if', 'even', 'though', 'for', 'how', 'however', 'if', 'if', 'only', 'in', 'case', 'in', 'order', 'that', 'neither', 'nor', 'now', 'once', 'only', 'or', 'provided', 'rather', 'than', 'since', 'so', 'so that', 'than', 'that', 'though', 'till', "'til", 'unless', 'until', 'when', 'whenever', 'where', 'whereas', 'wherever', 'whether', 'while', 'yet', '&']
PREPOSITIONS = ['of', 'in', 'to', 'for', 'with', 'on', 'at', 'from', 'by', 'about', 'as', 'into', 'like', 'through', 'after', 'over', 'between', 'out', 'against', 'during', 'without', 'before', 'under', 'around', 'among']
ARTICLES = ['the','a','an']
METHODS = [:fastfurious, :squeakquel, :thestreets, :theening]
class SequelGenerator
def self.generate_sequel
loop do
title = sequelize_title(random_film_title)
return(title) if title
end
end
private
def self.random_film_title
Imdb.random_film_title
end
def self.sequelize_title(title)
return if !title || !title.strip
title = title.strip
if has_numbers?(title)
return add_one_to_numbers(title)
end
METHODS.size.times do
i_title = title
o_title = self.sequelize_title_with_method(i_title, METHODS.sample)
return(o_title) if o_title
end
nil
end
def self.sequelize_title_with_method(title, method)
case method
when :fastfurious
return fastfurious_title(title)
when :squeakquel
return squeakquel_title(title)
when :thestreets
return thestreets_title(title)
when :theening
return theening_title(title)
end
nil
end
# NUMBERS
def self.has_numbers?(title) # TODO -- numbers in English too!
return(false) unless title
title.scan(/\d+/).any?
end
def self.add_one_to_numbers(title)
title.scan(/\d+/).each do |number|
title.gsub! number, (number.to_i + 1).to_s
end
title
end
# FASTFURIOUS: The Fast and the Furious = 2 Fast 2 Furious
def self.fastfurious_title(title)
return sequelize_components(componentize_film_title(title))
end
def self.componentize_film_title(title)
cleaned_title = clean_up_phrase(title)
words = cleaned_title.split(' ')
if (words.length == 2)
return words
else
return attempt_to_get_two_phrases_from(cleaned_title)
end
rescue
nil
end
def self.sequelize_components(components)
return nil unless components && (components.length == 2)
components.map { |c| '2 ' + c.strip.split(' ').map(&:capitalize).join(' ') }.join(' ')
end
def self.clean_up_phrase(phrase)
phrase = remove_articles(phrase)
phrase = phrase.scan(/^[^\(]+/).first # ignore anything in parentheses
phrase = phrase.scan(/^[^:]+/).first # ignore anything after a colon
phrase = phrase.gsub(/[\d_\W]+\Z/, '') # remove trailing numbers
phrase.gsub!("'s",'')
phrase.gsub!(".",'')
phrase.strip.split(' ').map(&:uncapitalize).join(' ')
rescue
nil
end
def self.remove_articles(phrase)
o_phrase = phrase
ARTICLES.each do |badword|
o_phrase.gsub!(/\b#{ badword }\b/i,'')
o_phrase.gsub!(' ',' ')
end
o_phrase
rescue
nil
end
def self.attempt_to_get_two_phrases_from(title)
return(nil) unless title
components = nil
CONJUNCTIONS.each do |conj|
next unless title.include?(" #{ conj } ")
some_components = title.split(" #{ conj } ")
if (some_components.length == 2)
components = some_components
break
end
end
return(components) if (components && components.length == 2)
PREPOSITIONS.each do |prep|
next unless title.include?(" #{ prep } ")
some_components = title.split(" #{ prep } ")
if (some_components.length == 2)
components = some_components
break
end
end
return(components) if (components && components.length == 2)
rescue
nil
end
# SQUEAKQUEL: Alvin and the Chipmunks = Alvin and the Chipmunks 2: The Squeakquel
def self.squeakquel_title(title)
return(nil) unless title
return "#{ title } 2: The Squeakquel"
end
# THESTREETS: Step Up = Step Up 2 The Streets
def self.thestreets_title(title)
return(nil) unless title
return "#{ title } 2 the Streets"
end
# THEENING: Noun of the Noun: The Nounening
def self.theening_title(title)
return(nil) unless title
last_word = title.split(' ').last
return(nil) unless last_word.is_noun?
# if it ends in y then replace the y with an i
if last_word.scan(/(.+)y\z/i).any?
last_word = "#{ last_word.scan(/(.+)y\z/i).first.first }i"
# if it ends in a vowel then remove the vowel
elsif last_word.scan(/[aeiou]\z/i).any?
last_word = last_word.scan(/(.+)[aeiou]\z/i).first.first
end
return "#{ title } 2: The #{ last_word }ening"
end
end