Skip to content

ngs/middleman-blog-similar

Repository files navigation

middleman-blog-similar

Gem Version Build Status Dependency Status Code Quality Coverage Status

middleman-blog-similar is an extension for middleman-blog that adds method to lookup similar article.

Usage

Middleman::Blog::BlogArticle#similar_articles returns an array of Middleman::Blog::BlogArticle instances.

h2 Similar Entries
ul
  - current_article.similar_articles.first(5).each do|article|
    li= link_to article.title, article.url

similar_articles helper is also available in article pages.

h2 Similar Entries
ul
  - similar_articles.first(5).each do|article|
    li= link_to article.title, article.url

Configuration

Gemfile

gem 'middleman-blog-similar'

config.rb

activate :similar

This extension finds similar articles using those are using tags by default.

Built-in Tagger

You can set taggers using tagger: option. MeCab and EngTagger adopters are built in this extension.

# Find by tags (default)
activate :similar, tagger: :tags

# Using MeCab / Need to add `gem 'natto'` in Gemfile
activate :similar, tagger: :mecab

# Using EngTagger / Need to add `gem 'entagger'` in Gemfile
activate :similar, tagger: :entagger

Using Lambda

You can use lambda as tagger

# Resource is a Middleman::Blog::BlogArticle
activate :similar, tagger: ->(resource) { [resource.data.category] }

Multiple Taggers

You can configure multiple taggers both built-in and lambda taggers.

activate :similar, tagger: {
  # key = tagger, value = weight
  mecab: 1,
  entagger: 1,
  tags: 3,
  # key = (ignored), value[0] = weight, value[1] = lambda
  custom: [5, ->(resource) { resource.data.category ? [resource.data.category] : [] }]
}

Database Location

This extension uses SQLite3 to calculate similarity between articles.

Database location is ${PROJECT_ROOT}/.similar.db by default.

You can specify database location using db: option.

# Expands to ${HOME}/similar.db
activate :similar, db: '~/similar.db'

# Expands to ${PROJECT_ROOT}/tmp/middleman-blog-similar.db
activate :similar, db: 'tmp/middleman-blog-similar.db'

# Stores in memory database
activate :similar, db: ':memory:'

License

Copyright (c) 2014-2017 Atsushi Nagase. MIT Licensed, see LICENSE for details.