Repository: nltk/nltk

Issue:
Porter stemmer returns a capital instead of lowercase 
This output is unexpected. The `In` returns the capitalize `In` from PorterStemmer's output.

```python
>>> from nltk.stem import PorterStemmer
>>> porter = PorterStemmer()
>>> porter.stem('In')
'In'
```

More details on https://stackoverflow.com/q/60387288/610569

Discussion:

Patch:
diff --git a/README b/README
new file mode 100644
index 000000000..e69de29bb
diff --git a/nltk/stem/porter.py b/nltk/stem/porter.py
index cb04f5213..c20e14f91 100644
--- a/nltk/stem/porter.py
+++ b/nltk/stem/porter.py
@@ -648,17 +648,21 @@ class PorterStemmer(StemmerI):
             word, [("ll", "l", lambda stem: self._measure(word[:-1]) > 1)]
         )
 
-    def stem(self, word):
-        stem = word.lower()
+    def stem(self, word, to_lowercase=True):
+        """
+        :param to_lowercase: if `to_lowercase=True` the word always lowercase
+        """
+        stem = word.lower() if to_lowercase else word
 
         if self.mode == self.NLTK_EXTENSIONS and word in self.pool:
-            return self.pool[word]
+            return self.pool[stem]
 
         if self.mode != self.ORIGINAL_ALGORITHM and len(word) <= 2:
             # With this line, strings of length 1 or 2 don't go through
             # the stemming process, although no mention is made of this
             # in the published algorithm.
-            return word
+            return stem
+
 
         stem = self._step1a(stem)
         stem = self._step1b(stem)


Test patch:
diff --git a/nltk/test/unit/test_stem.py b/nltk/test/unit/test_stem.py
index 52a0d6652..f578c1f66 100644
--- a/nltk/test/unit/test_stem.py
+++ b/nltk/test/unit/test_stem.py
@@ -143,3 +143,16 @@ class PorterTest(unittest.TestCase):
         Ensures that 'oed' can be stemmed without throwing an error.
         """
         assert PorterStemmer().stem('oed') == 'o'
+
+
+    def test_lowercase_option(self):
+        """Test for improvement on https://github.com/nltk/nltk/issues/2507
+
+        Ensures that stems are lowercased when `to_lowercase=True`
+        """
+        porter = PorterStemmer()
+        assert porter.stem('On') == 'on'
+        assert porter.stem('I') == 'i'
+        assert porter.stem('I', to_lowercase=False) == 'I'
+        assert porter.stem('Github') == 'github'
+        assert porter.stem('Github', to_lowercase=False) == 'Github'