Module: Strings

Defined in:
lib/lyrics/utils/strings.rb

Constant Summary collapse

@@word_separators =
" \t\n()[],.;:-¿?¡!\"/\\"

Class Method Summary collapse

Class Method Details

.build_google_feeling_lucky_url(query, site = nil) ⇒ Object



113
114
115
116
117
# File 'lib/lyrics/utils/strings.rb', line 113

def Strings.build_google_feeling_lucky_url( query, site=nil )
	url = "http://www.google.com/search?q=#{CGI.escape( query )}"
	url += "+site%3A#{site}" if site
	return url + "&btnI"
end

.capitalize(text, downcase = false, first_only = false) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/lyrics/utils/strings.rb', line 159

def Strings.capitalize( text, downcase=false, first_only=false )
	text = downcase ? Strings.downcase( text ) : text.to_s()
	if first_only
		text.sub!( /^([0-9a-zA-Zàáâãäåæçèéêëìíîïðñòóôõö×øùúûüýþÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞ])/ ) {|c| Strings.upcase( c ) }
	else
		text.sub!( /([0-9a-zA-Zàáâãäåæçèéêëìíîïðñòóôõö×øùúûüýþÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞ])/ ) {|c| Strings.upcase( c ) }
	end
	return text
end

.capitalize!(text, downcase = false, first_only = false) ⇒ Object



169
170
171
# File 'lib/lyrics/utils/strings.rb', line 169

def Strings.capitalize!( text, downcase=false, first_only=false )
	return text.replace( Strings.capitalize( text, downcase, first_only ) )
end

.cleanup_artist(artist, title) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/lyrics/utils/strings.rb', line 316

def Strings.cleanup_artist( artist, title )
	artist = artist.strip()
	if artist != ""
		if (md = /[ \(\[](ft\.|ft |feat\.|feat |featuring ) *([^\)\]]+)[\)\]]? *$/i.match( title.to_s() ))
			artist << " feat. " << md[2]
		else
			artist.gsub!( /[ \(\[](ft\.|ft |feat\.|feat |featuring ) *([^\)\]]+)[\)\]]? *$/i, " feat. \\2" )
		end
	end
	return artist
end

.cleanup_lyrics(lyrics) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/lyrics/utils/strings.rb', line 249

