• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nielstron / quantulum3 / 946

pending completion
946

cron

travis-ci-com

nielstron
Merge branch 'dev'

467 of 467 new or added lines in 14 files covered. (100.0%)

1812 of 1847 relevant lines covered (98.11%)

4.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/quantulum3/scripts/train.py
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
""" Train the disambiguator """
5✔
4

5
import argparse
5✔
6
import json
5✔
7
import logging
5✔
8
import time
5✔
9

10
from quantulum3.classifier import train_classifier
5✔
11

12
_LOGGER = logging.getLogger(__name__)
5✔
13

14

15
def main(args=None):
5✔
16
    logging.basicConfig(level=logging.INFO)
5✔
17
    parser = argparse.ArgumentParser(
5✔
18
        "train",
19
        description="Train unit disambiguator based on data in quantulum "
20
        "project folder",
21
    )
22
    parser.add_argument(
5✔
23
        "--store",
24
        "-s",
25
        help="store resulting classifier as a joblib file",
26
        action="store_true",
27
    )
28
    parser.add_argument(
5✔
29
        "--lang",
30
        "-l",
31
        help="language in which to train the classifier, default 'en_US'",
32
        type=str,
33
        default="en_US",
34
    )
35
    parser.add_argument(
5✔
36
        "--data",
37
        "-d",
38
        help=(
39
            "path to one or more json training files. If none are given, the default training "
40
            "data in the quantulum3 project folder is used. Multiple files can be given by "
41
            "specifying this argument multiple times (e.g. -d train1.json -d train2.json). "
42
            "See the project README for more information."
43
        ),
44
        type=str,
45
        action="append",
46
    )
47
    parser.add_argument(
5✔
48
        "--output",
49
        "-o",
50
        help=(
51
            "path to a folder where the resulting classifier is stored. If "
52
            "not given, the default location in the quantulum3 project folder "
53
            "is used"
54
        ),
55
        type=str,
56
        default=None,
57
    )
58

59
    args = parser.parse_args(args)
5✔
60

61
    if args.data is not None:
5✔
62
        training_set = []
5✔
63
        args.lang = None
5✔
64
        for file in args.data:
5✔
65
            with open(file) as f:
5✔
66
                training_set += json.load(f)
5✔
67
    else:
68
        training_set = None
5✔
69

70
    if args.output is not None:
5✔
71
        # override this option if an output file is given, feels intuitive to do
72
        args.store = True
5✔
73

74
    _LOGGER.info(
5✔
75
        "Start training for language {}, {}storing the classifier".format(
76
            args.lang, "" if args.store else "not "
77
        )
78
    )
79
    start = time.process_time()
5✔
80
    try:
5✔
81
        train_classifier(
5✔
82
            store=args.store,
83
            lang=args.lang,
84
            training_set=training_set,
85
            output_path=args.output,
86
        )
87
    except ImportError:
5✔
88
        _LOGGER.error(
5✔
89
            "Could not train the classifier. Make sure you have the "
90
            f"required dependencies installed. "
91
            "These can be installed in pip using the command "
92
            "'pip install quantulum3[classifier]'"
93
        )
94
    else:
95
        end = time.process_time()
5✔
96
        _LOGGER.info("Done in {} s".format(end - start))
5✔
97

98

99
if __name__ == "__main__":
5✔
100
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc