From cdec86df403cab19decc7013b6d392c6802036eb Mon Sep 17 00:00:00 2001 From: IanCa Date: Mon, 17 Jun 2024 18:11:16 -0500 Subject: [PATCH 1/2] Add a search util function to convert to long --- hed/models/basic_search_util.py | 38 ++++++++++++++++++++++++++ tests/models/test_basic_search_util.py | 20 ++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 hed/models/basic_search_util.py create mode 100644 tests/models/test_basic_search_util.py diff --git a/hed/models/basic_search_util.py b/hed/models/basic_search_util.py new file mode 100644 index 00000000..8029094f --- /dev/null +++ b/hed/models/basic_search_util.py @@ -0,0 +1,38 @@ +""" +Utilities to support HED searches based on strings. +""" +from hed.models.hed_string import HedString +from hed.models.hed_tag import HedTag + + +def convert_query_to_form(search_query, schema): + """Converts the given basic search query into a hed_string + + Parameters: + search_query(str): The basic search query to convert. + schema(HedSchema): The schema to use to convert tags + + Returns: + long_query(str): The converted search query, in long form. + """ + input_tags = HedString.split_hed_string(search_query) + output_string = "" + skippable_prefix = ("@", "~") + skippable_suffix = ("*", ) + for is_hed_tag, (startpos, endpos) in input_tags: + input_tag = search_query[startpos:endpos] + add_suffix = "" + if is_hed_tag: + if input_tag.startswith(skippable_prefix): + output_string += input_tag[:1] + input_tag = input_tag[1:] + + if input_tag.endswith(skippable_suffix): + add_suffix = input_tag[-1:] + input_tag = input_tag[:-1] + output_string += HedTag(input_tag, schema).long_tag + output_string += add_suffix + else: + output_string += input_tag + + return output_string diff --git a/tests/models/test_basic_search_util.py b/tests/models/test_basic_search_util.py new file mode 100644 index 00000000..91574f4c --- /dev/null +++ b/tests/models/test_basic_search_util.py @@ -0,0 +1,20 @@ +import unittest +from hed import load_schema_version +from hed.models.basic_search_util import convert_query_to_form + + +class TestConvertQueryToForm(unittest.TestCase): + schema = load_schema_version("8.3.0") + + def test_basic_convert(self): + input = "@Event, Head-part*, Time-interval/1" + expected_output = "@Event, Item/Biological-item/Anatomical-item/Body-part/Head-part*, Property/Data-property/Data-value/Spatiotemporal-value/Temporal-value/Time-interval/1" + + actual_output = convert_query_to_form(input, self.schema) + self.assertEqual(expected_output, actual_output) + + input = "@Head-part*, Event, Time-interval/1" + expected_output = "@Item/Biological-item/Anatomical-item/Body-part/Head-part*, Event, Property/Data-property/Data-value/Spatiotemporal-value/Temporal-value/Time-interval/1" + + actual_output = convert_query_to_form(input, self.schema) + self.assertEqual(expected_output, actual_output) \ No newline at end of file From 4d940712d3df7fd208037181098a409d34319ed4 Mon Sep 17 00:00:00 2001 From: IanCa Date: Mon, 17 Jun 2024 18:12:48 -0500 Subject: [PATCH 2/2] Rename convert_query --- hed/models/basic_search_util.py | 2 +- tests/models/test_basic_search_util.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hed/models/basic_search_util.py b/hed/models/basic_search_util.py index 8029094f..7e3224a5 100644 --- a/hed/models/basic_search_util.py +++ b/hed/models/basic_search_util.py @@ -5,7 +5,7 @@ from hed.models.hed_tag import HedTag -def convert_query_to_form(search_query, schema): +def convert_query(search_query, schema): """Converts the given basic search query into a hed_string Parameters: diff --git a/tests/models/test_basic_search_util.py b/tests/models/test_basic_search_util.py index 91574f4c..9701ca65 100644 --- a/tests/models/test_basic_search_util.py +++ b/tests/models/test_basic_search_util.py @@ -1,6 +1,6 @@ import unittest from hed import load_schema_version -from hed.models.basic_search_util import convert_query_to_form +from hed.models.basic_search_util import convert_query class TestConvertQueryToForm(unittest.TestCase): @@ -10,11 +10,11 @@ def test_basic_convert(self): input = "@Event, Head-part*, Time-interval/1" expected_output = "@Event, Item/Biological-item/Anatomical-item/Body-part/Head-part*, Property/Data-property/Data-value/Spatiotemporal-value/Temporal-value/Time-interval/1" - actual_output = convert_query_to_form(input, self.schema) + actual_output = convert_query(input, self.schema) self.assertEqual(expected_output, actual_output) input = "@Head-part*, Event, Time-interval/1" expected_output = "@Item/Biological-item/Anatomical-item/Body-part/Head-part*, Event, Property/Data-property/Data-value/Spatiotemporal-value/Temporal-value/Time-interval/1" - actual_output = convert_query_to_form(input, self.schema) + actual_output = convert_query(input, self.schema) self.assertEqual(expected_output, actual_output) \ No newline at end of file