import os import tempfile import unittest import numpy as np from quapy.data.reader import from_text, from_sparse, from_csv, reindex_labels, binarize class TestReader(unittest.TestCase): def _write_tmp(self, content, suffix='.txt'): fd, path = tempfile.mkstemp(suffix=suffix) with os.fdopen(fd, 'w') as f: f.write(content) self.addCleanup(os.remove, path) return path def test_from_text(self): path = self._write_tmp('1\tthis is positive\n0\tthis is negative\n') sentences, labels = from_text(path, verbose=0) self.assertEqual(sentences, ['this is positive', 'this is negative']) self.assertEqual(labels, [1, 0]) def test_from_text_skips_malformed_lines(self): # a line without a tab separator should be skipped (and warned about), not raise path = self._write_tmp('1\tgood line\nthis line has no label\n0\tanother good line\n') sentences, labels = from_text(path, verbose=0) self.assertEqual(sentences, ['good line', 'another good line']) self.assertEqual(labels, [1, 0]) def test_from_sparse(self): # format: