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

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

Parameters:

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


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
65
66
# File 'lib/asker/data/concept.rb', line 37

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



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

def context
  @context
end

#dataObject (readonly)

Data about this concept



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

def data
  @data
end

#filenameObject (readonly)

Filename where this concept is defined



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

def filename
  @filename
end

#idObject (readonly)

Unique identifer (Integer)



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

def id
  @id
end

#langObject (readonly)

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



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

def lang
  @lang
end

#namesObject (readonly)

Names used to identify or name this concept



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

def names
  @names
end

#processObject

(Boolean) if it is necesary generate questions



25
26
27
# File 'lib/asker/data/concept.rb', line 25

def process
  @process
end

#typeObject (readonly)

type = text -> Name values are only text



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

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



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

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

#imagesObject



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

def images
  @data[:images]
end

#name(option = :raw) ⇒ Object

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



70
71
72
# File 'lib/asker/data/concept.rb', line 70

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

#neighborsObject



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

def neighbors
  @data[:neighbors]
end

#process?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/asker/data/concept.rb', line 78

def process?
  @process
end

#reference_toObject



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

def reference_to
  @data[:reference_to]
end

#referenced_byObject



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

def referenced_by
  @data[:referenced_by]
end

#tablesObject



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

def tables
  @data[:tables]
end

#tagsObject

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



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

def tags
  @data[:tags]
end

#textObject



74
75
76
# File 'lib/asker/data/concept.rb', line 74

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

#textsObject



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

def texts
  @data[:texts]
end

#try_adding_neighbor(other) ⇒ Object



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

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



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

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