naive entity matching

This commit is contained in:
Andrew Brown 2019-03-01 16:28:22 -06:00
parent 02e68d73ae
commit c20ad8efee

View File

@ -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