I made a package like Weblio pop-up English-Japanese dictionary with Sublime Text3

The Weblio pop-up English-Japanese dictionary was convenient, so I thought it would be nice if the text editor had such a function.

Weblio pop-up English-Japanese dictionary displays dictionary search results like ↓. weblioポップアップ英和辞典.png
In the above example, the word you want to search is inverted, but there is also a setting to display it only by mouse over.

Aimed function

It turned out to be something like this.

PopTranslateEnglishIntoJapaneseの見た目.png

environment

Referenced site

code

PopTranslateEnglish.py


# coding=UTF-8
import sublime, sublime_plugin
import urllib.request
import xml.etree.ElementTree as ET

class popTranslateEnglishIntoJapaneseCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        search_word = self.view.substr(self.view.sel()[0])
        if search_word == "":
            print('none select word')
            return
        else:
            print(search_word)

        item_id = self.getItemID(search_word)
        text = self.getTranslatedText(item_id)
        if text == '':
            return

        text_arr = self.splitTranslatedText(text, '\t')
        self.view.show_popup_menu(text_arr, None)


    def getXmlElementText(self, url, tag):
        print('url : ' + url)
        try:
            xml = urllib.request.urlopen(url)
        except urllib.error.HTTPError as e:
            print('error code : ' + str(e.code))
            print('error read : ' + str(e.read()))
            return ''

        print(xml)
        tree = ET.parse(xml)
        root = tree.getroot()
        element = root.find('.//{http://btonic.est.co.jp/NetDic/NetDicV09}' + tag)
        text = element.text
        print(text)
        return text

    def getItemID(self, search_word):
        head = 'http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite?Dic=EJdict&Word='
        end = '&Scope=HEADWORD&Match=EXACT&Merge=OR&Prof=XHTML&PageSize=20&PageIndex=0'
        url = head + search_word + end
        return self.getXmlElementText(url, 'ItemID')

    def getTranslatedText(self, item_id):
        head = 'http://public.dejizo.jp/NetDicV09.asmx/GetDicItemLite?Dic=EJdict&Item='
        end = '&Loc=&Prof=XHTML'
        url = head + item_id + end
        return self.getXmlElementText(url, 'Body/div/div')

    def splitTranslatedText(self, translated_text, split_word):
        return translated_text.split(split_word)

What did you do?

  1. First, search for how to make a package Learn the basic method of making at the link below. [Since I started using sublime-text3, I also made a plugin](http://wood-roots.com/web%E5%88%B6%E4%BD%9C/%E3%83%84%E3% 83% BC% E3% 83% AB% E3% 82% A8% E3% 83% 87% E3% 82% A3% E3% 82% BF / sublime-text3% E3% 82% 92% E4% BD% BF% E3% 81% 84% E3% 81% AF% E3% 81% 98% E3% 82% 81% E3% 81% 9F% E3% 81% AE% E3% 81% A7% E3% 83% 97% E3% 83% A9% E3% 82% B0% E3% 82% A4% E3% 83% B3% E3% 82% 82% E4% BD% 9C% E3% 81% A3% E3% 81% A6% E3% 81% BF% E3% 81% 9F) By the way, I feel like I understand the description of python.

  2. How to translate ... At first I searched for Weblio's API but couldn't find it. I discovered that it seems that I can translate it at the link below. Sample Ruby code that calls the English-Japanese / Japanese-English dictionary API dejizo Apparently, the XML of the translation result is returned by describing the English words and parameters you want to search in the URL like a function. It feels like parsing the returned XML and getting the translation result. However, since the sample is Ruby, how to realize it with python ...

  3. urllib and xml.etree.ElementTree It seems that it can be realized by using these two libraries. You can get the data when accessing the URL with urllib, this time you can get the XML file. The flow of parsing the XML acquired by xml.etree.ElementTree. You can find out how to use each library by google.

  4. Time-consuming part

I'm getting the element here, but the part enclosed in'{}' is the namespace. For details, go to Working with XML with namespaces in Python (Element Tree) By the way, if you add'.//', you can search not only the child element but also the grandchild element and beyond.

  1. Finally I made the package for the first time, but it was as easy as possible in a day. I have registered GitHub but there is no repository. I will make it when I feel like it.

Recommended Posts

I made a package like Weblio pop-up English-Japanese dictionary with Sublime Text3
I made a python text
I made a package to filter time series with python
I made a puzzle game (like) with Tkinter in Python
I made a fortune with Python.
I made a daemon with Python
I made a package that can compare morphological analyzers with Python
W3C Validators didn't work with Sublime Text3 so I made it work
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a conversation partner like Siri
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Create a python3 build environment with Sublime Text3
[Python] I made a Youtube Downloader with Tkinter.
I made a simple Bitcoin wallet with pycoin
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a QR code image with CuteR
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
I made a ready-to-use syslog server with Play with Docker
I made a Christmas tree lighting game with Python
I made a vim learning game "PacVim" with Go
I made a window for Log output with Tkinter
I made a net news notification app with Python
I made a LINE BOT with Python and Heroku
I made a repeating text data generation tool "rpttxt"
I made a falling block game with Sense HAT
Set up a Python development environment with Sublime Text 2
〇✕ I made a game
Pythonbrew with Sublime Text
A story that stumbled when I made a chatbot with Transformer
I made a simple typing game with tkinter in Python
I made a LINE BOT that returns parrots with Go
I made a simple book application with python + Flask ~ Introduction ~
I made a resource monitor for Raspberry Pi with a spreadsheet
I want to install a package from requirements.txt with poetry
I made a rigid Pomodoro timer that works with CUI
I made a surveillance camera with my first Raspberry PI.
[AWS] I made a reminder BOT with LINE WORKS (implementation)
I made blackjack with python!
I made a discord bot
I made COVID19_simulator with JupyterLab
I made Word2Vec with Pytorch
I made wordcloud with Python.
I made a simple circuit with Python (AND, OR, NOR, etc.)