Coverage for nlp_manager/tests/regex_parser_tests.py: 100%
11 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/nlp_manager/tests/regex_parser_tests.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26Unit tests.
28"""
30import unittest
32from crate_anon.nlp_manager.regex_parser import (
33 OPTIONAL_POC,
34 OPTIONAL_RESULTS_IGNORABLES,
35 RELATION,
36 TENSE_INDICATOR,
37)
38from crate_anon.nlp_manager.tests.regex_test_helperfunc import (
39 assert_text_regex,
40)
43# =============================================================================
44# Unit tests
45# =============================================================================
48class ParserRegexesTests(unittest.TestCase):
49 @staticmethod
50 def test_parser_regexes() -> None:
51 verbose = True
53 # ---------------------------------------------------------------------
54 # Things to ignore
55 # ---------------------------------------------------------------------
57 assert_text_regex(
58 "OPTIONAL_RESULTS_IGNORABLES",
59 OPTIONAL_RESULTS_IGNORABLES,
60 [
61 ("(H)", ["(H)", ""]),
62 (" (H) ", [" (H) ", ""]),
63 (" (H) mg/L", [" (H) ", "", "", "", "L", ""]),
64 ("(HH)", ["(HH)", ""]),
65 ("(L)", ["(L)", ""]),
66 ("(LL)", ["(LL)", ""]),
67 ("(*)", ["(*)", ""]),
68 (" | (H) | ", [" | (H) | ", ""]),
69 ],
70 verbose=verbose,
71 )
72 assert_text_regex(
73 "OPTIONAL_POC",
74 OPTIONAL_POC,
75 [
76 (", POC", [", POC", ""]),
77 ],
78 )
80 # ---------------------------------------------------------------------
81 # Tense indicators
82 # ---------------------------------------------------------------------
84 assert_text_regex(
85 "TENSE_INDICATOR",
86 TENSE_INDICATOR,
87 [
88 ("a is b", ["is"]),
89 ("a was b", ["was"]),
90 ("a blah b", []),
91 ],
92 verbose=verbose,
93 )
95 # ---------------------------------------------------------------------
96 # Mathematical relations
97 # ---------------------------------------------------------------------
99 assert_text_regex(
100 "RELATION",
101 RELATION,
102 [
103 ("a < b", ["<"]),
104 ("a less than b", ["less than"]),
105 ("a <= b", ["<="]),
106 ("a = b", ["="]),
107 ("a equals b", ["equals"]),
108 ("a equal to b", ["equal to"]),
109 ("a >= b", [">="]),
110 ("a > b", [">"]),
111 ("a more than b", ["more than"]),
112 ("a greater than b", ["greater than"]),
113 ("a blah b", []),
114 ],
115 verbose=verbose,
116 )