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

[FIX] Fix infer text features #645

Merged
merged 2 commits into from
Apr 9, 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/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ def _infer_text_features(self):
if attr.is_string:
if first is None:
first = attr
if attr.attributes.get('include', 'False') == 'True':
incl = attr.attributes.get('include', False)
# variable attributes can be boolean from Orange 3.29
# they are string in older versions
# incl == True, since without == string "False" would be True
if incl == "True" or incl == True:
include_feats.append(attr)
if len(include_feats) == 0 and first:
include_feats.append(first)
Expand Down
29 changes: 29 additions & 0 deletions orangecontrib/text/tests/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,35 @@ def test_infer_text_features(self):
self.assertEqual(len(tf), 1)
self.assertEqual(tf[0].name, 'Text')

def test_infer_text_features_str_include(self):
"""
In orange 3.29 include attribute is read as boolean. corpus must still
support older versions of Orange where include attribute is a string.
Test behaviour with string attribute.
"""
c = Corpus.from_file('andersen')
c.domain["Content"].attributes["include"] = "False"
c._infer_text_features()
self.assertListEqual(c.text_features, [c.domain["Title"]])

c.domain["Content"].attributes["include"] = "True"
c._infer_text_features()
self.assertListEqual(c.text_features, [c.domain["Content"]])

def test_infer_text_features_bool_include(self):
"""
In orange 3.29 include attribute is read as boolean.
Test behaviour with boolean attribute.
"""
c = Corpus.from_file('andersen')
c.domain["Content"].attributes["include"] = False
c._infer_text_features()
self.assertListEqual(c.text_features, [c.domain["Title"]])

c.domain["Content"].attributes["include"] = True
c._infer_text_features()
self.assertListEqual(c.text_features, [c.domain["Content"]])

def test_documents(self):
c = Corpus.from_file('book-excerpts')
docs = c.documents
Expand Down
8 changes: 2 additions & 6 deletions orangecontrib/text/widgets/owcorpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,5 @@ def describe(features):


if __name__ == '__main__':
from AnyQt.QtWidgets import QApplication
app = QApplication([])
widget = OWCorpus()
widget.show()
app.exec()
widget.saveSettings()
from orangewidget.utils.widgetpreview import WidgetPreview
WidgetPreview(OWCorpus).run()