<commit_before>class Ontology
  module Symbols
    extend ActiveSupport::Concern

    included do
      has_many :symbols,
        autosave: false,
        class_name: 'OntologyMember::Symbol',
        extend:   Methods
    end

    module Methods
      def update_or_create_from_hash(hash, timestamp = Time.now)
        raise ArgumentError, 'No hash given.' unless hash.is_a? Hash

        e = where(text: hash['text']).first_or_initialize

        e.ontology   = @association.owner
        e.range      = hash['range']
        e.updated_at = timestamp

        unless hash['name'] || hash['kind']
          Rails.logger.warn(
            "Using work-around to determine symbol name and kind: #{e.inspect}")

          if e2 = Symbol.where(text: hash['text']).first
            e.name = e2.name
            e.kind = e2.kind
          else
            e.name = e.text
            e.kind = 'Undefined'
          end
        else
          e.name = hash['name']
          e.kind = hash['kind']
        end

        e.iri = hash['iri']
        e.label = hash['label']

        sep = '//'
        locid_portion =
          if e.name.include?('://')
            if e.label
              e.label
            else
              portion = e.name.end_with?('>') ? e.name[0..-2] : e.name
              portion = portion.split('#', 2).last
            end
          else
            e.name
          end
        e.locid = "#{e.ontology.locid}#{sep}#{locid_portion}"

        if e.range.to_s.include?(':')
          # remove path from range
          # Examples/Reichel:28.9 -> 28.9
          e.range = e.range.split(':', 2).last
        end

        e.save!
      end
    end

    def delete_edges
      %i[parent_id child_id].each do |key|
        EEdge.where(key => symbols.where(kind: 'Class')).delete_all
      end
    end

    def create_symbol_tree
      raise StandardError.new('Ontology is not OWL') unless owl?

      # Delete previous set of categories
      delete_edges
      subclasses =
        sentences.where("text LIKE '%SubClassOf%'").select do |sentence|
          sentence.text.split(' ').size == 4
        end
      transaction requires_new: true do
        subclasses.each do |s|
          c1, c2 = s.hierarchical_class_names

          unless c1 == 'Thing' || c2 == 'Thing'
            child_id = symbols.where('name = ? OR iri = ?', c1, c1).first.id<commit_msg>Fix symbols not occurring in the ontology.symbols association.<commit_after>class Ontology
  module Symbols
    extend ActiveSupport::Concern

    included do
      has_many :symbols,
        autosave: false,
        class_name: 'OntologyMember::Symbol',
        extend:   Methods
    end

    module Methods
      def update_or_create_from_hash(hash, timestamp = Time.now)
        raise ArgumentError, 'No hash given.' unless hash.is_a? Hash

        e = where(text: hash['text']).first_or_initialize

        e.ontology   = @association.owner
        e.range      = hash['range']
        e.updated_at = timestamp

        unless hash['name'] || hash['kind']
          Rails.logger.warn(
            "Using work-around to determine symbol name and kind: #{e.inspect}")

          if e2 = Symbol.where(text: hash['text']).first
            e.name = e2.name
            e.kind = e2.kind
          else
            e.name = e.text
            e.kind = 'Undefined'
          end
        else
          e.name = hash['name']
          e.kind = hash['kind']
        end

        e.iri = hash['iri']
        e.label = hash['label']

        sep = '//'
        locid_portion =
          if e.name.include?('://')
            if e.label
              e.label
            else
              portion = e.name.end_with?('>') ? e.name[0..-2] : e.name
              portion = portion.split('#', 2).last
            end
          else
            e.name
          end
        e.locid = "#{e.ontology.locid}#{sep}#{locid_portion}"

        if e.range.to_s.include?(':')
          # remove path from range
          # Examples/Reichel:28.9 -> 28.9
          e.range = e.range.split(':', 2).last
        end

        e.ontology.symbols << e if e.id.nil?
        e.save!
        e
      end
    end

    def delete_edges
      %i[parent_id child_id].each do |key|
        EEdge.where(key => symbols.where(kind: 'Class')).delete_all
      end
    end

    def create_symbol_tree
      raise StandardError.new('Ontology is not OWL') unless owl?

      # Delete previous set of categories
      delete_edges
      subclasses =
        sentences.where("text LIKE '%SubClassOf%'").select do |sentence|
          sentence.text.split(' ').size == 4
        end
      transaction requires_new: true do
        subclasses.each do |s|
          c1, c2 = s.hierarchical_class_names

          unless c1 == 'Thing' || c2 == 'Thing'
            child_id = symbols.where('name = ? OR iri = ?', c1, c1).first.id
{'fraction_non_alphanumeric': 0.11974443528441879, 'fraction_numerical': 0.008244023083264633, 'mean_word_length': 1.6577217962760131, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792348', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1481, 'n_words': 351}