Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bin/chardict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
from os import listdir, path
from sys import argv
import platformdirs

IGNORED_CHARS = "1234567890 \t\r\n\ufeff"

Expand Down Expand Up @@ -60,6 +61,30 @@ def sort_by_frequency(table, precision=3):
return results


def add(file_path: str, name: str = "", encoding="utf-8"):
corpus = read_corpus(file_path, name, encoding)
if corpus is not None:
data_path = Path(platformdirs.user_config_dir(APP_NAME, APP_AUTHOR)) / "corpora"
data_path.mkdir(parents=True, exist_ok=True)
data_path = data_path / f"{corpus["name"]}.json"
try:
with data_path.open("w", encoding="utf-8") as outfile:
json.dump(corpus, outfile, indent=4, ensure_ascii=False)
print(f"Corpus “{corpus['name']}” added to {data_path}")
except:
print(f"Error: could not write to {data_path}")


def rm(name: str):
corpus_path = Path(platformdirs.user_config_dir(APP_NAME, APP_AUTHOR)) / "corpora"
corpus_path = corpus_path / f"{name}.json"
try:
corpus_path.unlink()
print(f"Corpus “{name}” deleted")
except FileNotFoundError:
print(f"Corpus “{name}” does not exist")


if __name__ == "__main__":
if len(argv) == 2: # convert one file
data = parse_corpus(argv[1])
Expand Down
40 changes: 40 additions & 0 deletions bin/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from contextlib import contextmanager
from importlib import metadata
from pathlib import Path
from typing import Iterator, List, Literal, Union

import click
import chardict as corpus

@click.group()
def cli() -> None: ...

@cli.command()
@click.argument("filename")
@click.option("-n",
"--name",
default="",
type=str,
help="Define corpus name",
)
@click.option("-c",
"--encoding",
default="utf-8",
type=str)
def add(filename, name, encoding):
corpus.add(filename, name, encoding)

@cli.command()
@click.argument("name")
def rm(name):
corpus.rm(name)


# @cli.command()
# def version() -> None:
# """Show version number and exit."""
# click.echo(f"kalamine { metadata.version('kalamine') }")


if __name__ == "__main__":
cli()