Repository: iwpnd/toponym

Issue:
max() arg is an empty sequence
```python
from toponym import toponym, topodict

td = topodict.Topodict(language='russian')
td.load()

word = "Коми"

tn = toponym.Toponym(word, td)
tn.build()

print(tn.topo)`
```

Results in:

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-957119cce124> in <module>
      5 
      6 tn = toponym.Toponym(word, td)
----> 7 tn.build()
      8 
      9 print(tn.topo)

c:\br\pypy\topoynm_new\toponym-master\toponym\toponym.py in build(self)
     37         else:
     38             self.recipe = self.topodict[
---> 39                 self._get_longest_word_ending(self.word)
     40             ]
     41 

c:\br\pypy\topoynm_new\toponym-master\toponym\toponym.py in _get_longest_word_ending(self, word)
     57             x for x in possible_endings if x in self.topodict._dict.keys()]
     58 
---> 59         return max(matching_endings, key=len)
     60 
     61 

ValueError: max() arg is an empty sequence
```

Probably because there is no ending like that in the topodictionary.

Discussion:

Patch:
diff --git a/toponym/case.py b/toponym/case.py
index e8dda31..af4e103 100644
--- a/toponym/case.py
+++ b/toponym/case.py
@@ -1,3 +1,7 @@
+import logging
+
+logger = logging.getLogger(__name__)
+
 class Case(object):
     def __init__(self):
         pass
diff --git a/toponym/topodict.py b/toponym/topodict.py
index be2ec30..7b856d3 100644
--- a/toponym/topodict.py
+++ b/toponym/topodict.py
@@ -1,8 +1,16 @@
 import os
 import json
-
 from . import settings
-from .utils import (get_available_language_codes, print_available_languages, get_language_code, load_topodict)
+from .utils import (
+    get_available_language_codes, 
+    print_available_languages, 
+    get_language_code, 
+    load_topodict
+    )
+
+import logging
+
+logger = logging.getLogger(__name__)
 
 
 class Topodict:
@@ -35,11 +43,18 @@ class Topodict:
         elif word_ending in self._dict.keys():
             return self._dict[word_ending]
         else:
-            raise KeyError("{we} not in {language} topodict".format(
-                we=word_ending,
+            logger.warning("{word_ending} not in {language} topodict".format(
+                word_ending=word_ending,
                 language=self.language
-                )
-            )
+            ))
+            return self._default_recipe()
+    
+    def _default_recipe(self):
+        """Create default recipe in case there is no ending in actual recipe for word
+        """
+        return {
+            "nominative": ["", 0]
+        }
 
     def load(self):
         if not self.file:
diff --git a/toponym/toponym.py b/toponym/toponym.py
index bbea3aa..5efe6ef 100644
--- a/toponym/toponym.py
+++ b/toponym/toponym.py
@@ -1,6 +1,11 @@
 from toponym import case
 from collections import defaultdict
 import itertools
+import logging
+
+import logging
+
+logger = logging.getLogger(__name__)
 
 
 class Toponym(case.Case):
@@ -15,6 +20,7 @@ class Toponym(case.Case):
 
         self.topo_recipe = False
 
+
     def build(self):
 
         if isinstance(self.word, list):
@@ -48,6 +54,7 @@ class Toponym(case.Case):
                     case
                 )
 
+
     def _get_longest_word_ending(self, word):
         """
         """
@@ -56,7 +63,17 @@ class Toponym(case.Case):
         matching_endings = [
             x for x in possible_endings if x in self.topodict._dict.keys()]
 
-        return max(matching_endings, key=len)
+        if matching_endings:
+            return max(matching_endings, key=len)
+        else:
+            return ""        
+
+
+    def _create_default_toponym(self, word):
+        """
+        """
+        topo = {'nominative': word}
+        return topo
 
 
     def concat_case_dictionaries(self, list_of_dictionaries):
@@ -68,8 +85,6 @@ class Toponym(case.Case):
                 dd[key].append(val)
 
         for k, v in dd.items():
-            # if isinstance(v[0], str):
-            #     dd[k] = " ".join([x for x in dd[k]])
 
             if all([isinstance(x, str) for x in v]):
                 dd[k] = " ".join([x for x in dd[k]])
diff --git a/toponym/utils.py b/toponym/utils.py
index 8a665a1..f70fc0a 100644
--- a/toponym/utils.py
+++ b/toponym/utils.py
@@ -3,6 +3,9 @@ import json
 import logging
 
 from . import settings
+import logging
+
+logger = logging.getLogger(__name__)
 
 
 def get_available_language_codes():


Test patch:
diff --git a/tests/unit/test_toponym.py b/tests/unit/test_toponym.py
index df005ad..98518f1 100644
--- a/tests/unit/test_toponym.py
+++ b/tests/unit/test_toponym.py
@@ -76,3 +76,14 @@ def test_get_multi_word_toponym_multi_ending_single_ending_revers():
     assert tn.topo['nominative'] == 'Teto Testi'
     assert set(tn.topo['genitive']) == set(
         ['Teta Testo', 'Teta Testa'])
+
+
+def test_toponym_for_unknown_ending():
+    td = topodict.Topodict(language='russian')
+    td.load()
+
+    word = ""
+
+    tn = toponym.Toponym(word, td)
+    tn.build()
+    assert tn.topo['nominative'] == ""