Last active
September 16, 2020 04:24
-
-
Save sebastjan-hribar/6f16c1d87c88f60c0f19e06ddfe499bb to your computer and use it in GitHub Desktop.
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
# 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(language_id: source_lang) | |
.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} | |
end | |
def find_by_content(content) | |
segments | |
.where(content: content).first | |
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 | |
Hanami::Model.migration do | |
change do | |
alter_table :translation_records do | |
add_column :language_combination, String, null: false | |
end | |
end | |
end | |
Hanami::Model.migration do | |
change do | |
alter_table :translation_records do | |
add_column :uid, String, null: false, unique: true | |
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 | |
def find_by_uid(uid) | |
translation_records | |
.where(uid: uid) | |
end | |
end | |
# interactor for looking up similar translations | |
require 'hanami/interactor' | |
class TranslationFinder | |
include Hanami::Interactor | |
expose :found_translations, :languages, :domains, :styles | |
def initialize(repository: SegmentRepository.new) | |
@segment_repo = repository | |
end | |
def call(lookup_translations_attributes) | |
@found_translations = @segment_repo.find_by_segment_match(lookup_translations_attributes[:source_text], lookup_translations_attributes[:source_lang], lookup_translations_attributes[:target_lang], lookup_translations_attributes[:sim_score], lookup_translations_attributes[:max_results]).to_a | |
@found_translations.map! { |segment_back| segment_back.to_h } | |
end | |
end | |
############################# | |
# Output: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment