You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.8 KiB

5 years ago
  1. import io
  2. from django.test import TestCase
  3. from seqeval.metrics.sequence_labeling import get_entities
  4. from ..models import Label, Document
  5. from ..utils import BaseStorage, ClassificationStorage, SequenceLabelingStorage, Seq2seqStorage, CoNLLParser
  6. from ..utils import iterable_to_io
  7. class TestBaseStorage(TestCase):
  8. def test_extract_label(self):
  9. data = [{'labels': ['positive']}, {'labels': ['negative']}]
  10. actual = BaseStorage.extract_label(data)
  11. self.assertEqual(actual, [['positive'], ['negative']])
  12. def test_exclude_created_labels(self):
  13. labels = ['positive', 'negative']
  14. created = {'positive': Label(text='positive')}
  15. actual = BaseStorage.exclude_created_labels(labels, created)
  16. self.assertEqual(actual, ['negative'])
  17. def test_to_serializer_format(self):
  18. labels = ['positive']
  19. created = {}
  20. actual = BaseStorage.to_serializer_format(labels, created)
  21. self.assertEqual(len(actual), 1)
  22. self.assertEqual(actual[0]['text'], 'positive')
  23. self.assertIsNone(actual[0]['prefix_key'])
  24. self.assertEqual(actual[0]['suffix_key'], 'p')
  25. self.assertIsNotNone(actual[0]['background_color'])
  26. self.assertIsNotNone(actual[0]['text_color'])
  27. def test_get_shortkey_without_existing_shortkey(self):
  28. label = 'positive'
  29. created = {}
  30. actual = BaseStorage.get_shortkey(label, created)
  31. self.assertEqual(actual, ('p', None))
  32. def test_get_shortkey_with_existing_shortkey(self):
  33. label = 'positive'
  34. created = {('p', None)}
  35. actual = BaseStorage.get_shortkey(label, created)
  36. self.assertEqual(actual, ('p', 'ctrl'))
  37. def test_update_saved_labels(self):
  38. saved = {'positive': Label(text='positive', text_color='#000000')}
  39. new = [Label(text='positive', text_color='#ffffff')]
  40. actual = BaseStorage.update_saved_labels(saved, new)
  41. self.assertEqual(actual['positive'].text_color, '#ffffff')
  42. class TestClassificationStorage(TestCase):
  43. def test_extract_unique_labels(self):
  44. labels = [['positive'], ['positive', 'negative'], ['negative']]
  45. actual = ClassificationStorage.extract_unique_labels(labels)
  46. self.assertCountEqual(actual, ['positive', 'negative'])
  47. def test_make_annotations(self):
  48. docs = [Document(text='a', id=1), Document(text='b', id=2), Document(text='c', id=3)]
  49. labels = [['positive'], ['positive', 'negative'], ['negative']]
  50. saved_labels = {'positive': Label(text='positive', id=1), 'negative': Label(text='negative', id=2)}
  51. actual = ClassificationStorage.make_annotations(docs, labels, saved_labels)
  52. self.assertCountEqual(actual, [
  53. {'document': 1, 'label': 1},
  54. {'document': 2, 'label': 1},
  55. {'document': 2, 'label': 2},
  56. {'document': 3, 'label': 2},
  57. ])
  58. class TestSequenceLabelingStorage(TestCase):
  59. def test_extract_unique_labels(self):
  60. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  61. actual = SequenceLabelingStorage.extract_unique_labels(labels)
  62. self.assertCountEqual(actual, ['LOC', 'ORG'])
  63. def test_make_annotations(self):
  64. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  65. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  66. saved_labels = {'LOC': Label(text='LOC', id=1), 'ORG': Label(text='ORG', id=2)}
  67. actual = SequenceLabelingStorage.make_annotations(docs, labels, saved_labels)
  68. self.assertEqual(actual, [
  69. {'document': 1, 'label': 1, 'start_offset': 0, 'end_offset': 1},
  70. {'document': 2, 'label': 2, 'start_offset': 3, 'end_offset': 4},
  71. ])
  72. class TestSeq2seqStorage(TestCase):
  73. def test_make_annotations(self):
  74. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  75. labels = [['Hello!'], ['How are you?', "What's up?"]]
  76. actual = Seq2seqStorage.make_annotations(docs, labels)
  77. self.assertEqual(actual, [
  78. {'document': 1, 'text': 'Hello!'},
  79. {'document': 2, 'text': 'How are you?'},
  80. {'document': 2, 'text': "What's up?"},
  81. ])
  82. class TestCoNLLParser(TestCase):
  83. def test_calc_char_offset(self):
  84. f = io.BytesIO(
  85. b"EU\tORG\n"
  86. b"rejects\t_\n"
  87. b"German\tMISC\n"
  88. b"call\t_\n"
  89. )
  90. actual = next(CoNLLParser().parse(f))[0]
  91. self.assertEqual(actual, {
  92. 'text': 'EU rejects German call',
  93. 'labels': [[0, 2, 'ORG'], [11, 17, 'MISC']]
  94. })
  95. class TestIterableToIO(TestCase):
  96. def test(self):
  97. def iterable():
  98. yield b'fo'
  99. yield b'o\nbar\n'
  100. yield b'baz\nrest'
  101. stream = iterable_to_io(iterable())
  102. stream = io.TextIOWrapper(stream)
  103. self.assertEqual(stream.readlines(), ['foo\n', 'bar\n', 'baz\n', 'rest'])