Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OW Corpus Viewer: Add annotated corpus output #672

Merged
merged 2 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion orangecontrib/text/widgets/owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from Orange.data.domain import filter_visible
from Orange.widgets import gui
from Orange.widgets.settings import Setting, ContextSetting, PerfectDomainContextHandler
from Orange.widgets.utils.annotated_data import create_annotated_table
from Orange.widgets.widget import OWWidget, Msg, Input, Output
from orangecontrib.text.corpus import Corpus

Expand All @@ -31,6 +32,7 @@ class Inputs:
class Outputs:
matching_docs = Output("Matching Docs", Corpus, default=True)
other_docs = Output("Other Docs", Corpus)
corpus = Output("Corpus", Corpus)

settingsHandler = PerfectDomainContextHandler(
match_values = PerfectDomainContextHandler.MATCH_VALUES_ALL
Expand Down Expand Up @@ -422,7 +424,7 @@ def update_info(self):
self.ngram_range = ''

def commit(self):
matched = unmatched = None
matched = unmatched = annotated_corpus = None
corpus = self.corpus
if corpus is not None:
# it returns a set of selected documents which are in view
Expand All @@ -437,8 +439,10 @@ def commit(self):

matched = corpus[matched_mask] if len(matched_mask) else None
unmatched = corpus[unmatched_mask] if len(unmatched_mask) else None
annotated_corpus = create_annotated_table(corpus, matched_mask)
self.Outputs.matching_docs.send(matched)
self.Outputs.other_docs.send(unmatched)
self.Outputs.corpus.send(annotated_corpus)

def send_report(self):
self.report_items((
Expand Down
20 changes: 19 additions & 1 deletion orangecontrib/text/widgets/tests/test_owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def test_output(self):
self.assertEqual(
9, len(self.get_output(self.widget.Outputs.other_docs))
)
self.assertEqual(
len(self.corpus.domain.metas) + 1,
len(self.get_output(self.widget.Outputs.corpus).domain.metas)
)

self.widget.doc_list.selectAll() # selects current documents in list
self.assertEqual(
Expand All @@ -89,14 +93,22 @@ def test_output(self):
self.assertEqual(
5, len(self.get_output(self.widget.Outputs.other_docs))
)
output = self.get_output(self.widget.Outputs.corpus)
self.assertEqual(
len(self.get_output(self.widget.Outputs.matching_docs)),
sum(output.get_column_view("Selected")[0])
)

self.widget.regexp_filter = "human"
self.process_events()
# empty because none of mathching documents is selected
# empty because none of matching documents is selected
self.assertIsNone(self.get_output(self.widget.Outputs.matching_docs))
self.assertEqual(
9, len(self.get_output(self.widget.Outputs.other_docs))
)
output = self.get_output(self.widget.Outputs.corpus)
self.assertEqual(0,
sum(output.get_column_view("Selected")[0]))

self.widget.doc_list.selectAll()
self.assertEqual(
Expand All @@ -105,10 +117,16 @@ def test_output(self):
self.assertEqual(
4, len(self.get_output(self.widget.Outputs.other_docs))
)
output = self.get_output(self.widget.Outputs.corpus)
self.assertEqual(
len(self.get_output(self.widget.Outputs.matching_docs)),
sum(output.get_column_view("Selected")[0])
)

self.send_signal(self.widget.Inputs.corpus, None)
self.assertIsNone(self.get_output(self.widget.Outputs.matching_docs))
self.assertIsNone(self.get_output(self.widget.Outputs.other_docs))
self.assertIsNone(self.get_output(self.widget.Outputs.corpus))

def test_empty_corpus(self):
self.send_signal(self.widget.Inputs.corpus, self.corpus[:0])
Expand Down