Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# segment repository
class SegmentRepository < Hanami::Repository
associations do
has_many :translation_records
has_many :segments, through: :translation_records, as: :target_segments
belongs_to :language
end
def find_by_segment_match(source_text_for_lookup, source_lang, target_lang, sim_score, max_results)
aggregate(:translation_records)
.where {similarity(:content, source_text_for_lookup) > sim_score/100.00}
.select_append {float::similarity(:content, source_text_for_lookup).as(:similarity)}
.order {similarity(:content, source_text_for_lookup).desc}
.limit(max_results)
end
end
# translation_record migration
Hanami::Model.migration do
change do
create_table :translation_records do
primary_key :id
foreign_key :source_segment_id, :segments, on_delete: :cascade, null: false
foreign_key :target_segment_id, :segments, on_delete: :cascade, null: false
foreign_key :domain_id, :domains, on_delete: :cascade, null: false
foreign_key :style_id, :styles, on_delete: :cascade, null: false
column :project_name, String, null: false
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
# translation_record repository
class TranslationRecordRepository < Hanami::Repository
associations do
belongs_to :segment, as: :source_segment
belongs_to :segment, as: :target_segment
end
end
# interactor for looking up similar translations
require 'hanami/interactor'
class TranslationFinder
include Hanami::Interactor
expose :found_translations
def call(lookup_translations_attributes)
@found_translations = SegmentRepository.new.find_by_segment_match(lookup_translations_attributes[:source_text], 0.4, lookup_translations_attributes[:source_lang])
end
end
#############################
# Output:
#<ROM::Struct::Segment id=91 content="The car is blue." language_id=2 created_at=2020-04-21 17:30:45 UTC updated_at=2020-04-21 17:30:45 UTC similarity=0.789474 translation_records=[#<ROM::Struct::TranslationRecord id=56 source_segment_id=91 target_segment_id=92 domain_id=1 style_id=1 project_name="Modrina" created_at=2020-04-21 17:30:45 UTC updated_at=2020-04-21 17:30:45 UTC>]>
#<ROM::Struct::Segment id=109 content="The car is blue12." language_id=2 created_at=2020-04-23 15:19:33 UTC updated_at=2020-04-23 15:19:33 UTC similarity=0.714286 translation_records=[#<ROM::Struct::TranslationRecord id=57 source_segment_id=109 target_segment_id=110 domain_id=1 style_id=1 project_name="Modrina" created_at=2020-04-23 15:19:33 UTC updated_at=2020-04-23 15:19:33 UTC>]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.