Module: AI_calculate

Included in:
AI
Defined in:
lib/asker/ai/ai_calculate.rb

Overview

Methods that calculate something

Instance Method Summary collapse

Instance Method Details

#calculate_nearness_between_texts(text1, text2) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/asker/ai/ai_calculate.rb', line 28

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

#get_list1_and_list2_from(ltable) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/asker/ai/ai_calculate.rb', line 5

def get_list1_and_list2_from(ltable)
  # create <list1> with all the rows from the table
  list1 = []
  count = 1
  ltable.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 = []
  neighbors.each do |n|
    n[:concept].tables.each do |t2|
      next if t2.name != ltable.name
      t2.rows.each do |i|
        list2 << { id: count, weight: 0, data: i }
        count += 1
      end
    end
  end
  return list1, list2
end

#reorder_list_with_row(list, row) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asker/ai/ai_calculate.rb', line 37

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