def apply_cleaner_to_text(cls, cleaner, text, scheme, set_checked=False):
        """
        Applies a cleaning function to a text, and returns a label if the cleaned value wasn't NC.

        :param cleaner: Cleaning function to apply.
        :type cleaner: function of str -> str
        :param text: Text to apply the cleaner to.
        :type text: str
        :param scheme: Scheme containing codes which the string returned from the `cleaner` can be matched against.
        :type scheme: core_data_modules.data_models.CodeScheme
        :param set_checked: Whether to set the `checked` property of the applied Label.
        :type set_checked: bool
        """
        clean_value = cleaner(text)

        # Don't label data which the cleaners couldn't code
        if clean_value == Codes.NOT_CODED:
            return None

        # Construct a label for the clean_value returned by the cleaner
        code_id = scheme.get_code_with_match_value(clean_value)
        origin_id = Metadata.get_function_location(cleaner)
        return cls.make_label_from_cleaner_code(scheme,
                                                code_id,
                                                origin_id,
                                                set_checked=set_checked)
Example #2
0
 def test_get_function_location(self):
     function_location = Metadata.get_function_location(self.dummy_function)
     # call_location contains an absolute path, but this only tests the end of that path so that it can run
     # independently of the project's location.
     self.assertTrue(function_location.endswith("tests/traced_data/test_traced_data.py:19:dummy_function"))