Class: Unicoder::Builder::SequenceName

Inherits:
Object
  • Object
show all
Includes:
Unicoder::Builder, ReplaceCommonWords
Defined in:
lib/unicoder/builders/sequence_name.rb

Constant Summary collapse

REPLACE_COUNT =
100
REPLACE_BASE =
?{.ord
REPLACE_MIN_WORD_LENGTH =
3

Instance Attribute Summary

Attributes included from Unicoder::Builder

#formats, #index, #option

Instance Method Summary collapse

Methods included from ReplaceCommonWords

#replace_common_words!

Methods included from Unicoder::Builder

#assign, build, #export, #initialize, #meta, #parse_file

Instance Method Details

#assign_codepoint(codepoints, value, idx = , combine: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/unicoder/builders/sequence_name.rb', line 19

def assign_codepoint(codepoints, value, idx = @index[:SEQUENCES], combine: false)
  if option =~ /charkeys/
    key = codepoints.pack("U*")
  else
    key = codepoints
  end

  if idx.has_key?(codepoints)
    if combine
      idx[key] << " / #{value}"
    else
      # ignore new one
    end
  else
    idx[key] = value
  end

  @words += value.split
end

#initialize_indexObject



11
12
13
14
15
16
17
# File 'lib/unicoder/builders/sequence_name.rb', line 11

def initialize_index
  @index = {
    SEQUENCES: {},
    EMOJI_NOT_QUALIFIED: {},
  }
  @words = []
end

#parse!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/unicoder/builders/sequence_name.rb', line 39

def parse!
  parse_file :named_sequences, :line, regex: /^(?!#)(?<name>.+?);(?<codepoints>.+?)$/ do |line|
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, line["name"]
  end

  parse_file :named_sequences_prov, :line, regex: /^(?!#)(?<name>.+?);(?<codepoints>.+?)$/ do |line|
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, line["name"]
  end

  parse_file :standardized_variants, :line, regex: /^(?<codepoints>.+?);\s*(?<variant>.+?)\s*;\s*(?<context>.*?)\s*# (?<name>.+)$/ do |line|
    name = "#{line["name"].strip} (#{line["variant"]})"
    name << " [#{line["context"]}]" if line["context"] && !line["context"].empty?
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, name, combine: true
  end

  parse_file :standardized_variants, :line, regex: /^(?<codepoints>.+?); (?<name>.+?)\s*;$/ do |line|
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, line["name"]
  end

  parse_file :ivd_sequences, :line, regex: /^(?<codepoints>.+?);.*?; (?<name>.+?)$/ do |line|
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, line["name"], combine: true
  end

  parse_file :emoji_variation_sequences, :line, regex: /^(?<codepoints>.+?)\s*;\s*(?<variant>.+?)\s*;\s*# \(.*\)\s*(?<name>.+?)\s*$/ do |line|
    name = "#{line["name"].strip} (#{line["variant"]})"
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, name
  end

  parse_file :emoji_sequences, :line, regex: /^(?<codepoints>.+?)\s*;\s*(?<type>.+?)\s*; (?<name>.+?)\s*#/ do |line|
    next if line["type"] == "Basic_Emoji"
    name = line["name"].gsub(/\\x{(\h+)}/){ [$1.to_i(16)].pack("U") }.upcase
    assign_codepoint line["codepoints"].split.map{|cp| cp.to_i(16) }, name
  end

  parse_file :emoji_zwj_sequences, :line, regex: /^(?!#)(?<codepoints>.+?)\s*;.*?; (?<name>.+?)\s*#/ do |line|
    name = line["name"].gsub(/\\x{(\h+)}/){ [$1.to_i(16)].pack("U") }.upcase
    codepoints = line["codepoints"].split.map{|cp| cp.to_i(16) }
    assign_codepoint codepoints, name


    # Build all combinations of VS16 present and missing and add to second index
    if codepoints.include?(0xFE0F)
      sequence = codepoints.pack("U*")

      codepoints.slice_after(0xFE0F).reduce([[]]){|acc,cur|
        if cur.include? 0xFE0F
          acc.flat_map{|prev| [prev + (cur - [0xFE0F]), prev + cur] }
        else
          acc.map{|prev| prev + cur}
        end
      }.
      select {|sub_codepoints| sub_codepoints != codepoints }.
      each { |sub_codepoints|
        sub_sequence = sub_codepoints.pack("U*")
        @index[:EMOJI_NOT_QUALIFIED][sub_sequence] = sequence
      }
    end
  end

  replace_common_words! :SEQUENCES, @words, REPLACE_COUNT, REPLACE_BASE, REPLACE_MIN_WORD_LENGTH
end