sentences.rb revision 2cecff8c40ff62af965403b167f8143e1969d8ef
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translated = TranslatedSentence.where(audience_id: self)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa imported = self.direct_imported_ontologies.reduce([[], []]) do |arr, ontology|
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa other_split = ontology.split_translated_sentences
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa other_translated.delete_if do |translated_sentence|
366ce8d807067a97613cb23d49105d8a093c5015Eugen Kuksa sentence_ids.include?(translated_sentence.sentence_id)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa other_sentences.each { |os| arr.last << os }
366ce8d807067a97613cb23d49105d8a093c5015Eugen Kuksa [translated + imported.first, self.sentences + imported.last]
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # Find import-links which describe the following mapping:
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # some ontology imports self.
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def incoming_imports_with_mappings
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # INNER JOINS always return an empty result set if the
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # ON clause does not match.
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa Link.joins(:entity_mappings).where(source_id: self, kind: 'import')
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def create_translated_sentences
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa query, args = links_by_kind_query(self, 'import')
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa imported_ontology_ids = pluck_select([query, *args], :ontology_id).reverse
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa imported_ontology_ids.each { |o_id| Ontology.find(o_id).create_translated_sentences }
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa import_links = incoming_imports_with_mappings
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa if import_links.any?
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa combined_sentences.each do |sentence|
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa import_links.each do |import_link|
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa import_link.entity_mappings.each do |mapping|
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translate_sentence(sentence, mapping)
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa def translate_sentence(sentence, mapping)
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa applicable_sentence = TranslatedSentence.choose_applicable(sentence, mapping)
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa if mapping.applicable?(applicable_sentence)
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa translated_text = mapping.apply(applicable_sentence)
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa audience = mapping.link.target
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa translated_sentence = TranslatedSentence.where(
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa entity_mapping_id: mapping,
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa audience_id: audience,
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa sentence_id: sentence,
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa ontology_id: sentence.ontology).first_or_initialize
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa translated_sentence.translated_text = translated_text
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa translated_sentence.save
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translated_sentence
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def translate_sentences_for(audience_ontology)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa link = Link.where(source_id: audience_ontology, target_id: self, kind: 'import').first
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa if link && link.entity_mappings.any?
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa create_or_fetch_translations(audience_ontology, link, link.entity_mappings)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa default_translated_sentences
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # If there are no translations applicable, what do you do?
28001d576e67ba46ed481c5695f1e0827ff26007Eugen Kuksa def default_translated_sentences
28001d576e67ba46ed481c5695f1e0827ff26007Eugen Kuksa self.sentences
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def create_or_fetch_translations(audience_ontology, link, mappings, overwrite: false)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translations = TranslatedSentence.for(audience_ontology, sentences_from: self)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa if translations.any? && !overwrite
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translations
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa translations.delete_all
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa create_translations(audience_ontology, link, mappings)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def create_translations(audience_ontology, link, mappings)
923d69139038e74c0936e826bbfdc8717fbbc7b3Eugen Kuksa query, args = links_by_kind_query(self, 'import')
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa imported_ontology_ids = pluck_select([query, *args], :id).reverse
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa imported_ontology_ids.each { |o_id| Ontology.find(o_id).create_translated_sentences }
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa module Methods
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def update_or_create_from_hash(hash, timestamp = Time.now)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa sentence = find_or_initialize_by_name(hash['name'])
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa sentence.imported = hash['imported'] || false
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa sentence.text = hash['text'].to_s
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa sentence.range = hash['range']
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa sentence.updated_at = timestamp
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa sentence.save!
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa execute_sql "DELETE FROM entities_sentences WHERE sentence_id=#{sentence.id}"
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa execute_sql "INSERT INTO entities_sentences (sentence_id, entity_id, ontology_id)
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa SELECT #{sentence.id}, id, ontology_id FROM entities WHERE
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa ontology_id=#{@association.owner.id} AND text IN (?)",
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa hash['symbols']
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa sentence.set_display_text!