Repository: biolab/orange3-text

Issue:
Duplicate Detection: output cluster, not first instance
<!--
This is an issue template. Please fill in the relevant details in the
sections below.
-->

##### Text version
<!-- From menu _Options→Add-ons→Orange3-Text_ or code `orangecontrib.text.version.full_version` -->
0.5.0

##### Orange version
<!-- From menu _Help→About→Version_ or code `Orange.version.full_version` -->
3.16.dev

##### Expected behavior
Duplicate Cluster outputs duplicate documents.


##### Actual behavior
Duplicate Cluster outputs the first document in a corpus.


##### Steps to reproduce the behavior
Corpus - Bag of Words - Distances - Duplicate Detection. Regardless of which cluster you select, the output will always be the first document of a corpus.


##### Additional info (worksheets, data, screenshots, ...)





Discussion:

Resolving patch:
diff --git a/orangecontrib/text/widgets/owduplicates.py b/orangecontrib/text/widgets/owduplicates.py
--- a/orangecontrib/text/widgets/owduplicates.py
+++ b/orangecontrib/text/widgets/owduplicates.py
@@ -228,13 +228,16 @@ def send_corpus_without_duplicates(self):
             self.Outputs.corpus_without_duplicates.send(None)
 
     def send_duplicates(self):
-        index = self.table_view.selectionModel().currentIndex().row()
-        cluster = self.table_model[index][0]
-        mask = np.flatnonzero(self.clustering_mask == cluster.id)
-        c = self.corpus[mask]
-        c.name = '{} {}'.format(self.Outputs.duplicates.name, cluster)
+        c = None
+        indices = self.table_view.selectionModel().selectedIndexes()
+        if indices:
+            cluster = self.table_view.model().data(indices[0], Qt.EditRole)
+            mask = np.flatnonzero(self.clustering_mask == cluster.id)
+            c = self.corpus[mask]
+            c.name = '{} {}'.format(self.Outputs.duplicates.name, cluster)
         self.Outputs.duplicates.send(c)
 
+
     def send_report(self):
         self.report_items([
             ('Linkage', self.LINKAGE[self.linkage_method]),


Test patch:
diff --git a/orangecontrib/text/widgets/tests/test_owduplicates.py b/orangecontrib/text/widgets/tests/test_owduplicates.py
new file mode 100644
--- /dev/null
+++ b/orangecontrib/text/widgets/tests/test_owduplicates.py
@@ -0,0 +1,36 @@
+import unittest
+
+from Orange.data import Table
+from Orange.distance import Euclidean
+from Orange.widgets.tests.base import WidgetTest
+
+from orangecontrib.text.widgets.owduplicates import OWDuplicates
+
+
+class TestCorpusViewerWidget(WidgetTest):
+    def setUp(self):
+        self.widget = self.create_widget(OWDuplicates)
+        self.data = Table.from_file('iris')
+        self.distances = Euclidean(self.data)
+
+    def test_duplicates(self):
+        self.send_signal(self.widget.Inputs.distances, self.distances)
+        self.widget.table_view.selectRow(0)
+        out_corpus = self.get_output(self.widget.Outputs.duplicates)
+        self.assertEqual(len(out_corpus), 2)
+        self.widget.table_view.selectRow(3)
+        out_corpus = self.get_output(self.widget.Outputs.duplicates)
+        self.assertEqual(len(out_corpus), 1)
+
+    def test_deselecting(self):
+        self.send_signal(self.widget.Inputs.distances, self.distances)
+        self.widget.table_view.selectRow(0)
+        out_corpus = self.get_output(self.widget.Outputs.duplicates)
+        self.assertTrue(out_corpus)
+        self.widget.table_view.clearSelection()
+        out_corpus = self.get_output(self.widget.Outputs.duplicates)
+        self.assertIsNone(out_corpus)
+
+if __name__ == "__main__":
+    unittest.main()
+