Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into ib-fix-readline
Browse files Browse the repository at this point in the history
  • Loading branch information
wdv4758h committed Feb 6, 2017
2 parents 388e50e + 1bdd1f8 commit 87057f5
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
100 changes: 100 additions & 0 deletions zdict/dictionaries/moe.py
@@ -1,4 +1,5 @@
import json
import unicodedata # to detect Unicode category

from zdict.dictionary import DictBase
from zdict.exceptions import QueryError, NotFoundError
Expand Down Expand Up @@ -96,3 +97,102 @@ def query(self, word: str):
)

return record


def is_other_format(char):
return unicodedata.category(char) != 'Cf'


def remove_cf(data):
return ''.join(filter(is_other_format, data))


def clean(data, clean_cf=False):
'''
Clean the word segmentation
remove "`~" and things in Unicode 'Cf' category
'''
data = data.translate(str.maketrans('', '', '`~'))
if clean_cf:
return remove_cf(data)
else:
return data


class MoeDictTaiwanese(DictBase):

API = 'https://www.moedict.tw/t/{word}.json'

@property
def provider(self):
return 'moe-taiwanese'

@property
def title(self):
return '萌典(臺)'

def _get_url(self, word) -> str:
return self.API.format(word=word)

def show(self, record: Record):
content = json.loads(record.content)

# print word
self.color.print(clean(content.get('t', '')), 'yellow')

for word in content.get('h', ''):

# print pronounce
for key, display in (
# TODO: where is bopomofo ?
('T', '臺羅拼音'), # Tailo
):
self.color.print(display, end='')
self.color.print(
'[' + word.get(key, '') + ']',
'lwhite',
end=' ',
)

print()
print()

# print explain
for count, explain in enumerate(word.get('d', '')):

self.color.print('{order}. '.format(order=count+1), end='')
type = clean(explain.get('type', ''))
if type:
self.color.print(
'[' + type + ']',
'lgreen',
end=' ',
)

self.color.print(clean(explain.get('f', '')), end='')

for example in explain.get('e', ''):
self.color.print(
clean(example, True),
'indigo',
indent=2,
)

print()

print()

def query(self, word: str):
try:
content = self._get_raw(word)
except QueryError as exception:
raise NotFoundError(exception.word)

record = Record(
word=word,
content=content,
source=self.provider,
)

return record
51 changes: 50 additions & 1 deletion zdict/tests/dictionaries/test_moe.py
@@ -1,7 +1,7 @@
from pytest import raises
from unittest.mock import Mock, patch

from zdict.dictionaries.moe import MoeDict
from zdict.dictionaries.moe import MoeDict, MoeDictTaiwanese
from zdict.exceptions import NotFoundError, QueryError
from zdict.models import Record
from zdict.zdict import get_args
Expand Down Expand Up @@ -60,3 +60,52 @@ def test_show(self):

# god bless this method, hope that it do not raise any exception
self.dict.show(r)


class TestMoeDictTaiwanese:
def setup_method(self, method):
self.dict = MoeDictTaiwanese(get_args())

def teardown_method(self, method):
del self.dict

def test__get_url(self):
assert 'https://www.moedict.tw/t/木耳.json' == self.dict._get_url('木耳')

def test_provider(self):
assert self.dict.provider == 'moe-taiwanese'

def test_query_timeout(self):
self.dict._get_raw = Mock(side_effect=QueryError('木耳', 404))

with raises(NotFoundError):
self.dict.query('木耳')

self.dict._get_raw.assert_called_with('木耳')

@patch('zdict.dictionaries.moe.Record')
def test_query_normal(self, Record):
self.dict._get_raw = Mock(return_value='{}')
self.dict.query('木耳')
Record.assert_called_with(word='木耳',
content='{}',
source='moe-taiwanese')

def test_show(self):
content = '''
{
"h": [{
"T": "bo̍k-ní",
"_": "928",
"d": [{
"f": "蕈`菇~`類~。`生長~`在~`朽~`腐~`的~`樹~`幹~`上~ ...",
"type": "`名~"
}]
}],
"t": "`木~`耳~"
}
'''
r = Record(word='木耳', content=content, source=self.dict.provider)

# god bless this method, hope that it do not raise any exception
self.dict.show(r)

0 comments on commit 87057f5

Please sign in to comment.