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

Word Cloud: Remove words with zero weights from word cloud #501

Merged
merged 2 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 5 additions & 17 deletions orangecontrib/text/widgets/owwordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,13 @@ def _bow_words(self):
This function extract words from bag of words features and assign them
the frequency which is average bow count.
"""
bow_features = self._get_bow_variables()
if not bow_features:
return {}

average_bows = {
f.name: self.corpus.get_column_view(f)[0].mean()
for f in bow_features
f.name: self.corpus.X[:, i].mean()
for i, f in enumerate(self.corpus.domain.attributes)
ajdapretnar marked this conversation as resolved.
Show resolved Hide resolved
if f.attributes.get("bow-feature", False)
}
return average_bows

def _get_bow_variables(self):
"""
Extract bow variables from data
"""
return [
var
for var in self.corpus.domain.variables
if var.attributes.get("bow-feature", False)
]
# return only positive bow weights (those == 0 are non-existing words)
return {f: w for f, w in average_bows.items() if w > 0}

def handleNewSignals(self):
if self.topic is not None and len(self.topic):
Expand Down
35 changes: 22 additions & 13 deletions orangecontrib/text/widgets/tests/test_owworldcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ def test_bow_features(self):
v.attributes["bow-feature"] = True

self.send_signal(self.widget.Inputs.corpus, data)
self.assertDictEqual(
self.widget.corpus_counter, {"Word1": 1, "Word2": 2, "Word3": 2})
weights = list(zip(*sorted(self.widget.corpus_counter.items())))[1]
# due to computation error in computing mean use array_almost_equal
np.testing.assert_array_almost_equal(weights, [1, 2, 2])

output = self.get_output(self.widget.Outputs.word_counts)
np.testing.assert_array_equal([2, 2, 1], output.X.flatten())
np.testing.assert_array_almost_equal([2, 2, 1], output.X.flatten())
np.testing.assert_array_equal(
["Word2", "Word3", "Word1"], output.metas.flatten())
self.assertListEqual(
[(2.0, 'Word2'), (2.0, 'Word3'), (1.0, 'Word1')],
self.widget.tablemodel[:])
["Word3", "Word2", "Word1"], output.metas.flatten())
self.assertTupleEqual(
("Word3", "Word2", "Word1"),
list(zip(*self.widget.tablemodel[:]))[1])
np.testing.assert_array_almost_equal(
[2, 2, 1],
list(zip(*self.widget.tablemodel[:]))[0])

# try with one word not bow-feature
data = self.corpus[:3]
Expand All @@ -81,15 +86,19 @@ def test_bow_features(self):
v.attributes["bow-feature"] = True

self.send_signal(self.widget.Inputs.corpus, data)
self.assertDictEqual(
self.widget.corpus_counter, {"Word1": 1, "Word2": 2})
weights = list(zip(*sorted(self.widget.corpus_counter.items())))[1]
np.testing.assert_array_almost_equal(weights, [1, 2])

output = self.get_output(self.widget.Outputs.word_counts)
np.testing.assert_array_equal([2, 1], output.X.flatten())
np.testing.assert_array_almost_equal([2, 1], output.X.flatten())
np.testing.assert_array_equal(
["Word2", "Word1"], output.metas.flatten())
self.assertListEqual(
[(2.0, 'Word2'), (1.0, 'Word1')],
self.widget.tablemodel[:])
self.assertTupleEqual(
("Word2", "Word1"),
list(zip(*self.widget.tablemodel[:]))[1])
np.testing.assert_array_almost_equal(
[2, 1],
list(zip(*self.widget.tablemodel[:]))[0])

def test_bow_info(self):
"""
Expand Down