def Strings.cleanup_lyrics( lyrics )

	lyrics = HTMLEntities.decode( lyrics )

	prev_line = ""
	lines = []

	lyrics.split( /\r\n|\n|\r/ ).each do |line|

		# remove unnecesary spaces
		line.tr_s!( "\t ", " " )
		line.strip!()

		# quotes and double quotes
		line.gsub!( /`|´|’|‘|’|’/, "'" )
		line.gsub!( /''|&quot;|«|»|„|”|“|”/, "\"" )

		# suspensive points
		line.gsub!( /…+/, "..." )
		line.gsub!( /[,;]?\.{2,}/, "..." )

		# add space after "?", "!", ",", ";", ":", ".", ")" and "]" if not present
		line.gsub!( /([^\.]?[\?!,;:\.\)\]])([^ "'<])/, "\\1 \\2" )

		# remove spaces after "¿", "¡", "(" and ")"
		line.gsub!( /([¿¡\(\[]) /, "\\1" )

		# remove spaces before "?", "!", ",", ";", ":", ".", ")" and "]"
		line.gsub!( /\ ([\?!,;:\.\)\]])/, "\\1" )

		# remove space after ... at the beginning of sentence
		line.gsub!( /^\.\.\. /, "..." )

		# remove single points at end of sentence
		line.gsub!( /([^\.])\.$/, "\\1" )

		# remove commas and semicolons at end of sentence
		line.gsub!( /[,;]$/, "" )

		# fix english I pronoun capitalization
		line.gsub!( /([ "'\(\[])i([\ '",;:\.\?!\]\)]|$)/, "\\1I\\2" )

		# remove spaces after " or ' at the begin of sentence of before them when at the end
		line.sub!( /^(["']) /, "\\1" )
		line.sub!( /\ (["'])$/, "\\1" )

		# capitalize first alfabet character of the line
		Strings.capitalize!( line )

		# no more than one empty line at the time
		if ! line.empty? || ! prev_line.empty?
			lines << line
			prev_line = line
		end
	end

	if lines.length > 0 && lines[lines.length-1].empty?
		lines.delete_at( lines.length-1 )
	end

	return lines.join( "\n" )
end

.cleanup_lyrics!(lyrics) ⇒ Object



312
313
314
# File 'lib/lyrics/utils/strings.rb', line 312

def Strings.cleanup_lyrics!( lyrics )
	return lyrics.replace( Strings.cleanup_lyrics( lyrics ) )
end

.cleanup_title(title) ⇒ Object



328
329
330
331
332
# File 'lib/lyrics/utils/strings.rb', line 328

def Strings.cleanup_title( title )
	title = title.gsub( /[ \(\[](ft\.|ft |feat\.|feat |featuring ) *([^\)\]]+)[\)\]]? *$/i, "" )
	title.strip!()
	return title
end

.decode_htmlentities(var) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/lyrics/utils/strings.rb', line 235

def Strings.decode_htmlentities( var )
	if var.is_a?( String )
		return HTMLEntities.decode( var )
	elsif var.is_a?( Hash )
		ret = {}
		var.each() do |key, value|
			ret[key] = decode_htmlentities( value )
		end
		return ret
	else
		return var
	end
end

.decode_htmlentities!(var) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/lyrics/utils/strings.rb', line 226

def Strings.decode_htmlentities!( var )
	if var.is_a?( String )
		HTMLEntities.decode!( var )
	elsif var.is_a?( Hash )
		var.each() { |key, value| decode_htmlentities!( value ) }
	end
	return var
end

.descramble(text) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'lib/lyrics/utils/strings.rb', line 366

def Strings.descramble( text )
	text = text.to_s()
	2.times() do
		chars = text.split( ":" ).collect() { |c| c.to_i }
		chars.size.times() { |idx| chars[idx] = (chars[idx] - idx - 1) }
		text = chars.reverse().pack( "U*" )
	end
	return text
end

.descramble!(text) ⇒ Object



376
377
378
# File 'lib/lyrics/utils/strings.rb', line 376

def Strings.descramble!( text )
	return text.replace( Strings.descramble( text ) )
end

.downcase(text) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lyrics/utils/strings.rb', line 119

def Strings.downcase( text )
	begin
		return text.to_s().unpack( "U*" ).collect() do |c|
			if c >= 65 && c <= 90 # abcdefghijklmnopqrstuvwxyz
				c + 32
			elsif c >= 192 && c <= 222 # ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞ
				c + 32
			else
				c
			end
		end.pack( "U*" )
	rescue Exception # fallback to normal operation on error
		return text.downcase()
	end
end

.downcase!(text) ⇒ Object



135
136
137
# File 'lib/lyrics/utils/strings.rb', line 135

def Strings.downcase!( text )
	return text.replace( Strings.downcase( text ) )
end

.empty?(text) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/lyrics/utils/strings.rb', line 32

def Strings.empty?( text )
	text = text.to_s()
	return  text.empty? ? true : text.strip.empty?
end

.google_search_quote(text) ⇒ Object



107
108
109
110
111
# File 'lib/lyrics/utils/strings.rb', line 107

def Strings.google_search_quote( text )
	text = text.gsub( "\"", "" )
	text.gsub!( /^\ *the\ */i, "" )
	return Strings.empty?( text) ? "" : "\"#{text}\""
end

.latin12utf8(text) ⇒ Object



343
344
345
346
347
348
349
350
# File 'lib/lyrics/utils/strings.rb', line 343

def Strings.latin12utf8( text )
	begin
		return text.unpack( "C*" ).pack( "U*" )
	rescue Exception
		$stderr << "warning: conversion from Latin1 to UTF-8 failed\n"
		return text
	end
end

.normalize(token) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/lyrics/utils/strings.rb', line 209

def Strings.normalize( token )
	token = Strings.downcase( token )
	token.tr_s!( " \n\r\t.;:()[]", " " )
	token.strip!()
	token.gsub!( /`|´|’/, "'" )
	token.gsub!( /''|«|»/, "\"" )
	token.gsub!( /[&+]/, "and" )
	token.gsub!( /\ ('n'|'n|n') /, " and " )
	token.gsub!( /^the /, "" )
	token.gsub!( /, the$/, "" )
	return token
