We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Unpredictable ZeroDivisionErrors in direct_confirmation_measure
Hi guys,

first of all a big thank you for your tremendous work.
I am trying to train a LdaMulticore model guided by a coherence measure (UMASS). However I face the following problem:

- A ZeroDivisionError is thrown sporadically (both with the training and test set set, but definitely more frequently with the latter one). This makes it impossible to compare the coherence measure over a range of number of topics.

```
    num_docs = len(corpus)
    split = train_perc * num_docs // 100
    indices = list(range(num_docs))
    random.shuffle(indices)
   
   # Split into two sets and DISCARD empty documents
    train_corpus = [corpus[i] for i in indices[:split] if corpus[i]]
    test_corpus = [corpus[i] for i in indices[split:] if corpus[i]]

    # Number of passes (heuristic) (e.g. min_runs = 10)
    num_passes = max(num_topics, min_runs)

    lda_model = models.LdaMulticore(train_corpus,
                                         id2word=dictionary,
                                         batch=True,
                                         num_topics=num_topics,
                                         alpha="asymmetric",
                                         passes=num_passes)

    if metric == 'coherence':
            left_score = CoherenceModel(model=lda_model,
                                corpus=test_corpus,
                                dictionary=dictionary,
                                coherence="u_mass").get_coherence()



```

Error log:
```
Traceback (most recent call last):
File "DataGenerator.py", line 198, in <module>
main()
File "DataGenerator.py", line 45, in main
lda_model_file=OUTPUT_LDA_MODEL)
File "/cluster/home/lennartv/ma/python/deep/preprocessing/Projector.py", line 217, in create_projection
num_topics, lda_model = optimize_lda(corpus, dictionary)
File "/cluster/home/lennartv/ma/python/deep/preprocessing/Projector.py", line 107, in optimize_lda
coherence="u_mass").get_coherence()
File "/cluster/home/lennartv/.local/lib64/python3.4/site-packages/gensim/models/coherencemodel.py", line 202, in get_coherence
confirmed_measures = measure.conf(segmented_topics, per_topic_postings, num_docs)
File "/cluster/home/lennartv/.local/lib64/python3.4/site-packages/gensim/topic_coherence/direct_confirmation_measure.py", line 40, in log_conditional_probability
m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))
ZeroDivisionError: float division by zero
```

I tried to assess the problem and I noticed the following things:

- There seems to be a connection to how well the LDA algorithm converged. Varying the number of passes can mitigate the problem.

I don't have enough insight in the algorithm but the bug seems to stem from the fact that `w_star_docs` in the following code block can become an empty set under certain circumstances:

```
    m_lc = []
    for s_i in segmented_topics:
        for w_prime, w_star in s_i:
            w_prime_docs = per_topic_postings[w_prime]
            w_star_docs = per_topic_postings[w_star]
            co_docs = w_prime_docs.intersection(w_star_docs)
            m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))
            m_lc.append(m_lc_i)

    return m_lc
```

Thanks in advance for your efforts,
  Lennart

