-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathdicts.py
More file actions
35 lines (27 loc) · 1013 Bytes
/
dicts.py
File metadata and controls
35 lines (27 loc) · 1013 Bytes
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
from typing import List, Dict, Optional
class Word:
def __init__(self, translations: Dict[str, str]):
self.translations = translations
def __eq__(self, other):
return self.translations == other.translations
def keys(self):
return list(self.translations.keys())
class Dictionary:
def __init__(
self,
languages: List[str],
words: List[Dict[str, str]],
):
self.languages = languages
self.words = [Word(translations) for translations in words]
def __eq__(self, other):
return self.languages == other.languages and self.words == other.words
def translate(self, word: str, language: Optional[str]):
if language is not None:
for word_ in self.words:
if word_.translations[language] == word:
return word_
else:
for word_ in self.words:
if word in word_.translations.values():
return word_