Class: EntityExtractor

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

Instance Method Summary collapse

Constructor Details

#initialize(input, *extractfield) ⇒ EntityExtractor

Returns a new instance of EntityExtractor.



6
7
8
9
10
# File 'lib/entityextractor.rb', line 6

def initialize(input, *extractfield)
  @input = JSON.parse(input)
  @extractfield = *extractfield
  @output = Array.new
end

Instance Method Details

#extract(type, minchar, ignoreterms, *terms) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/entityextractor.rb', line 91

def extract(type, minchar, ignoreterms, *terms)
   flag = 0
   @input.each do |i|
     if i.length == 2
       i = @input
       flag = 1
     end

     addlist = Array.new
     
     # Generate set terms list
     if type == "set"
       @extractfield.each do |f|
         extractTerms(*terms, i, addlist, f)
       end

       i["extract"] = addlist
       @output.push(i)
     
     # Generate ALLCAPS terms list
     elsif type == "ALLCAPS"
       @extractfield.each do |f|
         savefield = i[f].to_s + " "
         parseALLCAPS(i[f].to_s, i, minchar, addlist, ignoreterms, savefield, f)
       end
       
       i["extract"] = addlist
       @output.push(i)

     # Extract dates
     elsif type == "date"
       @extractfield.each do |f|
         d = ExtractDates.new(i[f])
         outhash = d.chunk(i["path"])
         @output.push(outhash)
       end

     # Extract both set terms and ALLCAPS
     elsif type == "both"
       @extractfield.each do |f|
         savefield = i[f].to_s + " "
         parseALLCAPS(i[f].to_s, i, minchar, addlist, ignoreterms, savefield, f)
         extractTerms(*terms, i, addlist, f)
       end

       i["extract"] = addlist
       @output.push(i)
     end

     if flag == 1
       break
     end
   end 
end

#extractTerms(*terms, i, addlist, field) ⇒ Object

Extract terms input from preset list



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/entityextractor.rb', line 13

def extractTerms(*terms, i, addlist, field)
  count = 0
    
  # Check the item for each term
  terms.each do |t|
    count+=1
 
    if i[field].to_s.include? t
      addlist.push(t.upcase)
    end
  end
end

#genJSONObject

Generates JSON output



87
88
89
# File 'lib/entityextractor.rb', line 87

def genJSON
  JSON.pretty_generate(@output)
end

#getExtractObject

Get list of just extracted terms by occurrence



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/entityextractor.rb', line 68

def getExtract
  extracthash = Hash.new
  
  # Generate hash of all extracted terms
  @output.each do |i|
    i["extract"].each do |e|
      if extracthash.has_key? e
        extracthash[e] += 1
      else
        extracthash[e] = 1
      end
    end
  end
  
  # Sort hash
  return Hash[extracthash.sort_by { |k, v| v}]
end

#parseALLCAPS(toParse, i, minchar, addlist, ignoreterms, savefield, extractfield) ⇒ Object

Parses terms in all caps



27
28
29
30
31
32
33
34
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
65
# File 'lib/entityextractor.rb', line 27

def parseALLCAPS(toParse, i, minchar, addlist, ignoreterms, savefield, extractfield)
  if toParse =~ (/[A-Z]{#{minchar}}/)
    index = toParse =~ (/[A-Z]{#{minchar}}/)
    charnum = 0
    
    # Find word in all caps
    toParse.each_char do |c|
      if charnum >= index
        if toParse[c] == toParse[c].upcase && toParse[c] !~ (/[[:punct:]]/) && toParse[c] !~ (/[[:digit:]]/)
          charnum += 1
        else break
        end
      else
        charnum += 1
      end
    end

    # Remove any extra characters
    if toParse[charnum-2] == " "
      charnum = charnum-3
    elsif toParse[charnum-1] == " "
      charnum = charnum-2
    else charnum = charnum-1
    end
    
    # Filter out terms in ignoreterms array
    if !(ignoreterms.include? toParse[index..charnum])
      addlist.push(toParse[index..charnum])
    end
    
    parsedstring = toParse[0..charnum]
    toParse.slice! parsedstring
    parseALLCAPS(toParse, i, minchar, addlist, ignoreterms, savefield, extractfield)

  # If there are no (more) results, append addlist to JSON
  else
    i[extractfield] = savefield
  end
end