Class: MusicMetadataScore::TitleCase

Inherits:
Object
  • Object
show all
Defined in:
lib/music_metadata_score/title_case.rb

Class Method Summary collapse

Class Method Details

.add_artists_to_string(string, featuring_artists, type) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/music_metadata_score/title_case.rb', line 153

def self.add_artists_to_string(string, featuring_artists, type)
	featuring_artists.each do |a|
		string += a
		string += MusicMetadataScore::TitleCase.featuring_artists_join(featuring_artists, a, type)
	end
	return string
end

.add_space_after?(string, letter, i) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/music_metadata_score/title_case.rb', line 40

def self.add_space_after?(string, letter, i)
	if letter =~ /[,.)]/ && string[i + 1] != ' '
		true
	elsif letter == '"'
		if string[i - 1] =~ /[[:alpha:]]/ && string[i + 1] != ' '
			true
		end
	else
		false
	end
end

.add_space_before?(string, letter, i) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/music_metadata_score/title_case.rb', line 52

def self.add_space_before?(string, letter, i)
	if letter =~ /[(]/ && string[i - 1] != ' '
		true
	else
		false
	end
end

.add_spacing_to_punctuation(string) ⇒ Object

 Add spaces after any punctuation if it doesn’t already exist



30
31
32
33
34
35
36
37
38
# File 'lib/music_metadata_score/title_case.rb', line 30

def self.add_spacing_to_punctuation(string)
	newstring = ""
	string.split('').each_with_index do |letter, i|
		newstring += ' ' if MusicMetadataScore::TitleCase.add_space_before?(string, letter, i)
		newstring += letter
		newstring += ' ' if MusicMetadataScore::TitleCase.add_space_after?(string, letter, i)
	end
	return newstring
end

.build_proper_title(title, version, primary_artists, featuring_artists) ⇒ Object

Build proper title



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/music_metadata_score/title_case.rb', line 263

def self.build_proper_title(title, version, primary_artists, featuring_artists)
	# Title Case
	title = MusicMetadataScore::TitleCase.title_case(title)
	# find any versions
	split_out_version = MusicMetadataScore::TitleCase.strip_version_from_title(title, version)
	# reformat featuring artists
	title = MusicMetadataScore::TitleCase.strip_artists_from_title(split_out_version[:title], primary_artists, featuring_artists)
	title += MusicMetadataScore::TitleCase.featuring_artists_string(featuring_artists, "feat.")
	# replace abbreviations
	return {:title => title, :version => split_out_version[:version]}
end

.capitalize_anomalies(title) ⇒ Object

Capitalize any anomalies, like DJ



243
244
245
# File 'lib/music_metadata_score/title_case.rb', line 243

def self.capitalize_anomalies(title)
	title.gsub('Dj ', 'DJ ')
end

.capitalize_artist_the(title) ⇒ Object

Capitalize The’s in MusicMetadataScore::Artist Names



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/music_metadata_score/title_case.rb', line 229

def self.capitalize_artist_the(title)
	if title[0] != '(' && title.include?('(')
		split_title = title.split('(')
		inside_bracket = split_title[1].split(')')
		new_inside_bracket = inside_bracket[0].gsub('the', 'The')
		string = split_title[0] + "(" + new_inside_bracket + ")"
		string += inside_bracket[1] if inside_bracket[1]
		return string
	else
		return title
	end
end

.combined_artists(primary_artists, featuring_artists) ⇒ Object



175
176
177
178
179
180
# File 'lib/music_metadata_score/title_case.rb', line 175

def self.combined_artists(primary_artists, featuring_artists)
	featuring_artists.each do |a|
		primary_artists << a
	end
	primary_artists
end

.featuring_artists_join(featuring_artists, a, type) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/music_metadata_score/title_case.rb', line 161

def self.featuring_artists_join(featuring_artists, a, type)
	if featuring_artists.count == 1 || a == featuring_artists.last
		""
	elsif featuring_artists.index(a) == featuring_artists.count - 2 && type != "vs."
		" & "
	elsif type == "vs."
		" vs. "
	elsif type == "Meets"
		" Meets "
	else
		", "
	end
end

.featuring_artists_string(featuring_artists, type) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/music_metadata_score/title_case.rb', line 143

def self.featuring_artists_string(featuring_artists, type)
	string = ""
	if featuring_artists.count > 0
		string = " (#{type} " if type != 'vs.'
		string = MusicMetadataScore::TitleCase.add_artists_to_string(string, featuring_artists, type)
		string += ")"
	end
	return string
end

.featuring_wordsObject

Split string Capitalize all Find list of words If they’re not first or last, or have a bracket on the beginning or end make em lower case



8
9
10
11
12
13
14
15
16
# File 'lib/music_metadata_score/title_case.rb', line 8

def self.featuring_words
	[
		' Featuring',
		'Feat ',
		' Feat.',
		' With '
		
	]
end

.get_inside_brackets(string) ⇒ Object

Get Value Inside Brackets



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/music_metadata_score/title_case.rb', line 248

def self.get_inside_brackets(string)
	if string.include?('(') && string.include?(')')
		split_string = string.split('(')
		array = []
		split_string.each do |s|
			stripped = s.strip
			if stripped[stripped.length - 1] == ')'
				array << stripped[0..stripped.length - 2]
			end
		end
		array
	end
end

.improper_abbreviations?(title) ⇒ Boolean

Check for abbreviations

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/music_metadata_score/title_case.rb', line 196

def self.improper_abbreviations?(title)
	# TODO find pt. and vol. not formatted properly
	if !MusicMetadataScore::Score.field_is_blank?(title)
		if title.downcase.include?(' part') || title.downcase.include?(' volume') || title.downcase.include?(' number') || title.downcase.include?(' aka') || title.downcase.include?(' d.j') || title.downcase.include?(' num')
			true
		else
			false
		end
	else
		false
	end
end

.includes_featuring_artists?(title, featuring_artists) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
# File 'lib/music_metadata_score/title_case.rb', line 124

def self.includes_featuring_artists?(title, featuring_artists)
	value = false
	featuring_artists.each do |a|
		if title.include?(a)
			value = true
			break
		end
	end
	value
end

.includes_proper_featuring_artists?(title, featuring_artists) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
# File 'lib/music_metadata_score/title_case.rb', line 115

def self.includes_proper_featuring_artists?(title, featuring_artists)
	count = featuring_artists.count rescue 0
	if count > 0
		MusicMetadataScore::TitleCase.is_proper_featuring_artists?(title, featuring_artists)
	else
		true
	end
end

.includes_version?(title) ⇒ Boolean

Includes a Version

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
# File 'lib/music_metadata_score/title_case.rb', line 183

def self.includes_version?(title)
	if !MusicMetadataScore::Score.field_is_blank?(title)
		if title.downcase.include?('version') || title.downcase.include?('edition') || title.downcase.include?('issue') || title.downcase.include?('print') || title.downcase.include?('anniversary') || title.downcase.include?('remix')
			true
		else
			false
		end
	else
		false
	end
end

.is_proper_featuring_artists?(title, featuring_artists) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'lib/music_metadata_score/title_case.rb', line 135

def self.is_proper_featuring_artists?(title, featuring_artists)
	if title.include?(MusicMetadataScore::TitleCase.featuring_artists_string(featuring_artists, "feat.")) || title.include?(MusicMetadataScore::TitleCase.featuring_artists_string(featuring_artists, "with")) || title.include?(MusicMetadataScore::TitleCase.featuring_artists_string(featuring_artists, "vs.")) || title.include?(MusicMetadataScore::TitleCase.featuring_artists_string(featuring_artists, "Meets"))
		true
	else
		false
	end
end

.is_proper_title_format?(title, primary_artists, featuring_artists) ⇒ Boolean

Includes Featuring artistst

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/music_metadata_score/title_case.rb', line 107

def self.is_proper_title_format?(title, primary_artists, featuring_artists)
	if MusicMetadataScore.is_title_case?(title) && MusicMetadataScore::TitleCase.includes_proper_featuring_artists?(title, featuring_artists) && !MusicMetadataScore::TitleCase.includes_version?(title) && !MusicMetadataScore::TitleCase.improper_abbreviations?(title)
		true
	else
		false
	end
end

.lower_case_array(array) ⇒ Object

Pick words for lower case



87
88
89
90
91
92
93
94
# File 'lib/music_metadata_score/title_case.rb', line 87

def self.lower_case_array(array)
	array.each do |a|
		if a != array.first && a != array.last
			a = MusicMetadataScore::TitleCase.lower_case_string(a)
		end
	end
	return array
end

.lower_case_string(string) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/music_metadata_score/title_case.rb', line 96

def self.lower_case_string(string)
	#a, an, and, as, at, but, by, for, from, in, into, nor, of, off, on, onto, or, out, over, so, the, to, up, with and yet
	words = ["a", "an", "and", "as", "at", "but", "by", "for", "from", "in", "into", "nor", "of", "off", "on", "onto", "or", "out", "over", "so", "the", "to", "up", "with", "yet"] 		
	if words.include?(string.downcase)
		string.downcase!
	else
		string
	end
end

.remove_artist_names_from_title(title, artists) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/music_metadata_score/title_case.rb', line 315

def self.remove_artist_names_from_title(title, artists)
	artists.each do |a|
		if title.include?(" #{a} &")
			title.gsub!(" #{a} &", "")
		elsif title.include?(" #{a} and")
			title.gsub!(" #{a} and", "")
		elsif title.include?(" #{a} And")
			title.gsub!(" #{a} And", "")
		elsif title.include?(" #{a},")
			title.gsub!(" #{a},", "")
		else
			title.gsub!(" #{a}", "")
		end
	end
	title
end

.replace_improper_abbreviations(title) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/music_metadata_score/title_case.rb', line 209

def self.replace_improper_abbreviations(title)
	title.gsub!(" part", " Pt.")
	title.gsub!(" volume", " Vol.")
	title.gsub!(" number", " No.")
	title.gsub!(" no ", " No. ")
	title.gsub!(" num ", " No. ")
	title.gsub!(" d.j", " DJ")
	title.gsub!(" d.j.", " DJ")
	title.gsub!(" aka", " a.k.a")
	title.gsub!(" AKA", " a.k.a")
	title.gsub!("featuring", "feat.")
	title.gsub!("(feat ", "(feat. ")
	title.gsub!(" feat ", " feat. ")
	title.gsub!(" vs ", " Vs. ")
	title.gsub!(" VS ", " Vs. ")
	
	return title
end

.split_string(string) ⇒ Object

String to Array



61
62
63
64
65
66
67
# File 'lib/music_metadata_score/title_case.rb', line 61

def self.split_string(string)
	if string && string.is_a?(String)
		string.split(' ')
	else
		[]
	end
end

.strip_artists_from_outside_brackets_from_title(title, primary_artists, featuring_artists) ⇒ Object



310
311
312
313
# File 'lib/music_metadata_score/title_case.rb', line 310

def self.strip_artists_from_outside_brackets_from_title(title, primary_artists, featuring_artists)
	title = MusicMetadataScore::TitleCase.remove_artist_names_from_title(title, MusicMetadataScore::TitleCase.combined_artists(primary_artists, featuring_artists))
	MusicMetadataScore::TitleCase.strip_featuring_words_from_title(title)
end

.strip_artists_from_title(title, primary_artists, featuring_artists) ⇒ Object



292
293
294
295
296
# File 'lib/music_metadata_score/title_case.rb', line 292

def self.strip_artists_from_title(title, primary_artists, featuring_artists)
	title = MusicMetadataScore::TitleCase.strip_artists_in_brackets_from_title(title, primary_artists, featuring_artists)
	title = MusicMetadataScore::TitleCase.strip_artists_from_outside_brackets_from_title(title, primary_artists, featuring_artists)
	title
end

.strip_artists_in_brackets_from_title(title, primary_artists, featuring_artists) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/music_metadata_score/title_case.rb', line 298

def self.strip_artists_in_brackets_from_title(title, primary_artists, featuring_artists)
	inside_brackets = MusicMetadataScore::TitleCase.get_inside_brackets(title)
	if inside_brackets
		inside_brackets.each do |b|
			if MusicMetadataScore::TitleCase.includes_featuring_artists?(b, featuring_artists)
				title.gsub!(" (#{b})", "")
			end
		end
	end
	title
end

.strip_featuring_words_from_title(title) ⇒ Object



332
333
334
335
336
337
# File 'lib/music_metadata_score/title_case.rb', line 332

def self.strip_featuring_words_from_title(title)
	MusicMetadataScore::TitleCase.featuring_words.each do |w|
		title.gsub!(" #{w.strip}", "")
	end
	title
end

.strip_version_from_title(title, version) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/music_metadata_score/title_case.rb', line 275

def self.strip_version_from_title(title, version)
	inside_brackets = MusicMetadataScore::TitleCase.get_inside_brackets(title)
	#version = nil
	if inside_brackets
		inside_brackets.each do |b|
			if MusicMetadataScore::TitleCase.includes_version?(b)
				title.gsub!(" (#{b})", "")
				version = b.strip
			end
		end
	end
	return {
		:title => title.strip,
		:version => MusicMetadataScore::TitleCase.title_case(version)
	}
end

.title_case(string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/music_metadata_score/title_case.rb', line 18

def self.title_case(string)
	if string.is_a?(String)
		string = MusicMetadataScore::TitleCase.add_spacing_to_punctuation(string)
		array = MusicMetadataScore::TitleCase.lower_case_array(MusicMetadataScore::TitleCase.titleize_array(MusicMetadataScore::TitleCase.split_string(string.downcase)))
		new_string = MusicMetadataScore::TitleCase.capitalize_artist_the(array.join(' '))
		return MusicMetadataScore::TitleCase.capitalize_anomalies(new_string)
	else
		""
	end
end

.titleize(string) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/music_metadata_score/title_case.rb', line 77

def self.titleize(string)
	if string[0] =~ /[[:alpha:]]/
		string.capitalize!
	elsif string.length > 1 && !string.include?('feat.')
		string[1] = string[1].capitalize
	end
	return string
end

.titleize_array(array) ⇒ Object

Capitalize all first letters



70
71
72
73
74
75
# File 'lib/music_metadata_score/title_case.rb', line 70

def self.titleize_array(array)
	array.each do |a|
		a = MusicMetadataScore::TitleCase.titleize(a)
	end
	return array
end