summaryrefslogtreecommitdiff
path: root/scripts/edit-morph
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/edit-morph')
-rwxr-xr-xscripts/edit-morph42
1 files changed, 42 insertions, 0 deletions
diff --git a/scripts/edit-morph b/scripts/edit-morph
index 116b9681..244bbd61 100755
--- a/scripts/edit-morph
+++ b/scripts/edit-morph
@@ -90,6 +90,48 @@ class EditMorph(cliapp.Application):
with morphlib.savefile.SaveFile(file_name, 'w') as f:
morphology.write_to_file(f)
+ def cmd_to_json(self, args):
+ """Convert one or more FILES to JSON.
+
+ Assumes a .yaml extension, which will be removed in the output file.
+ """
+
+ if len(args) == 0:
+ raise cliapp.AppException("to-json expects one or more filenames")
+
+ for file_name in args:
+ try:
+ with open(file_name, 'r') as f:
+ text = f.read()
+ morphology = morphlib.morph2.Morphology(text)
+
+ if not file_name.endswith('.yaml'):
+ raise morphlib.Error('file name does not end with .yaml')
+ out_file_name = file_name[:-len('.yaml')]
+
+ with morphlib.savefile.SaveFile(out_file_name, 'w') as f:
+ morphology.update_text(text, f, convert_to='json')
+ except Exception as e:
+ print '%s: %s' % (file_name, e)
+
+ def cmd_to_yaml(self, args):
+ """Convert one or more FILES to YAML.
+
+ Adds a .yaml extension for each input file."""
+
+ if len(args) == 0:
+ raise cliapp.AppException("to-yaml expects one or more filenames")
+
+ for file_name in args:
+ try:
+ with open(file_name, 'r') as f:
+ text = f.read()
+ morphology = morphlib.morph2.Morphology(text)
+ with morphlib.savefile.SaveFile(file_name + '.yaml', 'w') as f:
+ morphology.update_text(text, f, convert_to='yaml')
+ except Exception as e:
+ print '%s: %s' % (file_name, e)
+
def sort_chunks(self, chunks_list):
"""Sort stratum chunks