From 14f9a16c3b3ae03816bae387068f08dfba5ba1aa Mon Sep 17 00:00:00 2001 From: Jens Fahnenbruck Date: Thu, 21 Jun 2012 11:34:43 +0200 Subject: [PATCH] give hight weight for name matching if you search for 'Chris Martin' coldplay will be higher ranked than the singer 'Chris Martin', and that shouldn't be so --- lib/models/music_brainz/artist.rb | 8 +++++++- test/test_musicbrainz_artist.rb | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/models/music_brainz/artist.rb b/lib/models/music_brainz/artist.rb index 447b8df..fe1aa03 100644 --- a/lib/models/music_brainz/artist.rb +++ b/lib/models/music_brainz/artist.rb @@ -60,7 +60,13 @@ module MusicBrainz :mbid => self.safe_get_attr(a, nil, 'id') } aliases = a.css('alias-list > alias').map{ |item| item.text } - if aliases.include? name + if artist[:name] == name + artist[:weight] += 100 + elsif artist[:name].downcase == name.downcase + artist[:weight] += 50 + elsif artist[:name].downcase.gsub(/\s/, '') == name.downcase.gsub(/\s/, '') + artist[:weight] += 25 + elsif aliases.include? name artist[:weight] += 20 elsif aliases.map{ |item| item.downcase }.include? name.downcase artist[:weight] += 10 diff --git a/test/test_musicbrainz_artist.rb b/test/test_musicbrainz_artist.rb index df21a4f..6977c88 100644 --- a/test/test_musicbrainz_artist.rb +++ b/test/test_musicbrainz_artist.rb @@ -13,6 +13,12 @@ class TestMusicbrainzArtist < Test::Unit::TestCase assert_operator(0, :<, matches.length) assert_equal("Kasabian", matches.first[:name]) end + + should "find name first than alias" do + matches = MusicBrainz::Artist.search('Chris Martin') + assert_operator(0, :<, matches.length) + assert_equal("Chris Martin", matches.first[:name]) + end should "get correct result by name" do @artist = MusicBrainz::Artist.find_by_name('Kasabian')