<commit_before>module Jekyll
  class RelatedPosts

    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      require 'classifier-reborn' if site.lsi
    end

    def build
      return [] unless site.posts.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.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.content, 11) - [post]
    end

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

    def display(output)
      $stdout.print("\n")
      $stdout.print(Jekyll.logger.formatted_topic(output))
      $stdout.flush
    end
  end
end<commit_msg>Cache the related posts most_recent_posts<commit_after>module Jekyll
  class RelatedPosts

    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      require 'classifier-reborn' if site.lsi
    end

    def build
      return [] unless site.posts.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.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.content, 11) - [post]
    end

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

    def display(output)
      $stdout.print("\n")
      $stdout.print(Jekyll.logger.formatted_topic(output))
      $stdout.flush
    end
  end
end
{'fraction_non_alphanumeric': 0.09272964736612974, 'fraction_numerical': 0.0043535045711798, 'mean_word_length': 1.9311224489795917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367121', 'n_tokens_mistral': 774, 'n_tokens_neox': 731, 'n_words': 160}