Class: Concept

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

Overview

Store Concept information rubocop:disable Metrics/ClassLength rubocop:disable Style/ClassVars

Constant Summary collapse

@@id =

Global Concept counter (concept identifier)

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initilize Concept rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/asker/data/concept.rb', line 35

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] = []          # Used by standard def inputs
  @data[:images] = []         # Used by [type => file and type => image_url] def inputs
  @data[:tables] = []
  @data[:neighbors] = []
  @data[:reference_to] = []
  @data[:referenced_by] = []

  read_data_from_xml(xml_data)
end

Instance Attribute Details

#contextObject (readonly)

Context inherits from map



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

def context
  @context
end

#dataObject (readonly)

Data about this concept



22
23
24
# File 'lib/asker/data/concept.rb', line 22

def data
  @data
end

#filenameObject (readonly)

Filename where this concept is defined



21
22
23
# File 'lib/asker/data/concept.rb', line 21

def filename
  @filename
end

#idObject (readonly)

Unique identifer (Integer)



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

def id
  @id
end

#langObject (readonly)

Lang code (By default is the same as map lang)



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

def lang
  @lang
end

#namesObject (readonly)

Names used to identify or name this concept



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

def names
  @names
end

#processObject

(Boolean) if it is necesary generate questions



23
24
25
# File 'lib/asker/data/concept.rb', line 23

def process
  @process
end

#typeObject (readonly)

type = text -> Name values are only text



20
21
22
# File 'lib/asker/data/concept.rb', line 20

def type
  @type
end

Instance Method Details

#calculate_nearness_to_concept(other) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/asker/data/concept.rb', line 93

def calculate_nearness_to_concept(other)
  a = ProjectData.instance.get(:weights)
  #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

#imagesObject



146
147
148
# File 'lib/asker/data/concept.rb', line 146

def images
  @data[:images]
end

#name(option = :raw) ⇒ Object

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



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

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

#neighborsObject



154
155
156
# File 'lib/asker/data/concept.rb', line 154

def neighbors
  @data[:neighbors]
end

#process?Boolean



76
77
78
# File 'lib/asker/data/concept.rb', line 76

def process?
  @process
end

#reference_toObject



158
159
160
# File 'lib/asker/data/concept.rb', line 158

def reference_to
  @data[:reference_to]
end

#referenced_byObject



162
163
164
# File 'lib/asker/data/concept.rb', line 162

def referenced_by
  @data[:referenced_by]
end

#tablesObject



150
151
152
# File 'lib/asker/data/concept.rb', line 150

def tables
  @data[:tables]
end

#tagsObject

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



138
139
140
# File 'lib/asker/data/concept.rb', line 138

def tags
  @data[:tags]
end

#textObject



72
73
74
# File 'lib/asker/data/concept.rb', line 72

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

#textsObject



142
143
144
# File 'lib/asker/data/concept.rb', line 142

def texts
  @data[:texts]
end

#try_adding_neighbor(other) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/asker/data/concept.rb', line 80

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 rubocop:disable Metrics/AbcSize



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/asker/data/concept.rb', line 119

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