wavecrafters blog

Our small contribution to the developer community

Related Posts for Octopress Using Categories

| Comments

I was trying to add the related posts feature without relying on the octopress-relatedposts plugin, because I had problems running the lsi in my computer. I also wanted, if possible, to make use of the categories to decide the importance of a given post. Then I came to this wonderful jekyll plugin, which does exacly what I wanted… but was programmed just for jekyll, which apparently uses tags instead of categories. So after a couple of minor changes, voila, it works in octopress

Installation

Copy related_posts.rb to your plugins directory

related_posts.rb
require 'jekyll/post'

module RelatedPosts

  # Used to remove #related_posts so that it can be overridden
  def self.included(klass)
    klass.class_eval do
      remove_method :related_posts
    end
  end

  # Calculate related posts.
  #
  # Returns [<Post>]
  def related_posts(posts)
    return [] unless posts.size > 1
    highest_freq = Jekyll::Post.category_freq(posts).values.max
    related_scores = Hash.new(0)
    posts.each do |post|
      post.categories.each do |category|
        if self.categories.include?(category) && post != self
          cat_freq = Jekyll::Post.category_freq(posts)[category]
          related_scores[post] += (1+highest_freq-cat_freq)
        end
      end
    end

    Jekyll::Post.sort_related_posts(related_scores)
  end

  module ClassMethods
    # Calculate the frequency of each tag.
    #
    # Returns {tag => freq, tag => freq, ...}
    def category_freq(posts)
      return @category_freq if @category_freq
      @category_freq = Hash.new(0)
      posts.each do |post|
        post.categories.each {|category| @category_freq[category] += 1}
      end
      @category_freq
    end

    # Sort the related posts in order of their score and date
    # and return just the posts
    def sort_related_posts(related_scores)
      related_scores.sort do |a,b|
        if a[1] < b[1]
          1
        elsif a[1] > b[1]
          -1
        else
          b[0].date <=> a[0].date
        end
      end.collect {|post,freq| post}
    end
  end

end

module Jekyll
  class Post
    include RelatedPosts
    extend RelatedPosts::ClassMethods
  end
end

Then create a file such as source/_includes/custom/asides/related.html with the following content

related.html
<section>
  <h1>Related Posts</h1>
  <ul class="posts">
    
      <li class="related">
        <a href="/blog/2013/12/16/mis-cositas/">Octopress, accents and rare characters</a>
      </li>
    
      <li class="related">
        <a href="/blog/2014/01/23/knn-normalizing-the-data/">k-NN: normalizing the data</a>
      </li>
    
      <li class="related">
        <a href="/blog/2013/12/27/playing-with-classifiers-knn/">Playing with classifiers: *k*-NN</a>
      </li>
    
  </ul>
</section>

Finall add the file to your default asides list in your _config.yml

_config.yml
default_asides: [custom/asides/related.html, ...]

Comments