Class: EnjuAccessor
- Inherits:
-
Object
- Object
- EnjuAccessor
- Defined in:
- lib/enju_accessor/enju_accessor.rb
Overview
An instance of this class holds the parsing result of a natural language query as anlyzed by Enju.
Instance Method Summary collapse
- #get_parse(sentence) ⇒ Object
-
#initialize(enju_cgi_url) ⇒ EnjuAccessor
constructor
A new instance of EnjuAccessor.
- #parse_sentence(sentence, offset_base = 0, mode = '') ⇒ Object
- #parse_text(text) ⇒ Object
- #read_parse(sentence, r) ⇒ Object
- #tag_sentence(sentence, offset_base = 0, mode = '') ⇒ Object
- #tag_text(text) ⇒ Object
Constructor Details
#initialize(enju_cgi_url) ⇒ EnjuAccessor
8 9 10 11 12 |
# File 'lib/enju_accessor/enju_accessor.rb', line 8 def initialize(enju_cgi_url) @enju_cgi = RestClient::Resource.new(enju_cgi_url) @sentencer = TextSentencer.new @tid_base, @rid_base = 0, 0 end |
Instance Method Details
#get_parse(sentence) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/enju_accessor/enju_accessor.rb', line 14 def get_parse (sentence) begin response = @enju_cgi.get :params => {:sentence=>sentence, :format=>'so'} rescue => e raise IOError, "Abnormal behavior of the Enju CGI server: #{e.message}." end parse = case response.code when 200 # 200 means success raise "Empty input." if response =~/^Empty line/ r = response.encode("ASCII-8BIT").force_encoding("UTF-8").to_s read_parse(sentence, r) else raise IOError, "Abnormal response from the Enju CGI server." end parse end |
#parse_sentence(sentence, offset_base = 0, mode = '') ⇒ Object
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 |
# File 'lib/enju_accessor/enju_accessor.rb', line 98 def parse_sentence (sentence, offset_base = 0, mode = '') @tid_base, @rid_base = 0, 0 unless mode == 'continue' toks, cons = get_parse(sentence) denotations = [] tid_mapping = {} idx_last = 0 toks.each do |id, tok| id = tid_mapping[id] = 'T' + (tok[:idx] + @tid_base).to_s denotations << {id:id, span:{begin: tok[:beg] + offset_base, end: tok[:end] + offset_base}, obj: tok[:pos]} idx_last = tok[:idx] end # puts toks.map{|t| t.to_s}.join("\n") cons.each do |id, con| thead = con[:sem_head] thead = cons[thead][:sem_head] until thead.start_with?('t') con[:thead] = thead end relations = [] rid_num = @rid_base toks.each do |id, tok| unless tok[:args].empty? tok[:args].each do |type, arg| arg = cons[arg][:thead] if arg.start_with?('c') next if tid_mapping[arg].nil? relations << {id: 'R' + rid_num.to_s, subj: tid_mapping[arg], obj: tid_mapping[id], pred: type.to_s.downcase + 'Of'} rid_num += 1 end end end @tid_base = @tid_base + idx_last + 1 @rid_base = rid_num {:denotations => denotations, :relations => relations} end |
#parse_text(text) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/enju_accessor/enju_accessor.rb', line 157 def parse_text (text) segments = @sentencer.segment(text) denotations, relations = [], [] segments.each_with_index do |s, i| mode = (i == 0)? nil : 'continue' annotation = parse_sentence(text[s[0]...s[1]], s[0], mode) denotations += annotation[:denotations] relations += annotation[:relations] end {:text=> text, :denotations => denotations, :relations => relations} end |
#read_parse(sentence, r) ⇒ Object
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/enju_accessor/enju_accessor.rb', line 33 def read_parse (sentence, r) toks = {} cons = {} adjustment = 0 # r is a parsing result in SO format. lines = r.split(/\r?\n/) idx = 0 lines.each do |line| # for each line of analysis b, e, attr_str = line.split(/\t/) b = b.to_i e = e.to_i node = Nokogiri::HTML.parse('<node ' + attr_str + '>') attrs = node.css('node').first.to_h if attrs['tok'] == "" base = attrs['base'] b += adjustment base.each_char{|c| adjustment += (1 - c.bytesize) if c !~ /\p{ASCII}/} e += adjustment id = attrs['id'] pos = attrs['pos'] pos = attrs['base'] if [',', '.', ':', '(', ')', '``', ''''].include?(pos) pos.sub!('$', '-DOLLAR-') pos = '-COLON-' if pos == 'HYPH' toks[id] = {beg: b, end:e, word:sentence[b ... e], idx:idx, base:base, pos:pos, cat:attrs['cat'], args:{}} toks[id][:args][:arg1] = attrs['arg1'] if attrs['arg1'] toks[id][:args][:arg2] = attrs['arg2'] if attrs['arg2'] toks[id][:args][:arg3] = attrs['arg3'] if attrs['arg3'] toks[id][:args][:mod] = attrs['mod'] if attrs['mod'] idx += 1 end end lines.each do |line| # for each line of analysis b, e, attr_str = line.split(/\t/) b = b.to_i e = e.to_i node = Nokogiri::HTML.parse('<node ' + attr_str + '>') attrs = node.css('node').first.to_h if attrs['cons'] == "" id = attrs['id'] head = attrs['head'] sem_head = attrs['sem_head'] cat = attrs['cat'] cons[id] = {head:head, sem_head: sem_head, cat:cat} end end # puts sentence # puts toks.map{|t| t.to_s}.join("\n") # puts cons.map{|c| c.to_s}.join("\n") # puts "-----" # exit [toks, cons] end |
#tag_sentence(sentence, offset_base = 0, mode = '') ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/enju_accessor/enju_accessor.rb', line 139 def tag_sentence (sentence, offset_base = 0, mode = '') @id_base = 0 unless mode == 'continue' toks, cons = get_parse(sentence) denotations = [] idx_last = 0 toks.each do |id, tok| denotations << {id: 'P' + (tok[:idx] + @id_base).to_s, span: {begin: tok[:beg] + offset_base, end: tok[:end] + offset_base}, obj: tok[:pos]} denotations << {id: 'B' + (tok[:idx] + @id_base).to_s, span: {begin: tok[:beg] + offset_base, end: tok[:end] + offset_base}, obj: tok[:base]} idx_last = tok[:idx] end @id_base = @id_base + idx_last + 1 {:denotations => denotations} end |
#tag_text(text) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/enju_accessor/enju_accessor.rb', line 171 def tag_text (text) segments = @sentencer.segment(text) denotations = [] segments.each_with_index do |s, i| mode = (i == 0)? nil : 'continue' annotation = tag_sentence(text[s[0]...s[1]], s[0], mode) denotations += annotation[:denotations] end {:text=> text, :denotations => denotations} end |