-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,37 @@ | ||
# DICT | ||
|
||
## 怎么安装 | ||
## Install | ||
|
||
- 下载 dict.py 到电脑里 | ||
```bash | ||
apt install python3 python3-pip git | ||
git clone https://github.com/YeXiaoRain/DICT.git -b python3 | ||
pip install --user requirememnts.txt | ||
|
||
```Bash | ||
curl -o dict.py https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py | ||
# edit ~/.bashrc | ||
alias y='python3 <your path>/DICT.py' | ||
# or | ||
alias g='python3 <your path>/googletranslator.py' | ||
``` | ||
|
||
- 编辑 ~/.bashrc | ||
## Usage | ||
|
||
```Bash | ||
vim ~/.bashrc | ||
``` | ||
|
||
- 在末尾加上 | ||
|
||
```Bash | ||
alias y='python3 <yourpath>/DICT.py' | ||
``` | ||
|
||
保存后执行`source ~/.bashrc`或重新开启终端可用 | ||
|
||
- 使用: | ||
|
||
```Bash | ||
```bash | ||
y 你好 | ||
y word or sentence | ||
g google 翻译可能需要proxy | ||
``` | ||
|
||
- 也可以直接执行脚本进行交互式翻译(解决 要翻译的内容带有引号或命令行特殊符号的问题): | ||
|
||
`y` | ||
|
||
- 也可以直接使用: | ||
|
||
```Bash | ||
curl -s https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py | python3 - word or sentence | ||
``` | ||
|
||
## 启用OpenapiYoudao | ||
## Enable OpenapiYoudao | ||
|
||
去`https://ai.youdao.com/`创建应用 | ||
|
||
复制`_config.ini`为`config.ini`并配置你的`APP_ID`和`APP_SECRET` | ||
|
||
如果没有配置则不会调用`OpenapiYoudao` | ||
|
||
## 其它翻译项目(如nodejs分支) | ||
## Deps | ||
|
||
|repo|source|comment|modified| | ||
|---|---|---|---| | ||
|[command-line-tool/dictionary](https://github.com/command-line-tool/dictionary)|youdaoapi|不支持句子|youdaodict| | ||
|[syaning/dict-en-zh](https://github.com/syaning/dict-en-zh)|youdao网页/shanbayapi|shanbay源比较垃圾|-| | ||
|[Toybreak/cliDict](https://github.com/Toybreak/cliDict)|bing|没有中->英 不支持句子|bingdict| | ||
|[justinleoye/tuzki-dict](https://github.com/justinleoye/tuzki-dict)||chrome插件|-| | ||
- ~~<http://fanyi.youdao.com>~~ | ||
- yodao.com | ||
- <https://openapi.youdao.com/api> | ||
- googletrans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# write by chatgpt3.5 | ||
import argparse | ||
from googletrans import Translator | ||
|
||
def google_translator(original_text): | ||
def detect_language(text): | ||
translator = Translator() | ||
result = translator.detect(text) | ||
return result.lang | ||
|
||
def translate_text(text, target_language='en'): | ||
translator = Translator() | ||
translation = translator.translate(text, dest=target_language) | ||
return translation.text | ||
# Detect source language | ||
source_language = detect_language(original_text) | ||
print(f"Source Language: {source_language}") | ||
|
||
# Translate text | ||
if source_language == 'zh-CN': | ||
target_language = 'en' | ||
else: | ||
target_language = 'zh-CN' | ||
|
||
translated_text = translate_text(original_text, target_language) | ||
print(f"Google Translation: {translated_text}") | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Translate text between Chinese and English.') | ||
parser.add_argument('text', nargs='+', help='The text to translate, multiple words are accepted.') | ||
|
||
args = parser.parse_args() | ||
original_text = ' '.join(args.text) | ||
google_translator(original_text) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
googletrans==4.0.0-rc1 |