Skip to content

Instantly share code, notes, and snippets.

@yagays
Last active May 15, 2021 00:19
Show Gist options
  • Save yagays/a0a977cdf2642cec8c32c6b8f66c8cf1 to your computer and use it in GitHub Desktop.
Save yagays/a0a977cdf2642cec8c32c6b8f66c8cf1 to your computer and use it in GitHub Desktop.
"""
$ python modify_sudachi_json.py --user_dict path/to/user.dic --debug
"""
import argparse
import errno
import json
import os
import site
from pathlib import Path
def modify_sudachi_json(args):
userdict_paths = [Path(path).absolute() for path in args.user_dict]
for userdict_path in userdict_paths:
if not userdict_path.exists():
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), str(userdict_path))
# find sudachi.json file path
base_dir = site.getsitepackages()[0]
sudachi_json_path = Path(f"{base_dir}/sudachipy/resources/sudachi.json")
# load and remove json
if sudachi_json_path.exists():
with sudachi_json_path.open() as f:
sudachi_json_content = json.load(f)
sudachi_json_path.unlink()
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), str(sudachi_json_path))
# add userDict to sudachi.json
sudachi_json_content["userDict"] = [str(path) for path in userdict_paths]
with sudachi_json_path.open(mode="w") as f:
f.write(json.dumps(sudachi_json_content, indent=4, ensure_ascii=False))
if args.debug:
print(f"sudachi.json: {sudachi_json_path}")
for userdict_path in userdict_paths:
print(f"userDict: {str(userdict_path)}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="modify to add userDict to sudachi.json")
parser.add_argument("--user_dict", nargs="*", required=True, help="specify user_dict paths to insert")
parser.add_argument("--debug", action="store_true", help="print sudachi.json and userDict path")
args = parser.parse_args()
modify_sudachi_json(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment