Repository: jaraco/inflect

Issue:
list index out of range in engine.postprocess
Inflect pip installed into virtual environment running python3.7 on ubuntu 18.04

After getting an error I wrote some debug statements into inflect.py starting on line 2215:

        def postprocess(self, orig, inflected):
            inflected = str(inflected)

            print("Inflected: " + inflected)
            if "|" in inflected:
                inflected = inflected.split("|")[self.classical_dict["all"]]
            result = inflected.split(" ")

            print("Orig: " + orig)
            print("Inflected: " + inflected)

            # Try to fix word wise capitalization
            for index, word in enumerate(orig.split(" ")):
                ...

My code:

    >>> import inflect
    >>> p = inflect.engine()
    >>> p.classical(all=True)
    >>> p.plural("Genie")
    Inflected: genies|genii
    Orig: Genie
    Inflected: genii
    'Genii'

    >>> p.plural("Master Genie")
    Inflected: Master genies|genii
    Orig: Master Genie
    Inflected: genii

    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "/omitted/for/privacy/lib/python3.7/site-packages/inflect.py", line 2260, in plural
        or self._plnoun(word, count),
      File "/omitted/for/privacy/lib/python3.7/site-packages/inflect.py", line 2230, in postprocess
        result[index] = result[index].capitalize()
    IndexError: list index out of range

So it looks like something went wrong in this line:
    inflected = inflected.split("|")[self.classical_dict["all"]]

Instead of returning "master genii" it returns just "genii"

**Ninja Edit**: This would be occuring on line 2226 in postprocess without the debug statements

Comments:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 5902ef4..a1c8450 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,3 +1,8 @@
+v5.3.0
+======
+
+* #108: Add support for pluralizing open compound nouns.
+
 v5.2.0
 ======
 
diff --git a/inflect.py b/inflect.py
index 23fece2..3bb946b 100644
--- a/inflect.py
+++ b/inflect.py
@@ -2286,8 +2286,19 @@ class engine:
     def postprocess(self, orig: str, inflected) -> str:
         inflected = str(inflected)
         if "|" in inflected:
-            inflected = inflected.split("|")[self.classical_dict["all"]]
-        result = inflected.split(" ")
+            word_options = inflected.split("|")
+            # When two parts of a noun need to be pluralized
+            if len(word_options[0].split(" ")) == len(word_options[1].split(" ")):
+                result = inflected.split("|")[self.classical_dict["all"]].split(" ")
+            # When only the last part of the noun needs to be pluralized
+            else:
+                result = inflected.split(" ")
+                for index, word in enumerate(result):
+                    if "|" in word:
+                        result[index] = word.split("|")[self.classical_dict["all"]]
+        else:
+            result = inflected.split(" ")
+
         # Try to fix word wise capitalization
         for index, word in enumerate(orig.split(" ")):
             if word == "I":


Test patch:
diff --git a/tests/test_compounds.py b/tests/test_compounds.py
index 52030e8..d968f97 100644
--- a/tests/test_compounds.py
+++ b/tests/test_compounds.py
@@ -61,3 +61,30 @@ def test_unit_handling_combined():
     }
     for singular, plural in test_cases.items():
         assert p.plural(singular) == plural
+
+
+def test_unit_open_compound_nouns():
+    test_cases = {
+        "high school": "high schools",
+        "master genie": "master genies",
+        "MASTER genie": "MASTER genies",
+        "Blood brother": "Blood brothers",
+        "prima donna": "prima donnas",
+        "prima DONNA": "prima DONNAS",
+    }
+    for singular, plural in test_cases.items():
+        assert p.plural(singular) == plural
+
+
+def test_unit_open_compound_nouns_classical():
+    p.classical(all=True)
+    test_cases = {
+        "master genie": "master genii",
+        "MASTER genie": "MASTER genii",
+        "Blood brother": "Blood brethren",
+        "prima donna": "prime donne",
+        "prima DONNA": "prime DONNE",
+    }
+    for singular, plural in test_cases.items():
+        assert p.plural(singular) == plural
+    p.classical(all=False)