Class: Concept
- Inherits:
-
Object
- Object
- Concept
- Defined in:
- lib/asker/data/concept.rb
Overview
Store Concept information
Constant Summary collapse
- @@id =
0
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#process ⇒ Object
Returns the value of attribute process.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #calculate_nearness_to_concept(other) ⇒ Object
-
#initialize(xml_data, filename, lang_code = 'en', context = []) ⇒ Concept
constructor
A new instance of Concept.
- #method_missing(method) ⇒ Object
- #name(option = :raw) ⇒ Object
- #process? ⇒ Boolean
- #text ⇒ Object
- #try_adding_neighbor(other) ⇒ Object
- #try_adding_references(other) ⇒ Object
Constructor Details
#initialize(xml_data, filename, lang_code = 'en', context = []) ⇒ Concept
Returns a new instance of Concept.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/asker/data/concept.rb', line 20 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
108 109 110 |
# File 'lib/asker/data/concept.rb', line 108 def method_missing(method) @data[method] end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
13 14 15 |
# File 'lib/asker/data/concept.rb', line 13 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/asker/data/concept.rb', line 15 def data @data end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
14 15 16 |
# File 'lib/asker/data/concept.rb', line 14 def filename @filename end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/asker/data/concept.rb', line 13 def id @id end |
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
13 14 15 |
# File 'lib/asker/data/concept.rb', line 13 def lang @lang end |
#names ⇒ Object (readonly)
Returns the value of attribute names.
14 15 16 |
# File 'lib/asker/data/concept.rb', line 14 def names @names end |
#process ⇒ Object
Returns the value of attribute process.
16 17 18 |
# File 'lib/asker/data/concept.rb', line 16 def process @process end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/asker/data/concept.rb', line 14 def type @type end |
Instance Method Details
#calculate_nearness_to_concept(other) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/asker/data/concept.rb', line 74 def calculate_nearness_to_concept(other) weights = Project.instance.formula_weights li_max1 = @context.count li_max2 = @data[:tags].count li_max3 = @data[:tables].count lf_alike1 = lf_alike2 = lf_alike3 = 0.0 # check if exists this items from concept1 into concept2 @context.each { |i| lf_alike1 += 1.0 unless other.context.index(i).nil? } @data[:tags].each { |i| lf_alike2 += 1.0 unless other..index(i).nil? } @data[:tables].each { |i| lf_alike3 += 1.0 unless other.tables.index(i).nil? } lf_alike = (lf_alike1 * weights[0] + lf_alike2 * weights[1] + lf_alike3 * weights[2]) li_max = (li_max1 * weights[0] + li_max2 * weights[1] + li_max3 * weights[2]) (lf_alike * 100.0 / li_max) end |
#name(option = :raw) ⇒ Object
52 53 54 |
# File 'lib/asker/data/concept.rb', line 52 def name(option = :raw) DataField.new(@names[0], @id, @type).get(option) end |
#process? ⇒ Boolean
60 61 62 |
# File 'lib/asker/data/concept.rb', line 60 def process? @process end |
#text ⇒ Object
56 57 58 |
# File 'lib/asker/data/concept.rb', line 56 def text @data[:texts][0] || '...' end |
#try_adding_neighbor(other) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/asker/data/concept.rb', line 64 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/asker/data/concept.rb', line 93 def try_adding_references(other) reference_to = 0 @data[:tags].each { |i| reference_to += 1 unless other.names.index(i.downcase).nil? } @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 if reference_to.positive? @data[:reference_to] << other.name other.data[:referenced_by] << name end end |