Skip to content

Commit 6e5075d

Browse files
committed
Adds SuggestedPatterns and related Detector
Why are these changes being introduced: * LIRS staff and UXWS have both noted that searching for Standards is not useful in our system Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TCO-141 How does this address that need: * Adds a regex pattern based system that is similar to, but different than, SuggestedResources * These patterns can be used for more than just Standards, so the models were made to be more generic in name * The SuggestedPatterns will be stored in our database, which is a bit different than our other regular expression based detectors. These feel different enough to warrant this difference as we want to store additional metadata about what to display for these types of patterns when the other regex patterns use external resources to fetch data. Document any side effects to this change: * I'm not confident the naming is ideal
1 parent 75cc48e commit 6e5075d

File tree

15 files changed

+326
-2
lines changed

15 files changed

+326
-2
lines changed

app/controllers/demo_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ def detections
2323
@detections[:lcsh] = Detector::Lcsh.new(@searchterm).detections
2424
@detections[:standard_identifiers] = Detector::StandardIdentifiers.new(@searchterm).detections
2525
@detections[:suggested_resources] = Detector::SuggestedResource.full_term_match(@searchterm)
26+
@detections[:suggested_resources_patterns] = Detector::SuggestedResourcePattern.new(@searchterm)
2627
end
2728
end

app/graphql/types/detectors_type.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ def standard_identifiers
2525
end
2626
end
2727

28+
# Prefer Term based SuggestedResources over Pattern Based Suggested Resources
2829
def suggested_resources
29-
Detector::SuggestedResource.full_term_match(@object).map do |suggested_resource|
30+
traditional_suggested_resources.presence || pattern_based_suggested_resources
31+
end
32+
33+
def traditional_suggested_resources
34+
Detector::SuggestedResource.full_term_match(@object) do |suggested_resource|
35+
{ title: suggested_resource.title, url: suggested_resource.url }
36+
end
37+
end
38+
39+
def pattern_based_suggested_resources
40+
Detector::SuggestedResourcePattern.new(@object).detections do |suggested_resource|
3041
{ title: suggested_resource.title, url: suggested_resource.url }
3142
end
3243
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
class Detector
4+
# Detector::SuggestedResourcePattern handles detections for SuggestedResources based on patterns stored in our
5+
# SuggestedPattern model
6+
class SuggestedResourcePattern
7+
attr_reader :detections
8+
9+
# shared singleton methods
10+
extend Detector::BulkChecker
11+
12+
def initialize(phrase)
13+
@detections = {}
14+
check_patterns(phrase)
15+
end
16+
17+
# check_patterns loops through all stored patterns from SuggestedPattern model, checks to see if they produce
18+
# matches for the incoming `phrase`, and if so creates a Hash with useful data
19+
#
20+
# @note Not using shared PatternChecker as we want to include additional data in the returned object
21+
# @param phrase [String]. A string representation of a searchterm (not an actual Term object)
22+
# @return primarily intended to add matches to @detections
23+
def check_patterns(phrase)
24+
sps = []
25+
SuggestedPattern.find_each do |sp|
26+
next unless Regexp.new(sp.pattern).match(phrase)
27+
28+
sps << {
29+
shortcode: sp.shortcode,
30+
title: sp.title,
31+
url: sp.url
32+
}
33+
@detections = sps
34+
end
35+
end
36+
37+
# The record method will consult the set of regex-based detectors that are defined in
38+
# SuggestedPattern records. Any matches will be registered as Detection records.
39+
#
40+
# @note There are multiple patterns within SuggestedPattern records. Each check is capable of generating
41+
# a separate Detection record.
42+
#
43+
# @return nil
44+
def self.record(term)
45+
sp = Detector::SuggestedResourcePattern.new(term.phrase)
46+
47+
sp.detections.each do
48+
Detection.find_or_create_by(
49+
term:,
50+
detector: Detector.where(name: 'SuggestedResourcePattern').first,
51+
detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')
52+
)
53+
end
54+
55+
nil
56+
end
57+
end
58+
end

