@@ -117,8 +117,8 @@ def _process(self, word: str) -> str:
117
117
:param word: Word
118
118
:return: Word without invalid chars
119
119
"""
120
- assert isinstance (word , str ), 'word must be an string'
121
- s = '' .join (i for i in word if not i .isdigit ()) # remove numbers
120
+ assert isinstance (word , str ), 'word must be a string'
121
+ s : str = '' .join (i for i in word if not i .isdigit ()) # remove numbers
122
122
if self ._tokenize : # tokenize
123
123
s = ut .tokenize (s )
124
124
s = s .lower () # lowercase
@@ -133,19 +133,18 @@ def _bsoup(self, link: str, encoding: str = 'utf-8') -> Optional['BeautifulSoup'
133
133
:param encoding: Web encoding
134
134
:return: Parsed web. None if error
135
135
"""
136
- bs_keys = list (_CACHED_SOUPS .keys ())
136
+ bs_keys : List [ str ] = list (_CACHED_SOUPS .keys ())
137
137
if link in bs_keys :
138
138
return _CACHED_SOUPS [link ]
139
139
if link in self ._test_cached_file .keys ():
140
- f = open (self ._test_cached_file [link ], encoding = 'utf8' )
141
- data = '' .join (f .readlines ())
142
- f .close ()
140
+ with open (self ._test_cached_file [link ], encoding = 'utf8' ) as f :
141
+ data = '' .join (f .readlines ())
143
142
else :
144
143
try :
145
144
data = self .__request (link , encoding )
146
145
except (urllib .error .HTTPError , ValueError ):
147
146
return None
148
- bs = BeautifulSoup (data , 'html.parser' )
147
+ bs : 'BeautifulSoup' = BeautifulSoup (data , 'html.parser' )
149
148
_CACHED_SOUPS [link ] = bs
150
149
if len (bs_keys ) >= self ._max_cached_websites :
151
150
# noinspection PyTypeChecker
@@ -182,7 +181,7 @@ def _save_bsoup(self, link: str, filename: str, encoding: str = 'utf-8') -> None
182
181
:param filename: Output file
183
182
:param encoding: Website encoding
184
183
"""
185
- bs = self ._bsoup (link , encoding )
184
+ bs : 'BeautifulSoup' = self ._bsoup (link , encoding )
186
185
with open (filename , 'w' , encoding = 'utf8' ) as out :
187
186
out .write (str (bs .prettify ()))
188
187
@@ -208,7 +207,7 @@ def __synonym_com(self, word: str, _type: str) -> SynonymType:
208
207
if bs is None :
209
208
return []
210
209
results = bs .find_all ('div' , {'class' : 'section' })
211
- en_words = []
210
+ en_words : SynonymType = []
212
211
for section in results : # Iterate each section
213
212
title = section .find_all ('h3' , {'class' : 'section-title' })
214
213
if len (title ) == 0 :
@@ -229,12 +228,12 @@ def __synonym_com(self, word: str, _type: str) -> SynonymType:
229
228
sectionlist = sectionlist [0 ]
230
229
if 'href' not in str (sectionlist ): # Not links, but words
231
230
for w in sectionlist .findAll ('li' ):
232
- wr = w .text .strip ()
231
+ wr : str = w .text .strip ()
233
232
if '(' not in wr and wr not in en_words : # Avoid onld english
234
233
en_words .append (wr )
235
234
else :
236
235
for w in sectionlist .findAll ('a' ):
237
- wr = w .text .strip ()
236
+ wr : str = w .text .strip ()
238
237
if '(' not in wr and wr not in en_words : # Avoid onld english
239
238
en_words .append (wr )
240
239
return en_words
@@ -248,14 +247,14 @@ def synonym(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Sy
248
247
:param dictionary: Dictionary to retrieve the synonyms
249
248
:return: Synonyms list
250
249
"""
251
- words = []
250
+ words : SynonymType = []
252
251
word = self ._process (word )
253
252
lang = lang .lower ()
254
253
255
254
assert dictionary in (DICT_EDUCALINGO , DICT_SYNONYMCOM , DICT_THESAURUS ), 'Unsupported dictionary'
256
255
if lang not in self ._langs .keys () or not self ._langs [lang ][0 ]:
257
256
raise InvalidLangCode (f'{ lang } code is not supported for synonyms' )
258
- if word == '' :
257
+ elif word == '' :
259
258
return words
260
259
261
260
if dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS :
@@ -313,16 +312,16 @@ def antonym(self, lang: str, word: str, dictionary: str = DICT_SYNONYMCOM) -> An
313
312
:param dictionary: Dictionary to retrieve the antonyms
314
313
:return: Synonyms list
315
314
"""
316
- words = []
315
+ words : AntonymType = []
317
316
word = self ._process (word )
318
317
319
318
assert dictionary in DICT_SYNONYMCOM , 'Unsupported dictionary'
320
319
if lang not in self ._langs .keys () or not self ._langs [lang ][3 ]:
321
320
raise InvalidLangCode (f'{ lang } code is not supported for antonyms' )
322
- if word == '' :
321
+ elif word == '' :
323
322
return words
324
323
325
- if dictionary == DICT_SYNONYMCOM and lang == 'en' :
324
+ elif dictionary == DICT_SYNONYMCOM and lang == 'en' :
326
325
en_words = self .__synonym_com (word , 'Antonyms' )
327
326
for w in en_words :
328
327
if w not in words :
@@ -378,7 +377,7 @@ def meaning(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Me
378
377
elif word == '' :
379
378
return types , words , wiki
380
379
381
- if dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS :
380
+ elif dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS :
382
381
bs = self .__search_educalingo (lang , word = word .replace (' ' , '-' ))
383
382
if bs is not None :
384
383
results = [i for i in bs .find_all ('div' , {'id' : 'cuadro_categoria_gramatical' })]
@@ -434,8 +433,7 @@ def meaning(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Me
434
433
435
434
return definitions
436
435
437
- else :
438
- raise InvalidDictionary (f'Dictionary { dictionary } cannot handle language { lang } ' )
436
+ raise InvalidDictionary (f'Dictionary { dictionary } cannot handle language { lang } ' )
439
437
440
438
def get_meanings (self , dictionary : str = DICT_EDUCALINGO ) -> List [MeaningType ]:
441
439
"""
@@ -457,9 +455,9 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
457
455
:param dictionary: Dictionary to retrieve the translations if ``to`` is empty
458
456
:return: List of (Lang tag, translated word)
459
457
"""
460
- assert isinstance (lang , str ), 'lang code must be an string'
461
- assert isinstance (to , str ), 'to lang code must be an string'
462
- words = []
458
+ assert isinstance (lang , str ), 'lang code must be a string'
459
+ assert isinstance (to , str ), 'to lang code must be a string'
460
+ words : TranslationType = []
463
461
word = self ._process (word )
464
462
465
463
assert dictionary in DICT_EDUCALINGO , 'Unsupported dictionary'
@@ -473,7 +471,7 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
473
471
if lang not in self ._langs .keys () or not self ._langs [lang ][2 ]:
474
472
raise InvalidLangCode (f'{ lang } code is not supported for translation' )
475
473
476
- if lang in _EDUCALINGO_LANGS :
474
+ elif lang in _EDUCALINGO_LANGS :
477
475
bs = self .__search_educalingo (lang , word = word .replace (' ' , '-' ))
478
476
if bs is None :
479
477
return words
@@ -488,7 +486,6 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
488
486
lang_name = lang_name [0 ].find_all ('strong' , {})
489
487
if len (lang_name ) != 1 :
490
488
continue
491
- # lang_name = lang_name[0].text.strip().capitalize()
492
489
493
490
# Find non-links
494
491
lang_nonlink = j .find_all ('span' , {'class' : 'negro' })
@@ -507,9 +504,6 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
507
504
# Sort translations
508
505
words = sorted (words , key = lambda x : x [0 ])
509
506
510
- # else:
511
- # raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
512
-
513
507
return words
514
508
515
509
def get_translations (self , to : str = '' , dictionary : str = DICT_EDUCALINGO ) -> List [TranslationType ]:
0 commit comments