Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add classifier #67

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/jekyll/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Tagger < Generator

class << self; attr_accessor :types, :site; end

@@classifiers = {
'log' => proc{|classes, value, min, max|
scaled = (classes * (Math.log(value) - Math.log(min)) / (Math.log(max) - Math.log(min))).to_i
scaled == classes ? classes : scaled + 1},
'linear' => proc{|classes, value, min, max| (1..max).quantile(value, classes)}
}

def generate(site)
self.class.site = self.site = site

Expand Down Expand Up @@ -68,25 +75,19 @@ def new_tag(tag, posts)
end

def add_tag_cloud(num = 5, name = 'tag_data')
s, t = site, { name => calculate_tag_cloud(num) }
classifier = config['tag_classifier'] || 'linear'
s, t = site, { name => calculate_tag_cloud(@@classifiers[classifier].curry[num]) }
s.respond_to?(:add_payload) ? s.add_payload(t) : s.config.update(t)
end

# Calculates the css class of every tag for a tag cloud. The possible
# classes are: set-1..set-5.
#
# [[<TAG>, <CLASS>], ...]
def calculate_tag_cloud(num = 5)
range = 0

tags = active_tags.map { |tag, posts|
[tag.to_s, range < (size = posts.size) ? range = size : size]
}


range = 1..range

tags.sort!.map! { |tag, size| [tag, range.quantile(size, num)] }
def calculate_tag_cloud(classifier)
tags = active_tags.map { |tag, posts| [tag, posts.size] }
min, max = tags.map { |tag, size| size }.minmax
tags.sort!.map! { |tag, size| [tag, classifier.call(size, min, max)] }
end

def active_tags
Expand Down