clean up emoji keywords

pull/1/head
Alpha Chen 2 years ago
parent 82c75d7606
commit 508f5f7eff
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -12,7 +12,7 @@ require "gemoji"
medium_dark_skin_tone: ["🏾", "Medium-Dark Skin Tone"], medium_dark_skin_tone: ["🏾", "Medium-Dark Skin Tone"],
dark_skin_tone: ["🏿", "Dark Skin Tone"], dark_skin_tone: ["🏿", "Dark Skin Tone"],
}.each do |key, (unicode_alias, desc)| }.each do |key, (unicode_alias, desc)|
Emoji.create(key) do |char| Emoji.create(key.to_s) do |char|
char.add_unicode_alias unicode_alias char.add_unicode_alias unicode_alias
char.description = desc char.description = desc
end end
@ -27,22 +27,47 @@ task :rebuild => "build" do
Emoji.all.each do |emoji| Emoji.all.each do |emoji|
uuid = SecureRandom.uuid uuid = SecureRandom.uuid
name = [ keywords = [emoji.description]
emoji.raw,
emoji.description, keywords.concat(
emoji.aliases.map { "\"#{_1}\"" }.join(", "), emoji.aliases
] .map { _1.gsub(?_, " ") }
name << "(#{emoji.tags.join(", ")})" unless emoji.tags.empty? .reject { str_match(emoji.description, _1) }
)
keywords.concat(emoji.tags)
File.write("build/#{emoji.name} [#{uuid}].json", JSON.dump({ File.write("build/#{emoji.name} [#{uuid}].json", JSON.dump({
alfredsnippet: { alfredsnippet: {
snippet: emoji.raw, snippet: emoji.raw,
dontautoexpand: true, dontautoexpand: true,
uid: uuid, uid: uuid,
name: name.join(" "), name: "#{emoji.raw} #{keywords.join(", ")}",
keyword: emoji.name, keyword: emoji.name,
}, },
})) }))
end end
end end
task :default => :rebuild task :default => :rebuild
def str_match(a, b)
return true if a.gsub(/[^\w]+/, "").downcase.include?(b.gsub(/[^\w]+/, "").downcase)
levenshtein(a, b) < 2
end
def levenshtein(a, b)
memo = Hash.new {|h,(a,b)|
h[[a,b]] = (->() {
return b.length if a.empty?
return a.length if b.empty?
return h[[a[1..], b[1..]]] if a[0] == b[0]
1 + [
h[[a, b[1..]]],
h[[a[1..], b]],
h[[a[1..], b[1..]]],
].min
}).()
}
memo[[a, b].map(&:downcase)]
end

Loading…
Cancel
Save