|
| 1 | +""" |
| 2 | +thainlp dataset/corpus management command line. |
| 3 | +""" |
| 4 | +import argparse |
| 5 | + |
| 6 | +from pythainlp import cli, corpus |
| 7 | +from pythainlp.tools import get_pythainlp_data_path |
| 8 | + |
| 9 | + |
| 10 | +class App: |
| 11 | + def __init__(self, argv): |
| 12 | + parser = argparse.ArgumentParser( |
| 13 | + prog="data", usage="thainlp data <subcommand>", |
| 14 | + ) |
| 15 | + parser.add_argument( |
| 16 | + "subcommand", |
| 17 | + type=str, |
| 18 | + choices=["catalog", "info", "get", "rm", "path"], |
| 19 | + help="action on dataset/corpus", |
| 20 | + ) |
| 21 | + args = parser.parse_args(argv[2:3]) |
| 22 | + getattr(self, args.subcommand)(argv) |
| 23 | + |
| 24 | + def get(self, argv): |
| 25 | + parser = argparse.ArgumentParser( |
| 26 | + description="Download a dataset", |
| 27 | + usage="thainlp data get <dataset_name>", |
| 28 | + ) |
| 29 | + parser.add_argument( |
| 30 | + "dataset_name", type=str, help="dataset/corpus's name", |
| 31 | + ) |
| 32 | + args = parser.parse_args(argv[3:]) |
| 33 | + if corpus.download(args.dataset_name): |
| 34 | + print("Downloaded successfully.") |
| 35 | + else: |
| 36 | + print("Not found.") |
| 37 | + |
| 38 | + def rm(self, argv): |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + description="Remove a dataset", |
| 41 | + usage="thainlp data rm <dataset_name>", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "dataset_name", type=str, help="dataset/corpus's name", |
| 45 | + ) |
| 46 | + args = parser.parse_args(argv[3:]) |
| 47 | + if corpus.remove(args.dataset_name): |
| 48 | + print("Removed successfully.") |
| 49 | + else: |
| 50 | + print("Not found.") |
| 51 | + |
| 52 | + def info(self, argv): |
| 53 | + parser = argparse.ArgumentParser( |
| 54 | + description="Print information about a dataset", |
| 55 | + usage="thainlp data info <dataset_name>", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "dataset_name", type=str, help="dataset/corpus's name", |
| 59 | + ) |
| 60 | + args = parser.parse_args(argv[3:]) |
| 61 | + info = corpus.get_corpus_db_detail(args.dataset_name) |
| 62 | + if info: |
| 63 | + print(info) |
| 64 | + else: |
| 65 | + print("Not found.") |
| 66 | + |
| 67 | + def catalog(self, argv): |
| 68 | + """Print dataset/corpus available for download.""" |
| 69 | + corpus_db = corpus.get_corpus_db(corpus.corpus_db_url()) |
| 70 | + corpus_db = corpus_db.json() |
| 71 | + corpus_names = sorted(corpus_db.keys()) |
| 72 | + print("Dataset/corpus available for download:") |
| 73 | + for name in corpus_names: |
| 74 | + print(f"- {name} {corpus_db[name]['version']}", end="") |
| 75 | + corpus_info = corpus.get_corpus_db_detail(name) |
| 76 | + if corpus_info: |
| 77 | + print(f" (Local: {corpus_info['version']})") |
| 78 | + else: |
| 79 | + print() |
| 80 | + |
| 81 | + print("\nUse subcommand 'get' to download dataset.") |
| 82 | + print("Example: thainlp data get crfcut") |
| 83 | + |
| 84 | + def path(self, argv): |
| 85 | + """Print path for local dataset.""" |
| 86 | + print(get_pythainlp_data_path()) |
0 commit comments