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] score-documents: handle document titles with newlines #754

Merged
merged 1 commit into from
Nov 25, 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
10 changes: 10 additions & 0 deletions orangecontrib/text/widgets/owscoredocuments.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ def __init__(self, parent=None):
super().__init__(parent=parent)
self._extremes = {}

@staticmethod
def simplify(s):
"""Remove tab and newline characters from the string"""
for ch in "\n\t\r":
s = s.replace(ch, " ")
return s

def data(self, index, role=Qt.DisplayRole):
if index.column() > 0 and role == gui.BarRatioRole and index.isValid():
# for all except first columns return ratio for distribution bar
Expand All @@ -291,6 +298,9 @@ def data(self, index, role=Qt.DisplayRole):
return (value - vmin) / ((vmax - vmin) or 1)
if role in (gui.BarRatioRole, Qt.DisplayRole):
dat = super().data(index, Qt.EditRole)
if role == Qt.DisplayRole and index.column() == 0:
# in document title column remove newline characters from titles
dat = self.simplify(dat)
return dat
if role == Qt.BackgroundColorRole and index.column() == 0:
return TableModel.ColorForRole[TableModel.Meta]
Expand Down
9 changes: 9 additions & 0 deletions orangecontrib/text/widgets/tests/test_owscoredocuments.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,15 @@ def test_output_unique(self):
output = self.get_output(self.widget.Outputs.selected_documents)
self.assertTrue("Word count (1)" in output.domain)

def test_titles_no_newline(self):
corpus = Corpus.from_file("andersen")
corpus.metas[0, 0] = corpus.metas[0, 0] + "\ntest"
corpus.set_title_variable("Title")
self.send_signal(self.widget.Inputs.corpus, corpus)
self.assertEqual(
"The Little Match-Seller test", self.widget.view.model().index(0, 0).data()
)


if __name__ == "__main__":
unittest.main()