forked from peterdocter/mdict-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdict.py
58 lines (46 loc) · 1.73 KB
/
mdict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python
import sys
from bs4 import BeautifulSoup
import subprocess
class Mdict:
def __init__(self, word):
self.word = word
p = subprocess.Popen(['bin/dicttool', '-e', word],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
self._soup = BeautifulSoup(out, "lxml")
p_zh = subprocess.Popen(['bin/dicttool', '-z', word],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out_zh, err_zh = p_zh.communicate()
self._soup_zh = BeautifulSoup(out_zh, "lxml")
@property
def IPAs(self):
return [item.text.strip() for item in self._soup.find("span", "hg").findAll("span", {"d:pr": "US_IPA"})]
@property
def etym(self):
res = self._soup.find("span", "etym")
return(res.text if res else None)
@property
def derivatives(self):
return [item.find("span", "l").text.strip() for item in self._soup.findAll("span", "subEntry")]
@property
def roots(self):
return [item.text.strip() for item in self._soup.findAll("span", "ff")]
@property
def origin(self):
return [item.text.strip() for item in self._soup.find("span", "etym").findAll("span", "la")]
@property
def definitions_zh(self):
return [item.find("span", "trans").text.strip() for item in self._soup_zh.findAll("span", "trg")]
def printword(w):
print(w.word)
print("|{}|".format(','.join(w.IPAs)))
for i, k in enumerate(w.definitions_zh):
print(i+1, ".", k)
print("\n{}\n".format(w.etym))
if __name__ == '__main__':
word = sys.argv[1]
q = Mdict(word)
printword(q)