|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from argparse import ArgumentParser, Namespace |
| 4 | +from contextlib import suppress |
| 5 | +import logging.config |
| 6 | +from typing import Dict, Any |
| 7 | + |
| 8 | +from guesslangtools import hacks |
| 9 | +from guesslangtools.common import Config |
| 10 | +from guesslangtools.app import run_workflow |
| 11 | + |
| 12 | + |
| 13 | +LOGGING_CONFIG: Dict[str, Any] = { |
| 14 | + 'version': 1, |
| 15 | + 'disable_existing_loggers': False, |
| 16 | + 'formatters': { |
| 17 | + 'simple': { |
| 18 | + 'format': '%(asctime)s %(levelname)s: %(message)s', |
| 19 | + 'datefmt': '%H:%M:%S', |
| 20 | + } |
| 21 | + }, |
| 22 | + 'handlers': { |
| 23 | + 'console': { |
| 24 | + 'class': 'logging.StreamHandler', |
| 25 | + 'level': 'DEBUG', |
| 26 | + 'formatter': 'simple', |
| 27 | + } |
| 28 | + }, |
| 29 | + 'root': { |
| 30 | + 'level': 'DEBUG', |
| 31 | + 'handlers': ['console'], |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +LOGGER = logging.getLogger(__name__) |
| 36 | + |
| 37 | + |
| 38 | +def main() -> None: |
| 39 | + parser = ArgumentParser(description='Guesslang data preparation tool') |
| 40 | + parser.add_argument( |
| 41 | + '-d', '--debug', action='store_true', |
| 42 | + help='display debug messages') |
| 43 | + |
| 44 | + parser.add_argument( |
| 45 | + 'CACHE_DIR', |
| 46 | + help='directory where the generated content will be stored') |
| 47 | + parser.add_argument( |
| 48 | + '--nb-train-files', type=int, default=27000, |
| 49 | + help='number of training files per language') |
| 50 | + parser.add_argument( |
| 51 | + '--nb-valid-files', type=int, default=4000, |
| 52 | + help='number of validation files per language') |
| 53 | + parser.add_argument( |
| 54 | + '--nb-test-files', type=int, default=4000, |
| 55 | + help='number of testing files per language') |
| 56 | + parser.add_argument( |
| 57 | + '--nb-repo', type=int, default=4000, |
| 58 | + help='number of repositories per language') |
| 59 | + |
| 60 | + parser.add_argument( |
| 61 | + '--hack-repo-dist', action='store_true', default=False, |
| 62 | + help='show the number of selected repositories per languages') |
| 63 | + parser.add_argument( |
| 64 | + '--hack-add-repo', nargs='+', metavar='LANGUAGE', |
| 65 | + help='select more repositories for the listed languages') |
| 66 | + parser.add_argument( |
| 67 | + '--hack-only-downloaded-repo', action='store_true', default=False, |
| 68 | + help='only use the repositories that have already been downloaded') |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + items = vars(args).items() |
| 72 | + hack_args = any(val for name, val in items if name.startswith('hack_')) |
| 73 | + |
| 74 | + log_level = 'DEBUG' if args.debug else 'INFO' |
| 75 | + LOGGING_CONFIG['root']['level'] = log_level |
| 76 | + logging.config.dictConfig(LOGGING_CONFIG) |
| 77 | + |
| 78 | + Config.setup( |
| 79 | + cache_dir=args.CACHE_DIR, |
| 80 | + nb_repositories=args.nb_repo, |
| 81 | + nb_train=args.nb_train_files, |
| 82 | + nb_valid=args.nb_valid_files, |
| 83 | + nb_test=args.nb_test_files, |
| 84 | + ) |
| 85 | + |
| 86 | + with suppress(KeyboardInterrupt): |
| 87 | + if hack_args: |
| 88 | + run_hacks(args) |
| 89 | + else: |
| 90 | + run_workflow() |
| 91 | + |
| 92 | + |
| 93 | +def run_hacks(args: Namespace) -> None: |
| 94 | + if args.hack_repo_dist: |
| 95 | + hacks.show_repositories_distribution() |
| 96 | + |
| 97 | + if args.hack_add_repo: |
| 98 | + hacks.select_more_repositories(args.hack_add_repo) |
| 99 | + |
| 100 | + if args.hack_only_downloaded_repo: |
| 101 | + hacks.select_only_downloaded_repo() |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + main() |
0 commit comments