Skip to content

Instantly share code, notes, and snippets.

@ysakasin

ysakasin/emoji_statistics.rb Secret

Created Dec 7, 2018
Embed
What would you like to do?
# This script is released under the public domain.
# What's this?
# This script counts how many times emoji are used on your slack.
# It requires exported slack data.
# How to use it:
# $ ruby emoji_statistics.rb /path/to/exported/dir
require 'json'
path = ARGV[0]
if !Dir.exist?(path)
puts path
STDERR.puts "Error: invalid argument"
exit(1)
end
class Statistics
def initialize()
@stat = {}
end
def add(emoji, num = 1)
@stat[emoji] ||= 0
@stat[emoji] += num
end
def sort()
@stat.sort {|(_, x), (_, y)| x <=> y}
end
end
@msg = Statistics.new
@reaction = Statistics.new
@total = Statistics.new
EMOJI_REGEX = /:[^:\s\.\/A-Z]+:/
Dir.glob(File.join(path, "**/*.json")).each do |file|
str = File.read(file)
obj = JSON.parse(str)
obj.each do |msg|
msg["text"]&.scan(EMOJI_REGEX)&.each do |emoji|
@msg.add(emoji)
@total.add(emoji)
end
msg["reactions"]&.each do |r|
emoji = ":#{r["name"]}:"
@reaction.add(emoji, r["count"])
@total.add(emoji, r["count"])
end
end
end
puts "in msg"
puts @msg.sort.reverse[0..19]
puts "--------------------"
puts "in reaction "
puts @reaction.sort.reverse[0..19]
puts "--------------------"
puts "in total "
puts @total.sort.reverse[0..19]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment