Skip to content

Instantly share code, notes, and snippets.

@lfoppiano
Created August 11, 2020 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfoppiano/3aca6180bcd338a7c624737b3813e200 to your computer and use it in GitHub Desktop.
Save lfoppiano/3aca6180bcd338a7c624737b3813e200 to your computer and use it in GitHub Desktop.
Migrate delft preprocessors to JSON
import json
import os
import pathlib
import sys
from delft.sequenceLabelling.preprocess import WordPreprocessor
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Invalid parameters. Usage: python json_migration.py model directory. "
"The new preprocessor will be saved in the same directory as the old preprocessor ")
sys.exit(-1)
input_directory = sys.argv[1]
if os.path.isdir(input_directory):
for file in os.listdir(input_directory):
if file.endswith("preprocessor.pkl"):
input_file= pathlib.Path(input_directory, file)
p = WordPreprocessor().load(input_file)
output_dict = vars(p)
output_path = pathlib.Path(input_file.with_suffix('').as_posix() + '.json')
with open(output_path, 'w') as fp:
json.dump(output_dict, fp, sort_keys=False, indent=4)
elif os.path.isfile(input_directory):
print("Please specify a directory where the model is located. ")
@lfoppiano
Copy link
Author

Steps:

  • git checkout 2dad199dbc6e011ae3b270b260e4784b7ffe2d17
  • <activate the delft virtual environment>
  • python preprocessor_migration.py data/models/sequenceLabelling/my_model_I_want_to_migrate/

You should have a new file preprocessor.json in the data/models/sequenceLabelling/my_model_I_want_to_migrate/ directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment