Class: SangsooNam::Jekyll::TFIDFRelatedPosts

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-related-blog-posts.rb

Instance Method Summary collapse

Constructor Details

#initializeTFIDFRelatedPosts

Returns a new instance of TFIDFRelatedPosts.



11
12
13
14
15
16
# File 'lib/jekyll-related-blog-posts.rb', line 11

def initialize
  @docs = Array.new
  @keywords = Array.new
  @tags_and_categories = Array.new
  @stopwords_filter = Stopwords::Snowball::Filter.new('en')
end

Instance Method Details

#add_post(post) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll-related-blog-posts.rb', line 18

def add_post(post)
  tags = post.data['tags'].map { |e| "@tag:" + e }.map(&:to_sym)
  categories = post.data['categories'].map { |e| "@category:" + e }.map(&:to_sym)
  doc = {
    post: post,
    content: (stem(post.content) + stem(post.data['title']) + tags + categories)
  }
  @docs << doc
  @keywords += doc[:content]
  @tags_and_categories += tags + categories
end

#build(site) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-related-blog-posts.rb', line 30

def build(site)
  @keywords.uniq!
  @tags_and_categories.uniq!
  @weights = custom_weights(@tags_and_categories)
  related = build_related_docs_with_score(site.config['related_posts_count'] || 4)

  @docs.each do |doc|
    doc[:post].instance_variable_set(:@related_posts,related[doc].map { |x| x[:post] })
  end
end