Module: Strings

Defined in:
lib/wiki_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



110
111
112
113
114
# File 'lib/wiki_lyrics/utils/strings.rb', line 110

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



156
157
158
159
160
161
162
163
164
# File 'lib/wiki_lyrics/utils/strings.rb', line 156

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



166
167
168
# File 'lib/wiki_lyrics/utils/strings.rb', line 166

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

.cleanup_artist(artist, title) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/wiki_lyrics/utils/strings.rb', line 313

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



246
247
248
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
# File 'lib/wiki_lyrics/utils/strings.rb', line 246

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



309
310
311
# File 'lib/wiki_lyrics/utils/strings.rb', line 309

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

.cleanup_title(title) ⇒ Object



325
326
327
328
329
# File 'lib/wiki_lyrics/utils/strings.rb', line 325

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

.decode_htmlentities(var) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/wiki_lyrics/utils/strings.rb', line 232

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



223
224
225
226
227
228
229
230
# File 'lib/wiki_lyrics/utils/strings.rb', line 223

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



363
364
365
366
367
368
369
370
371
# File 'lib/wiki_lyrics/utils/strings.rb', line 363

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



373
374
375
# File 'lib/wiki_lyrics/utils/strings.rb', line 373

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

.downcase(text) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wiki_lyrics/utils/strings.rb', line 116

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



132
133
134
# File 'lib/wiki_lyrics/utils/strings.rb', line 132

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

.empty?(text) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/wiki_lyrics/utils/strings.rb', line 29

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

.google_search_quote(text) ⇒ Object



104
105
106
107
108
# File 'lib/wiki_lyrics/utils/strings.rb', line 104

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

.latin12utf8(text) ⇒ Object



340
341
342
343
344
345
346
347
# File 'lib/wiki_lyrics/utils/strings.rb', line 340

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



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/wiki_lyrics/utils/strings.rb', line 206

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



219
220
221
# File 'lib/wiki_lyrics/utils/strings.rb', line 219

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

.random_token(length = 10) ⇒ Object



70
71
72
73
74
75
# File 'lib/wiki_lyrics/utils/strings.rb', line 70

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



77
78
79
# File 'lib/wiki_lyrics/utils/strings.rb', line 77

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

.remove_invalid_filename_chars!(filename) ⇒ Object



81
82
83
84
# File 'lib/wiki_lyrics/utils/strings.rb', line 81

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

.remove_vocal_accents(text) ⇒ Object



86
87
88
# File 'lib/wiki_lyrics/utils/strings.rb', line 86

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

.remove_vocal_accents!(text) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/wiki_lyrics/utils/strings.rb', line 90

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



349
350
351
352
353
354
355
356
357
# File 'lib/wiki_lyrics/utils/strings.rb', line 349

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



359
360
361
# File 'lib/wiki_lyrics/utils/strings.rb', line 359

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

.shell_escape(text) ⇒ Object



46
47
48
# File 'lib/wiki_lyrics/utils/strings.rb', line 46

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

.shell_quote(text) ⇒ Object



34
35
36
# File 'lib/wiki_lyrics/utils/strings.rb', line 34

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

.shell_unescape(text) ⇒ Object



50
51
52
# File 'lib/wiki_lyrics/utils/strings.rb', line 50

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

.shell_unquote(text) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/wiki_lyrics/utils/strings.rb', line 38

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



62
63
64
# File 'lib/wiki_lyrics/utils/strings.rb', line 62

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

.sql_quote(text) ⇒ Object



54
55
56
# File 'lib/wiki_lyrics/utils/strings.rb', line 54

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

.sql_unescape(text) ⇒ Object



66
67
68
# File 'lib/wiki_lyrics/utils/strings.rb', line 66

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

.sql_unquote(text) ⇒ Object



58
59
60
# File 'lib/wiki_lyrics/utils/strings.rb', line 58

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

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



170
171
172
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
# File 'lib/wiki_lyrics/utils/strings.rb', line 170

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



202
203
204
# File 'lib/wiki_lyrics/utils/strings.rb', line 202

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

.upcase(text) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/wiki_lyrics/utils/strings.rb', line 136

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



152
153
154
# File 'lib/wiki_lyrics/utils/strings.rb', line 152

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

.utf82latin1(text) ⇒ Object



331
332
333
334
335
336
337
338
# File 'lib/wiki_lyrics/utils/strings.rb', line 331

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