Class: Concept

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

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, context) ⇒ Concept

Initilize Concept

Parameters:

  • xml_data (XML)
  • filename (String)
  • lang_code (String)
  • context (Array)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asker/data/concept.rb', line 27

def initialize(xml_data, filename, lang, context)
  @@id += 1
  @id = @@id

  @filename = filename
  @process = false
  @lang = lang # LangFactory.instance.get(lang_code)
  @context = context
  @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



12
13
14
# File 'lib/asker/data/concept.rb', line 12

def context
  @context
end

#dataObject (readonly)

Data about this concept



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

def data
  @data
end

#filenameObject (readonly)

Filename where this concept is defined



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

def filename
  @filename
end

#idObject (readonly)

Unique identifer (Integer)



10
11
12
# File 'lib/asker/data/concept.rb', line 10

def id
  @id
end

#langObject (readonly)

Lang object



11
12
13
# File 'lib/asker/data/concept.rb', line 11

def lang
  @lang
end

#namesObject (readonly)

Names used to identify or name this concept



13
14
15
# File 'lib/asker/data/concept.rb', line 13

def names
  @names
end

#processObject

(Boolean) if it is necesary generate questions



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

def process
  @process
end

#typeObject (readonly)

type = text -> Name values are only text



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

def type
  @type
end

Instance Method Details

#calculate_nearness_to_concept(other) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/asker/data/concept.rb', line 72

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



116
117
118
# File 'lib/asker/data/concept.rb', line 116

def images
  @data[:images]
end

#name(option = :raw) ⇒ Object



50
51
52
# File 'lib/asker/data/concept.rb', line 50

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

#neighborsObject



124
125
126
# File 'lib/asker/data/concept.rb', line 124

def neighbors
  @data[:neighbors]
end

#process?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/asker/data/concept.rb', line 58

def process?
  @process
end

#reference_toObject



128
129
130
# File 'lib/asker/data/concept.rb', line 128

def reference_to
  @data[:reference_to]
end

#referenced_byObject



132
133
134
# File 'lib/asker/data/concept.rb', line 132

def referenced_by
  @data[:referenced_by]
end

#tablesObject



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

def tables
  @data[:tables]
end

#tagsObject



108
109
110
# File 'lib/asker/data/concept.rb', line 108

def tags
  @data[:tags]
end

#textObject



54
55
56
# File 'lib/asker/data/concept.rb', line 54

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

#textsObject



112
113
114
# File 'lib/asker/data/concept.rb', line 112

def texts
  @data[:texts]
end

#try_adding_neighbor(other) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/asker/data/concept.rb', line 62

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/asker/data/concept.rb', line 91

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