diff --git a/app/models/document_entity.rb b/app/models/document_entity.rb index 2eb93313..bc229df2 100644 --- a/app/models/document_entity.rb +++ b/app/models/document_entity.rb @@ -4,7 +4,37 @@ class DocumentEntity < ApplicationRecord after_create :match_notebook_page - def match_notebook_page - # TODO: Attempt to link to a Notebook.ai page of the same name + # TODO should this be some chain of method aliases maybe? or cached on object? + def document_owner + document_analysis.document.user + end + + def match_notebook_page! + matched_entity = document_owner.send(entity_relation).detect do |entity| + entity_match?(entity) + end + + # If we found a Notebook.ai entity to match to, hurrah! + if matched_entity.present? + update( + entity_type: matched_entity.class.name, + entity_id: matched_entity.id + ) + end + + # Return true/false for whether we found a match + matched_entity.present? + end + + def entity_relation + # 'Character' => 'characters' + self.entity_type.downcase.pluralize + end + + def entity_match?(entity) + return unless entity.respond_to?(:name) + + # TODO: levenshtein distance threshold comparison? + entity.name == self.text end end