From a8bcbe6ca5f0c799305f1a0f2de7a299e453e1a0 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Sat, 3 Jan 2015 08:14:54 +0100 Subject: [PATCH 1/7] add configurable threshold and classifier --- lib/jekyll/tagging.rb | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 1c29a0f..8ee83fe 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -13,6 +13,20 @@ 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 initialize(config) + @ignored_tags=config["ignored_tags"] || [] + @threshold=(config["tag_threshold"] || '1').to_i + @permalink_style=config['tag_permalink_style'] + @classifier=config['tag_classifier'] || 'linear' + end + def generate(site) self.class.site = self.site = site @@ -50,7 +64,7 @@ def new_tag(tag, posts) end def add_tag_cloud(num = 5, name = 'tag_data') - s, t = site, { name => calculate_tag_cloud(num) } + 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 @@ -58,26 +72,18 @@ def add_tag_cloud(num = 5, name = 'tag_data') # classes are: set-1..set-5. # # [[, ], ...] - 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 - return site.tags unless site.config["ignored_tags"] - site.tags.reject { |t| site.config["ignored_tags"].include? t[0] } + site.tags.reject { |t,posts| @ignored_tags.include? t or posts.size < @threshold} end def pretty? - @pretty ||= (site.permalink_style == :pretty || site.config['tag_permalink_style'] == 'pretty') + @pretty ||= (site.permalink_style == :pretty || @permalink_style == 'pretty') end end @@ -125,12 +131,11 @@ def tags(obj) def keywords(obj) return '' if not obj['tags'] tags = obj['tags'].dup - tags.join(',') + tags.join(',') end def active_tag_data(site = Tagger.site) - return site.config['tag_data'] unless site.config["ignored_tags"] - site.config["tag_data"].reject { |tag, set| site.config["ignored_tags"].include? tag } + return site.config['tag_data'] end end From 7b78cac9fd4b6a3c6b5287f00b7ad9d5d0d490d5 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Sat, 3 Jan 2015 08:20:04 +0100 Subject: [PATCH 2/7] cosmetics --- lib/jekyll/tagging.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 8ee83fe..a370248 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -79,7 +79,7 @@ def calculate_tag_cloud(classifier) end def active_tags - site.tags.reject { |t,posts| @ignored_tags.include? t or posts.size < @threshold} + site.tags.reject { |tag, posts| @ignored_tags.include? tag or posts.size < @threshold} end def pretty? From 6bcb17858f8ffb37ca869ef19ec76bb423ab87c0 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Sat, 3 Jan 2015 08:14:54 +0100 Subject: [PATCH 3/7] add configurable threshold and classifier --- lib/jekyll/tagging.rb | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 2637751..523482a 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -29,6 +29,20 @@ 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 initialize(config) + @ignored_tags=config["ignored_tags"] || [] + @threshold=(config["tag_threshold"] || '1').to_i + @permalink_style=config['tag_permalink_style'] + @classifier=config['tag_classifier'] || 'linear' + end + def generate(site) self.class.site = self.site = site @@ -68,7 +82,7 @@ def new_tag(tag, posts) end def add_tag_cloud(num = 5, name = 'tag_data') - s, t = site, { name => calculate_tag_cloud(num) } + 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 @@ -76,26 +90,18 @@ def add_tag_cloud(num = 5, name = 'tag_data') # classes are: set-1..set-5. # # [[, ], ...] - 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 - return site.tags unless site.config["ignored_tags"] - site.tags.reject { |t| site.config["ignored_tags"].include? t[0] } + site.tags.reject { |t,posts| @ignored_tags.include? t or posts.size < @threshold} end def pretty? - @pretty ||= (site.permalink_style == :pretty || site.config['tag_permalink_style'] == 'pretty') + @pretty ||= (site.permalink_style == :pretty || @permalink_style == 'pretty') end end @@ -149,8 +155,7 @@ def keywords(obj) end def active_tag_data(site = Tagger.site) - return site.config['tag_data'] unless site.config["ignored_tags"] - site.config["tag_data"].reject { |tag, set| site.config["ignored_tags"].include? tag } + return site.config['tag_data'] end end From 65443a6eacbbb323337b614d03678fe4a13d7244 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Sat, 3 Jan 2015 08:20:04 +0100 Subject: [PATCH 4/7] cosmetics --- lib/jekyll/tagging.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 523482a..e4164b7 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -97,7 +97,7 @@ def calculate_tag_cloud(classifier) end def active_tags - site.tags.reject { |t,posts| @ignored_tags.include? t or posts.size < @threshold} + site.tags.reject { |tag, posts| @ignored_tags.include? tag or posts.size < @threshold} end def pretty? From 0bcc3c47a30bed3386bd4f41d6a0f99b15aa96fe Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Fri, 20 Oct 2017 14:40:19 +0200 Subject: [PATCH 5/7] undo other changes --- lib/jekyll/tagging.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index e4164b7..0d251a7 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -29,13 +29,6 @@ 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 initialize(config) @ignored_tags=config["ignored_tags"] || [] @threshold=(config["tag_threshold"] || '1').to_i @@ -82,7 +75,7 @@ def new_tag(tag, posts) end def add_tag_cloud(num = 5, name = 'tag_data') - s, t = site, { name => calculate_tag_cloud(@@classifiers[@classifier].curry[num]) } + s, t = site, { name => calculate_tag_cloud(num) } s.respond_to?(:add_payload) ? s.add_payload(t) : s.config.update(t) end @@ -91,9 +84,16 @@ def add_tag_cloud(num = 5, name = 'tag_data') # # [[, ], ...] 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)] } + 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)] } end def active_tags @@ -101,7 +101,7 @@ def active_tags end def pretty? - @pretty ||= (site.permalink_style == :pretty || @permalink_style == 'pretty') + @pretty ||= (site.permalink_style == :pretty || site.config['tag_permalink_style'] == 'pretty') end end @@ -155,7 +155,8 @@ def keywords(obj) end def active_tag_data(site = Tagger.site) - return site.config['tag_data'] + return site.config['tag_data'] unless site.config["ignored_tags"] + site.config["tag_data"].reject { |tag, set| site.config["ignored_tags"].include? tag } end end From b9ce74b7a0c2b087939579aa971d2266f1f296a9 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Fri, 20 Oct 2017 14:42:09 +0200 Subject: [PATCH 6/7] remove initialize --- lib/jekyll/tagging.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 0d251a7..6773e45 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -29,13 +29,6 @@ class Tagger < Generator class << self; attr_accessor :types, :site; end - def initialize(config) - @ignored_tags=config["ignored_tags"] || [] - @threshold=(config["tag_threshold"] || '1').to_i - @permalink_style=config['tag_permalink_style'] - @classifier=config['tag_classifier'] || 'linear' - end - def generate(site) self.class.site = self.site = site @@ -83,7 +76,7 @@ def add_tag_cloud(num = 5, name = 'tag_data') # classes are: set-1..set-5. # # [[, ], ...] - def calculate_tag_cloud(classifier) + def calculate_tag_cloud(num = 5) range = 0 tags = active_tags.map { |tag, posts| @@ -97,7 +90,8 @@ def calculate_tag_cloud(classifier) end def active_tags - site.tags.reject { |tag, posts| @ignored_tags.include? tag or posts.size < @threshold} + threshold = (config["tag_threshold"] || '1').to_i + site.tags.reject { |tag, posts| @ignored_tags.include? tag or posts.size < threshold} end def pretty? From bd04740945630b0be342f722f4745eba47c80570 Mon Sep 17 00:00:00 2001 From: Stefan Niederhauser Date: Fri, 20 Oct 2017 15:02:32 +0200 Subject: [PATCH 7/7] fix code --- lib/jekyll/tagging.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tagging.rb b/lib/jekyll/tagging.rb index 6773e45..53ba0dd 100644 --- a/lib/jekyll/tagging.rb +++ b/lib/jekyll/tagging.rb @@ -91,7 +91,7 @@ def calculate_tag_cloud(num = 5) def active_tags threshold = (config["tag_threshold"] || '1').to_i - site.tags.reject { |tag, posts| @ignored_tags.include? tag or posts.size < threshold} + site.tags.reject { |tag, posts| site.config["ignored_tags"].include? tag or posts.size < threshold} end def pretty?