From 1941c2a45df3f0b854424020b0220f502c6498de Mon Sep 17 00:00:00 2001 From: Alex Skrenchuk Date: Fri, 15 Nov 2024 15:20:42 -0800 Subject: [PATCH] remove recommender_v1 --- lib/ncbo_annotator.rb | 1 - lib/ncbo_recommender.rb | 88 ---------------------------------------- lib/recommendation.rb | 35 ---------------- test/test_recommender.rb | 47 --------------------- 4 files changed, 171 deletions(-) delete mode 100644 lib/ncbo_recommender.rb delete mode 100644 lib/recommendation.rb delete mode 100644 test/test_recommender.rb diff --git a/lib/ncbo_annotator.rb b/lib/ncbo_annotator.rb index e1e76c3b..3fbbae28 100644 --- a/lib/ncbo_annotator.rb +++ b/lib/ncbo_annotator.rb @@ -14,7 +14,6 @@ require_relative 'ncbo_annotator/mgrep/mgrep' require_relative 'ncbo_annotator/config' require_relative 'ncbo_annotator/monkeypatches' -require_relative 'ncbo_recommender' # Require all models project_root = File.dirname(File.absolute_path(__FILE__)) diff --git a/lib/ncbo_recommender.rb b/lib/ncbo_recommender.rb deleted file mode 100644 index 03e72d74..00000000 --- a/lib/ncbo_recommender.rb +++ /dev/null @@ -1,88 +0,0 @@ -require 'logger' -require 'ontologies_linked_data' -require_relative 'recommendation' - -module Recommender - module Models - - class NcboRecommender - - def initialize() - @logger = Kernel.const_defined?("LOGGER") ? Kernel.const_get("LOGGER") : Logger.new(STDOUT) - end - - DEFAULT_HIERARCHY_LEVELS = 5 - - def recommend(text, ontologies=[], include_classes=false) - annotator = Annotator::Models::NcboAnnotator.new - annotations = annotator.annotate(text, { - ontologies: ontologies, - semantic_types: [], - filter_integers: false, - expand_class_hierarchy: true, - expand_hierarchy_levels: DEFAULT_HIERARCHY_LEVELS, - expand_with_mappings: false, - min_term_size: nil, - whole_word_only: true, - with_synonyms: true - }) - - recommendations = {} - classes_matched = [] - - annotations.each do |ann| - classId = ann.annotatedClass.id.to_s - ont = ann.annotatedClass.submission.ontology - ontologyId = ont.id.to_s - - unless recommendations.include?(ontologyId) - cls_count = get_ontology_class_count(ont) - next if cls_count <= 0 # skip any ontologies without a ready latest submission - recommendations[ontologyId] = Recommendation.new - recommendations[ontologyId].ontology = ont - recommendations[ontologyId].numTermsTotal = cls_count - end - - rec = recommendations[ontologyId] - cls_ont_key = "#{classId}_#{ontologyId}" - unless classes_matched.include?(cls_ont_key) - classes_matched << cls_ont_key - rec.annotatedClasses << ann.annotatedClass if include_classes - rec.numTermsMatched += 1 - end - rec.increment_score(ann) - end - - recommendations.values.each {|v| v.normalize_score} - return recommendations.values.sort {|a,b| b.score <=> a.score} - end - - - private - - def get_ontology_class_count(ont) - sub = nil - begin - #TODO: there appears to be a bug that does not allow retrieving submission by its id - # because the id is incorrect. The workaround is to get the ontology object and - # then retrieve its latest submission. - sub = LinkedData::Models::Ontology.find(ont.id).first.latest_submission - rescue - @logger.error("Unable to retrieve latest submission for #{ont.id.to_s} in Recommender.") - end - return 0 if sub.nil? - begin - sub.bring(metrics: LinkedData::Models::Metric.attributes) - cls_count = sub.metrics.classes - rescue - @logger.error("Unable to retrieve metrics for latest submission of #{ont.id.to_s} in Recommender.") - cls_count = LinkedData::Models::Class.where.in(sub).count - end - return cls_count || 0 - end - - - end - - end -end diff --git a/lib/recommendation.rb b/lib/recommendation.rb deleted file mode 100644 index 3ced1ded..00000000 --- a/lib/recommendation.rb +++ /dev/null @@ -1,35 +0,0 @@ -module Recommender - - class Recommendation - - include LinkedData::Hypermedia::Resource - - attr_accessor :ontology, :score, :numTermsMatched, :numTermsTotal, :annotatedClasses - - embed :annotatedClasses - - def initialize - @score = 0 - @numTermsMatched = 0 - @numTermsTotal = 0 - @annotatedClasses = [] - end - - def increment_score(annotation) - annotation.annotations.each do |occ| - if occ[:matchType] == "PREF" - @score += 10 - elsif occ[:matchType] == "SYN" - @score += 5 - end - end - @score += annotation.hierarchy.length * 2 - end - - def normalize_score() - @score = (@score / Math.log10(@numTermsTotal)).round(2) - end - - end - -end \ No newline at end of file diff --git a/test/test_recommender.rb b/test/test_recommender.rb deleted file mode 100644 index b3c4bca3..00000000 --- a/test/test_recommender.rb +++ /dev/null @@ -1,47 +0,0 @@ -require_relative 'test_case' -require 'json' -require 'redis' - -class TestRecommender < TestCase - - def self.before_suite - @@text = < 0, msg='Failed to return annotatedClasses') - end - end - -end -