[383a81]: / development / testcase / rasa_test.py

Download this file

169 lines (148 with data), 7.3 kB

  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
import os
import sys
import random
import string
import asyncio
import unittest
from rasa.model import get_model
from rasa.core.agent import Agent
from rasa.nlu.test import run_evaluation
from rasa.core.test import test as core_test
path = './production/rasa-server/rasa/'
sys.path.append(path)
class TestRasaMethods(unittest.TestCase):
model_path = './production/rasa-server/rasa/models/'
unpacked_model = get_model(model_path)
tmp_name = ''.join(random.choices(
string.ascii_uppercase + string.digits, k=10))
"""
NLU TEST
"""
nlu_model = os.path.join(unpacked_model, "nlu")
# Normal NLU tests data
test_data = './production/rasa-server/rasa/train_test_split/test_data.yml'
async_test_result = run_evaluation(test_data,
nlu_model,
successes=True,
errors=True,
output_directory=f'/tmp/{tmp_name}',
disable_plotting=True,
report_as_dict=True,
)
test_result = asyncio.run(async_test_result)
# Typo NLU tests data
test_data_typo = './production/rasa-server/rasa/train_test_split/test_data_typo.yml'
async_test_result_typo = run_evaluation(test_data_typo,
nlu_model,
successes=True,
errors=True,
output_directory=f'/tmp/{tmp_name}',
disable_plotting=True,
report_as_dict=True,
)
test_result_typo = asyncio.run(async_test_result_typo)
"""
CORE TEST
"""
# Normal Core test data
test_story = './production/rasa-server/rasa/tests/test_stories.yml'
_agent = Agent.load(unpacked_model)
async_test_results_core = core_test(test_story,
_agent,
e2e=False,
disable_plotting=True,
)
test_result_core = asyncio.run(async_test_results_core)
# Test f1_score of intents
def test_f1_intent(self):
threshold = 0.9
test_result = self.test_result
# Check if intent extractor is in the pipeline
if test_result['intent_evaluation'] is not None:
# Check if multiple intent extractors are in the pipeline
if 'report' not in test_result['intent_evaluation']:
for intent in test_result['intent_evaluation']:
f1_score = test_result['intent_evaluation'][intent]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['intent_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Test f1_score of entities
def test_f1_entity(self):
threshold = 0.9
test_result = self.test_result
# Check if entity extractor is in the pipeline
if test_result['entity_evaluation'] is not None:
# Check if multiple entity extractors are in the pipeline
if 'report' not in test_result['entity_evaluation']:
for entity in test_result['entity_evaluation']:
f1_score = test_result['entity_evaluation'][entity]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['entity_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Test f1_score of reponse selectors
def test_f1_response_selector(self):
threshold = 0.9
test_result = self.test_result
# Check if reponse selectors is in the pipeline
if test_result['response_selection_evaluation'] is not None:
# Check if multiple reponse selectors are in the pipeline
if 'report' not in test_result['response_selection_evaluation']:
for entity in test_result['response_selection_evaluation']:
f1_score = test_result['response_selection_evaluation'][entity]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['response_selection_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Test f1_score of intents - Typo contained data
def test_f1_intent_typo(self):
threshold = 0.9
test_result = self.test_result_typo
# Check if intent extractor is in the pipeline
if test_result['intent_evaluation'] is not None:
# Check if multiple intent extractors are in the pipeline
if 'report' not in test_result['intent_evaluation']:
for intent in test_result['intent_evaluation']:
f1_score = test_result['intent_evaluation'][intent]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['intent_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Test f1_score of entities - Typo contained data
def test_f1_entity_typo(self):
threshold = 0.9
test_result = self.test_result_typo
# Check if entity extractor is in the pipeline
if test_result['entity_evaluation'] is not None:
# Check if multiple entity extractors are in the pipeline
if 'report' not in test_result['entity_evaluation']:
for entity in test_result['entity_evaluation']:
f1_score = test_result['entity_evaluation'][entity]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['entity_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Test f1_score of reponse selectors - Typo contained data
def test_f1_response_selector_typo(self):
threshold = 0.9
test_result = self.test_result_typo
# Check if reponse selectors is in the pipeline
if test_result['response_selection_evaluation'] is not None:
# Check if multiple reponse selectors are in the pipeline
if 'report' not in test_result['response_selection_evaluation']:
for entity in test_result['response_selection_evaluation']:
f1_score = test_result['response_selection_evaluation'][entity]['f1_score']
self.assertTrue(f1_score > threshold)
else:
f1_score = test_result['response_selection_evaluation']['f1_score']
self.assertTrue(f1_score > threshold)
# Check f1_score of the rasa core - Test stories
def test_f1_core(self):
threshold = 0.8
test_result = self.test_result_core
f1_score = test_result['f1']
self.assertTrue(f1_score > threshold)
if __name__ == '__main__':
# Run tests
unittest.main()