module Emoninja::Data
Utility to grab consortium’s data for emojis
Public Class Methods
argo()
click to toggle source
@return Hashie::Mash
# File lib/emoninja/data.rb, line 39 def argo @argo ||= data.each_with_object(Hashie::Mash.new) do |h, memo| h[:keywords].each do |kw| (memo[kw] ||= []) << h[:glyph] unless stopword?(kw) end end end
data()
click to toggle source
# File lib/emoninja/data.rb, line 11 def data @data ||= Grabber.load end
emoji(term, kind: :glyph, exact: false, number: nil, lang: nil)
click to toggle source
# File lib/emoninja/data.rb, line 49 def emoji term, kind: :glyph, exact: false, number: nil, lang: nil match = exact(term, lang: lang)[kind] return match if match || exact return nil unless (match = keywords(term, lang: lang).map { |kw| kw[kind] }) number ? match[number] : match.sample end
exact(term, lang: nil)
click to toggle source
@return Hashie::Mash
# File lib/emoninja/data.rb, line 26 def exact term, lang: nil term = marshal term, lang Hashie::Mash.new(cache[:exacts][term.to_s.downcase] ? data[cache[:exacts][term.to_s.downcase]] : {}) end
keywords(term, lang: nil)
click to toggle source
@return [Hashie::Mash]
# File lib/emoninja/data.rb, line 20 def keywords term, lang: nil term = marshal term, lang (cache[:keywords][term.to_s.downcase] || []).map(&data.method(:[])).map(&Hashie::Mash.method(:new)) end
stopword?(term)
click to toggle source
# File lib/emoninja/data.rb, line 15 def stopword? term term.length < STOPWORD_MIN_LENGTH || options[:stopwords].include?(term) end
vocabulary()
click to toggle source
@return Hash
# File lib/emoninja/data.rb, line 34 def vocabulary @vocabulary ||= data.map { |h| [h[:name], h[:glyph]] }.to_h.reject { |k, _| stopword?(k) } end
Private Class Methods
cache()
click to toggle source
# File lib/emoninja/data.rb, line 59 def cache return @cache if @cache @cache = data.each_with_index.with_object(keywords: {}, exacts: {}) do |(e, i), memo| memo[:exacts][e[:name].downcase] = i e[:keywords].each do |kw| (memo[:keywords][kw.downcase] ||= []) << i end end end
marshal(term, lang = nil)
click to toggle source
# File lib/emoninja/data.rb, line 70 def marshal term, lang = nil return term if lang.nil? || options.translate!.languages!.none? { |l| l == lang.to_s } I18n.key term, lang end