Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
修复有的词获取不到 defination 的问题 (#75)
Browse files Browse the repository at this point in the history
* 修复有的词获取不到 defination 的问题

* fix lost spaces in sentences
  • Loading branch information
wd authored and megachweng committed Nov 15, 2019
1 parent 457d544 commit c8f2f52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
26 changes: 22 additions & 4 deletions addon/queryApi/eudict.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ def __init__(self, html, term):

@property
def definition(self) -> list:
els = self._soap.select('div #ExpFCChild li') # 多词性
els = self._soap.select('div #ExpFCChild .exp') if not els else els # 单一词性
ret = []
div = self._soap.select('div #ExpFCChild')
if not div:
return ret

div = div[0]
els = div.select('li') # 多词性
if not els: # 单一词性
els = div.select('.exp')
if not els: # 还有一奇怪的情况,不在任何的标签里面
trans = div.find(id='trans')
trans.replace_with('') if trans else ''

script = div.find('script')
script.replace_with('') if script else ''

for atag in div.find_all('a'): # 赞踩这些字样
atag.replace_with('')
els = [div]

for el in els:
ret.append(el.get_text(strip=True))

return ret

@property
Expand Down Expand Up @@ -88,7 +106,7 @@ def sentence(self) -> list:
for el in els:
try:
line = el.select('p')
sentence = line[0].get_text(strip=True)
sentence = "".join([ str(c) for c in line[0].contents])
sentence_translation = line[1].get_text(strip=True)
ret.append((sentence, sentence_translation))
except KeyError as e:
Expand Down Expand Up @@ -152,7 +170,7 @@ def query(cls, word) -> dict:
queryResult = None
try:
rsp = cls.session.get(cls.url.format(word), timeout=cls.timeout)
logger.debug(f'code:{rsp.status_code}- word:{word} text:{rsp.text}')
logger.debug(f'code:{rsp.status_code}- word:{word} text:{rsp.text[:100]}')
queryResult = cls.parser(rsp.text, word).result
except Exception as e:
logger.exception(e)
Expand Down
15 changes: 14 additions & 1 deletion test/test_queryapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ def test_eudict_with_none():
ret = check_result(res)
assert set(ret) - set(keys) == set()

def test_eudict_implication_all():
def test_eudict_implication():
# 不包含图片,定义不在正常规则内,包含 trans
res = api.query('implication')
ret = check_result(res)
assert set(ret) - set(['image']) == set()

def test_eudict_epitomize():
# 不包含图片,定义不在正常规则内
res = api.query('epitomize')
ret = check_result(res)
assert set(ret) - set(['image', 'phrase']) == set()

def test_eudict_periodical():
# 包含图片,定义不在正常规则内
res = api.query('periodical')
ret = check_result(res)
assert set(ret) - set(['image', 'phrase']) == set()

0 comments on commit c8f2f52

Please sign in to comment.