Class: EivuFingerprinterAcoustid::Objects::Match

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/eivu_fingerprinter_acoustid/objects/match.rb

Constant Summary collapse

THRESHOLD_MIN =
12
OFFSET_IN_STRING =
1
OFFSET_REORDERED =
5
THRESHOLD_REORDERED =
0.85

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recording:, result_score:, release_group:, original_release_group_name:, matched_release_group_name:, result_id:) ⇒ Match

Returns a new instance of Match.



27
28
29
30
31
32
33
34
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 27

def initialize(recording:, result_score:, release_group:, original_release_group_name:, matched_release_group_name:, result_id:)
  @release_group               = release_group
  @recording                   = recording
  @result_score                = result_score
  @original_release_group_name = original_release_group_name
  @matched_release_group_name  = matched_release_group_name
  @result_id                   = result_id
end

Instance Attribute Details

#matched_release_group_nameObject (readonly)

Returns the value of attribute matched_release_group_name.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def matched_release_group_name
  @matched_release_group_name
end

#original_release_group_nameObject (readonly)

Returns the value of attribute original_release_group_name.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def original_release_group_name
  @original_release_group_name
end

#recordingObject (readonly)

Returns the value of attribute recording.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def recording
  @recording
end

#release_groupObject (readonly) Also known as: releasegroup

Returns the value of attribute release_group.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def release_group
  @release_group
end

#result_idObject (readonly)

Returns the value of attribute result_id.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def result_id
  @result_id
end

#result_scoreObject (readonly)

Returns the value of attribute result_score.



17
18
19
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 17

def result_score
  @result_score
end

Instance Method Details

#<=>(other) ⇒ Object

returns 1 if self > other returns 0 if self == other returns -1 if self < other



39
40
41
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 39

def <=> (other)
  result_score <=> other.result_score
end

#distanceObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 43

def distance
  @distance ||=
    begin
      orig_album    = original_release_group_name&.downcase
      matched_album = release_group.title&.downcase
      l_distance = DamerauLevenshtein.distance(orig_album, matched_album)
      if l_distance <= THRESHOLD_MIN
        l_distance
      else
        shorter, longer = [orig_album, matched_album].sort{|x,y| x.length <=> y .length}
        return THRESHOLD_MIN + OFFSET_IN_STRING if longer.include?(shorter)

        shorter_cleansed = shorter.gsub(/[^0-9a-z ]/i, '')
        longer_cleansed  = longer.gsub(/[^0-9a-z ]/i, '')
        find_count = shorter_cleansed.split.count do |word|
          longer_cleansed.include?(word)
        end
        reordered_percentage = find_count / shorter_cleansed.split.count.to_f
        return THRESHOLD_MIN + OFFSET_REORDERED if reordered_percentage >= THRESHOLD_REORDERED

        raise "fix this: distance is #{l_distance}"
      end
    end
end


68
69
70
71
# File 'lib/eivu_fingerprinter_acoustid/objects/match.rb', line 68

def print_artists
  puts "release_group: #{release_group.artists.collect(&:name).join(', ')}"
  puts "recording: #{recording.artists.collect(&:name).join(', ')}"
end