Skip to content

Instantly share code, notes, and snippets.

@prokop75
Created November 7, 2016 21:51
Show Gist options
  • Save prokop75/f8ec928bc10a328668e288419ea8836d to your computer and use it in GitHub Desktop.
Save prokop75/f8ec928bc10a328668e288419ea8836d to your computer and use it in GitHub Desktop.
require 'json'
require "set"
class MenuItem
def initialize(id, name)
@id = id
@name = name
@categories = {}
@all_categories = Set.new
@referenced_categories = Set.new
@category_names = {}
@category_flow = {}
@questions = []
@first_order_categories = Set.new
end
def add_category(c, name)
@categories[c] = Set.new
@category_names[c] = name
@category_flow[c] = 0
@questions[c] = []
@all_categories.add(c)
end
def add_lead_to_category(c, lead)
if (lead!=0)
@categories[c].add(lead)
@referenced_categories.add(lead)
end
end
def add_question(c, question_id, text, css_class)
@questions[c] << [question_id, text, css_class]
end
def calculate_flow
roots = @all_categories - @referenced_categories
roots.each do |r|
count = @categories[r].count
@category_flow[r] = 1.0
@categories[r].each do |c|
calculate_flow_node(c, 1.0/count)
end
end
end
def calculate_flow_node(n,flow)
@category_flow[n] += flow
@categories[n].each do |c|
calculate_flow_node(c, flow/@categories[n].count)
end
end
def find_first_order_categories
@categories.keys.each do |c|
if (@category_flow[c].round(5) == 1.0)
@first_order_categories.add(c)
end
end
end
def print
puts "Item #{@id} .: #{@name}"
@categories.keys.each do |c|
if (@first_order_categories.include?(c))
puts " #{c} .: #{@category_names[c]} -> #{@questions[c]}"
end
end
end
def print_questions
puts "Item #{@id} .: #{@name}"
@categories.keys.each do |ca
if (@first_order_categories.include?(c))
@questions[c].each do |id, text, css_class|
if !(css_class.include? "upgrade-parent")
puts " #{id} .: #{text}"
end
end
end
end
end
end
class SnSMenuPreprocessor
def initialize(filename)
data = File.read(filename)
@menu = JSON.parse(data)
@menu_items = extract_items
end
def extract_items
menu_items = {}
@menu['menuGroups'].each do |mg|
mg['menuItems'].each do |i|
item = menu_items[i['itemId']] = MenuItem.new(i['itemId'], i['name'])
questionSet = i['questionSet']
questionSet.each do |qs|
if (qs[0]=='categories') && (categories = qs[1])
categories.each do |c|
item.add_category(c['categoryId'], c['title'])
c['questions'].each do |q|
item.add_question(c['categoryId'], q['questionId'], q['text'], q['cssClass'])
q['answers'].each do |a|
item.add_lead_to_category(c['categoryId'], a['leadToCategoryId'])
end
end
end
end
end
end
end
menu_items
end
def print_category_graph
@menu_items.each do |id,m|
m.print
end
end
def print_questions
@menu_items.each do |id,m|
m.print_questions
end
end
def find_first_order_categories
@menu_items.each do |id,m|
m.calculate_flow
m.find_first_order_categories
end
end
end
# Formatting the way the sets print
class Set
def to_s
to_a.join(', ')
end
end
m = SnSMenuPreprocessor.new("sns_menu_18005.json")
m.find_first_order_categories
m.print_questions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment