geekdict 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9a7876ba4eddfb0497523ab7f318588fc1ef1abf754b5f75771b0f2bbc29092
4
- data.tar.gz: 460d3c363a9622fec12bb463854239355293827327d7d7639338c306a36f99a3
3
+ metadata.gz: 584fbc9ba64e0e69f1a8cc5a55660b2b9d5a6b229d560972da8abd88a582f778
4
+ data.tar.gz: 74ddd2908ffb00c437bae03b479953d495b8f798766ba2a27519695463352c41
5
5
  SHA512:
6
- metadata.gz: a66b0934455eebc3bc5c511510abc261d7c8ddb59cc91f4ea66b4b30ee6b8d709f39451e62d4674731db803ec88f105c064c8722b56b9e073abd0c38597b8c1a
7
- data.tar.gz: 64d730010c52a0e5c7a98b169ff7fef55ed34eab4863b32781aefd18889ffa946cab96dbc5d33516668488d87ddb3be830a111a7b75092e552c3fc29f2d106f2
6
+ metadata.gz: fa89f1efbaa00051e2ad0da3f5c3963207ae3e20023d43e6afff705ea878b2aa61b42cbe050729811e2325744fe98679b289e93ec5dd6652fcf4514d75d2b988
7
+ data.tar.gz: 5e0548e8bbad29b21f6a5cfd2cda6acf438aac5092d15f6d3baec9fa606fe9ca6bcbb503bdb39782e118fc4d36e58c0ab8daa6d5449a43f154ef15a87fcc79c1
data/README.md CHANGED
@@ -12,18 +12,36 @@ You can install via rubygems:
12
12
 
13
13
  Configure
14
14
  --------
15
- This translate uses OpenAI's GPT-3.5. Ensure you have `OPENAI_API_KEY` configured as environment variable.
15
+ GeekDict supports multiple translation providers:
16
+
17
+ 1. **OpenRouter** (default): Uses the openai/o3-mini model through OpenRouter. Set the `OPENROUTER_API_KEY` environment variable.
18
+ 2. **OpenAI**: Uses OpenAI's GPT-3.5. Set the `OPENAI_API_KEY` environment variable.
19
+ 3. **Youdao**: Uses Youdao translation API. Configure in `~/.geekdict.yml`.
16
20
 
17
21
 
18
22
  Commands
19
23
  --------
20
24
  ### Translate a word
21
25
 
26
+ # Using the default OpenRouter provider
22
27
  $geekdict t test
23
- n. 试验;检验
24
- vt. 试验;测试
25
- vi. 试验;测试
26
- n. (Test)人名;(英)特斯特
28
+
29
+ # Specify a provider
30
+ $geekdict t test -p openai
31
+ $geekdict t test --provider=youdao
32
+
33
+ # Example output
34
+ 测试 (cè shì)
35
+
36
+ Explanation:
37
+ The word "测试" in Chinese means "test" in English. It refers to the process of evaluating or examining something to determine its quality, performance, or knowledge. It can be used in various contexts, such as academic exams, software testing, or product quality testing.
38
+
39
+ Example:
40
+ 1. 我们明天有一场数学测试。
41
+ We have a math test tomorrow.
42
+
43
+ 2. 这个软件需要经过严格的测试才能发布。
44
+ This software needs to undergo rigorous testing before it can be released.
27
45
 
28
46
  Command Help
29
47
  ------------
@@ -41,6 +59,7 @@ Use *help* command to get detail information.
41
59
  Options:
42
60
  -d, [--debug], [--no-debug]
43
61
  -o, [--open], [--no-open]
62
+ -p, [--provider=PROVIDER] # Translation provider to use (openrouter, openai, youdao)
44
63
 
45
64
  Translate a word
46
65
 
data/lib/geekdict/cli.rb CHANGED
@@ -2,18 +2,32 @@ require "thor"
2
2
  require_relative 'local_history'
3
3
  require_relative 'version'
4
4
  require_relative 'openai/gpt.rb'
5
+ require_relative 'openrouter/api.rb'
5
6
 
6
7
  module GeekDict
7
8
 
8
9
  class CLI < Thor
9
10
 
10
11
  desc "t", "Translate a word"
11
- option :debug, :aliases=>'-d', :type=>:boolean,:default=>false
12
- option :open, :aliases => '-o', :type => :boolean, :default=> false
12
+ option :debug, :aliases=>'-d', :type=>:boolean, :default=>false
13
+ option :open, :aliases=>'-o', :type=>:boolean, :default=>false
14
+ option :provider, :aliases=>'-p', :type=>:string, :default=>'openrouter', :enum=>['openai', 'openrouter', 'youdao'], :desc=>"Translation provider to use"
13
15
  def t(word)
14
16
  GeekDict.debugger options[:debug]
15
17
  LocalHistory.save word
16
- result = GeekDict::OpenAI.translate word
18
+
19
+ provider = options[:provider].downcase
20
+ result = case provider
21
+ when 'openai'
22
+ GeekDict::OpenAI.translate(word)
23
+ when 'openrouter'
24
+ GeekDict::OpenRouter.translate(word)
25
+ when 'youdao'
26
+ GeekDict::Youdao.translate(word)
27
+ else
28
+ GeekDict::OpenRouter.translate(word)
29
+ end
30
+
17
31
  puts result
