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 ↓.
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.
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)
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.
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 ...
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.
Time-consuming part
urllib.request.urlopen used in getXmlElementText method It seems to be urllib.request.urlopen or urllib.urlopen depending on the version of python. May the word of import change?
XML has namespace and tag search fails If you pass the tag you want to search to the find method, it will return the child element. However, if there is a namespace, it seems that you have to add the namespace to the tag.
element = root.find('.//{http://btonic.est.co.jp/NetDic/NetDicV09}' + tag)
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.
Recommended Posts