-
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.
Fleshout metadata system and sentiment analysis
- Remove Question#config and QuestionType#settings favor using SelfDescribing instead - Add Dragnet::TextSentiment integrate with Answer and to generate score for long answers that have been marked for calculation
- Loading branch information
1 parent
b2aec87
commit ad732e8
Showing
21 changed files
with
235 additions
and
123 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
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 was deleted.
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
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module SelfDescribable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_many :meta_data_records, class_name: 'MetaDatum', as: :self_describable | ||
with Parsing | ||
end | ||
|
||
# @param [String, Symbol] key | ||
# @param alt | ||
def get_meta(key, alt = nil) | ||
records = meta_data_records.where(key: key) | ||
return alt if records.empty? | ||
|
||
parsing.parse_meta(records) | ||
end | ||
alias get_setting get_meta | ||
|
||
# @return [Hash{Symbol => Object}] | ||
def meta_data | ||
return @meta_data if @meta_data | ||
|
||
grouped = meta_data_records.group_by { _1.key.to_sym } | ||
@meta_data = grouped.transform_values! { parsing.parse_meta(_1, grouped) } | ||
end | ||
alias settings meta_data | ||
|
||
def meta_data? | ||
!meta_data_records.empty? | ||
end | ||
|
||
# @param [String, Symbol] key | ||
# @param value | ||
# | ||
# @return [Array<MetaDatum>] | ||
def create_meta_datum!(key, value) | ||
meta_data_records.create!(Evaluation.meta_attributes(key, value)) | ||
end | ||
|
||
# @param [String, Symbol] key | ||
# @param value | ||
# | ||
# @return [MetaDatum] | ||
def build_meta_datum(key, value) | ||
meta_data_records.build(Evaluation.meta_attributes(key, value)) | ||
end | ||
|
||
# @param [String, Symbol] key | ||
# | ||
# @return [MetaDatum] | ||
def destroy_meta_datum(key) | ||
meta_data_records.where(key: key).destroy | ||
end | ||
|
||
# @param [Hash] data_hash | ||
# | ||
# @return [void] | ||
def meta_data=(data_hash) | ||
self.meta_data_records = data_hash.flat_map do |key, value| | ||
Evaluation.meta_attributes(key, value).map do |attributes| | ||
MetaDatum.new(attributes) | ||
end | ||
end | ||
end | ||
alias settings= meta_data= | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module SelfDescribable | ||
# @api private | ||
module Evaluation | ||
module_function | ||
|
||
def meta_attributes(key, value) | ||
case value | ||
when Array | ||
multi_meta_attributes(key, value) | ||
when Hash | ||
ref_meta_attributes(key, value) | ||
else | ||
[single_meta_attributes(key, value)] | ||
end | ||
end | ||
|
||
def multi_meta_attributes(key, values) | ||
values.map do |value| | ||
single_meta_attributes(key, value) | ||
end | ||
end | ||
|
||
def ref_meta_attributes(key, deref) | ||
deref.reduce([]) do |a, (k, v)| | ||
a << single_meta_attributes(key, k, type: :ref) | ||
if v.is_a?(Hash) | ||
ref_meta_attributes(k, v).each do |attr| | ||
a << attr | ||
end | ||
a | ||
else | ||
a << single_meta_attributes(k, v) | ||
end | ||
end | ||
end | ||
|
||
def single_meta_attributes(key, value, type: value.class) | ||
{ key: key, value: String(value), key_type: type.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module SelfDescribable | ||
class Parsing < Dragnet::Advice | ||
advises SelfDescribable | ||
|
||
def parse_meta(records, grouped = nil) | ||
return if records.empty? | ||
|
||
if records.count == 1 | ||
parse_single_meta(records.first) | ||
elsif records.first.key_type != 'ref' | ||
parse_multi_meta(records) | ||
else | ||
parse_ref_meta(records, grouped) | ||
end | ||
end | ||
|
||
def parse_ref_meta(records, grouped) | ||
records.each_with_object({}) do |record, h| | ||
key = record.value.to_sym | ||
records = grouped ? grouped[key] : self_describable.meta_data_records.where(key: key) | ||
h[key] = parse_meta(records, grouped) | ||
grouped&.delete(key) # remove referenced keys from grouping | ||
end | ||
end | ||
|
||
def parse_multi_meta(records) | ||
records.map do |record| | ||
parse_single_meta(record) | ||
end | ||
end | ||
|
||
class Error < RuntimeError; end | ||
|
||
def parse_single_meta(datum) | ||
case datum.key_type | ||
when 'String' | ||
datum.value | ||
when 'Symbol' | ||
datum.value.to_sym | ||
when 'TrueClass' | ||
true | ||
when 'FalseClass' | ||
false | ||
when 'Integer', 'Float', 'Rational' | ||
Kernel.public_send(datum.key_type, datum.value) | ||
when 'Date' | ||
Date.parse(datum.value) | ||
when 'Time' | ||
Time.zone.parse(datum.value) | ||
else | ||
raise Error, "unable to parse values of type #{datum.key_type}" | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
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.