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.

155 lines
5.1 KiB

  1. from django.test import TestCase
  2. from seqeval.metrics.sequence_labeling import get_entities
  3. from ..models import Label, Document
  4. from ..utils import BaseStorage, ClassificationStorage, SequenceLabelingStorage, Seq2seqStorage, CoNLLParser
  5. from ..utils import Color
  6. class TestColor(TestCase):
  7. def test_random_color(self):
  8. color = Color.random()
  9. self.assertTrue(0 <= color.red <= 255)
  10. self.assertTrue(0 <= color.green <= 255)
  11. self.assertTrue(0 <= color.blue <= 255)
  12. def test_hex(self):
  13. color = Color(red=255, green=192, blue=203)
  14. self.assertEqual(color.hex, '#ffc0cb')
  15. def test_contrast_color(self):
  16. color = Color(red=255, green=192, blue=203)
  17. self.assertEqual(color.contrast_color.hex, '#000000')
  18. color = Color(red=199, green=21, blue=133)
  19. self.assertEqual(color.contrast_color.hex, '#ffffff')
  20. class TestBaseStorage(TestCase):
  21. def test_extract_label(self):
  22. data = [{'labels': ['positive']}, {'labels': ['negative']}]
  23. actual = BaseStorage.extract_label(data)
  24. self.assertEqual(actual, [['positive'], ['negative']])
  25. def test_exclude_created_labels(self):
  26. labels = ['positive', 'negative']
  27. created = {'positive': Label(text='positive')}
  28. actual = BaseStorage.exclude_created_labels(labels, created)
  29. self.assertEqual(actual, ['negative'])
  30. def test_to_serializer_format(self):
  31. labels = ['positive']
  32. created = {}
  33. actual = BaseStorage.to_serializer_format(labels, created, random_seed=123)
  34. self.assertEqual(actual, [{
  35. 'text': 'positive',
  36. 'prefix_key': None,
  37. 'suffix_key': 'p',
  38. 'background_color': '#0d1668',
  39. 'text_color': '#ffffff',
  40. }])
  41. def test_get_shortkey_without_existing_shortkey(self):
  42. label = 'positive'
  43. created = {}
  44. actual = BaseStorage.get_shortkey(label, created)
  45. self.assertEqual(actual, ('p', None))
  46. def test_get_shortkey_with_existing_shortkey(self):
  47. label = 'positive'
  48. created = {('p', None)}
  49. actual = BaseStorage.get_shortkey(label, created)
  50. self.assertEqual(actual, ('p', 'ctrl'))
  51. def test_update_saved_labels(self):
  52. saved = {'positive': Label(text='positive', text_color='#000000')}
  53. new = [Label(text='positive', text_color='#ffffff')]
  54. actual = BaseStorage.update_saved_labels(saved, new)
  55. self.assertEqual(actual['positive'].text_color, '#ffffff')
  56. class TestClassificationStorage(TestCase):
  57. def test_extract_unique_labels(self):
  58. labels = [['positive'], ['positive', 'negative'], ['negative']]
  59. actual = ClassificationStorage.extract_unique_labels(labels)
  60. self.assertCountEqual(actual, ['positive', 'negative'])
  61. def test_make_annotations(self):
  62. docs = [Document(text='a', id=1), Document(text='b', id=2), Document(text='c', id=3)]
  63. labels = [['positive'], ['positive', 'negative'], ['negative']]
  64. saved_labels = {'positive': Label(text='positive', id=1), 'negative': Label(text='negative', id=2)}
  65. actual = ClassificationStorage.make_annotations(docs, labels, saved_labels)
  66. self.assertCountEqual(actual, [
  67. {'document': 1, 'label': 1},
  68. {'document': 2, 'label': 1},
  69. {'document': 2, 'label': 2},
  70. {'document': 3, 'label': 2},
  71. ])
  72. class TestSequenceLabelingStorage(TestCase):
  73. def test_extract_unique_labels(self):
  74. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  75. actual = SequenceLabelingStorage.extract_unique_labels(labels)
  76. self.assertCountEqual(actual, ['LOC', 'ORG'])
  77. def test_make_annotations(self):
  78. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  79. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  80. saved_labels = {'LOC': Label(text='LOC', id=1), 'ORG': Label(text='ORG', id=2)}
  81. actual = SequenceLabelingStorage.make_annotations(docs, labels, saved_labels)
  82. self.assertEqual(actual, [
  83. {'document': 1, 'label': 1, 'start_offset': 0, 'end_offset': 1},
  84. {'document': 2, 'label': 2, 'start_offset': 3, 'end_offset': 4},
  85. ])
  86. class TestSeq2seqStorage(TestCase):
  87. def test_make_annotations(self):
  88. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  89. labels = [['Hello!'], ['How are you?', "What's up?"]]
  90. actual = Seq2seqStorage.make_annotations(docs, labels)
  91. self.assertEqual(actual, [
  92. {'document': 1, 'text': 'Hello!'},
  93. {'document': 2, 'text': 'How are you?'},
  94. {'document': 2, 'text': "What's up?"},
  95. ])
  96. class TestCoNLLParser(TestCase):
  97. def test_calc_char_offset(self):
  98. words = ['EU', 'rejects', 'German', 'call']
  99. tags = ['B-ORG', 'O', 'B-MISC', 'O']
  100. entities = get_entities(tags)
  101. actual = CoNLLParser.calc_char_offset(words, tags)
  102. self.assertEqual(entities, [('ORG', 0, 0), ('MISC', 2, 2)])
  103. self.assertEqual(actual, {
  104. 'text': 'EU rejects German call',
  105. 'labels': [[0, 2, 'ORG'], [11, 17, 'MISC']]
  106. })