Class: EntityExtractor
- Inherits:
-
Object
- Object
- EntityExtractor
- Defined in:
- lib/entityextractor.rb
Instance Method Summary collapse
- #extract(type, minchar, ignoreterms, terms, ignorefields, caseinfo, mapto, *append) ⇒ Object
-
#extractTerms(extractlist, i, addlist, field) ⇒ Object
Extract terms input from preset list.
-
#genJSON ⇒ Object
Generates JSON output.
-
#getExtract ⇒ Object
Get list of just extracted terms by occurrence.
-
#initialize(input, fieldoutname, *extractfield) ⇒ EntityExtractor
constructor
A new instance of EntityExtractor.
-
#parseALLCAPS(toParse, i, minchar, addlist, ignoreterms, savefield, extractfield) ⇒ Object
Parses terms in all caps.
Constructor Details
#initialize(input, fieldoutname, *extractfield) ⇒ EntityExtractor
Returns a new instance of EntityExtractor.
7 8 9 10 11 12 |
# File 'lib/entityextractor.rb', line 7 def initialize(input, fieldoutname, *extractfield) @input = JSON.parse(input) @fieldoutname = fieldoutname @extractfield = *extractfield @output = Array.new end |
Instance Method Details
#extract(type, minchar, ignoreterms, terms, ignorefields, caseinfo, mapto, *append) ⇒ Object
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/entityextractor.rb', line 99 def extract(type, minchar, ignoreterms, terms, ignorefields, caseinfo, mapto, *append) flag = 0 h = HandleInput.new(terms, ignorefields, caseinfo) extractlist = h.detecttype @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(extractlist, i, addlist, f) end if mapto i[@fieldoutname] = h.mapout(addlist, mapto) else i[@fieldoutname] = addlist end @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[@fieldoutname] = addlist @output.push(i) # Extract dates elsif type == "date" @extractfield.each do |f| d = ExtractDates.new(i[f]) appendhash = Hash.new append.each do |a| appendhash[a] = i[a] end outhash = d.chunk(appendhash) @output.push(outhash) if !outhash.empty? 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(extractlist, i, addlist, f) end if mapto i[@fieldoutname] = h.mapout(addlist, mapto) else i[@fieldoutname] = addlist end @output.push(i) end if flag == 1 break end end end |
#extractTerms(extractlist, i, addlist, field) ⇒ Object
Extract terms input from preset list
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/entityextractor.rb', line 15 def extractTerms(extractlist, i, addlist, field) count = 0 downcased = i[field].to_s.downcase # Check the item for each term extractlist.each do |t, c| count+=1 if c == true if i[field].to_s.include? t addlist.push(t) end else if downcased.include? t.downcase addlist.push(t) end end end end |
#genJSON ⇒ Object
Generates JSON output
95 96 97 |
# File 'lib/entityextractor.rb', line 95 def genJSON JSON.pretty_generate(@output) end |
#getExtract ⇒ Object
Get list of just extracted terms by occurrence
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/entityextractor.rb', line 76 def getExtract extracthash = Hash.new # Generate hash of all extracted terms @output.each do |i| i[@fieldoutname].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
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 66 67 68 69 70 71 72 73 |
# File 'lib/entityextractor.rb', line 35 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 |