sentences.rb revision c25fef96f425dc1ff4f747e6f340ca5a060ce865
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen translated = TranslatedSentence.where(audience_id: self)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen imported = self.direct_imported_ontologies.reduce([[], []]) do |arr, ontology|
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen other_split = ontology.split_translated_sentences
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen other_translated.delete_if do |translated_sentence|
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen sentence_ids.include?(translated_sentence.sentence_id)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen other_translated.each { |ot| arr.first << ot }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen other_sentences.each { |os| arr.last << os }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen [translated + imported.first, self.sentences + imported.last]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen # Find import-links which describe the following mapping:
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen # some ontology imports self.
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen def incoming_imports_with_mappings
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen # INNER JOINS always return an empty result set if the
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen # ON clause does not match.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Link.joins(:entity_mappings).where(source_id: self, kind: 'import')
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen def create_translated_sentences
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen query, args = links_by_kind_query(self, 'import')
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen imported_ontology_ids = pluck_select([query, *args], :ontology_id).reverse
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen imported_ontology_ids.each { |o_id| Ontology.find(o_id).create_translated_sentences }
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen import_links = incoming_imports_with_mappings
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen if import_links.any?
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen combined_sentences.each do |sentence|
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen import_links.each do |import_link|
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen import_link.entity_mappings.each do |mapping|
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen translate_sentence(sentence, mapping)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen def translate_sentence(sentence, mapping)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen applicable_sentence = TranslatedSentence.choose_applicable(sentence, mapping)
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen if mapping.applicable?(applicable_sentence)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen translated_text = mapping.apply(applicable_sentence)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen audience = mapping.link.target
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen translated_sentence = TranslatedSentence.where(
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen entity_mapping_id: mapping,
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen audience_id: audience,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen sentence_id: sentence,
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen ontology_id: mapping.link.source).first_or_initialize
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen translated_sentence.translated_text = translated_text
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen translated_sentence.save
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen translated_sentence
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen def translate_sentences_for(audience_ontology)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen link = Link.where(source_id: audience_ontology, target_id: self, kind: 'import').first
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen if link && link.entity_mappings.any?
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen create_or_fetch_translations(audience_ontology, link, link.entity_mappings)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen default_translated_sentences
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen # If there are no translations applicable, what do you do?
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen def default_translated_sentences
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen self.sentences
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen def create_or_fetch_translations(audience_ontology, link, mappings, overwrite: false)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen translations = TranslatedSentence.for(audience_ontology, sentences_from: self)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen if translations.any? && !overwrite
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen translations.delete_all
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen create_translations(audience_ontology, link, mappings)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen def create_translations(audience_ontology, link, mappings)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen query, args = links_by_kind_query(self, 'import')
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen imported_ontology_ids = pluck_select([query, *args], :id).reverse
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen imported_ontology_ids.each { |o_id| Ontology.find(o_id).create_translated_sentences }
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen module Methods
6288d3611eda14a017dae9927b73f46afb646c96Timo Sirainen def update_or_create_from_hash(hash, timestamp = Time.now)
a2637488c8d514ec1ac3914811deee814f9761b3Timo Sirainen sentence = find_or_initialize_by_name(hash['name'])
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.imported = hash['imported'] || false
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.text = hash['text'].to_s
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.range = hash['range']
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.updated_at = timestamp
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.save!
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen execute_sql "DELETE FROM entities_sentences WHERE sentence_id=#{sentence.id}"
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen execute_sql "INSERT INTO entities_sentences (sentence_id, entity_id, ontology_id)
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen SELECT #{sentence.id}, id, ontology_id FROM entities WHERE
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen ontology_id=#{@association.owner.id} AND text IN (?)",
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen hash['symbols']
299183fbb6ec5d0828a0880da372540421ac4665Timo Sirainen sentence.set_display_text!