app/models/suggested_pattern.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# == Schema Information
2+
#
3+
# Table name: suggested_patterns
4+
#
5+
# id :integer not null, primary key
6+
# title :string not null
7+
# url :string not null
8+
# pattern :string not null
9+
# shortcode :string not null
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
#
13+
class SuggestedPattern < ApplicationRecord
14+
validates :title, presence: true
15+
validates :url, presence: true
16+
validates :pattern, presence: true, uniqueness: true
17+
validates :shortcode, presence: true, uniqueness: true
18+
end

app/models/term.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def record_detections
5555
Detector::Journal.record(self)
5656
Detector::Lcsh.record(self)
5757
Detector::SuggestedResource.record(self)
58+
Detector::SuggestedResourcePattern.record(self)
5859

5960
nil
6061
end

app/views/demo/view.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,15 @@
108108
<% else %>
109109
<p>No suggested resoure found.</p>
110110
<% end %>
111+
112+
<h2>Suggested Resource (Patterns)</h2>
113+
<% if @detections[:suggested_resources_patterns].detections.present? %>
114+
<p>Suggested resoure patterns found.</p>
115+
<% @detections[:suggested_resources_patterns].detections.each do |sr| %>
116+
<h3><%= sr[:title] %></h3>
117+
<p>URL: <%= link_to(sr[:url], sr[:url]) %></p>
118+
<% end %>
119+
120+
<% else %>
121+
<p>No suggested resoure patterns found.</p>
122+
<% end %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class AddSuggestedPatterns < ActiveRecord::Migration[7.2]
2+
def change
3+
create_table :suggested_patterns do |t|
4+
t.string :title, null: false
5+
t.string :url, null: false
6+
t.string :pattern, null: false
7+
t.string :shortcode, null: false
8+
9+
t.timestamps
10+
end
11+
end
12+
end

db/schema.rb

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/seeds.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
Detector.find_or_create_by(name: 'SuggestedResource')
4343
Detector.find_or_create_by(name: 'Citation')
4444
Detector.find_or_create_by(name: 'Barcode')
45+
Detector.find_or_create_by(name: 'SuggestedResourcePattern')
4546

4647
# DetectorCategories
4748
DetectorCategory.find_or_create_by(
@@ -84,5 +85,42 @@
8485
category: Category.find_by(name: 'Transactional'),
8586
confidence: 0.2
8687
)
88+
DetectorCategory.find_or_create_by(
89+
detector: Detector.find_by(name: 'SuggestedResourcePattern'),
90+
category: Category.find_by(name: 'Transactional'),
91+
confidence: 0.9
92+
)
93+
94+
# Patterns for Suggested Resources
95+
SuggestedPattern.find_or_create_by(
96+
title: 'Looking for Standards?',
97+
url: 'https://libguides.mit.edu/standards',
98+
pattern: '(IEC|iec)(\\s)(\\d{5})',
99+
shortcode: 'iec'
100+
)
101+
SuggestedPattern.find_or_create_by(
102+
title: 'Looking for Standards?',
103+
url: 'https://libguides.mit.edu/standards',
104+
pattern: '(ASCE|asce)(\\s)(\\d)',
105+
shortcode: 'asce'
106+
)
107+
SuggestedPattern.find_or_create_by(
108+
title: 'Looking for Standards?',
109+
url: 'https://libguides.mit.edu/standards',
110+
pattern: '(IEEE|ieee)\\s+(?:Std\\s+)?([PC]?[0-9]{3,4})',
111+
shortcode: 'ieee'
112+
)
113+
SuggestedPattern.find_or_create_by(
114+
title: 'Looking for Standards?',
115+
url: 'https://libguides.mit.edu/standards',
116+
pattern: '(ISO|iso)\\s(\\d{1,5})',
117+
shortcode: 'iso'
118+
)
119+
SuggestedPattern.find_or_create_by(
120+
title: 'Looking for Standards?',
121+
url: 'https://libguides.mit.edu/standards',
122+
pattern: '(ASTM|astm)\\s',
123+
shortcode: 'astm'
124+
)
87125

88126
Rails.logger.info('Seeding DB complete')

test/fixtures/detectors.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ journal:
3333

3434
suggestedresource:
3535
name: 'SuggestedResource'
36+
37+
suggestedresourcepattern:
38+
name: 'SuggestedResourcePattern'

0 commit comments

Comments
 (0)