end

.normalize!(token) ⇒ Object



222
223
224
# File 'lib/lyrics/utils/strings.rb', line 222

def Strings.normalize!( token )
	return token.replace( Strings.normalize( token ) )
end

.random_token(length = 10) ⇒ Object



73
74
75
76
77
78
# File 'lib/lyrics/utils/strings.rb', line 73

def Strings.random_token( length=10 )
	chars = ( "a".."z" ).to_a() + ( "0".."9" ).to_a()
	token = ""
	1.upto( length ) { |i| token << chars[rand(chars.size-1)] }
	return token
end

.remove_invalid_filename_chars(filename) ⇒ Object



80
81
82
# File 'lib/lyrics/utils/strings.rb', line 80

def Strings.remove_invalid_filename_chars( filename )
	return Strings.remove_invalid_filename_chars!( String.new( filename ) )
end

.remove_invalid_filename_chars!(filename) ⇒ Object



84
85
86
87
# File 'lib/lyrics/utils/strings.rb', line 84

def Strings.remove_invalid_filename_chars!( filename )
	filename.tr_s!( "*?:|/\\<>", "" )
	return filename
end

.remove_vocal_accents(text) ⇒ Object



89
90
91
# File 'lib/lyrics/utils/strings.rb', line 89

def Strings.remove_vocal_accents( text )
	return Strings.remove_vocal_accents!( String.new( text ) )
end

.remove_vocal_accents!(text) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lyrics/utils/strings.rb', line 93

def Strings.remove_vocal_accents!( text )
	text.gsub!( /á|à|ä|â|å|ã/, "a" )
	text.gsub!( /Á|À|Ä|Â|Å|Ã/, "A" )
	text.gsub!( /é|è|ë|ê/, "e" )
	text.gsub!( /É|È|Ë|Ê/, "E" )
	text.gsub!( /í|ì|ï|î/, "i" )
	text.gsub!( /Í|Ì|Ï|Î/, "I" )
	text.gsub!( /ó|ò|ö|ô/, "o" )
	text.gsub!( /Ó|Ò|Ö|Ô/, "O" )
	text.gsub!( /ú|ù|ü|û/, "u" )
	text.gsub!( /Ú|Ù|Ü|Û/, "U" )
	return text
end

.scramble(text) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/lyrics/utils/strings.rb', line 352

def Strings.scramble( text )
	text = text.to_s()
	2.times() do
		chars = text.unpack( "U*" ).reverse()
		chars.size.times() { |idx| chars[idx] = (chars[idx] + idx + 1) }
		text = chars.collect() { |c| c.to_s }.join( ":" )
	end
	return text
end

.scramble!(text) ⇒ Object



362
363
364
# File 'lib/lyrics/utils/strings.rb', line 362

def Strings.scramble!( text )
	return text.replace( Strings.scramble( text ) )
end

.shell_escape(text) ⇒ Object



49
50
51
# File 'lib/lyrics/utils/strings.rb', line 49

