sentences.rb revision 7f2dad9b7bcc8a9c37626bab9d5f9c3dfa0bccbe
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated = TranslatedSentence.where(audience_id: self)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch direct_imported_ontologies.reduce([[], []]) do |arr, ontology|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch other_split = ontology.split_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch other_translated.delete_if do |translated_sentence|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence_ids.include?(translated_sentence.sentence_id)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch other_translated.each { |ot| arr.first << ot }
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch other_sentences.each { |os| arr.last << os }
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch [translated + imported.first, sentences + imported.last]
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch # Find import-mappings which describe the following mapping:
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch # some ontology imports self.
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def incoming_imports_with_mappings
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch # INNER JOINS always return an empty result set if the
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch # ON clause does not match.
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch Mapping.joins(:symbol_mappings).where(source_id: self, kind: 'import')
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def create_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch query, args = mappings_by_kind_query(self, 'import')
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch imported_ontology_ids =
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch pluck_select([query, *args], :ontology_id).reverse
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch imported_ontology_ids.each do |o_id|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch Ontology.find(o_id).create_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch import_mappings = incoming_imports_with_mappings
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch if import_mappings.any?
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch combined_sentences.each do |sentence|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch import_mappings.each do |import_mapping|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch import_mapping.symbol_mappings.each do |mapping|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translate_sentence(sentence, mapping)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def translate_sentence(sentence, mapping)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch applicable_sentence =
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch TranslatedSentence.choose_applicable(sentence, mapping)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch if mapping.applicable?(applicable_sentence)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated_text = mapping.apply(applicable_sentence)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch audience = mapping.mapping.target
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated_sentence = TranslatedSentence.where(
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch symbol_mapping_id: mapping,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch audience_id: audience,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence_id: sentence,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch ontology_id: sentence.ontology).first_or_initialize
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated_sentence.translated_text = translated_text
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated_sentence.save
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translated_sentence
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def translate_sentences_for(audience_ontology)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch mapping = Mapping.where(source_id: audience_ontology,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch target_id: self,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch kind: 'import').first
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch if mapping && mapping.symbol_mappings.any?
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch create_or_fetch_translations(audience_ontology, mapping,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch mapping.symbol_mappings)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch default_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch # If there are no translations applicable, what do you do?
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def default_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def create_or_fetch_translations(audience_ontology, mapping, mappings,
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch overwrite: false)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translations =
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch TranslatedSentence.for(audience_ontology, sentences_from: self)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch if translations.any? && !overwrite
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch translations.delete_all
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch create_translations(audience_ontology, mapping, mappings)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def create_translations(_audience_ontology, _mapping, _mappings)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch query, args = mappings_by_kind_query(self, 'import')
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch imported_ontology_ids = pluck_select([query, *args], :id).reverse
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch imported_ontology_ids.each do |o_id|
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch Ontology.find(o_id).create_translated_sentences
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch module Methods
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch def update_or_create_from_hash(hash, timestamp = Time.now)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen sentence = find_or_initialize_by_name(hash['name'])
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.imported = hash['imported'] || false
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.text = hash['text'].to_s
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.range = hash['range']
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.updated_at = timestamp
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.locid = "#{sentence.ontology.locid}#{sep}#{sentence.name}"
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.save!
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch "DELETE FROM sentences_symbols WHERE sentence_id=#{sentence.id}")
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch "INSERT INTO sentences_symbols (sentence_id, symbol_id, ontology_id)
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch SELECT #{sentence.id}, id, ontology_id FROM symbols WHERE
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch ontology_id=#{@association.owner.id} AND text IN (?)",
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch hash['symbols'])
5fb662c8dff409ed8c8385cde59325b944af66b4Stephan Bosch sentence.set_display_text!