-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from MITLibraries/tco-83-categorization
Implement Categorization
- Loading branch information
Showing
17 changed files
with
471 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class CategoriesType < Types::BaseObject | ||
description 'Information about one category linked to this search term' | ||
|
||
field :confidence, Float, null: false, description: 'The application\'s confidence that the term belongs to this category - measured from 0.0 to 1.0' | ||
field :name, String, null: false, description: 'The name of this category' | ||
|
||
def name | ||
@object.category.name | ||
end | ||
end | ||
end |
This file contains 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 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 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 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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# A Categorization is a joining record between a Term and a Category, created when a set of Detections are summarized to | ||
# calculate the final confidence that a Term belongs to a specific Category. | ||
# | ||
# There is a uniqueness constraint on the combination of term_id, category_id, and detector_version. | ||
# | ||
# New records can be created by passing a Term, Category, and a confidence score. The model will look up the current | ||
# detector version, and include that in the record. | ||
# | ||
# == Schema Information | ||
# | ||
# Table name: categorizations | ||
# | ||
# id :integer not null, primary key | ||
# category_id :integer not null | ||
# term_id :integer not null | ||
# confidence :float | ||
# detector_version :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Categorization < ApplicationRecord | ||
belongs_to :term | ||
belongs_to :category | ||
|
||
# We use the before_create hook to prevent needing to override the initialize method, which Rails frowns upon. | ||
before_create :set_defaults | ||
|
||
# These scopes allow for easy filtering of Categorization records by a single parameter. | ||
scope :current, -> { where(detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')) } | ||
|
||
private | ||
|
||
# This looks up the current Detector Version from the environment, storing the value as part of the record which is | ||
# about to be saved. This prevents the rest of the application from having to worry about this value, while also | ||
# providing a mechanism to prevent duplicate records from being created. | ||
def set_defaults | ||
self.detector_version = ENV.fetch('DETECTOR_VERSION', 'unset') | ||
end | ||
end |
This file contains 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 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 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 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,14 @@ | ||
class CreateCategorization < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :categorizations do |t| | ||
t.belongs_to :category, null: false, foreign_key: true | ||
t.belongs_to :term, null: false, foreign_key: true | ||
t.float :confidence | ||
t.string :detector_version | ||
|
||
t.timestamps | ||
end | ||
add_index :categorizations, [:term_id, :category_id, :detector_version], unique: true | ||
add_index :categorizations, [:category_id, :term_id, :detector_version], 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 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 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.