Use Python for word translation

Source: Internet
Author: User

In the English literature reading, there are often some of the words do not know, but for some PDF readers, such as Foxit Reader to use the translation function needs to click (Extra->translate), and enable the translation function will not be able to use the annotation function, This will be extremely inconvenient for the reader. So in order to easily query the word, I developed a Windows environment for the Python program to monitor and query the word, and the only action is to select the word and press CTRL + C.

The Python program is divided into three parts: get the words you need to query from another application, word translation and word display.

Getting the word that needs to be queried from another application involves interprocess communication, and the simplest method on Windows is to use the Clipboard feature. There are several ways to use the Clipboard in Python, such as using Win32clipboard (http://blog.csdn.net/jackeriss/article/details/40722833). The use of the QT implementation of the CLIPBOARD,QT Clipboard implementation supports the callback when the system Clipboard content changes. Specific implementation:

# get QT Clipboard and bind callback function
Self.clipboard = Qapplication.clipboard () self.clipboard.dataChanged.connect (self.on_clipboard_changed)
    #callback function    defon_clipboard_changed (self): Data=Self.clipboard.mimeData ()ifdata.hastext (): Word=Data.text (). Strip () m= Re.match (r'[a-za-z]+', Word)ifM:self.setword (Word)#self.setwindowflags (Self.windowflags () & QtCore.Qt.WindowStaysOnTopHint)                #self.setwindowstate (Self.windowstate () & ~qtcore.qt.windowminimized | QtCore.Qt.WindowActive)trans =querywords (Word) Self.settrans (trans)" "Tip the window content has changed, but cannot move the window to the Forground" "Self.activatewindow ()Else:                Print(word)

Word translation directly calls Youdao word translation, word translation of the link format is http://dict.youdao.com/w/{}/, where {} is the word that needs to be queried. Get a single page, using BeautifulSoup to extract the final translation results.

defquerywords (word):" "use Youdao translation to query the word" "URL='http://dict.youdao.com/w/{}/'. Format (word) HTML=getURL (URL) soup= BeautifulSoup (Html.text,'Html.parser') Trans_container= Soup.find (class_='Trans-container')        if  notTrans_container:" "Not found the translation" "        return[Word] trans_li= Trans_container.find_all ('Li') Trans_data= [Li.text.strip () forLiinchTrans_li]returnTrans_data

The final display is the interface written through QT, originally wanted to query the word, the interface window automatically activates and moves to the front, but Windows does not allow the window to be moved to the front end without user action, so the end is just to prompt the user through the activation window.

The complete code is located in https://github.com/llf2017/pyDict/blob/master/pyDict.py

Use Python for word translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.