Sha256: f8d0752c5a8933a37d3b18d0a5214435ce34a12219b2e4e5489f52c5834cbed3

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Bunto
  class RelatedPosts
    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      Bunto::External.require_with_graceful_fail('classifier-reborn') if site.lsi
    end

    def build
      return [] unless site.posts.docs.size > 1

      if site.lsi
        build_index
        lsi_related_posts
      else
        most_recent_posts
      end
    end

    def build_index
      self.class.lsi ||= begin
        lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
        display("Populating LSI...")

        site.posts.docs.each do |x|
          lsi.add_item(x)
        end

        display("Rebuilding index...")
        lsi.build_index
        display("")
        lsi
      end
    end

    def lsi_related_posts
      self.class.lsi.find_related(post, 11)
    end

    def most_recent_posts
      @most_recent_posts ||= (site.posts.docs.reverse - [post]).first(10)
    end

    def display(output)
      $stdout.print("\n")
      $stdout.print(Bunto.logger.formatted_topic(output))
      $stdout.flush
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bunto-3.0.0 lib/bunto/related_posts.rb
bunto-2.0.0.pre lib/bunto/related_posts.rb
bunto-1.0.0 lib/bunto/related_posts.rb