def Strings.shell_escape( text )
	return text.gsub( "\\", "\\\\\\" ).gsub( "\"", "\\\"" ).gsub( "`", "\\\\`" ).gsub( %q/'/, %q/\\\'/ ).gsub( " ", "\\ " )
end

.shell_quote(text) ⇒ Object



37
38
39
# File 'lib/lyrics/utils/strings.rb', line 37

def Strings.shell_quote( text )
	return "\"" + text.gsub( "\\", "\\\\\\" ).gsub( "\"", "\\\"" ).gsub( "`", "\\\\`" ) + "\""
end

.shell_unescape(text) ⇒ Object



53
54
55
# File 'lib/lyrics/utils/strings.rb', line 53

def Strings.shell_unescape( text )
	return text.gsub( "\\ ", " " ).gsub( "\\'", "'" ).gsub( "\\`", "`" ).gsub( "\\\"", "\"" )
end

.shell_unquote(text) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lyrics/utils/strings.rb', line 41

def Strings.shell_unquote( text )
	if text.slice( 0, 1 ) == "\""
		return text.gsub( "\\`", "`" ).gsub( "\\\"", "\"" ).slice( 1..-2 )
	else # if text.slice( 0, 1 ) == "'"
		return text.slice( 1..-2 )
	end
end

.sql_escape(text) ⇒ Object



65
66
67
# File 'lib/lyrics/utils/strings.rb', line 65

def Strings.sql_escape( text )
	return text.gsub( "'", "''" )
end

.sql_quote(text) ⇒ Object



57
58
59
# File 'lib/lyrics/utils/strings.rb', line 57

def Strings.sql_quote( text )
	return "'" + Strings.sql_escape( text ) + "'"
end

.sql_unescape(text) ⇒ Object



69
70
71
# File 'lib/lyrics/utils/strings.rb', line 69

def Strings.sql_unescape( text )
	return text.gsub( "''", "'" )
end

.sql_unquote(text) ⇒ Object



61
62
63
# File 'lib/lyrics/utils/strings.rb', line 61

def Strings.sql_unquote( text )
	return Strings.sql_unescape( text.slice( 1..-2 ) )
end

.titlecase(text, correct_case = true, downcase = false) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lyrics/utils/strings.rb', line 173

def Strings.titlecase( text, correct_case=true, downcase=false )
	text = Strings.capitalize( text, downcase )
	word_start = true
	text = text.unpack( "U*" ).collect() do |c|
		if word_start
			chr = [c].pack( "U*" )
			if ! @@word_separators.include?( chr )
				word_start = false
				c = Strings.upcase( chr ).unpack( "U*" )[0]
			end
		else
			chr = c < 256 ? c.chr() : [c].pack( "U*" )
			word_start = true if @@word_separators.include?( chr )
		end
		c
	end.pack( "U*" )
	if correct_case
		lc_words = [
			"the", "a", "an", # articles
			"and", "but", "or", "nor", # conjunctions
			"'n'", "'n", "n'", # and contractions
			"as", "at", "by", "for", "in", "of", "on", "to", # short prepositions
			#"from", "into", "onto", "with", "over" # not so short prepositions
			"feat", "vs", # special words
		]
		lc_words.each() do |lc_word|
			text.gsub!( /\ #{lc_word}([ ,;:\.-?!\"\/\\\)])/i, " #{lc_word}\\1" )
		end
	end
	return text
end

.titlecase!(text, correct_case = true, downcase = false) ⇒ Object



205
206
207
# File 'lib/lyrics/utils/strings.rb', line 205

def Strings.titlecase!( text, correct_case=true, downcase=false )
	return text.replace( Strings.titlecase( text, correct_case, downcase ) )
end

.upcase(text) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/lyrics/utils/strings.rb', line 139

def Strings.upcase( text )
	begin
		return text.to_s().unpack( "U*" ).collect() do |c|
			if c >= 97 && c <= 122 # ABCDEFGHIJKLMNOPQRSTUVWXYZ
				c - 32
			elsif c >= 224 && c <= 254 # àáâãäåæçèéêëìíîïðñòóôõö×øùúûüýþ
				c - 32
			else
				c
			end
		end.pack( "U*" )
	rescue Exception # fallback to normal operation on error
		return text.upcase()
	end
end

.upcase!(text) ⇒ Object



155
156
157
# File 'lib/lyrics/utils/strings.rb', line 155

def Strings.upcase!( text )
	return text.replace( Strings.upcase( text ) )
end

.utf82latin1(text) ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/lyrics/utils/strings.rb', line 334

def Strings.utf82latin1( text )
	begin
		return text.unpack( "U*" ).pack( "C*" )
	rescue Exception
		$stderr << "warning: conversion from UTF-8 to Latin1 failed\n"
		return text
	end
end