Class: Vocalist::Nationality

Inherits:
Object
  • Object
show all
Defined in:
lib/vocalist/nationality.rb

Overview

Class which resolves an artist to its nationality

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNationality

Create a new instance. There is no data loaded at this point, everything needs to be loaded separate.



9
10
11
# File 'lib/vocalist/nationality.rb', line 9

def initialize()
  @tag_map = {}
end

Class Method Details

.__count_tag(tag, entry) ⇒ Object

Helper function to compute tag count and an maybe-nil entry

Parameters:

  • tag (Scrobbler::Tag)
  • entry (Fixnum, Nil)


89
90
91
92
# File 'lib/vocalist/nationality.rb', line 89

def self.__count_tag(tag, entry)
  count = (entry.nil? ? 0 : entry)
  count += [tag.count, 1].max
end

Instance Method Details

#add_mapping(tag, country) ⇒ Object

Parameters:

  • tag (String)

    The tag mapped to the country (case-insensitive)

  • country (String)

    The country identified be tag (case-sensitive)



40
41
42
# File 'lib/vocalist/nationality.rb', line 40

def add_mapping(tag, country)
  @tag_map[tag.downcase] = country
end

#by_scrobbler_tags(tags) ⇒ String

Classify an artist by its tags on Last.fm.

Parameters:

  • tags (Array<Scrobbler::Tag>)

    The artist’s tags from Last.FM

Returns:

  • (String)

    The country this artist may belong to



69
70
71
72
# File 'lib/vocalist/nationality.rb', line 69

def by_scrobbler_tags(tags)
  result = by_scrobbler_tags_ex(tags).to_a.each { |s| s.reverse! }.max
  result[1] unless result.nil?
end

#by_scrobbler_tags_ex(tags) ⇒ Hash<String, Fixnum>

Classify an artist by its tags on Last.fm.

Extended version: Returns all possible countries.

Parameters:

  • tags (Array<Scrobbler::Tag>)

    The artist’s tags from Last.FM

Returns:

  • (Hash<String, Fixnum>)

    The countries this artist may belong to and their score (a higher score => probability is higher)



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vocalist/nationality.rb', line 51

def by_scrobbler_tags_ex(tags)
  countries = {}
  tags.each do |tag|
    # by default we only handle tags case-insensitive
    country = @tag_map[tag.name.downcase]
    if !country.nil? then
      countries[country] = Nationality::__count_tag(tag, countries[country])
    end
  end
  
  countries
end

#has_tag?(tag) ⇒ Boolean

Do we already know about a tag?

Parameters:

  • tag (String)

    The tag that should be checked if we already have a mapping including it.

Returns:

  • (Boolean)

    True, if there is a mapping including this tag.



80
81
82
# File 'lib/vocalist/nationality.rb', line 80

def has_tag?(tag)
  @tag_map.has_key?(tag.downcase)
end

#load_file(file) ⇒ void

This method returns an undefined value.

Load a given file

Parameters:

  • file (String)

    A file name where to load mappings from.



28
29
30
31
32
33
34
# File 'lib/vocalist/nationality.rb', line 28

def load_file(file)
  file_data = YAML.load(File.read(file))
  country = file_data[:country]
  file_data[:tags].each do |tag|
    @tag_map[tag.downcase] = country
  end
end

#load_files(files) ⇒ void

This method returns an undefined value.

Loads the specified Files

Parameters:

  • files (Array<String>)

    A list of file names (could be regular expressions, e.g. ‘lib/*/.yml’)



18
19
20
21
22
# File 'lib/vocalist/nationality.rb', line 18

def load_files(files)
  Dir.glob(files).each do |file|
    load_file(file)
  end
end