-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoe.py
140 lines (129 loc) · 5.38 KB
/
moe.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf8 -*-
import re
import requests
# copied from stackoverflow
HANUNI = re.compile(ur'^[⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]+$', re.UNICODE)
def guoyu(uid, txt):
print u'UID %s 查國語萌典: %s' % (uid, txt)
r = 'undefined'
if HANUNI.match(txt):
get = requests.get('https://www.moedict.tw/a/%s.json' % txt)
if get.status_code == 200:
j = get.json()
if 'r' in j and 'n' in j:
r = u'%s (%s部%d劃)\n' % (stripWordSeg(j['t']), stripWordSeg(j['r']), j['n'])
else:
r = stripWordSeg(j['t']) + '\n'
for h in j['h']:
i = 1
if 'b' in h and 'p' in h:
r = r + u'%s %s\n' % (h['b'], h['p'])
for d in h['d']:
if 'type' in d:
word_class = u'[%s詞]' % stripWordSeg(d['type'])
else:
word_class = ''
r = r + '%d. %s %s\n' % (i, word_class, stripWordSeg(d['f']))
if 'e' in d:
for ex in d['e']:
r = r + u' %s\n' % stripWordSeg(ex)
i = i + 1
if 's' in h:
r = r + u'相似詞: %s' % stripWordSeg(h['s'])
return r
elif get.status_code == 404:
return u'查無此字。'
else:
app.logger.warn(str(get.status_code))
app.logger.warn(str(get.text))
return u'系統錯誤,請稍候再試。'
else:
return u'查詢字串內含非漢字的字元,請重新輸入。'
return r
def taigi(uid, txt):
print u'UID %s 查台語萌典: %s' % (uid, txt)
r = 'undefined'
if HANUNI.match(txt):
get = requests.get('https://www.moedict.tw/t/%s.json' % txt)
if get.status_code == 200:
j = get.json()
if 'r' in j and 'n' in j:
r = u'%s (%s部%d劃)\n' % (stripWordSeg(j['t']), stripWordSeg(j['r']), j['n'])
else:
r = stripWordSeg(j['t']) + '\n'
for h in j['h']:
i = 1
reading = stripWordSeg(h.get('reading', u'發'))
if 'T' in h:
r = r + u'%s音: %s\n' % (reading, h['T'])
for d in h['d']:
if 'type' in d:
word_class = u'[%s詞] ' % stripWordSeg(d['type'])
else:
word_class = ''
r = r + '%d. %s%s\n' % (i, word_class, stripWordSeg(d['f']))
if 'e' in d:
for ex in d['e']:
r = r + u'%s\n' % renderMoeExample(stripWordSeg(ex))
i = i + 1
if 's' in h:
r = r + u'相似詞: %s' % stripWordSeg(h['s'])
return r
# MP3 in https://1763c5ee9859e0316ed6-db85b55a6a3fbe33f09b9245992383bd.ssl.cf1.rackcdn.com/04208.mp3
# j['h'][0]['_'] left pad 0 to 5 digits
elif get.status_code == 404:
return u'查無此字。'
else:
app.logger.warn(str(get.status_code))
app.logger.warn(str(get.text))
return u'系統錯誤,請稍候再試。'
else:
return u'查詢字串內含非漢字的字元,請重新輸入。'
return
def hakkafa(uid, txt):
print u'UID %s 查客語萌典: %s' % (uid, txt)
r = 'undefined'
if HANUNI.match(txt):
get = requests.get('https://www.moedict.tw/h/%s.json' % txt)
if get.status_code == 200:
j = get.json()
r = stripWordSeg(j['t']) + '\n'
for h in j['h']:
i = 1
reading = stripWordSeg(h.get('reading', u'發'))
if 'p' in h:
r = r + h['p'].replace(u'\u20de', '') + '\n' # 不要四方框
for d in h['d']:
if 'type' in d and d['type'] != '':
word_class = u'[%s詞] ' % stripWordSeg(d['type'])
else:
word_class = ''
r = r + '%d. %s%s\n' % (i, word_class, stripWordSeg(d['f']))
if 'e' in d:
for ex in d['e']:
r = r + u'%s\n' % renderMoeExample(stripWordSeg(ex))
i = i + 1
if 's' in h:
r = r + u'相似詞: %s' % stripWordSeg(h['s'])
return r
# MP3 in https://1763c5ee9859e0316ed6-db85b55a6a3fbe33f09b9245992383bd.ssl.cf1.rackcdn.com/04208.mp3
# j['h'][0]['='] left pad 0 to 5 digits
elif get.status_code == 404:
return u'查無此字。'
else:
pprint.pprint(txt)
app.logger.warn(str(get.status_code))
app.logger.warn(str(get.text))
return u'系統錯誤,請稍候再試。'
else:
return u'查詢字串內含非漢字的字元,請重新輸入。'
return
def renderMoeExample(s):
r = s.replace(u'\ufff9', u' ') \
.replace(u'\ufffa', u'\n ') \
.replace(u'\ufffb', u'\n (') + ')'
return r.replace(u'\n ()', '') # XXX: Dirty Hack
def stripWordSeg(s):
#TAG_RE = re.compile(r'<[^>]+>')
#return TAG_RE.sub('', s)
return s.replace('`', '').replace('~', '')