Repository: nltk/nltk

Issue:
`corpus_bleu` function does not catch all the expections when calling `weights[0][0]`
In your codes https://github.com/nltk/nltk/blob/e2d368e00ef806121aaa39f6e5f90d9f8243631b/nltk/translate/bleu_score.py#L201

I pass in `weights = array([0.25, 0.25, 0.25, 0.25])` and find this error:

```
File "/home/cyzhao/miniconda3/envs/prompt/lib/python3.11/site-packages/nltk/translate/bleu_score.py", line 200, in corpus_bleu
    weights[0][0]
    ~~~~~~~~~~^^^
IndexError: invalid index to scalar variable.
"""
```

I then find out the reason why.

Using `try weights[0][0]` right off the bat is quite baffling, especially since the function's parameter is supposed to be a tuple. Yet, this inexplicable operation is performed first. Moreover, after executing this step, not all exceptions are completely caught. The `weights` passed in by the framework are `array([0.25, 0.25, 0.25, 0.25])`, and for `ndarray` the error is `IndexError: invalid index to scalar variable`. Hence, you guys haven't caught all the exceptions, leading to the situation where one must pass a tuple `(0.25, 0.25, 0.25, 0.25)` to be caught by this try-except block.

Discussion:

Patch:
diff --git a/nltk/translate/bleu_score.py b/nltk/translate/bleu_score.py
index f6c232e1c..da445bc3e 100644
--- a/nltk/translate/bleu_score.py
+++ b/nltk/translate/bleu_score.py
@@ -198,7 +198,7 @@ def corpus_bleu(
 
     try:
         weights[0][0]
-    except TypeError:
+    except:
         weights = [weights]
     max_weight_length = max(len(weight) for weight in weights)
 


Test patch:
diff --git a/nltk/test/unit/translate/test_bleu.py b/nltk/test/unit/translate/test_bleu.py
index ac3565e74..990b76406 100644
--- a/nltk/test/unit/translate/test_bleu.py
+++ b/nltk/test/unit/translate/test_bleu.py
@@ -5,6 +5,8 @@ Tests for BLEU translation evaluation metric
 import io
 import unittest
 
+import numpy as np
+
 from nltk.data import find
 from nltk.translate.bleu_score import (
     SmoothingFunction,
@@ -217,6 +219,14 @@ class TestBLEUFringeCases(unittest.TestCase):
         except AttributeError:
             pass  # unittest.TestCase.assertWarns is only supported in Python >= 3.2.
 
+    def test_numpy_weights(self):
+        # Test case where there's 0 matches
+        references = ["The candidate has no alignment to any of the references".split()]
+        hypothesis = "John loves Mary".split()
+
+        weights = np.array([0.25] * 4)
+        assert sentence_bleu(references, hypothesis, weights) == 0
+
 
 class TestBLEUvsMteval13a(unittest.TestCase):
     def test_corpus_bleu(self):