Skip to content

gold2correct

Helper functions for converting goldstandard files to .correct.txt files.

main()

Convert goldstandard files to .correct.txt files.

Source code in corpustools/gold2correct.py
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def main():
    """Convert goldstandard files to .correct.txt files."""
    args = parse_options()

    sanity_check()

    logging.getLogger().setLevel(logging.INFO)
    converter_manager = ConverterManager(
        goldstandard=True,
    )

    converter_manager.collect_files(args.sources)
    gold_files = [
        orig_file
        for orig_file in converter_manager.files
        if not orig_file.orig.name.endswith("correct.txt")
    ]
    if gold_files:
        print(f"Found {len(gold_files)} goldstandard files to convert")

    for orig_file in gold_files:
        try:
            converter_manager.convert(orig_file)
        except (AttributeError, NameError) as error:
            print(f"Conversion failed for {orig_file.orig}: {error}")
            continue

        if not orig_file.converted.exists():
            print(f"\nCould not convert {orig_file}")
            continue

        xml_printer = XMLPrinter(lang=orig_file.lang, all_paragraphs=True)
        xml_printer.parse_file(orig_file.converted.absolute().as_posix())
        text = xml_printer.process_file().getvalue().replace(" ΒΆ", "\n")

        test_text = "".join(line.strip() for line in text.splitlines())
        if not test_text:
            print(
                f"\nFile {orig_file.orig} is empty after conversion, "
                "not writing .correct.txt file"
            )
            continue

        try:
            correct_path = make_corpus_path(
                orig_file.orig.with_suffix(".correct.txt").as_posix()
            )
            mover(
                orig_file,
                correct_path,
            )
            correct_file = orig_file.orig.with_suffix(".correct.txt")
            correct_file.write_text(text, encoding="utf-8")
            metadata = make_corpus_path(correct_file.as_posix()).metadata
            metadata.set_variable("conversion_status", "correct")
            metadata.write_file()

        except GitCommandError as error:
            if "destination exists" in str(error):
                orig_vcs = versioncontrol.vcs(orig_file.orig_corpus_dir)
                orig_vcs.remove(orig_file.orig)
                orig_vcs.remove(orig_file.xsl)
            else:
                raise

parse_options()

Parse the commandline options.

Returns:

Type Description
Namespace

the parsed commandline arguments

Source code in corpustools/gold2correct.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def parse_options():
    """Parse the commandline options.

    Returns:
        (argparse.Namespace): the parsed commandline arguments
    """
    parser = argparse.ArgumentParser(
        parents=[argparse_version.parser],
        description="Convert goldstandard go .correct.txt files.",
    )

    parser.add_argument(
        "sources",
        nargs="+",
        help="The original file(s) or \
                        directory/ies where the original files exist",
    )

    args = parser.parse_args()

    return args