source/Emoji/gemoji-parser/lib/gemoji-parser.rb
def unicode_regex(opts={})
    return @unicode_regex if defined?(@unicode_regex) && !opts[:rehash]
  
    scores_file = File.expand_path('../../db/scores.json', __FILE__)
    scores = File.open(scores_file, 'r:UTF-8') { |data| JSON.parse(data.read) }
    pattern = []

    Emoji.all.each do |emoji|
      score_id = nil
      u = emoji.unicode_aliases.map do |char|
        score_id = char if scores[char]
        char.codepoints.map { |c| '\u{%s}' % c.to_s(16).rjust(4, '0') }.join('')
      end

      if u.any?
        pattern << {
          :pattern => unicode_matcher(u),
          :score => score_id ? scores[score_id].to_i : 0
        }
      end
    end

    pattern.sort! { |a, b| b[:score] - a[:score] }
    pattern.map! { |p| p[:pattern] }

    @unicode_pattern = "(?:#{ pattern.join('|') })#{ U_FE0F }?"
    @unicode_regex = Regexp.new("(#{@unicode_pattern})")
  end
# Creates an optimized regular expression for matching unicode symbols.
# - Options: rehash:boolean
Creates an optimized regular expression for matching unicode symbols.
Options: rehash:boolean
Creates an optimized regular expression for matching unicode symbols . Options : rehash : boolean
def unicode_regex(opts={})
    return @unicode_regex if defined?(@unicode_regex) && !opts[:rehash]
    scores_file = File.expand_path('../../db/scores.json', __FILE__)
    scores = File.open(scores_file, 'r:UTF-8') { |data| JSON.parse(data.read) }
    pattern = []
    Emoji.all.each do |emoji|
      score_id = nil
      u = emoji.unicode_aliases.map do |char|
        score_id = char if scores[char]
        char.codepoints.map { |c| '\u{%s}' % c.to_s(16).rjust(4, '0') }.join('')
      end
      if u.any?
        pattern << {
          :pattern => unicode_matcher(u),
          :score => score_id ? scores[score_id].to_i : 0
        }
      end
    end
    pattern.sort! { |a, b| b[:score] - a[:score] }
    pattern.map! { |p| p[:pattern] }
    @unicode_pattern = "(?:#{ pattern.join('|') })#{ U_FE0F }?"
    @unicode_regex = Regexp.new("(#{@unicode_pattern})")
  end
def unicode_regex ( opts = { } ) return @unicode_regex if defined? ( @unicode_regex ) && ! opts [ :rehash ] scores_file = File . expand_path ( '../../db/scores.json' , __FILE__ ) scores = File . open ( scores_file , 'r:UTF-8' ) { | data | JSON . parse ( data . read ) } pattern = [ ] Emoji . all . each do | emoji | score_id = nil u = emoji . unicode_aliases . map do | char | score_id = char if scores [ char ] char . codepoints . map { | c | '\u{%s}' % c . to_s ( 16 ) . rjust ( 4 , '0' ) } . join ( '' ) end if u . any? pattern << { :pattern => unicode_matcher ( u ) , :score => score_id ? scores [ score_id ] . to_i : 0 } end end pattern . sort! { | a , b | b [ :score ] - a [ :score ] } pattern . map! { | p | p [ :pattern ] } @unicode_pattern = "(?:#{ pattern.join('|') })#{ U_FE0F }?" @unicode_regex = Regexp . new ( "(#{@unicode_pattern})" ) end
Creates an optimized regular expression for matching unicode symbols.
Creates an optimized regular expression for matching unicode symbols .
opts