Class: ExtractSetTerms

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_set_terms.rb

Instance Method Summary collapse

Constructor Details

#initialize(item, extract_field, to_extract, extract_term_fields, case_sensitive, save_field) ⇒ ExtractSetTerms

Returns a new instance of ExtractSetTerms.



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/extract_set_terms.rb', line 2

def initialize(item, extract_field, to_extract, extract_term_fields, case_sensitive, save_field)
  @item = item
  @extract_field = extract_field

  @to_extract = JSON.parse(to_extract)
  @extract_term_fields = extract_term_fields
  @case_sensitive = case_sensitive

  @extract_dict = Hash.new
  @save_field = save_field
  @item_out = item
end

Instance Method Details

#extractTermsObject

Process input list and go through all terms and fields



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/extract_set_terms.rb', line 70

def extractTerms
  # Process input list
  @to_extract.is_a?(Hash) ? processHashInput : processArrayInput(@to_extract, nil)
  @item_out[@save_field] = Array.new

  # Go through each term and field to check for matches
  @extract_dict.each do |term|
    item_case_sensitivity = isCaseSensitive?(term)
    @extract_field.each do |field|

      # Add to list of terms if it matches
      if matchTerm?(term[0], @item[field], item_case_sensitivity)
        @item_out[@save_field].push(term[1])
      end

    end
  end

  # Deduplicate and return
  @item_out[@save_field].uniq!
  return @item_out
end

#isCaseSensitive?(term) ⇒ Boolean

Check if item is case sensitive

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/extract_set_terms.rb', line 54

def isCaseSensitive?(term)
  if @case_sensitive == "case-sensitive"
    return true
  elsif @case_sensitive == "case-insensitive"
    return false
  else
    # Handle item by item variations
    is_case_sensitive = @to_extract[term[1]][@case_sensitive]
    if is_case_sensitive == "Yes"
      return true 
    else return false
    end
  end
end

#matchTerm?(term, text, case_sensitive) ⇒ Boolean

Check if the term appears in the text

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/extract_set_terms.rb', line 40

def matchTerm?(term, text, case_sensitive)
  # Downcase term and text if not case sensitive
  if case_sensitive == false
    term = term.downcase
    text = text.downcase
  end
  
  # Return if it maches
  if text.to_s.match(/\b(#{term})\b/)
    return true
  end
end

#processArrayInput(extract_arr, map_val) ⇒ Object

Add all items in array to dictionary of terms to extract



32
33
34
35
36
37
# File 'lib/extract_set_terms.rb', line 32

def processArrayInput(extract_arr, map_val)
  extract_arr.each do |term| 
    map_val = term if map_val == nil
    @extract_dict[term] = map_val
  end
end

#processHashInputObject

Gets a list of terms to extract



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/extract_set_terms.rb', line 16

def processHashInput
  # Go through each item then each field
  @to_extract.each do |ex_key, ex_value|
    ex_value.each do |ex_field, ex_term|
      
      # Check if it is the right field
      if ex_field == @extract_term_fields || @extract_term_fields.include?(ex_field)
        # Make dictionary of terms to extract and overall mapping
        ex_term.is_a?(Array) ? processArrayInput(ex_term, ex_key) : @extract_dict[term] = ex_key
      end

    end
  end
end