Neo-GATE / neo-gate_adapt.py
apiergentili's picture
Add script for Neo-GATE formatting
830b384
raw
history blame
3.36 kB
# Copyright 2024 FBK
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
import argparse
import json
import pandas as pd
from typing import Dict, List, Tuple
__version__ = '0.1'
def get_tagset_mapping(tagset_filename: str) -> dict:
"""
Loads a `dict` from the JSON file, which should be organized as a dictionary containing
tags from Neo-GATE's tagset as keys and the corresponding desired forms as values.
For an example of a tagset mapping, see the file `schwa.json`.
For more information, visit https://huggingface.co/datasets/FBK-MT/Neo-GATE.
"""
with open(tagset_filename, 'r') as tagset_file:
return json.load(tagset_file)
def get_neogate(neogate_filename: str) -> Tuple[List[str], List[str]]:
"""
Reads the tagged references and the annotations from the corresponding columns of the NeoGATE
TSV file.
"""
neogate = pd.read_csv(neogate_filename, delimiter='\t')
return neogate['REF-TAGGED'].to_list(), neogate['ANNOTATION'].to_list()
def annotate_with_tags(tagset: Dict[str, str], str_to_annotate: str) -> str:
"""
Replaces all the tags defined in `tagset` with the corresponding form in `str_to_annotate`.
"""
for key in tagset:
str_to_annotate = str_to_annotate.replace(key, tagset[key])
return str_to_annotate
def main(args):
tagset = get_tagset_mapping(args.tagset)
refs, anns = get_neogate(args.neogate)
assert len(refs) == len(anns), \
"The number of references does not match the number of annotations."
with open(f"{args.out}.ann", 'w') as annotations_file, \
open(f"{args.out}.ref", 'w') as references_file:
for ann, ref in zip(refs, anns):
annotations_file.write(annotate_with_tags(tagset, ann))
annotations_file.write("\n")
references_file.write(annotate_with_tags(tagset, ref))
references_file.write("\n")
if __name__ == '__main__':
"""
This script adapts Neo-GATE's annotations and references to the desired neomorpheme paradigm.
The script requires a JSON file containing the tagset mapping for the desired paradigm.
For more information, visit https://huggingface.co/datasets/FBK-MT/Neo-GATE.
The resulting references will be saved in a `.ref` file, whereas the annotations will be saved
in a `.ann` file.
"""
print(f"Neo-GATE adaptation script {__version__}.")
parser = argparse.ArgumentParser()
parser.add_argument('--neogate', type=str, default='./Neo-GATE.tsv',
help="TSV file containing Neo-GATE.")
parser.add_argument('--tagset', type=str, required=True,
help="JSON file containing tags as keys and the desired forms as values.")
parser.add_argument('--out', type=str, help="Output file name.", required=True)
args = parser.parse_args()
main(args)