Commit
·
830b384
1
Parent(s):
5f2da4c
Add script for Neo-GATE formatting
Browse files# Why is the change needed?
This change introduces the adaptation script for Neo-GATE.
# What changes does the patch introduce?
The patch adds the script `neo-gate_adapt.py`, which formats annotations and references with the forms found in the provided tagset mapping. A tagset mapping is a dictionary which maps tags from Neo-GATE's tagset to the desired forms to be evaluated.
# How was this patch tested?
Manual runs.
- neo-gate_adapt.py +84 -0
neo-gate_adapt.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2024 FBK
|
2 |
+
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License
|
14 |
+
import argparse
|
15 |
+
import json
|
16 |
+
import pandas as pd
|
17 |
+
from typing import Dict, List, Tuple
|
18 |
+
|
19 |
+
|
20 |
+
__version__ = '0.1'
|
21 |
+
|
22 |
+
|
23 |
+
def get_tagset_mapping(tagset_filename: str) -> dict:
|
24 |
+
"""
|
25 |
+
Loads a `dict` from the JSON file, which should be organized as a dictionary containing
|
26 |
+
tags from Neo-GATE's tagset as keys and the corresponding desired forms as values.
|
27 |
+
|
28 |
+
For an example of a tagset mapping, see the file `schwa.json`.
|
29 |
+
For more information, visit https://huggingface.co/datasets/FBK-MT/Neo-GATE.
|
30 |
+
"""
|
31 |
+
with open(tagset_filename, 'r') as tagset_file:
|
32 |
+
return json.load(tagset_file)
|
33 |
+
|
34 |
+
|
35 |
+
def get_neogate(neogate_filename: str) -> Tuple[List[str], List[str]]:
|
36 |
+
"""
|
37 |
+
Reads the tagged references and the annotations from the corresponding columns of the NeoGATE
|
38 |
+
TSV file.
|
39 |
+
"""
|
40 |
+
neogate = pd.read_csv(neogate_filename, delimiter='\t')
|
41 |
+
return neogate['REF-TAGGED'].to_list(), neogate['ANNOTATION'].to_list()
|
42 |
+
|
43 |
+
|
44 |
+
def annotate_with_tags(tagset: Dict[str, str], str_to_annotate: str) -> str:
|
45 |
+
"""
|
46 |
+
Replaces all the tags defined in `tagset` with the corresponding form in `str_to_annotate`.
|
47 |
+
"""
|
48 |
+
for key in tagset:
|
49 |
+
str_to_annotate = str_to_annotate.replace(key, tagset[key])
|
50 |
+
return str_to_annotate
|
51 |
+
|
52 |
+
|
53 |
+
def main(args):
|
54 |
+
tagset = get_tagset_mapping(args.tagset)
|
55 |
+
refs, anns = get_neogate(args.neogate)
|
56 |
+
assert len(refs) == len(anns), \
|
57 |
+
"The number of references does not match the number of annotations."
|
58 |
+
with open(f"{args.out}.ann", 'w') as annotations_file, \
|
59 |
+
open(f"{args.out}.ref", 'w') as references_file:
|
60 |
+
for ann, ref in zip(refs, anns):
|
61 |
+
annotations_file.write(annotate_with_tags(tagset, ann))
|
62 |
+
annotations_file.write("\n")
|
63 |
+
references_file.write(annotate_with_tags(tagset, ref))
|
64 |
+
references_file.write("\n")
|
65 |
+
|
66 |
+
|
67 |
+
if __name__ == '__main__':
|
68 |
+
"""
|
69 |
+
This script adapts Neo-GATE's annotations and references to the desired neomorpheme paradigm.
|
70 |
+
The script requires a JSON file containing the tagset mapping for the desired paradigm.
|
71 |
+
For more information, visit https://huggingface.co/datasets/FBK-MT/Neo-GATE.
|
72 |
+
|
73 |
+
The resulting references will be saved in a `.ref` file, whereas the annotations will be saved
|
74 |
+
in a `.ann` file.
|
75 |
+
"""
|
76 |
+
print(f"Neo-GATE adaptation script {__version__}.")
|
77 |
+
parser = argparse.ArgumentParser()
|
78 |
+
parser.add_argument('--neogate', type=str, default='./Neo-GATE.tsv',
|
79 |
+
help="TSV file containing Neo-GATE.")
|
80 |
+
parser.add_argument('--tagset', type=str, required=True,
|
81 |
+
help="JSON file containing tags as keys and the desired forms as values.")
|
82 |
+
parser.add_argument('--out', type=str, help="Output file name.", required=True)
|
83 |
+
args = parser.parse_args()
|
84 |
+
main(args)
|