alessandro trinca tornidor
commited on
Commit
·
3e4106b
1
Parent(s):
0b98421
refactor: refactor delete_entry() to reduce its complexity
Browse files
my_ghost_writer/custom_synonym_handler.py
CHANGED
|
@@ -21,7 +21,6 @@ class CustomSynonymHandler:
|
|
| 21 |
if relation_type not in self.lexicon[word]:
|
| 22 |
self.lexicon[word][relation_type] = []
|
| 23 |
self.lexicon[word][relation_type].append(group)
|
| 24 |
-
# Update inverted index
|
| 25 |
for w in group["words"]:
|
| 26 |
if w not in self.inverted_index:
|
| 27 |
self.inverted_index[w] = set()
|
|
@@ -34,13 +33,16 @@ class CustomSynonymHandler:
|
|
| 34 |
# Remove from inverted index
|
| 35 |
for relation_groups in self.lexicon[word].values():
|
| 36 |
for group in relation_groups:
|
| 37 |
-
|
| 38 |
-
if w in self.inverted_index:
|
| 39 |
-
self.inverted_index[w].discard(word)
|
| 40 |
-
if not self.inverted_index[w]:
|
| 41 |
-
del self.inverted_index[w]
|
| 42 |
del self.lexicon[word]
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def get_related(self, word: str, relation_type: str) -> list[dict[str, Any]]:
|
| 45 |
word = word.lower()
|
| 46 |
if word in self.lexicon and relation_type in self.lexicon[word]:
|
|
|
|
| 21 |
if relation_type not in self.lexicon[word]:
|
| 22 |
self.lexicon[word][relation_type] = []
|
| 23 |
self.lexicon[word][relation_type].append(group)
|
|
|
|
| 24 |
for w in group["words"]:
|
| 25 |
if w not in self.inverted_index:
|
| 26 |
self.inverted_index[w] = set()
|
|
|
|
| 33 |
# Remove from inverted index
|
| 34 |
for relation_groups in self.lexicon[word].values():
|
| 35 |
for group in relation_groups:
|
| 36 |
+
self._update_group_words(group, word)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
del self.lexicon[word]
|
| 38 |
|
| 39 |
+
def _update_group_words(self, group, word):
|
| 40 |
+
for w in group["words"]:
|
| 41 |
+
if w in self.inverted_index:
|
| 42 |
+
self.inverted_index[w].discard(word)
|
| 43 |
+
if not self.inverted_index[w]:
|
| 44 |
+
del self.inverted_index[w]
|
| 45 |
+
|
| 46 |
def get_related(self, word: str, relation_type: str) -> list[dict[str, Any]]:
|
| 47 |
word = word.lower()
|
| 48 |
if word in self.lexicon and relation_type in self.lexicon[word]:
|