Add tool for merging phonagen json files into one.

This commit is contained in:
Feufochmar 2018-06-09 20:17:47 +02:00
parent 61f4e1bf35
commit ae69a4be26
2 changed files with 30 additions and 1 deletions

22
py-phonagen/phonagen-merge.py Executable file
View File

@ -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)

View File

@ -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)