From ae69a4be26e3b1d1b6c196ea4f63cf40a5028c1d Mon Sep 17 00:00:00 2001 From: Feufochmar Date: Sat, 9 Jun 2018 20:17:47 +0200 Subject: [PATCH] Add tool for merging phonagen json files into one. --- py-phonagen/phonagen-merge.py | 22 ++++++++++++++++++++++ py-phonagen/phonagen.py | 9 ++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 py-phonagen/phonagen-merge.py diff --git a/py-phonagen/phonagen-merge.py b/py-phonagen/phonagen-merge.py new file mode 100755 index 0000000..17358ec --- /dev/null +++ b/py-phonagen/phonagen-merge.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python3 +import argparse +import phonagen + +def parseArgs(): + # Define argument parser + parser = argparse.ArgumentParser(description='Merge several phonagen files into a single one.') + parser.add_argument('files', metavar='file', help='files to convert merge', nargs='+') + parser.add_argument('--output', metavar='output-file', help='Output file for the generator. The file is printed to standard output if not given.', default='') + # Parse arguments + return parser.parse_args() + +# Main +if __name__ == '__main__': + args = parseArgs() + outputPhonagenFile = phonagen.PhonagenFile() + for file in args.files: + phonagenFile = phonagen.PhonagenFile() + phonagenFile.load(file) + outputPhonagenFile.mergeFrom(phonagenFile) + # + outputPhonagenFile.writeTo(args.output) diff --git a/py-phonagen/phonagen.py b/py-phonagen/phonagen.py index 6254993..492e43b 100644 --- a/py-phonagen/phonagen.py +++ b/py-phonagen/phonagen.py @@ -143,7 +143,7 @@ class ChainGenerator(Generator): for chainStruct in struct['chains']: dist = Distribution() dist.fromJsonStruct(chainStruct['possible-outputs'], itemRef = 'value', occurencesRef = 'occurences') - self.chains.update({chainStruct['input']: dist}) + self.chains.update({tuple(chainStruct['input']): dist}) def fromExamples(self, file, phonology): """Train a chain generator on an example file""" @@ -232,3 +232,10 @@ class PhonagenFile: else: with open(file, 'w', encoding='utf-8') as outputFile: json.dump(outputStruct, outputFile, ensure_ascii=False) + + def mergeFrom(self, otherFile): + """Add all phonologies and generators from the other file into this one.""" + for phonology in otherFile.phonologies.values(): + self.addPhonology(phonology) + for generator in otherFile.generators.values(): + self.addGenerator(generator)