jekyll-generate-tags 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db89f3d933db9c98e8d73fd9614cbeb3448c7efdb944ffb8408c51457ec13d47
4
+ data.tar.gz: ffe7e155720e1c28ce180065b32a6091f80e5d2a8920c9e4195bc6575b69ed0a
5
+ SHA512:
6
+ metadata.gz: 6d624f2eef4068bd51ae550e9c7ccab7a2e8dc5a3a94b467a20389671bc81cbaea84c8aaee46c7b37f03d3b29ea63f2e15bf950857780172e6cb55fb5f38bff5
7
+ data.tar.gz: a90a9dee0fae9a44a9942109c2908aaeee9a9b50865cbc0271a26a4daf976493f332c8e216d92ea086e8e6511d93d9f8b18b58f15bb3233917df2ce208634f59
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /vendor/
11
+ Gemfile.lock
12
+ .tags-cache
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jekyll-generate-tags.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Pat Migliaccio pat@patmigliaccio.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # jekyll-generate-tags
2
+
3
+ Classify Jekyll post content automatically using NLP ([huggingface.co/facebook/bart-large-mnli](https://huggingface.co/facebook/bart-large-mnli)) based on a set of site-wide labels.
4
+
5
+ ## Installation
6
+
7
+ ### Prerequisites
8
+
9
+ * Python
10
+
11
+ ### Install
12
+
13
+ Add this line to your project's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'jekyll-generate-tags'
17
+ ```
18
+
19
+ Add the following to your site's _config.yml:
20
+
21
+ ```yml
22
+ plugins:
23
+ - jekyll-generate-tags
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ Use the following tag to reference a comma-delimited string of tag matches (in order of confidence).
29
+
30
+ ```md
31
+ {{ page.generatedtags }}
32
+ ```
33
+
34
+ _Note: When building the site for the first time, it may take a bit to run through existing posts to generate each set of tags._
35
+
36
+ ### Custom Labels
37
+
38
+ _Optional_: Add the following property to the `_config.yml` file to include custom labels for selection as tags.
39
+
40
+ ```yml
41
+ tag_options: software engineering,coding,programming languages
42
+ ```
43
+
44
+ These can also be updated in the `tag_options.txt` file within the root of the project or updated inside the gem itself.
45
+
46
+ ### Confidence
47
+
48
+ By default, labels are only selected as tags if they have a greater than `.50` confidence value.
49
+
50
+ _Optional_: Add the following property to the `_config.yml` file to alter the confidence value according to desired preference.
51
+
52
+ ```yml
53
+ tag_confidence: .30
54
+ ```
55
+
56
+ ## Caching
57
+
58
+ Includes a caching-mechanism that allows for faster re-builds when the content, labels, or confidence values don't change. The outputted tags are stored in the `.tags-cache` folder. Depending on how your site builds are handled, it probably makes sense to check this folder in to eliminate the need to reprocess each post at time of deployment.
59
+
60
+ ### Clear Cache
61
+
62
+ To clear the cache entirely, simply delete the `.tags-cache` folder.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jekyll/generate/tags"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ bundle exec rake build
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/jekyll-generate-tags/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "jekyll-generate-tags"
5
+ spec.version = Jekyll::GenerateTags::VERSION
6
+ spec.authors = ["Pat Migliaccio"]
7
+ spec.email = ["pat@patmigliaccio.com"]
8
+
9
+ spec.summary = "Classify Jekyll post content automatically using NLP"
10
+ spec.homepage = "https://github.com/patmigliaccio/jekyll-generate-tags"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/patmigliaccio/jekyll-generate-tags"
18
+ spec.metadata["changelog_uri"] = "https://github.com/patmigliaccio/jekyll-generate-tags"
19
+
20
+ spec.add_runtime_dependency "jekyll", "~> 3.9.2"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
@@ -0,0 +1,18 @@
1
+ import sys
2
+ from transformers import pipeline
3
+
4
+ content = sys.argv[1]
5
+ tags = sys.argv[2]
6
+ confidence = float(sys.argv[3])
7
+
8
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
9
+
10
+ data = classifier(content, tags)
11
+
12
+ labels = data['labels']
13
+ scores = data['scores']
14
+
15
+ # Filter the labels based on the score
16
+ filtered_labels = [label for label, score in zip(labels, scores) if score > confidence]
17
+
18
+ print(",".join(filtered_labels))
@@ -0,0 +1,90 @@
1
+ require 'shellwords'
2
+ require 'openssl'
3
+ require 'fileutils'
4
+
5
+ require "jekyll"
6
+ require "jekyll-generate-tags/version"
7
+
8
+ module Jekyll
9
+ module GenerateTags
10
+ class Error < StandardError; end
11
+
12
+ class Generator
13
+ @@hash_key = "cache-key"
14
+ @@cache_folder_name = ".tags-cache"
15
+ @@tag_options_name = "tag_options.txt"
16
+ @@default_confidence = ".50"
17
+
18
+ def generate_tags(content, tags, confidence)
19
+ # Do tags exist in `_config.yml`?
20
+ if tags.nil?
21
+ # Look for tags in project root
22
+ tags_path = File.expand_path(@@tag_options_name)
23
+ if !File.exists?(tags_path)
24
+ # Use default tags from gem
25
+ tags_path = File.expand_path(@@tag_options_name, __dir__)
26
+ end
27
+ tags_file = File.read(tags_path)
28
+ lines = tags_file.split("\n")
29
+ tags = lines.join(",")
30
+ end
31
+
32
+ if confidence.nil?
33
+ confidence = @@default_confidence
34
+ end
35
+
36
+ cache_key = content + tags + String(confidence)
37
+
38
+ result = self.get_cache(cache_key)
39
+ if result.nil?
40
+ script_path = File.expand_path("generate.py", __dir__)
41
+ arg1 = Shellwords.escape(content)
42
+ arg2 = Shellwords.escape(tags)
43
+ arg3 = Shellwords.escape(String(confidence))
44
+ result = `python #{script_path} #{arg1} #{arg2} #{arg3}`
45
+
46
+ self.set_cache(cache_key, result)
47
+ end
48
+
49
+ result
50
+ end
51
+
52
+ # Stores newly generated tags in a file for fast reuse
53
+ def set_cache(key, value)
54
+ cache_path = get_cache_file_path(key)
55
+ # Store file
56
+ File.open(cache_path, "w") do |file|
57
+ file.puts(value)
58
+ end
59
+ end
60
+
61
+ # Retrieves existing generated tags from a cache file for fast reuse
62
+ def get_cache(key)
63
+ cache_path = get_cache_file_path(key)
64
+ # Retrieve value from file
65
+ if File.exists?(cache_path)
66
+ result = File.read(cache_path)
67
+ result
68
+ end
69
+ end
70
+
71
+ def create_cache_folder()
72
+ cache_folder_path = File.expand_path(@@cache_folder_name)
73
+ # Check if the folder exists
74
+ if !Dir.exists?(cache_folder_path)
75
+ # Create the folder
76
+ FileUtils.mkdir_p(cache_folder_path)
77
+ end
78
+ end
79
+
80
+ def get_cache_file_path(key)
81
+ create_cache_folder()
82
+
83
+ # Create the HMAC
84
+ hmac = OpenSSL::HMAC.hexdigest('sha256', @@hash_key, key)
85
+ cache_path = File.expand_path("#{@@cache_folder_name}/#{hmac}.txt")
86
+ cache_path
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,10 @@
1
+ require "rubygems/installer"
2
+
3
+ module Jekyll
4
+ class Installer < Gem::Installer
5
+ def install
6
+ # install the Python packages from the requirements.txt file
7
+ system "pip install -r #{File.join(gem_dir, "requirements.txt")}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ transformers>=4.25.1
2
+ torch>=1.13.1
3
+ torchvision>=0.14.1
4
+ sentencepiece>=0.1.97
@@ -0,0 +1,6 @@
1
+ from setuptools import setup
2
+
3
+ setup(
4
+ name='jekyll-generate-tags',
5
+ version='0.1',
6
+ )
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module GenerateTags
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen-string-literal: true
2
+
3
+ require "jekyll"
4
+ require "jekyll-generate-tags/generator"
5
+
6
+ # register the generate_tags method as a Jekyll hook
7
+ Jekyll::Hooks.register :site, :pre_render do |site, payload|
8
+ generator = Jekyll::GenerateTags::Generator.new
9
+
10
+ tag_options = site.config["tag_options"]
11
+ tag_confidence = site.config["tag_confidence"]
12
+
13
+ posts = payload["site"]["posts"]
14
+ posts.each do |post|
15
+ post_name = File.basename(post.path, '.md')
16
+
17
+ tags = generator.generate_tags(post.content, tag_options, tag_confidence)
18
+
19
+ # add the generated tags to the document's front matter
20
+ post.data["generatedtags"] = tags
21
+ end
22
+ end
data/tag_options.txt ADDED
@@ -0,0 +1,3 @@
1
+ software engineering
2
+ coding
3
+ programming languages
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-generate-tags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Migliaccio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.9.2
27
+ description:
28
+ email:
29
+ - pat@patmigliaccio.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - bin/console
40
+ - bin/setup
41
+ - jekyll-generate-tags.gemspec
42
+ - lib/jekyll-generate-tags.rb
43
+ - lib/jekyll-generate-tags/generate.py
44
+ - lib/jekyll-generate-tags/generator.rb
45
+ - lib/jekyll-generate-tags/installer.rb
46
+ - lib/jekyll-generate-tags/requirements.txt
47
+ - lib/jekyll-generate-tags/setup.py
48
+ - lib/jekyll-generate-tags/version.rb
49
+ - tag_options.txt
50
+ homepage: https://github.com/patmigliaccio/jekyll-generate-tags
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org
55
+ homepage_uri: https://github.com/patmigliaccio/jekyll-generate-tags
56
+ source_code_uri: https://github.com/patmigliaccio/jekyll-generate-tags
57
+ changelog_uri: https://github.com/patmigliaccio/jekyll-generate-tags
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.1.4
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Classify Jekyll post content automatically using NLP
77
+ test_files: []