-
Notifications
You must be signed in to change notification settings - Fork 0
Adds Suggested Resource detection based on regex patterns #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3899df9
Annotate existing models cleanup
JPrevost c04cc68
Minor tweak to bulk_checker
JPrevost 75cc48e
Cleanup no longer needed table prefix and annotate
JPrevost a1f3da6
Adds SuggestedPatterns and related Detector
JPrevost 28687ec
Don't categorize SuggestedResourcePattern (yet)
JPrevost f7ed8ec
Detector::SuggestedResourcePattern doc fixup
JPrevost 643ed50
Adds docs for `log_summary` method
JPrevost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Detector | ||
| # Detector::SuggestedResourcePattern handles detections for patterns stored in our SuggestedPattern model | ||
| class SuggestedResourcePattern | ||
| attr_reader :detections | ||
|
|
||
| # shared singleton methods | ||
| extend Detector::BulkChecker | ||
|
|
||
| def initialize(phrase) | ||
| @detections = {} | ||
| check_patterns(phrase) | ||
| end | ||
|
|
||
| # check_patterns loops through all stored patterns from SuggestedPattern model, checks to see if they produce | ||
| # matches for the incoming `phrase`, and if so creates a Hash with useful data | ||
| # | ||
| # @note Not using shared PatternChecker as we want to include additional data in the returned object | ||
| # @param phrase [String]. A string representation of a searchterm (not an actual Term object) | ||
| # @return primarily intended to add matches to @detections | ||
| def check_patterns(phrase) | ||
| sps = [] | ||
| SuggestedPattern.find_each do |sp| | ||
| next unless Regexp.new(sp.pattern).match(phrase) | ||
|
|
||
| sps << { | ||
| shortcode: sp.shortcode, | ||
| title: sp.title, | ||
| url: sp.url | ||
| } | ||
| @detections = sps | ||
| end | ||
| end | ||
|
|
||
| # The record method will consult the set of regex-based detectors that are defined in | ||
| # SuggestedPattern records. Any matches will be registered as Detection records. | ||
| # | ||
| # @note There are multiple patterns within SuggestedPattern records. Each check is capable of generating | ||
| # a separate Detection record. | ||
| # | ||
| # @return nil | ||
| def self.record(term) | ||
| sp = Detector::SuggestedResourcePattern.new(term.phrase) | ||
|
|
||
| sp.detections.each do | ||
| Detection.find_or_create_by( | ||
| term:, | ||
| detector: Detector.where(name: 'SuggestedResourcePattern').first, | ||
| detector_version: ENV.fetch('DETECTOR_VERSION', 'unset') | ||
| ) | ||
| end | ||
|
|
||
| nil | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: suggested_patterns | ||
| # | ||
| # id :integer not null, primary key | ||
| # title :string not null | ||
| # url :string not null | ||
| # pattern :string not null | ||
| # shortcode :string not null | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # | ||
| class SuggestedPattern < ApplicationRecord | ||
| validates :title, presence: true | ||
| validates :url, presence: true | ||
| validates :pattern, presence: true, uniqueness: true | ||
| validates :shortcode, presence: true, uniqueness: true | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class AddSuggestedPatterns < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :suggested_patterns do |t| | ||
| t.string :title, null: false | ||
| t.string :url, null: false | ||
| t.string :pattern, null: false | ||
| t.string :shortcode, null: false | ||
matt-bernhardt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :suggested_patterns, :pattern, unique: true | ||
| add_index :suggested_patterns, :shortcode, unique: true | ||
| end | ||
| end | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,3 +55,5 @@ web_of_knowledge: | |
| nobel_laureate: | ||
| value: 'bawendi moungi' | ||
|
|
||
| astm: | ||
| value: 'astm 1' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # == Schema Information | ||
| # | ||
| # Table name: suggested_patterns | ||
| # | ||
| # id :integer not null, primary key | ||
| # title :string not null | ||
| # url :string not null | ||
| # pattern :string not null | ||
| # shortcode :string not null | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # | ||
|
|
||
| astm: | ||
| title: Looking for ASTM Standards? | ||
| url: 'https://example.com/standards' | ||
| pattern: '(ASTM|astm)\s' | ||
| shortcode: astm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| class Detector | ||
| class SuggestedResourcePatternTest < ActiveSupport::TestCase | ||
| test 'pattern matches return as expected' do | ||
| match = SuggestedResourcePattern.new('astm standard thing and stuff') | ||
|
|
||
| assert_predicate(match.detections, :present?) | ||
| end | ||
|
|
||
| test 'no patterns detected return as expected' do | ||
| match = SuggestedResourcePattern.new('hello!') | ||
|
|
||
| assert_not_predicate(match.detections, :present?) | ||
| end | ||
|
|
||
| test 'record does relevant work' do | ||
| detection_count = Detection.count | ||
| t = terms('astm') | ||
| Detector::SuggestedResourcePattern.record(t) | ||
|
|
||
| assert_equal(detection_count + 1, Detection.count) | ||
| end | ||
|
|
||
| test 'record does nothing when not needed' do | ||
| detection_count = Detection.count | ||
| t = terms('journal_nature_medicine') | ||
|
|
||
| Detector::SuggestedResourcePattern.record(t) | ||
|
|
||
| assert_equal(detection_count, Detection.count) | ||
| end | ||
|
|
||
| test 'record respects changes to the DETECTOR_VERSION value' do | ||
| # Create a relevant detection | ||
| t = terms('astm') | ||
| Detector::SuggestedResourcePattern.record(t) | ||
|
|
||
| detection_count = Detection.count | ||
|
|
||
| # Calling the record method again doesn't do anything, but does not error. | ||
| Detector::SuggestedResourcePattern.record(t) | ||
|
|
||
| assert_equal(detection_count, Detection.count) | ||
|
|
||
| # Calling the record method after DETECTOR_VERSION is incremented results in a new Detection | ||
| ClimateControl.modify DETECTOR_VERSION: 'updated' do | ||
| Detector::SuggestedResourcePattern.record(t) | ||
|
|
||
| assert_equal detection_count + 1, Detection.count | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.