File size: 2,405 Bytes
025384a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b75ad3
 
025384a
 
 
 
 
 
 
70d4503
9b75ad3
70d4503
9b75ad3
025384a
 
 
 
 
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
import json
import unittest

from aip_trainer.lambdas import lambdaSpeechToScore
from tests import EVENTS_FOLDER


def check_output_by_field(output, key, match, expected_output):
    import re

    assert len(output[key].strip()) > 0
    for word in output[key].lstrip().rstrip().split(" "):
        word_check = re.findall(match, word.strip())
        assert len(word_check) == 1
        assert word_check[0] == word.strip()
    output[key] = expected_output[key]
    return output


class TestGetAccuracyFromRecordedAudio(unittest.TestCase):
    def test_GetAccuracyFromRecordedAudio(self):
        self.maxDiff = None

        with open(EVENTS_FOLDER / "GetAccuracyFromRecordedAudio.json", "r") as src:
            inputs_outputs = json.load(src)
        inputs = inputs_outputs["inputs"]
        outputs = inputs_outputs["outputs"]
        for event_name, event_content in inputs.items():
            expected_output = outputs[event_name]
            output = lambdaSpeechToScore.lambda_handler(event_content, [])
            output = json.loads(output)
            assert len(output["matched_transcripts"].strip()) > 0
            assert len(output["matched_transcripts_ipa"].strip()) > 0
            assert len(output["ipa_transcript"].strip()) > 0
            assert len(output["real_transcripts_ipa"].strip()) > 0
            output = check_output_by_field(output, "is_letter_correct_all_words", '[01]+', expected_output)
            output = check_output_by_field(output, "end_time", '\d+\.\d+', expected_output)
            output = check_output_by_field(output, "start_time", '\d+\.\d+', expected_output)
            output = check_output_by_field(output, "pronunciation_accuracy", '\d+', expected_output)
            output["matched_transcripts"] = expected_output["matched_transcripts"]
            output["matched_transcripts_ipa"] = expected_output["matched_transcripts_ipa"]
            output["pronunciation_accuracy"] = expected_output["pronunciation_accuracy"]
            output["pair_accuracy_category"] = expected_output["pair_accuracy_category"]
            output["ipa_transcript"] = expected_output["ipa_transcript"]
            output["real_transcript"] = expected_output["real_transcript"]
            output["real_transcripts_ipa"] = expected_output["real_transcripts_ipa"]
            self.assertEqual(expected_output, output)


if __name__ == '__main__':
    unittest.main()