From 9bcc5e59297469c27a230ade7d2dbdb9d17e0294 Mon Sep 17 00:00:00 2001
From: pwalkow <pawel.walkowiak@hotmail.com>
Date: Thu, 23 Nov 2023 09:39:44 +0100
Subject: [PATCH] Add try-except

---
 src/dictionaries/morphosyntactic/ner_file.py | 38 +++++++++++---------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/dictionaries/morphosyntactic/ner_file.py b/src/dictionaries/morphosyntactic/ner_file.py
index f31dde7..55a4b65 100644
--- a/src/dictionaries/morphosyntactic/ner_file.py
+++ b/src/dictionaries/morphosyntactic/ner_file.py
@@ -92,22 +92,28 @@ class NERFileMorphosyntacticDictionary(MorphosyntacticDictionary):
 
             if original_entry_type_name in self._dictionary:
                 entry_type = original_entry_type_name
-
-                if morpho_tag in self._dictionary[entry_type] \
-                        and morpho_tag in self._dictionary[entry_type]:
-                    lemma = random.choice(
-                        list(self._dictionary[entry_type][morpho_tag].keys())
-                    )
-
-                    word = self._dictionary[entry_type][morpho_tag][lemma]
-                else:
-                    morpho_tag = random.choice(
-                        list(self._dictionary[entry_type].keys())
-                    )
-                    lemma = random.choice(
-                        list(self._dictionary[entry_type][morpho_tag].keys())
-                    )
-                    word = lemma
+                try:
+                    if entry_type in self._dictionary \
+                            and morpho_tag in self._dictionary[entry_type] \
+                            and len(list(self._dictionary[
+                                             entry_type][morpho_tag].keys())) > 0:
+                        lemma = random.choice(
+                            list(self._dictionary[entry_type][morpho_tag].keys())
+                        )
+
+                        word = self._dictionary[entry_type][morpho_tag][lemma]
+                    else:
+                        morpho_tag = random.choice(
+                            list(self._dictionary[entry_type].keys())
+                        )
+                        lemma = random.choice(
+                            list(self._dictionary[entry_type][morpho_tag].keys())
+                        )
+                        word = lemma
+                except IndexError as exp:
+                    print(f"IndexError entry_type {entry_type} morpho_tag {morpho_tag}")
+                    print(exp)
+                    print(f"Dictionary {self._dictionary[entry_type][morpho_tag]}")
 
         if word is None and self._always_replace:
             entry_type = random.choice(list(self._dictionary.keys()))
-- 
GitLab