Esempio n. 1
0
    def to_con(self, write_location=None):
        """
        Formats the Annotation object to a valid con file. Optionally writes the string to a specified location.
        :param write_location: Optional path to an output file; if provided but not an existing file, will be
            created. If this parameter is not provided, nothing will be written to file.
        :return: A string representation of the annotations in the con format.
        """
        if not self.source_text_path:
            raise FileNotFoundError(
                "The annotation can not be converted to the con format without the source text."
                " Please provide the path to the source text as source_text_path in the object's"
                " constructor.")

        temp_ann_file = tempfile.mktemp()
        with open(temp_ann_file, 'w+') as f:
            self.to_ann(f.name)
            con_text = convert_brat_to_con(f.name, self.source_text_path)

        if write_location:
            if os.path.isfile(write_location):
                logging.warning("Overwriting file at: %s", write_location)
            with open(write_location, 'w+') as f:
                f.write(con_text)

        return con_text
Esempio n. 2
0
 def test_invalid_brat_text(self):
     """Assert that invalid brat text produces no output."""
     con_output = convert_brat_to_con(self.bad_brat_file_path,
                                      self.text_file_path)
     self.assertFalse(con_output)
Esempio n. 3
0
 def test_invalid_file_path(self):
     """Passes an invalid file path to convert_brat_to_con()."""
     with self.assertRaises(FileNotFoundError):
         convert_brat_to_con("this isn't a valid file path",
                             "neither is this")
Esempio n. 4
0
 def test_valid_brat_matching_text_name(self):
     """
     Assert that the con output matches the sample con text when the automatic text-file-finding feature is utilized
     """
     con_output = convert_brat_to_con(self.brat_file_path)
     self.assertEqual(con_output, con_text)
Esempio n. 5
0
 def test_valid_brat_to_con(self):
     """Convert the test file from brat to con. Assert that the con output matches the sample con text."""
     con_output = convert_brat_to_con(self.brat_file_path,
                                      self.text_file_path)
     self.assertEqual(con_output, con_text)