歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

linux下方便的使用有道詞典

直接上圖,覺得合適就用,想查單詞時,復制單詞粘貼到終端,非常方便。不要騙自己說你想知道音標,想知道詳細的例句,大多時候我們是只需要快速知道這個單詞的意思。實現方法是大牛實現的,使用python調用有道的接口,但我忘記出處了,抱歉!

我也折騰過有道詞典的安裝,後來安裝好了,但體驗非常差,之前是占用資源巨多,後來換了個高配的筆記本,還是感覺不好用。奉勸在折騰的同學一句,不要在折騰有道詞典的安裝。ubuntu上安裝還挺方便的,centos上安裝這詞典真心不容易。我現在直接使用終端查單詞,浏覽器直接使用有道的翻譯插件。挺方便的!

youdao終端查單詞其實很簡單,就幾行python代碼,我直接貼上來吧,復制代碼並在/usr/local/bin下建個可執行文件就可以使用了。

[code]sudo gedit  /usr/local/bin/dict.py
粘貼代碼:

[code]    #! /usr/bin/python  
import re;  
import urllib;  
import urllib2;  
import sys;  
def debug():  
    xml = open("word.xml").read();  
    print get_text(xml);  
    print get_elements_by_path(xml, "custom-translation/content");  
    #print_translations(xml, False, False);  

def get_elements_by_path(xml, elem):  
    if type(xml) == type(''):  
        xml = [xml];  
    if type(elem) == type(''):  
        elem = elem.split('/');  
    if (len(xml) == 0):  
        return [];  
    elif (len(elem) == 0):  
        return xml;  
    elif (len(elem) == 1):  
        result = [];  
        for item in xml:  
            result += get_elements(item, elem[0]);  
        return result;  
    else:  
        subitems = [];  
        for item in xml:  
            subitems += get_elements(item, elem[0]);  
        return get_elements_by_path(subitems, elem[1:]);  

textre = re.compile("<\!\[CDATA\[(.*?)\]\]", re.DOTALL);  
def get_text(xml):  
    #print 10*"..."+"\n"
    match = re.search(textre, xml);  
    if not match:  
        return xml;  
    #print 40*"..."
    #print match
    #print 40*"..."
    return match.group(1);  

def get_elements(xml, elem):  
    p = re.compile("<" + elem + ">" + "(.*?)</" + elem + ">", re.DOTALL);  
    it = p.finditer(xml);  
    result = [];  
    for m in it:  
        result.append(m.group(1));  
    return result;  

GREEN = "\033[1;32m";  
DEFAULT = "\033[0;49m";  
BOLD = "\033[1m";  
UNDERLINE = "\033[4m";  
NORMAL = "\033[m";  
RED = "\033[1;31m"  

def crawl_xml(queryword):  
    return urllib2.urlopen("http://dict.yodao.com/search?keyfrom=dict.python&q=" + urllib.quote_plus(queryword) + "&xmlDetail=true&doctype=xml").read();  

def print_translations(xml, with_color, detailed):  
    #print xml;  
    original_query = get_elements(xml, "original-query");  
    queryword = get_text(original_query[0]);  
    custom_translations = get_elements(xml, "custom-translation");  
    print BOLD + UNDERLINE + queryword + NORMAL;  
    translated = False;  

    for cus in custom_translations:  
        source = get_elements_by_path(cus, "source/name");  

        print RED + "Translations from " + source[0] + DEFAULT;  
        contents = get_elements_by_path(cus, "translation/content");  
        if with_color:  
            for content in contents[0:5]:  
                print GREEN + get_text(content) + DEFAULT;  
        else:  
            for content in contents[0:5]:  
                print get_text(content);  
        translated = True;  
    yodao_translations = get_elements(xml, "yodao-web-dict");  
    printed = False;  
    for trans in yodao_translations:  
        webtrans = get_elements(trans, "web-translation");  
        for web in webtrans[0:5]:  
            if not printed:  
                print RED + "Translations from yodao:" + DEFAULT;  
                printed = True;  
                keys = get_elements(web, "key");  
            values = get_elements_by_path(web, "trans/value");  
            summaries = get_elements_by_path(web, "trans/summary");  
            key = keys[0].strip();  
            value = values[0].strip();  
            #summary = summaries[0].strip();  
                #lines = get_elements(summary, "line");  
            if with_color:  
                print BOLD +  get_text(key) + ":\t" +DEFAULT + GREEN + get_text(value) + NORMAL;  
                #for line in lines:  
                #    print GREEN + get_text(line) + DEFAULT;  
                #print get_text(summary) + DEFAULT;  
            else:  
                print get_text(value);  
                #print get_text(summary);  
                    #translated = True;  
                    #if not detailed:  
                    #        break  

def usage():  
    print "usage: dict.py word_to_translate";  
def main(argv):  
    if len(argv) <= 0:  
        usage();  
        #debug();  
        sys.exit(1);  
    xml = crawl_xml("".join(argv));  
    print_translations(xml, True, False);  
    #print_translations(xml, True, True);  
    #print_translations(xml, False, False);  

if __name__ == "__main__":  
    main(sys.argv[1:]);
gedit /usr/local/bin/youdao

粘貼如下代碼:

[code]#!/bin/bash
python /usr/local/bin/dict.py $1;
echo -n "input:";
while read input;
do
    python /usr/local/bin/dict.py ${input};
    echo -n "input:";
done

最後一步,給文件添加可執行權限:

[code] sudo chmod + x /usr/local/bin/dict.py
 sudo chmod + x /usr/local/bin/youdao
隨便打開一個終端做個測試,輸入:

[code]youdao  english
看是不是可以正常查詞啦!

祝好運!!

Copyright © Linux教程網 All Rights Reserved