Class: LlmTranslator
- Inherits:
-
Object
- Object
- LlmTranslator
- Defined in:
- lib/llm_translator.rb
Constant Summary collapse
- CONFIG_PATH =
File.('~/.llm_translator')
Instance Attribute Summary collapse
-
#auto ⇒ Object
Returns the value of attribute auto.
-
#detail ⇒ Object
Returns the value of attribute detail.
-
#from ⇒ Object
Returns the value of attribute from.
-
#history ⇒ Object
Returns the value of attribute history.
-
#text ⇒ Object
Returns the value of attribute text.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #command(args) ⇒ Object
-
#initialize(text) ⇒ LlmTranslator
constructor
A new instance of LlmTranslator.
- #run ⇒ Object
- #to_result ⇒ Object
Constructor Details
#initialize(text) ⇒ LlmTranslator
Returns a new instance of LlmTranslator.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/llm_translator.rb', line 132 def initialize(text) if !Dir.exists?(CONFIG_PATH) Dir.mkdir(CONFIG_PATH) end @text = text @history= File.open("#{CONFIG_PATH}/history.csv", 'a+') @url = 'https://openapi.youdao.com/api' @appkey = '0b53e81a10846a3a' @appscr = 'JLKxQO22jBft6w3PgLBh5as5iNJ1w5wS' @sign_type = 'v3' @auto = true @detail= false end |
Instance Attribute Details
#auto ⇒ Object
Returns the value of attribute auto.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def auto @auto end |
#detail ⇒ Object
Returns the value of attribute detail.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def detail @detail end |
#from ⇒ Object
Returns the value of attribute from.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def from @from end |
#history ⇒ Object
Returns the value of attribute history.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def history @history end |
#text ⇒ Object
Returns the value of attribute text.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
125 126 127 |
# File 'lib/llm_translator.rb', line 125 def to @to end |
Instance Method Details
#command(args) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/llm_translator.rb', line 172 def command(args) OptionParser.new do |parser| parser. = "用法: llm_translator [options]" parser.separator "" parser.separator "指定选项:" parser.on('-d', '--detail', '显示详细翻译结果,默认false') do self.detail = true end parser.on('-a', '--auto', '严格按照指定from和to进行,默认false') do self.auto = true end parser.on('-i TEXT', '--input=TEXT', '待翻译文本, 必填项') do |text| self.text = text end parser.on('-f FROM', '--from=FROM', '源语言') do |from| self.from = from end parser.on('-t TO', '--to=TO', '目标语言') do |to| self.to = to end parser.separator "" parser.separator "通用选项:" parser.on_tail("-h", "--help", "帮助信息") do puts parser exit end parser.on_tail("--lang", "语言编码信息") do puts LANG_CODE exit end parser.on_tail("--version", "版本信息") do puts '0.0.1' exit end end.parse!(args) if self.text.nil? puts '请输入文本' puts '-i, --input=TEXT 待翻译文本, 必填项' exit end run end |
#run ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/llm_translator.rb', line 164 def run result = to_result() history.write("#{text} #{result['translation'][0]} #{Time.now}\n") if @history return result if @detail result['translation'][0] end |
#to_result ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/llm_translator.rb', line 146 def to_result salt=SecureRandom::uuid curtime = Time.now.utc.to_i.to_s input = @text.length > 20 ? @text[0..10] + @text.length.to_s + @text[-10..-1] : @text params = { q: @text, from: @from, to: @to, appKey: @appkey, salt: salt, sign: Digest::SHA256.hexdigest(@appkey + input + salt + curtime + @appscr), signType: @sign_type, curtime: curtime, strict: !@auto } JSON.parse Net::HTTP.post_form(URI(@url), params).body end |