--- 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/topic_coherence/direct_confirmation_measure.py`
Content:
```
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2013 Radim Rehurek <radimrehurek@seznam.cz>
5 # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html
6 
7 """
8 This module contains functions to compute direct confirmation on a pair of words or word subsets.
9 """
10 
11 import logging
12 import numpy as np
13 
14 logger = logging.getLogger(__name__)
15 
16 EPSILON = 1e-12  # Should be small. Value as suggested in paper.
17 
18 def log_conditional_probability(segmented_topics, per_topic_postings, num_docs):
19     """
20     This function calculates the log-conditional-probability measure
21     which is used by coherence measures such as U_mass.
22     This is defined as: m_lc(S_i) = log[(P(W', W*) + e) / P(W*)]
23 
24     Args:
25     ----
26     segmented_topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.
27     per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics.
28     num_docs : Total number of documents in corresponding corpus.
29 
30     Returns:
31     -------
32     m_lc : List of log conditional probability measure on each set in segmented topics.
33     """
34     m_lc = []
35     for s_i in segmented_topics:
36         for w_prime, w_star in s_i:
37             w_prime_docs = per_topic_postings[w_prime]
38             w_star_docs = per_topic_postings[w_star]
39             co_docs = w_prime_docs.intersection(w_star_docs)
40             m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))
41             m_lc.append(m_lc_i)
42 
43     return m_lc
44 
45 def log_ratio_measure(segmented_topics, per_topic_postings, num_docs, normalize=False):
46     """
47     If normalize=False:
48         Popularly known as PMI.
49         This function calculates the log-ratio-measure which is used by
50         coherence measures such as c_v.
51         This is defined as: m_lr(S_i) = log[(P(W', W*) + e) / (P(W') * P(W*))]
52 
53     If normalize=True:
54         This function calculates the normalized-log-ratio-measure, popularly knowns as
55         NPMI which is used by coherence measures such as c_v.
56         This is defined as: m_nlr(S_i) = m_lr(S_i) / -log[P(W', W*) + e]
57 
58     Args:
59     ----
60     segmented topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.
61     per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics
62     num_docs : Total number of documents in corpus. Used for calculating probability.
63 
64     Returns:
65     -------
66     m_lr : List of log ratio measures on each set in segmented topics.
67     """
68     m_lr = []
69     for s_i in segmented_topics:
70         for w_prime, w_star in s_i:
71             w_prime_docs = per_topic_postings[w_prime]
72             w_star_docs = per_topic_postings[w_star]
73             co_docs = w_prime_docs.intersection(w_star_docs)
74             if normalize:
75                 # For normalized log ratio measure
76                 numerator = log_ratio_measure([[(w_prime, w_star)]], per_topic_postings, num_docs)[0]
77                 co_doc_prob = len(co_docs) / float(num_docs)
78                 m_lr_i = numerator / (-np.log(co_doc_prob + EPSILON))
79             else:
80                 # For log ratio measure without normalization
81                 numerator = (len(co_docs) / float(num_docs)) + EPSILON
82                 denominator = (len(w_prime_docs) / float(num_docs)) * (len(w_star_docs) / float(num_docs))
83                 m_lr_i = np.log(numerator / denominator)
84             m_lr.append(m_lr_i)
85 
86     return m_lr
87
```
--- 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/topic_coherence/direct_confirmation_measure.py b/gensim/topic_coherence/direct_confirmation_measure.py
--- a/gensim/topic_coherence/direct_confirmation_measure.py
+++ b/gensim/topic_coherence/direct_confirmation_measure.py
@@ -37,7 +37,10 @@
             w_prime_docs = per_topic_postings[w_prime]
             w_star_docs = per_topic_postings[w_star]
             co_docs = w_prime_docs.intersection(w_star_docs)
-            m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))
+            if  w_star_docs:
+                m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))
+            else:
+                m_lc_i = 0
             m_lc.append(m_lc_i)
 
     return m_lc

{"golden_diff": "diff --git a/gensim/topic_coherence/direct_confirmation_measure.py b/gensim/topic_coherence/direct_confirmation_measure.py\n--- a/gensim/topic_coherence/direct_confirmation_measure.py\n+++ b/gensim/topic_coherence/direct_confirmation_measure.py\n@@ -37,7 +37,10 @@\n             w_prime_docs = per_topic_postings[w_prime]\n             w_star_docs = per_topic_postings[w_star]\n             co_docs = w_prime_docs.intersection(w_star_docs)\n-            m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\n+            if  w_star_docs:\n+                m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\n+            else:\n+                m_lc_i = 0\n             m_lc.append(m_lc_i)\n \n     return m_lc\n", "issue": "Unpredictable ZeroDivisionErrors in direct_confirmation_measure\nHi guys,\r\n\r\nfirst of all a big thank you for your tremendous work.\r\nI am trying to train a LdaMulticore model guided by a coherence measure (UMASS). However I face the following problem:\r\n\r\n- A ZeroDivisionError is thrown sporadically (both with the training and test set set, but definitely more frequently with the latter one). This makes it impossible to compare the coherence measure over a range of number of topics.\r\n\r\n```\r\n    num_docs = len(corpus)\r\n    split = train_perc * num_docs // 100\r\n    indices = list(range(num_docs))\r\n    random.shuffle(indices)\r\n   \r\n   # Split into two sets and DISCARD empty documents\r\n    train_corpus = [corpus[i] for i in indices[:split] if corpus[i]]\r\n    test_corpus = [corpus[i] for i in indices[split:] if corpus[i]]\r\n\r\n    # Number of passes (heuristic) (e.g. min_runs = 10)\r\n    num_passes = max(num_topics, min_runs)\r\n\r\n    lda_model = models.LdaMulticore(train_corpus,\r\n                                         id2word=dictionary,\r\n                                         batch=True,\r\n                                         num_topics=num_topics,\r\n                                         alpha=\"asymmetric\",\r\n                                         passes=num_passes)\r\n\r\n    if metric == 'coherence':\r\n            left_score = CoherenceModel(model=lda_model,\r\n                                corpus=test_corpus,\r\n                                dictionary=dictionary,\r\n                                coherence=\"u_mass\").get_coherence()\r\n\r\n\r\n\r\n```\r\n\r\nError log:\r\n```\r\nTraceback (most recent call last):\r\nFile \"DataGenerator.py\", line 198, in <module>\r\nmain()\r\nFile \"DataGenerator.py\", line 45, in main\r\nlda_model_file=OUTPUT_LDA_MODEL)\r\nFile \"/cluster/home/lennartv/ma/python/deep/preprocessing/Projector.py\", line 217, in create_projection\r\nnum_topics, lda_model = optimize_lda(corpus, dictionary)\r\nFile \"/cluster/home/lennartv/ma/python/deep/preprocessing/Projector.py\", line 107, in optimize_lda\r\ncoherence=\"u_mass\").get_coherence()\r\nFile \"/cluster/home/lennartv/.local/lib64/python3.4/site-packages/gensim/models/coherencemodel.py\", line 202, in get_coherence\r\nconfirmed_measures = measure.conf(segmented_topics, per_topic_postings, num_docs)\r\nFile \"/cluster/home/lennartv/.local/lib64/python3.4/site-packages/gensim/topic_coherence/direct_confirmation_measure.py\", line 40, in log_conditional_probability\r\nm_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\r\nZeroDivisionError: float division by zero\r\n```\r\n\r\nI tried to assess the problem and I noticed the following things:\r\n\r\n- There seems to be a connection to how well the LDA algorithm converged. Varying the number of passes can mitigate the problem.\r\n\r\nI don't have enough insight in the algorithm but the bug seems to stem from the fact that `w_star_docs` in the following code block can become an empty set under certain circumstances:\r\n\r\n```\r\n    m_lc = []\r\n    for s_i in segmented_topics:\r\n        for w_prime, w_star in s_i:\r\n            w_prime_docs = per_topic_postings[w_prime]\r\n            w_star_docs = per_topic_postings[w_star]\r\n            co_docs = w_prime_docs.intersection(w_star_docs)\r\n            m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\r\n            m_lc.append(m_lc_i)\r\n\r\n    return m_lc\r\n```\r\n\r\nThanks in advance for your efforts,\r\n  Lennart\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) 2013 Radim Rehurek <radimrehurek@seznam.cz>\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"\nThis module contains functions to compute direct confirmation on a pair of words or word subsets.\n\"\"\"\n\nimport logging\nimport numpy as np\n\nlogger = logging.getLogger(__name__)\n\nEPSILON = 1e-12  # Should be small. Value as suggested in paper.\n\ndef log_conditional_probability(segmented_topics, per_topic_postings, num_docs):\n    \"\"\"\n    This function calculates the log-conditional-probability measure\n    which is used by coherence measures such as U_mass.\n    This is defined as: m_lc(S_i) = log[(P(W', W*) + e) / P(W*)]\n\n    Args:\n    ----\n    segmented_topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.\n    per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics.\n    num_docs : Total number of documents in corresponding corpus.\n\n    Returns:\n    -------\n    m_lc : List of log conditional probability measure on each set in segmented topics.\n    \"\"\"\n    m_lc = []\n    for s_i in segmented_topics:\n        for w_prime, w_star in s_i:\n            w_prime_docs = per_topic_postings[w_prime]\n            w_star_docs = per_topic_postings[w_star]\n            co_docs = w_prime_docs.intersection(w_star_docs)\n            m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\n            m_lc.append(m_lc_i)\n\n    return m_lc\n\ndef log_ratio_measure(segmented_topics, per_topic_postings, num_docs, normalize=False):\n    \"\"\"\n    If normalize=False:\n        Popularly known as PMI.\n        This function calculates the log-ratio-measure which is used by\n        coherence measures such as c_v.\n        This is defined as: m_lr(S_i) = log[(P(W', W*) + e) / (P(W') * P(W*))]\n\n    If normalize=True:\n        This function calculates the normalized-log-ratio-measure, popularly knowns as\n        NPMI which is used by coherence measures such as c_v.\n        This is defined as: m_nlr(S_i) = m_lr(S_i) / -log[P(W', W*) + e]\n\n    Args:\n    ----\n    segmented topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.\n    per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics\n    num_docs : Total number of documents in corpus. Used for calculating probability.\n\n    Returns:\n    -------\n    m_lr : List of log ratio measures on each set in segmented topics.\n    \"\"\"\n    m_lr = []\n    for s_i in segmented_topics:\n        for w_prime, w_star in s_i:\n            w_prime_docs = per_topic_postings[w_prime]\n            w_star_docs = per_topic_postings[w_star]\n            co_docs = w_prime_docs.intersection(w_star_docs)\n            if normalize:\n                # For normalized log ratio measure\n                numerator = log_ratio_measure([[(w_prime, w_star)]], per_topic_postings, num_docs)[0]\n                co_doc_prob = len(co_docs) / float(num_docs)\n                m_lr_i = numerator / (-np.log(co_doc_prob + EPSILON))\n            else:\n                # For log ratio measure without normalization\n                numerator = (len(co_docs) / float(num_docs)) + EPSILON\n                denominator = (len(w_prime_docs) / float(num_docs)) * (len(w_star_docs) / float(num_docs))\n                m_lr_i = np.log(numerator / denominator)\n            m_lr.append(m_lr_i)\n\n    return m_lr\n", "path": "gensim/topic_coherence/direct_confirmation_measure.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) 2013 Radim Rehurek <radimrehurek@seznam.cz>\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\n\"\"\"\nThis module contains functions to compute direct confirmation on a pair of words or word subsets.\n\"\"\"\n\nimport logging\nimport numpy as np\n\nlogger = logging.getLogger(__name__)\n\nEPSILON = 1e-12  # Should be small. Value as suggested in paper.\n\ndef log_conditional_probability(segmented_topics, per_topic_postings, num_docs):\n    \"\"\"\n    This function calculates the log-conditional-probability measure\n    which is used by coherence measures such as U_mass.\n    This is defined as: m_lc(S_i) = log[(P(W', W*) + e) / P(W*)]\n\n    Args:\n    ----\n    segmented_topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.\n    per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics.\n    num_docs : Total number of documents in corresponding corpus.\n\n    Returns:\n    -------\n    m_lc : List of log conditional probability measure on each set in segmented topics.\n    \"\"\"\n    m_lc = []\n    for s_i in segmented_topics:\n        for w_prime, w_star in s_i:\n            w_prime_docs = per_topic_postings[w_prime]\n            w_star_docs = per_topic_postings[w_star]\n            co_docs = w_prime_docs.intersection(w_star_docs)\n            if  w_star_docs:\n                m_lc_i = np.log(((len(co_docs) / float(num_docs)) + EPSILON) / (len(w_star_docs) / float(num_docs)))\n            else:\n                m_lc_i = 0\n            m_lc.append(m_lc_i)\n\n    return m_lc\n\ndef log_ratio_measure(segmented_topics, per_topic_postings, num_docs, normalize=False):\n    \"\"\"\n    If normalize=False:\n        Popularly known as PMI.\n        This function calculates the log-ratio-measure which is used by\n        coherence measures such as c_v.\n        This is defined as: m_lr(S_i) = log[(P(W', W*) + e) / (P(W') * P(W*))]\n\n    If normalize=True:\n        This function calculates the normalized-log-ratio-measure, popularly knowns as\n        NPMI which is used by coherence measures such as c_v.\n        This is defined as: m_nlr(S_i) = m_lr(S_i) / -log[P(W', W*) + e]\n\n    Args:\n    ----\n    segmented topics : Output from the segmentation module of the segmented topics. Is a list of list of tuples.\n    per_topic_postings : Output from the probability_estimation module. Is a dictionary of the posting list of all topics\n    num_docs : Total number of documents in corpus. Used for calculating probability.\n\n    Returns:\n    -------\n    m_lr : List of log ratio measures on each set in segmented topics.\n    \"\"\"\n    m_lr = []\n    for s_i in segmented_topics:\n        for w_prime, w_star in s_i:\n            w_prime_docs = per_topic_postings[w_prime]\n            w_star_docs = per_topic_postings[w_star]\n            co_docs = w_prime_docs.intersection(w_star_docs)\n            if normalize:\n                # For normalized log ratio measure\n                numerator = log_ratio_measure([[(w_prime, w_star)]], per_topic_postings, num_docs)[0]\n                co_doc_prob = len(co_docs) / float(num_docs)\n                m_lr_i = numerator / (-np.log(co_doc_prob + EPSILON))\n            else:\n                # For log ratio measure without normalization\n                numerator = (len(co_docs) / float(num_docs)) + EPSILON\n                denominator = (len(w_prime_docs) / float(num_docs)) * (len(w_star_docs) / float(num_docs))\n                m_lr_i = np.log(numerator / denominator)\n            m_lr.append(m_lr_i)\n\n    return m_lr\n", "path": "gensim/topic_coherence/direct_confirmation_measure.py"}]}