lexnlp.tests package¶
Submodules¶
lexnlp.tests.dictionary_comparer module¶
-
class
lexnlp.tests.dictionary_comparer.
DictionaryComparer
(check_order: bool = True, allow_extra_key: bool = False, number_precision_percent: float = 0.001)¶ Bases:
object
-
check_dicts_equal
(expected: dict, actual: dict) → List[str]¶
-
compare_list_of_dicts
(expected: List[dict], actual: List[dict]) → List[str]¶
-
lexnlp.tests.lexnlp_tests module¶
Common routines for testing NLP functions against test data stored separately in CSV files.
-
lexnlp.tests.lexnlp_tests.
assert_in
(function_name: str, text: str, expected_in, actual_results: Set, problems_file: str = '/home/alex/dev/michael/contraxsuite/lexpredict-lexnlp/benchmarks/problems_hr.txt', do_raise: bool = True, do_write_to_file: bool = True, test_data_file: str = None) → Optional[str]¶
-
lexnlp.tests.lexnlp_tests.
assert_set_equal
(function_name: str, text: str, actual_results: Set, expected_results: Set, problems_file: str = '/home/alex/dev/michael/contraxsuite/lexpredict-lexnlp/benchmarks/problems_hr.txt', do_raise: bool = True, do_write_to_file: bool = True, debug_print: bool = True, test_data_file: str = None) → Optional[str]¶
-
lexnlp.tests.lexnlp_tests.
benchmark
(benchmark_name: str, func: Callable, *args, benchmark_file: str = '/home/alex/dev/michael/contraxsuite/lexpredict-lexnlp/benchmarks/benchmarks.csv', **kwargs)¶
-
lexnlp.tests.lexnlp_tests.
benchmark_decorator
(function, *args, **kwargs)¶
-
lexnlp.tests.lexnlp_tests.
benchmark_extraction_func
(func: Callable, text, **kwargs)¶
-
lexnlp.tests.lexnlp_tests.
build_extraction_func_name
(func: Callable, **kwargs)¶
-
lexnlp.tests.lexnlp_tests.
fmt_results
(results: Union[Set, List, Tuple])¶
-
lexnlp.tests.lexnlp_tests.
fmt_short_text
(text: str, max_len: int = 40)¶
-
lexnlp.tests.lexnlp_tests.
iter_test_data_text_and_tuple
(file_name: str = None, call_stack_offset: int = 0)¶ Reads test data from external file and iterates through pairs of text -> expected value. Types of values are lost during write-read to csv operations and they all are returned as strings. :param file_name: Optional parameter - to read data from the specified file :param call_stack_offset: Optional parameter to allow proper calculation of test data file name when this function is called not straight from the test method but from a stack of other methods. :return:
-
lexnlp.tests.lexnlp_tests.
test_extraction_func
(expected, func: Callable, text, benchmark_name: str = None, test_data_file: str = None, expected_data_converter: Callable = None, actual_data_converter: Callable = None, do_raise: bool = True, debug_print: bool = False, test_only_expected_in: bool = False, **kwargs)¶
-
lexnlp.tests.lexnlp_tests.
test_extraction_func_on_test_data
(func: Callable, benchmark_name: str = None, expected_data_converter: Callable = None, actual_data_converter: Callable = None, test_only_expected_in: bool = False, debug_print: bool = False, start_from_csv_line: int = None, test_data_path: str = None, **kwargs)¶ Tests the provided function against the test data loaded from external file. The provided function is expected to extract a tuple of some objects from string text. Format of the extracted tuple should match the format in test data. Test data is expected to be in the file name calculated by this_test_data_path(..) method. For each “text -> expected tuple” entry from test data file the result of the provided function is calculated and compared against the expected one as lists using “assert_list_equal”
-
lexnlp.tests.lexnlp_tests.
this_test_data_path
(create_dirs: bool = False, caller_stack_offset: int = 1)¶ Calculate test data file path for the test function which called this one. The path/name are calculated using python execution stack trace routines. Template: test_data/pack/a/ge/test_file_name/test_function_name.csv :param create_dirs: If set to true then all the folders required to writing the test data file will be created. :param caller_stack_offset: Offset of the test function in this function’s call stack. Should be 1 if this function is called straight from the test function. Plus 1 for each next intermediate function call. :return:
-
lexnlp.tests.lexnlp_tests.
write_test_data_text_and_tuple
(texts: tuple, values: tuple, column_names: tuple)¶ Writes test data to external file for further using in tests. File name is calculated by this_test_data_path(..) function (test_data/pack/a/ge/test_file_name/test_method_name). Test data is written in CSV format with the special rules to allow multiple expected value tuples per single text. Header: Text,Component 1 Title,…,Component N Title
Text 1,Value Component 1.1 of Text 1,…,Value Component 1.N of Text 1 ,Value Component 2.1 of Text 1,…,Value Component 2.N of Text 1 … ,Value Component M.1 of Text 1,…,Value Component M.N of Text 1 Text 2,Value Component 1.1 of Text 2,…,Value Component 1.N of Text 2 ,Value Component 2.1 of Text 2,…,Value Component 2.N of Text 2 … ,Value Component K.1 of Text 2,…,Value Component K.N of Text 2 …
So if there is no text in the first column in a row - this means to match the values to the last filled text above.
- Parameters
texts – Tuple of text stirngs (“texts” column).
values – Tuple of expected values matching texts on the same index (expected values column(s))
column_names – Names of the columns. Should be: Text, Name of First Entry in Values, Name of Second Entry…
- Returns
lexnlp.tests.typed_annotations_tests module¶
-
class
lexnlp.tests.typed_annotations_tests.
ParsingError
(**kwargs)¶ Bases:
object
-
class
lexnlp.tests.typed_annotations_tests.
TypedAnnotationsTester
(**kwargs)¶ Bases:
object
-
check_case_annotations
(ants: List[lexnlp.extract.common.annotations.text_annotation.TextAnnotation], case: lexnlp.tests.typed_annotations_tests.TypedAntTestCase, case_index: int)¶
-
process_cases
() → Tuple[int, int]¶
-
process_file
()¶
-
process_one_check
(ant: lexnlp.extract.common.annotations.text_annotation.TextAnnotation, check: lexnlp.tests.typed_annotations_tests.TypedFieldCheck, case_index: int, check_index: int) → None¶
-
read_cases
()¶ Sample file contains:
1.11.SCADA System means a supervisory control and data acquisition system such as the S/3 Software or Licensee’s OASyS(R) product. ——– total=1 0)locale=”en” 0)trademark=”OASyS (R)”
-
test_and_raise_errors
(parsing_method: Callable, file_name: str, entity_type: Type)¶
-
test_parser
(parsing_method: Callable, file_name: str, entity_type: Type)¶
-
-
class
lexnlp.tests.typed_annotations_tests.
TypedFieldCheck
(index: int = 0, path: List[str] = None, value: str = '', comparison: str = '=', check_all: bool = False)¶ Bases:
object
Class is used to check one annotation’s attribute against provided value E.g., “0)date.month<6” means:
check first annotation within the sample
check that annotation’s month value strictly less than 6
- or “..)coords.1>0” means:
check that coords[1] > 0 for all (..) annotations in this sample
-
compare_values
(val: Any) → Tuple[bool, bool]¶
-
comparison_name
= {'!=': 'not equal to', '<': 'less than', '<=': 'less or equal to', '=': 'equal to', '>': 'greater than', '>=': 'greater or equal to'}¶
-
property
comparison_string
¶
-
date_formats
= ['%Y.%m.%d', '%Y-%m-%d']¶
-
datetime_formats
= ['%Y.%m.%d %H:%M:%S', '%Y-%m-%d %H:%M:%S', '%Y.%m.%d %H:%M', '%Y-%m-%d %H:%M']¶
-
get_ant_field_value
(ant: lexnlp.extract.common.annotations.text_annotation.TextAnnotation) → Any¶
-
static
parse
(line: str)¶
-
parse_date
(val_type: Type) → datetime.date¶
-
property
path_string
¶
-
reg_compare_split
= regex.Regex('!=|<=|>=|=|<|>', flags=regex.V0)¶
lexnlp.tests.upload_benchmarks module¶
-
lexnlp.tests.upload_benchmarks.
build_index_name
(index_prefix: str, d: None.datetime.date) → str¶
-
lexnlp.tests.upload_benchmarks.
parse_args
(args)¶
-
lexnlp.tests.upload_benchmarks.
process_data
(csv_file_name: str, index: str, process_func: Callable[List[Dict], int])¶
-
lexnlp.tests.upload_benchmarks.
safe
()¶
-
lexnlp.tests.upload_benchmarks.
safe_float
(*args, **kwargs)¶
-
lexnlp.tests.upload_benchmarks.
safe_int
(*args, **kwargs)¶
lexnlp.tests.utility_for_testing module¶
-
lexnlp.tests.utility_for_testing.
annotate_text
(text: str, annotations: List[lexnlp.extract.common.annotations.text_annotation.TextAnnotation]) → str¶ - Parameters
text – source text
annotations – text annotations
- Returns
html, where annotations are replaced by HREFs + annotations’ list in the end of the document
-
lexnlp.tests.utility_for_testing.
load_resource_document
(doc_path: str, encoding: str = 'ascii') → str¶ load file as string from test_data folder
-
lexnlp.tests.utility_for_testing.
save_test_document
(doc_path: str, text: str, encoding: str = 'utf-8') → None¶ saves text as a file in test_data folder