def mapping_to_file(self, output_path: str = GEN_DIR, file_type: str = 'json'): ''' Write mapping to file ''' if not os.path.isdir(output_path): raise Exception("Path %s is not a directory", output_path) fn = os.path.join( output_path, self.kwargs.get('in_lang', 'und') + "_to_" + self.kwargs.get('out_lang', 'und') + "." + file_type) fieldnames = ['in', 'out', 'context_before', 'context_after'] filtered = [{k: v for k, v in io.items() if k in fieldnames} for io in self.mapping] if file_type == 'json': with open(fn, 'w', encoding='utf8') as f: json.dump(filtered, f, indent=4) elif file_type == 'csv': with open(fn, 'w', encoding='utf8') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) for io in filtered: writer.writerow(io) else: raise exceptions.IncorrectFileType( f'File type {file_type} is invalid.')
def mapping_to_file(self, output_path: str = GEN_DIR, file_type: str = "json"): """ Write mapping to file """ if not os.path.isdir(output_path): raise Exception("Path %s is not a directory", output_path) fn = os.path.join( output_path, self.kwargs.get("in_lang", "und") + "_to_" + self.kwargs.get("out_lang", "und") + "." + file_type, ) fieldnames = ["in", "out", "context_before", "context_after"] filtered = [{k: v for k, v in io.items() if k in fieldnames} for io in self.mapping] if file_type == "json": with open(fn, "w", encoding="utf8") as f: json.dump(filtered, f, indent=4, ensure_ascii=False, cls=CompactJSONMappingEncoder) elif file_type == "csv": with open(fn, "w", encoding="utf8") as f: writer = csv.DictWriter(f, fieldnames=fieldnames) for io in filtered: writer.writerow(io) else: raise exceptions.IncorrectFileType( f"File type {file_type} is invalid.")
def load_abbreviations_from_file(path): ''' Helper method to load abbreviations from file. ''' if path.endswith('csv'): abbs = [] with open(path, encoding='utf8') as f: reader = csv.reader(f) abbs = flatten_abbreviations(reader) else: raise exceptions.IncorrectFileType( f'Sorry, abbreviations must be stored as CSV files. You provided the following: {path}') return abbs
def load_from_file(path: str) -> list: """ Helper method to load mapping from file. """ if path.endswith("csv"): mapping = load_from_csv(path, ",") elif path.endswith("tsv"): mapping = load_from_csv(path, "\t") elif path.endswith("psv"): mapping = load_from_csv(path, "|") elif path.endswith("xlsx"): mapping = load_from_workbook(path) elif path.endswith("json"): with open(path, encoding="utf8") as f: mapping = json.load(f) else: raise exceptions.IncorrectFileType( f"File {path} is not a valid mapping filetype." ) return validate(mapping, path)
def load_abbreviations_from_file(path): """ Helper method to load abbreviations from file. """ if path.endswith("csv"): abbs = [] with open(path, encoding="utf8") as f: reader = csv.reader(f, delimiter=",") abbs = flatten_abbreviations(reader) elif path.endswith("tsv"): abbs = [] with open(path, encoding="utf8") as f: reader = csv.reader(f, delimiter="\t") abbs = flatten_abbreviations(reader) elif path.endswith("psv"): abbs = [] with open(path, encoding="utf8") as f: reader = csv.reader(f, delimiter="|") abbs = flatten_abbreviations(reader) else: raise exceptions.IncorrectFileType( f"Sorry, abbreviations must be stored as CSV/TSV/PSV files. You provided the following: {path}" ) return abbs