Sha256: a581fbe41944cae67784924a2e6b14e5d09613c2eb206aa3db4780afcab005f3

Contents?: true

Size: 1.87 KB

Versions: 27

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

# Methods that calculate something
module AICalculate
  ##
  # Calculate and return list1 and list2
  # * return list1 (Array) List with all the rows from the table
  # * return list2 (Array) List with similar rows (same table name) from the neighbours tables
  # @param p_table (Table)
  # rubocop:disable Metrics/MethodLength
  # rubocop:disable Metrics/AbcSize
  def get_list1_and_list2_from(p_table)
    # create <list1> with all the rows from the table
    list1 = []
    count = 1
    p_table.rows.each do |i|
      list1 << { id: count, weight: 0, data: i }
      count += 1
    end

    # create a <list2> with similar rows (same table name) from the neighbours tables
    list2 = []
    concept.neighbors.each do |n|
      n[:concept].tables.each do |t2|
        next if t2.name != p_table.name

        t2.rows.each do |i|
          list2 << { id: count, weight: 0, data: i }
          count += 1
        end
      end
    end
    [list1, list2]
  end
  # rubocop:enable Metrics/MethodLength
  # rubocop:enable Metrics/AbcSize

  def calculate_nearness_between_texts(text1, text2)
    return 0.0 if text2.nil? || text2.empty?

    words = text1.split(' ')
    count = 0
    words.each { |w| count += 1 if text2.include?(w) }
    (count * 100 / words.count)
  end

  # rubocop:disable Metrics/MethodLength
  # rubocop:disable Metrics/AbcSize
  def reorder_list_with_row(list, row)
    # evaluate every row of the list2
    list.each do |r|
      if r[:id] == row[:id]
        r[:weight] = -300
      else
        val = 0
        s = row[:data].count
        s.times do |i|
          val += calculate_nearness_between_texts(row[:data][i], r[:data][i])
        end
        val /= s
        r[:weight] = val
      end
    end
    list.sort! { |a, b| a[:weight] <=> b[:weight] }
    list.reverse!
  end
  # rubocop:enable Metrics/MethodLength
  # rubocop:enable Metrics/AbcSize
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
asker-tool-2.5.9 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.8 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.7 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.6 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.5 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.4 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.3 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.2 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.1 lib/asker/ai/ai_calculate.rb
asker-tool-2.5.0 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.5 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.4 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.3 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.2 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.1 lib/asker/ai/ai_calculate.rb
asker-tool-2.4.0 lib/asker/ai/ai_calculate.rb
asker-tool-2.3.4 lib/asker/ai/ai_calculate.rb
asker-tool-2.3.3 lib/asker/ai/ai_calculate.rb
asker-tool-2.3.2 lib/asker/ai/ai_calculate.rb
asker-tool-2.3.1 lib/asker/ai/ai_calculate.rb