Class: Concept

Inherits:
Object
  • Object
show all
Defined in:
lib/asker/data/concept.rb

Overview

Store Concept information

Constant Summary collapse

@@id =

Concept counter

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_data, filename, lang_code = 'en', context = []) ⇒ Concept

Initilize Concept

Parameters:

  • xml_data (XML)
  • filename (String)
  • lang_code (String) (defaults to: 'en')
  • context (Array) (defaults to: [])


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asker/data/concept.rb', line 28

def initialize(xml_data, filename, lang_code = 'en', context = [])
  @@id += 1
  @id = @@id

  @filename = filename
  @process = false
  @lang = LangFactory.instance.get(lang_code)

  if context.class == Array
    @context = context
  elsif context.nil?
    @context = []
  else
    @context = context.split(',')
    @context.collect!(&:strip)
  end
  @names = ['concept.' + @id.to_s]
  @type  = 'text'

  @data = {}
  @data[:tags] = []
  @data[:texts] = []
  @data[:images] = []         # TODO: By now, We'll treat images separated from texts
  @data[:textfile_paths] = [] # TODO: By now, We'll treat this separated from texts
  @data[:tables] = []
  @data[:neighbors] = []
  @data[:reference_to] = []
  @data[:referenced_by] = []

 read_data_from_xml(xml_data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

rubocop:enable Metrics/MethodLength



121
122
123
# File 'lib/asker/data/concept.rb', line 121

def method_missing(method)
  @data[method]
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/asker/data/concept.rb', line 15

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/asker/data/concept.rb', line 17

def data
  @data
end

#filenameObject (readonly)

Returns the value of attribute filename.



16
17
18
# File 'lib/asker/data/concept.rb', line 16

def filename
  @filename
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/asker/data/concept.rb', line 15

def id
  @id
end

#langObject (readonly)

Returns the value of attribute lang.



15
16
17
# File 'lib/asker/data/concept.rb', line 15

def lang
  @lang
end

#namesObject (readonly)

Returns the value of attribute names.



16
17
18
# File 'lib/asker/data/concept.rb', line 16

def names
  @names
end

#processObject

Returns the value of attribute process.



18
19
20
# File 'lib/asker/data/concept.rb', line 18

def process
  @process
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/asker/data/concept.rb', line 16

def type
  @type
end

Instance Method Details

#calculate_nearness_to_concept(other) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/asker/data/concept.rb', line 82

def calculate_nearness_to_concept(other)
  a = Application.instance.config['ai']['formula_weights']
  weights = a.split(',').map(&:to_f)

  max1 = @context.count
  max2 = @data[:tags].count
  max3 = @data[:tables].count

  alike1 = alike2 = alike3 = 0.0

  # check if exists this items from concept1 into concept2
  @context.each { |i| alike1 += 1.0 unless other.context.index(i).nil? }
  @data[:tags].each { |i| alike2 += 1.0 unless other.tags.index(i).nil? }
  @data[:tables].each { |i| alike3 += 1.0 unless other.tables.index(i).nil? }

  alike = (alike1 * weights[0] + alike2 * weights[1] + alike3 * weights[2])
  max = (max1 * weights[0] + max2 * weights[1] + max3 * weights[2])
  (alike * 100.0 / max)
end

#name(option = :raw) ⇒ Object



60
61
62
# File 'lib/asker/data/concept.rb', line 60

def name(option = :raw)
  DataField.new(@names[0], @id, @type).get(option)
end

#process?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/asker/data/concept.rb', line 68

def process?
  @process
end

#textObject



64
65
66
# File 'lib/asker/data/concept.rb', line 64

def text
  @data[:texts][0] || '...'
end

#try_adding_neighbor(other) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/asker/data/concept.rb', line 72

def try_adding_neighbor(other)
  p = calculate_nearness_to_concept(other)
  return if p.zero?

  @data[:neighbors] << { concept: other, value: p }
  # Sort neighbors list
  @data[:neighbors].sort! { |a, b| a[:value] <=> b[:value] }
  @data[:neighbors].reverse!
end

#try_adding_references(other) ⇒ Object

rubocop:disable Metrics/MethodLength



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/asker/data/concept.rb', line 103

def try_adding_references(other)
  reference_to = 0
  @data[:tags].each do |i|
    reference_to += 1 unless other.names.index(i.downcase).nil?
  end
  @data[:texts].each do |t|
    text = t.clone
    text.split(' ').each do |word|
      reference_to += 1 unless other.names.index(word.downcase).nil?
    end
  end
  return unless reference_to.positive?

  @data[:reference_to] << other.name
  other.data[:referenced_by] << name
end