We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Python 3.7.3, gensim 3.8.1: UnboundLocalError: local variable 'doc_no2' referenced before assignment
Client code:
`    model = LogEntropyModel(corpus=data_corpus, normalize=True)
`
Referenced code:
https://github.com/RaRe-Technologies/gensim/blob/44ea7931c916349821aa1c717fbf7e90fb138297/gensim/models/logentropy_model.py#L115

Exception thrown:

```
  File "/anaconda3/lib/python3.7/site-packages/gensim/models/logentropy_model.py", line 76, in __init__
    self.initialize(corpus)
  File "/anaconda3/lib/python3.7/site-packages/gensim/models/logentropy_model.py", line 115, in initialize
    if doc_no2 != doc_no:
UnboundLocalError: local variable 'doc_no2' referenced before assignment

```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `gensim/models/logentropy_model.py`
Content:
```
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html
5 
6 """This module allows simple Bag of Words (BoW) represented corpus to be transformed into log entropy space.
7 It implements Log Entropy Model that produces entropy-weighted logarithmic term frequency representation.
8 
9 Empirical study by Lee et al. 2015 [1]_ suggests log entropy-weighted model yields better results among other forms of
10 representation.
11 
12 References
13 ----------
14 .. [1] Lee et al. 2005. An Empirical Evaluation of Models of Text Document Similarity.
15        https://escholarship.org/uc/item/48g155nq
16 
17 """
18 
19 import logging
20 import math
21 
22 from gensim import interfaces, matutils, utils
23 
24 logger = logging.getLogger(__name__)
25 
26 
27 class LogEntropyModel(interfaces.TransformationABC):
28     r"""Objects of this class realize the transformation between word-document co-occurrence matrix (int)
29     into a locally/globally weighted matrix (positive floats).
30 
31     This is done by a log entropy normalization, optionally normalizing the resulting documents to unit length.
32     The following formulas explain how o compute the log entropy weight for term :math:`i` in document :math:`j`:
33 
34     .. math::
35 
36         local\_weight_{i,j} = log(frequency_{i,j} + 1)
37 
38         P_{i,j} = \frac{frequency_{i,j}}{\sum_j frequency_{i,j}}
39 
40         global\_weight_i = 1 + \frac{\sum_j P_{i,j} * log(P_{i,j})}{log(number\_of\_documents + 1)}
41 
42         final\_weight_{i,j} = local\_weight_{i,j} * global\_weight_i
43 
44     Examples
45     --------
46     .. sourcecode:: pycon
47 
48         >>> from gensim.models import LogEntropyModel
49         >>> from gensim.test.utils import common_texts
50         >>> from gensim.corpora import Dictionary
51         >>>
52         >>> dct = Dictionary(common_texts)  # fit dictionary
53         >>> corpus = [dct.doc2bow(row) for row in common_texts]  # convert to BoW format
54         >>> model = LogEntropyModel(corpus)  # fit model
55         >>> vector = model[corpus[1]]  # apply model to document
56 
57     """
58 
59     def __init__(self, corpus, normalize=True):
60         """
61 
62         Parameters
63         ----------
64         corpus : iterable of iterable of (int, int)
65             Input corpus in BoW format.
66         normalize : bool, optional
67             If True, the resulted log entropy weighted vector will be normalized to length of 1,
68             If False - do nothing.
69 
70         """
71         self.normalize = normalize
72         self.n_docs = 0
73         self.n_words = 0
74         self.entr = {}
75         if corpus is not None:
76             self.initialize(corpus)
77 
78     def __str__(self):
79         return "LogEntropyModel(n_docs=%s, n_words=%s)" % (self.n_docs, self.n_words)
80 
81     def initialize(self, corpus):
82         """Calculates the global weighting for all terms in a given corpus and transforms the simple
83         count representation into the log entropy normalized space.
84 
85         Parameters
86         ----------
87         corpus : iterable of iterable of (int, int)
88             Corpus is BoW format
89 
90         """
91         logger.info("calculating counts")
92         glob_freq = {}
93         glob_num_words, doc_no = 0, -1
94         for doc_no, bow in enumerate(corpus):
95             if doc_no % 10000 == 0:
96                 logger.info("PROGRESS: processing document #%i", doc_no)
97             glob_num_words += len(bow)
98             for term_id, term_count in bow:
99                 glob_freq[term_id] = glob_freq.get(term_id, 0) + term_count
100 
101         # keep some stats about the training corpus
102         self.n_docs = doc_no + 1
103         self.n_words = glob_num_words
104 
105         # and finally compute the global weights
106         logger.info(
107             "calculating global log entropy weights for %i documents and %i features (%i matrix non-zeros)",
108             self.n_docs, len(glob_freq), self.n_words
109         )
110         logger.debug('iterating over corpus')
111         for doc_no2, bow in enumerate(corpus):
112             for key, freq in bow:
113                 p = (float(freq) / glob_freq[key]) * math.log(float(freq) / glob_freq[key])
114                 self.entr[key] = self.entr.get(key, 0.0) + p
115         if doc_no2 != doc_no:
116             raise ValueError("LogEntropyModel doesn't support generators as training data")
117 
118         logger.debug('iterating over keys')
119         for key in self.entr:
120             self.entr[key] = 1 + self.entr[key] / math.log(self.n_docs + 1)
121 
122     def __getitem__(self, bow):
123         """Get log entropy representation of the input vector and/or corpus.
124 
125         Parameters
126         ----------
127         bow : list of (int, int)
128             Document in BoW format.
129 
130         Returns
131         -------
132         list of (int, float)
133             Log-entropy vector for passed `bow`.
134 
135         """
136         # if the input vector is in fact a corpus, return a transformed corpus
137         is_corpus, bow = utils.is_corpus(bow)
138         if is_corpus:
139             return self._apply(bow)
140 
141         # unknown (new) terms will be given zero weight (NOT infinity/huge)
142         vector = [
143             (term_id, math.log(tf + 1) * self.entr.get(term_id))
144             for term_id, tf in bow
145             if term_id in self.entr
146         ]
147         if self.normalize:
148             vector = matutils.unitvec(vector)
149         return vector
150
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/gensim/models/logentropy_model.py b/gensim/models/logentropy_model.py
--- a/gensim/models/logentropy_model.py
+++ b/gensim/models/logentropy_model.py
@@ -108,6 +108,9 @@
             self.n_docs, len(glob_freq), self.n_words
         )
         logger.debug('iterating over corpus')
+
+        # initialize doc_no2 index in case corpus is empty
+        doc_no2 = 0
         for doc_no2, bow in enumerate(corpus):
             for key, freq in bow:
                 p = (float(freq) / glob_freq[key]) * math.log(float(freq) / glob_freq[key])

{"golden_diff": "diff --git a/gensim/models/logentropy_model.py b/gensim/models/logentropy_model.py\n--- a/gensim/models/logentropy_model.py\n+++ b/gensim/models/logentropy_model.py\n@@ -108,6 +108,9 @@\n             self.n_docs, len(glob_freq), self.n_words\n         )\n         logger.debug('iterating over corpus')\n+\n+        # initialize doc_no2 index in case corpus is empty\n+        doc_no2 = 0\n         for doc_no2, bow in enumerate(corpus):\n             for key, freq in bow:\n                 p = (float(freq) / glob_freq[key]) * math.log(float(freq) / glob_freq[key])\n", "issue": "Python 3.7.3, gensim 3.8.1: UnboundLocalError: local variable 'doc_no2' referenced before assignment\nClient code:\r\n`    model = LogEntropyModel(corpus=data_corpus, normalize=True)\r\n`\r\nReferenced code:\r\nhttps://github.com/RaRe-Technologies/gensim/blob/44ea7931c916349821aa1c717fbf7e90fb138297/gensim/models/logentropy_model.py#L115\r\n\r\nException thrown:\r\n\r\n```\r\n  File \"/anaconda3/lib/python3.7/site-packages/gensim/models/logentropy_model.py\", line 76, in __init__\r\n    self.initialize(corpus)\r\n  File \"/anaconda3/lib/python3.7/site-packages/gensim/models/logentropy_model.py\", line 115, in initialize\r\n    if doc_no2 != doc_no:\r\nUnboundLocalError: local variable 'doc_no2' referenced before assignment\r\n\r\n```\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"This module allows simple Bag of Words (BoW) represented corpus to be transformed into log entropy space.\nIt implements Log Entropy Model that produces entropy-weighted logarithmic term frequency representation.\n\nEmpirical study by Lee et al. 2015 [1]_ suggests log entropy-weighted model yields better results among other forms of\nrepresentation.\n\nReferences\n----------\n.. [1] Lee et al. 2005. An Empirical Evaluation of Models of Text Document Similarity.\n       https://escholarship.org/uc/item/48g155nq\n\n\"\"\"\n\nimport logging\nimport math\n\nfrom gensim import interfaces, matutils, utils\n\nlogger = logging.getLogger(__name__)\n\n\nclass LogEntropyModel(interfaces.TransformationABC):\n    r\"\"\"Objects of this class realize the transformation between word-document co-occurrence matrix (int)\n    into a locally/globally weighted matrix (positive floats).\n\n    This is done by a log entropy normalization, optionally normalizing the resulting documents to unit length.\n    The following formulas explain how o compute the log entropy weight for term :math:`i` in document :math:`j`:\n\n    .. math::\n\n        local\\_weight_{i,j} = log(frequency_{i,j} + 1)\n\n        P_{i,j} = \\frac{frequency_{i,j}}{\\sum_j frequency_{i,j}}\n\n        global\\_weight_i = 1 + \\frac{\\sum_j P_{i,j} * log(P_{i,j})}{log(number\\_of\\_documents + 1)}\n\n        final\\_weight_{i,j} = local\\_weight_{i,j} * global\\_weight_i\n\n    Examples\n    --------\n    .. sourcecode:: pycon\n\n        >>> from gensim.models import LogEntropyModel\n        >>> from gensim.test.utils import common_texts\n        >>> from gensim.corpora import Dictionary\n        >>>\n        >>> dct = Dictionary(common_texts)  # fit dictionary\n        >>> corpus = [dct.doc2bow(row) for row in common_texts]  # convert to BoW format\n        >>> model = LogEntropyModel(corpus)  # fit model\n        >>> vector = model[corpus[1]]  # apply model to document\n\n    \"\"\"\n\n    def __init__(self, corpus, normalize=True):\n        \"\"\"\n\n        Parameters\n        ----------\n        corpus : iterable of iterable of (int, int)\n            Input corpus in BoW format.\n        normalize : bool, optional\n            If True, the resulted log entropy weighted vector will be normalized to length of 1,\n            If False - do nothing.\n\n        \"\"\"\n        self.normalize = normalize\n        self.n_docs = 0\n        self.n_words = 0\n        self.entr = {}\n        if corpus is not None:\n            self.initialize(corpus)\n\n    def __str__(self):\n        return \"LogEntropyModel(n_docs=%s, n_words=%s)\" % (self.n_docs, self.n_words)\n\n    def initialize(self, corpus):\n        \"\"\"Calculates the global weighting for all terms in a given corpus and transforms the simple\n        count representation into the log entropy normalized space.\n\n        Parameters\n        ----------\n        corpus : iterable of iterable of (int, int)\n            Corpus is BoW format\n\n        \"\"\"\n        logger.info(\"calculating counts\")\n        glob_freq = {}\n        glob_num_words, doc_no = 0, -1\n        for doc_no, bow in enumerate(corpus):\n            if doc_no % 10000 == 0:\n                logger.info(\"PROGRESS: processing document #%i\", doc_no)\n            glob_num_words += len(bow)\n            for term_id, term_count in bow:\n                glob_freq[term_id] = glob_freq.get(term_id, 0) + term_count\n\n        # keep some stats about the training corpus\n        self.n_docs = doc_no + 1\n        self.n_words = glob_num_words\n\n        # and finally compute the global weights\n        logger.info(\n            \"calculating global log entropy weights for %i documents and %i features (%i matrix non-zeros)\",\n            self.n_docs, len(glob_freq), self.n_words\n        )\n        logger.debug('iterating over corpus')\n        for doc_no2, bow in enumerate(corpus):\n            for key, freq in bow:\n                p = (float(freq) / glob_freq[key]) * math.log(float(freq) / glob_freq[key])\n                self.entr[key] = self.entr.get(key, 0.0) + p\n        if doc_no2 != doc_no:\n            raise ValueError(\"LogEntropyModel doesn't support generators as training data\")\n\n        logger.debug('iterating over keys')\n        for key in self.entr:\n            self.entr[key] = 1 + self.entr[key] / math.log(self.n_docs + 1)\n\n    def __getitem__(self, bow):\n        \"\"\"Get log entropy representation of the input vector and/or corpus.\n\n        Parameters\n        ----------\n        bow : list of (int, int)\n            Document in BoW format.\n\n        Returns\n        -------\n        list of (int, float)\n            Log-entropy vector for passed `bow`.\n\n        \"\"\"\n        # if the input vector is in fact a corpus, return a transformed corpus\n        is_corpus, bow = utils.is_corpus(bow)\n        if is_corpus:\n            return self._apply(bow)\n\n        # unknown (new) terms will be given zero weight (NOT infinity/huge)\n        vector = [\n            (term_id, math.log(tf + 1) * self.entr.get(term_id))\n            for term_id, tf in bow\n            if term_id in self.entr\n        ]\n        if self.normalize:\n            vector = matutils.unitvec(vector)\n        return vector\n", "path": "gensim/models/logentropy_model.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"This module allows simple Bag of Words (BoW) represented corpus to be transformed into log entropy space.\nIt implements Log Entropy Model that produces entropy-weighted logarithmic term frequency representation.\n\nEmpirical study by Lee et al. 2015 [1]_ suggests log entropy-weighted model yields better results among other forms of\nrepresentation.\n\nReferences\n----------\n.. [1] Lee et al. 2005. An Empirical Evaluation of Models of Text Document Similarity.\n       https://escholarship.org/uc/item/48g155nq\n\n\"\"\"\n\nimport logging\nimport math\n\nfrom gensim import interfaces, matutils, utils\n\nlogger = logging.getLogger(__name__)\n\n\nclass LogEntropyModel(interfaces.TransformationABC):\n    r\"\"\"Objects of this class realize the transformation between word-document co-occurrence matrix (int)\n    into a locally/globally weighted matrix (positive floats).\n\n    This is done by a log entropy normalization, optionally normalizing the resulting documents to unit length.\n    The following formulas explain how o compute the log entropy weight for term :math:`i` in document :math:`j`:\n\n    .. math::\n\n        local\\_weight_{i,j} = log(frequency_{i,j} + 1)\n\n        P_{i,j} = \\frac{frequency_{i,j}}{\\sum_j frequency_{i,j}}\n\n        global\\_weight_i = 1 + \\frac{\\sum_j P_{i,j} * log(P_{i,j})}{log(number\\_of\\_documents + 1)}\n\n        final\\_weight_{i,j} = local\\_weight_{i,j} * global\\_weight_i\n\n    Examples\n    --------\n    .. sourcecode:: pycon\n\n        >>> from gensim.models import LogEntropyModel\n        >>> from gensim.test.utils import common_texts\n        >>> from gensim.corpora import Dictionary\n        >>>\n        >>> dct = Dictionary(common_texts)  # fit dictionary\n        >>> corpus = [dct.doc2bow(row) for row in common_texts]  # convert to BoW format\n        >>> model = LogEntropyModel(corpus)  # fit model\n        >>> vector = model[corpus[1]]  # apply model to document\n\n    \"\"\"\n\n    def __init__(self, corpus, normalize=True):\n        \"\"\"\n\n        Parameters\n        ----------\n        corpus : iterable of iterable of (int, int)\n            Input corpus in BoW format.\n        normalize : bool, optional\n            If True, the resulted log entropy weighted vector will be normalized to length of 1,\n            If False - do nothing.\n\n        \"\"\"\n        self.normalize = normalize\n        self.n_docs = 0\n        self.n_words = 0\n        self.entr = {}\n        if corpus is not None:\n            self.initialize(corpus)\n\n    def __str__(self):\n        return \"LogEntropyModel(n_docs=%s, n_words=%s)\" % (self.n_docs, self.n_words)\n\n    def initialize(self, corpus):\n        \"\"\"Calculates the global weighting for all terms in a given corpus and transforms the simple\n        count representation into the log entropy normalized space.\n\n        Parameters\n        ----------\n        corpus : iterable of iterable of (int, int)\n            Corpus is BoW format\n\n        \"\"\"\n        logger.info(\"calculating counts\")\n        glob_freq = {}\n        glob_num_words, doc_no = 0, -1\n        for doc_no, bow in enumerate(corpus):\n            if doc_no % 10000 == 0:\n                logger.info(\"PROGRESS: processing document #%i\", doc_no)\n            glob_num_words += len(bow)\n            for term_id, term_count in bow:\n                glob_freq[term_id] = glob_freq.get(term_id, 0) + term_count\n\n        # keep some stats about the training corpus\n        self.n_docs = doc_no + 1\n        self.n_words = glob_num_words\n\n        # and finally compute the global weights\n        logger.info(\n            \"calculating global log entropy weights for %i documents and %i features (%i matrix non-zeros)\",\n            self.n_docs, len(glob_freq), self.n_words\n        )\n        logger.debug('iterating over corpus')\n\n        # initialize doc_no2 index in case corpus is empty\n        doc_no2 = 0\n        for doc_no2, bow in enumerate(corpus):\n            for key, freq in bow:\n                p = (float(freq) / glob_freq[key]) * math.log(float(freq) / glob_freq[key])\n                self.entr[key] = self.entr.get(key, 0.0) + p\n        if doc_no2 != doc_no:\n            raise ValueError(\"LogEntropyModel doesn't support generators as training data\")\n\n        logger.debug('iterating over keys')\n        for key in self.entr:\n            self.entr[key] = 1 + self.entr[key] / math.log(self.n_docs + 1)\n\n    def __getitem__(self, bow):\n        \"\"\"Get log entropy representation of the input vector and/or corpus.\n\n        Parameters\n        ----------\n        bow : list of (int, int)\n            Document in BoW format.\n\n        Returns\n        -------\n        list of (int, float)\n            Log-entropy vector for passed `bow`.\n\n        \"\"\"\n        # if the input vector is in fact a corpus, return a transformed corpus\n        is_corpus, bow = utils.is_corpus(bow)\n        if is_corpus:\n            return self._apply(bow)\n\n        # unknown (new) terms will be given zero weight (NOT infinity/huge)\n        vector = [\n            (term_id, math.log(tf + 1) * self.entr.get(term_id))\n            for term_id, tf in bow\n            if term_id in self.entr\n        ]\n        if self.normalize:\n            vector = matutils.unitvec(vector)\n        return vector\n", "path": "gensim/models/logentropy_model.py"}]}