-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
test_extractor.py
176 lines (165 loc) · 5.5 KB
/
test_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from typing import Any, Text, Dict, List
import pytest
from rasa.nlu.constants import TEXT
from rasa.nlu.training_data import Message
from rasa.nlu.extractors.extractor import EntityExtractor
from rasa.nlu.tokenizers.whitespace_tokenizer import WhitespaceTokenizer
from rasa.nlu.training_data.formats import MarkdownReader
@pytest.mark.parametrize(
"text, tags, confidences, expected_entities",
[
(
"I am flying from San Fransisco to Amsterdam",
{
"entity": ["O", "O", "O", "O", "city", "city", "O", "city"],
"role": ["O", "O", "O", "O", "from", "from", "O", "to"],
},
{
"entity": [1.0, 1.0, 1.0, 1.0, 0.98, 0.78, 1.0, 0.89],
"role": [1.0, 1.0, 1.0, 1.0, 0.98, 0.78, 1.0, 0.89],
},
[
{
"entity": "city",
"start": 17,
"end": 30,
"value": "San Fransisco",
"role": "from",
"confidence_entity": 0.78,
"confidence_role": 0.78,
},
{
"entity": "city",
"start": 34,
"end": 43,
"value": "Amsterdam",
"role": "to",
"confidence_entity": 0.89,
"confidence_role": 0.89,
},
],
),
(
"I am flying from San Fransisco to Amsterdam",
{
"entity": ["O", "O", "O", "O", "city", "city", "O", "city"],
"group": ["O", "O", "O", "O", "1", "1", "O", "1"],
},
None,
[
{
"entity": "city",
"start": 17,
"end": 30,
"value": "San Fransisco",
"group": "1",
},
{
"entity": "city",
"start": 34,
"end": 43,
"value": "Amsterdam",
"group": "1",
},
],
),
(
"Amsterdam",
{"entity": ["city"], "role": ["O"], "group": ["O"]},
None,
[{"entity": "city", "start": 0, "end": 9, "value": "Amsterdam"}],
),
(
"New-York",
{"entity": ["city", "city"], "role": ["O", "O"], "group": ["O", "O"]},
None,
[{"entity": "city", "start": 0, "end": 8, "value": "New-York"}],
),
(
"Amsterdam, Berlin, and London",
{
"entity": ["city", "city", "O", "city"],
"role": ["O", "O", "O", "O"],
"group": ["O", "O", "O", "O"],
},
None,
[
{"entity": "city", "start": 0, "end": 9, "value": "Amsterdam"},
{"entity": "city", "start": 11, "end": 17, "value": "Berlin"},
{"entity": "city", "start": 23, "end": 29, "value": "London"},
],
),
(
"Amsterdam Berlin and London",
{
"entity": ["city", "city", "O", "city"],
"role": ["O", "O", "O", "O"],
"group": ["O", "O", "O", "O"],
},
None,
[
{"entity": "city", "start": 0, "end": 16, "value": "Amsterdam Berlin"},
{"entity": "city", "start": 21, "end": 27, "value": "London"},
],
),
],
)
def test_convert_tags_to_entities(
text: Text,
tags: Dict[Text, List[Text]],
confidences: Dict[Text, List[float]],
expected_entities: List[Dict[Text, Any]],
):
extractor = EntityExtractor()
tokenizer = WhitespaceTokenizer()
message = Message(text)
tokens = tokenizer.tokenize(message, TEXT)
actual_entities = extractor.convert_predictions_into_entities(
text, tokens, tags, confidences
)
assert actual_entities == expected_entities
@pytest.mark.parametrize(
"text, warnings",
[
(
"## intent:test\n"
"- I want to fly from [Berlin](location) to [ San Fransisco](location)\n",
1,
),
(
"## intent:test\n"
"- I want to fly from [Berlin ](location) to [San Fransisco](location)\n",
1,
),
(
"## intent:test\n"
"- I want to fly from [Berlin](location) to [San Fransisco.](location)\n"
"- I have nothing to say.",
1,
),
(
"## intent:test\n"
"- I have nothing to say.\n"
"- I want to fly from [Berlin](location) to[San Fransisco](location)\n",
1,
),
(
"## intent:test\n"
"- I want to fly from [Berlin](location) to[San Fransisco](location)\n"
"- Book a flight from [London](location) to [Paris.](location)\n",
2,
),
],
)
def test_check_check_correct_entity_annotations(text: Text, warnings: int):
reader = MarkdownReader()
tokenizer = WhitespaceTokenizer()
training_data = reader.reads(text)
tokenizer.train(training_data)
with pytest.warns(UserWarning) as record:
EntityExtractor.check_correct_entity_annotations(training_data)
assert len(record) == warnings
assert all(
[excerpt in record[0].message.args[0]]
for excerpt in ["Misaligned entity annotation in sentence"]
)