18
32
  end
19
33
 
@@ -25,7 +25,24 @@ module GeekDict
25
25
 
26
26
  def system_prompt(word)
27
27
  <<-EOS
28
- You are helpful and enthusiastic language translator. If the word or sentense is Chinese, translate it into English. If it is English, translate into Chinese. If the word is incorrectly spelled or it's not a correct word in English or Chinese, try your best to find the closest word. In addition to the translation, offer a comprehensive explanation of the word along with one or two examples to illustrate its usage. Do NOT add Pinyin in the example sentences for Chinese.
28
+ You are a precise language translator specializing in English-Chinese translation. Follow these guidelines:
29
+
30
+ 1. If the input is Chinese, translate to English. If English, translate to Chinese.
31
+ 2. For words with multiple meanings, prioritize the most common usage first.
32
+ 3. For misspelled or incorrect words, suggest the closest correct word.
33
+ 4. For idiomatic expressions, provide both literal and figurative translations.
34
+ 5. For technical terms, include the field/domain where appropriate.
35
+
36
+ Format your response as follows:
37
+ - Translation: [primary translation]
38
+ - Explanation: [concise explanation of meaning and usage]
39
+ - Examples:
40
+ 1. [example sentence in target language]
41
+ [translation in source language]
42
+ 2. [second example if helpful]
43
+ [translation in source language]
44
+
45
+ Keep explanations clear and concise. Do NOT include pinyin in Chinese text.
29
46
  EOS
30
47
  end
31
48
  end
@@ -0,0 +1,63 @@
1
+ require 'json'
2
+ require 'httpclient'
3
+
4
+ module GeekDict
5
+ module OpenRouter
6
+ module_function
7
+
8
+ def translate(word)
9
+ @debugger = GeekDict.debugger
10
+
11
+ client = HTTPClient.new
12
+ headers = {
13
+ 'Content-Type' => 'application/json',
14
+ 'Authorization' => "Bearer #{ENV.fetch('OPENROUTER_API_KEY')}"
15
+ }
16
+
17
+ body = {
18
+ model: 'openai/o3-mini',
19
+ messages: [
20
+ { role: "system", content: system_prompt(word) },
21
+ { role: "user", content: word }
22
+ ],
23
+ temperature: 0.2
24
+ }
25
+
26
+ response = client.post(
27
+ 'https://openrouter.ai/api/v1/chat/completions',
28
+ body.to_json,
29
+ headers
30
+ )
31
+
32
+ if response.status == 200
33
+ result = JSON.parse(response.body)
34
+ result.dig("choices", 0, "message", "content")
35
+ else
36
+ "Error: Failed to get translation (#{response.status})"
37
+ end
38
+ end
39
+
40
+ def system_prompt(word)
41
+ <<-EOS
42
+ You are a precise language translator specializing in English-Chinese translation. Follow these guidelines:
43
+
44
+ 1. If the input is Chinese, translate to English. If English, translate to Chinese.
45
+ 2. For words with multiple meanings, prioritize the most common usage first.
46
+ 3. For misspelled or incorrect words, suggest the closest correct word.
47
+ 4. For idiomatic expressions, provide both literal and figurative translations.
48
+ 5. For technical terms, include the field/domain where appropriate.
49
+
50
+ Format your response as follows:
51
+ - Translation: [primary translation]
52
+ - Explanation: [concise explanation of meaning and usage]
53
+ - Examples:
54
+ 1. [example sentence in target language]
55
+ [translation in source language]
56
+ 2. [second example if helpful]
57
+ [translation in source language]
58
+
59
+ Keep explanations clear and concise. Do NOT include pinyin in Chinese text.
60
+ EOS
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module GeekDict
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/geekdict.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'geekdict/cli'
2
2
  require 'geekdict/debugger'
3
3
  require 'geekdict/youdao/api'
4
+ require 'geekdict/openai/gpt'
5
+ require 'geekdict/openrouter/api'
4
6
 
5
7
  module GeekDict
6
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geekdict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wenbing Li
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-01 00:00:00.000000000 Z
11
+ date: 2025-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -113,6 +113,7 @@ files:
113
113
  - lib/geekdict/debugger.rb
114
114
  - lib/geekdict/local_history.rb
115
115
  - lib/geekdict/openai/gpt.rb
116
+ - lib/geekdict/openrouter/api.rb
116
117
  - lib/geekdict/version.rb
117
118
  - lib/geekdict/youdao/api.rb
118
119
  - lib/geekdict/youdao/config.rb
@@ -123,7 +124,7 @@ homepage: https://github.com/wbinglee/geekdict
123
124
  licenses:
124
125
  - MIT
125
126
  metadata: {}
126
- post_install_message:
127
+ post_install_message:
127
128
  rdoc_options: []
128
129
  require_paths:
129
130
  - lib
@@ -138,8 +139,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  - !ruby/object:Gem::Version
139
140
  version: '0'
140
141
  requirements: []
141
- rubygems_version: 3.3.26
142
- signing_key:
142
+ rubygems_version: 3.3.7
143
+ signing_key:
143
144
  specification_version: 4
144
145
  summary: A command line tool for translation.
145
146
  test_files: