Class: Eij::Translator
- Inherits:
-
Object
- Object
- Eij::Translator
- Defined in:
- lib/eij/translator.rb
Instance Method Summary collapse
- #ch_reset ⇒ Object
- #exit_msg(res) ⇒ Object
- #format_jp ⇒ Object
- #grab_inner_item(key, i) ⇒ Object
- #grab_item(key) ⇒ Object
-
#initialize(key) ⇒ Translator
constructor
A new instance of Translator.
- #jap ⇒ Object
- #lookup ⇒ Object
- #lookup_jukugo(div) ⇒ Object
- #lookup_prims(div) ⇒ Object
- #lookup_usedin(div) ⇒ Object
- #out ⇒ Object
- #print_num_lines ⇒ Object
- #to_eng ⇒ Object
- #to_jap ⇒ Object
Constructor Details
#initialize(key) ⇒ Translator
5 6 7 8 9 10 |
# File 'lib/eij/translator.rb', line 5 def initialize(key) @col = %x{bash -lic 'echo $COLUMNS'} @msg = key @res = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) } ch_reset end |
Instance Method Details
#ch_reset ⇒ Object
12 13 14 |
# File 'lib/eij/translator.rb', line 12 def ch_reset @ch = 'a' end |
#exit_msg(res) ⇒ Object
16 17 18 |
# File 'lib/eij/translator.rb', line 16 def exit_msg(res) "Results: #{res}. Search query too vague!\n" end |
#format_jp ⇒ Object
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/eij/translator.rb', line 60 def format_jp @msg = @msg.gsub(":@;", "\n") @msg.strip! @msg = @msg.gsub(" \n", "") divs = @msg.split(/\n/) prim = divs[1..-1] prim_list = [] offset = 0 if prim.count > 52 print exit_msg prim.count exit end prim.each_with_index do |str, index| @ch = 'A' if @ch.ord == 123 chm = "{#{@ch}} " if !str.include? "-->" prim_list[index] = "#{chm.colorize(index-offset)}#{str}" indx = 0 str.split(/\d\./).each do |split| if split.split.size > 0 indx += 1 @res[@ch][indx] = "#{split}" end end @ch = @ch.ord.next.chr else prim_list[index] = "#{str}" offset += 1 end end prim_merge = prim_list.join("\n") prim_merge += "\n" @msg.sub!(@msg, prim_merge) end |
#grab_inner_item(key, i) ⇒ Object
51 52 53 54 |
# File 'lib/eij/translator.rb', line 51 def grab_inner_item(key, i) ch_reset @msg = @res[key][i.to_i] end |
#grab_item(key) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/eij/translator.rb', line 39 def grab_item(key) ch_reset if @res[key].size > 1 @msg = @res[key].map do |x| "#{x[0]}#{x[1]}" end @msg = @msg.join else @msg = @res[key][1] end end |
#jap ⇒ Object
20 21 22 23 |
# File 'lib/eij/translator.rb', line 20 def jap @msg = %x{bash -lic "source ./func.sh; jj #{@msg}"} format_jp end |
#lookup ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/eij/translator.rb', line 116 def lookup @msg = %x{bash -lic 'source ./func.sh; dfind #{@msg}'} divs = @msg.split(":;!;") @msg = divs[0] @msg = @msg.gsub(":@;", "\n") @msg.strip! if @msg.include? "No matches found:" print exit_msg 0 exit elsif @msg.include? "Disambiguation required:" print "Disambiguation required:\n" print_num_lines else @msg += "\n" lookup_prims divs[1] if divs[1].size > 0 lookup_jukugo divs[2] if divs[2].size > 0 lookup_usedin divs[3] if divs[3].size > 0 end end |
#lookup_jukugo(div) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/eij/translator.rb', line 147 def lookup_jukugo(div) prim_list = [] @ch = @ch.ord.next.chr offset = 0 div.split("\n").each_with_index do |str, index| chm = "{#{@ch}} " if str[0].to_s.contains_cjk? prim_list[index] = "#{chm.colorize(index-offset)}#{str}" @res[@ch][1] = str.strip @ch = @ch.ord.next.chr else prim_list[index] = str offset += 1 end end prim_merge = prim_list.join("\n") @msg.sub!(div, prim_merge) end |
#lookup_prims(div) ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/eij/translator.rb', line 136 def lookup_prims(div) prim_list = [] div.split("+").each_with_index do |str, index| chm = ":#{index+1}:" prim_list[index] = chm.colorize(index) + str.strip @res[@ch][index+1] = " " + str.strip + " " end prim_merge = prim_list.join(", ") @msg.sub!(div, "\n{#{@ch}} ".blue + prim_merge) end |
#lookup_usedin(div) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/eij/translator.rb', line 166 def lookup_usedin(div) prim_list = [] offset = 0 strsize = 0 div.split("\n").each_with_index do |str, index| @ch = 'A' if @ch.ord == 123 chm = "{#{@ch}}" if str == "USED IN:" prim_list[index] = "\n" + str.strip + "\n" offset += 1 elsif str.strip.size > 0 if strsize + str.size * 2 >= @col.to_i newl = "\n" strsize = 0 end prim_list[index] = "#{chm.colorize(index-offset)}#{str.strip}#{newl}" @res[@ch][1] = str.strip strsize += str.size @ch = @ch.ord.next.chr end end prim_merge = prim_list.join("") prim_merge.gsub!(", , ", "") @msg.sub!(div, prim_merge) end |
#out ⇒ Object
56 57 58 |
# File 'lib/eij/translator.rb', line 56 def out print @msg.strip + "\n" end |
#print_num_lines ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/eij/translator.rb', line 98 def print_num_lines lst = {} @msg.split("\n").each_with_index do |str, index| if index > 0 print "#{(index).to_s.rjust(2).colorize(index)}#{str}\n" lst[(index).to_s] = str.split(" ")[0] end end print "#: " begin @msg = lst[gets.strip] rescue Exception => e exit end lookup end |
#to_eng ⇒ Object
25 26 27 28 |
# File 'lib/eij/translator.rb', line 25 def to_eng @msg = %x{bash -lic "source ./func.sh; je #{@msg}"} format_jp end |
#to_jap ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/eij/translator.rb', line 30 def to_jap if @msg.contains_cjk? jap else @msg = %x{bash -lic 'source ./func.sh; ej #{@msg}'} format_jp end end |