-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck_for_typos.py
64 lines (52 loc) · 2.24 KB
/
check_for_typos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Checks all translations of one language for typos.
Runs the corrector on it and prints out the result but doesn't write any changes to the mediawiki system.
Usage:
python3 check_for_typos.py language_code
"""
import argparse
from configparser import ConfigParser
import logging
from os.path import abspath, dirname, join
import sys
from typing import List
from pywikitools.correctbot.bot import CorrectBot
def parse_arguments() -> argparse.Namespace:
"""
Parses the arguments given from outside
Returns:
argparse.Namespace: parsed arguments
"""
log_levels: List[str] = ['debug', 'info', 'warning', 'error']
parser = argparse.ArgumentParser()
parser.add_argument("language_code", help="Language code")
parser.add_argument("-l", "--loglevel", choices=log_levels, default="warning", help="set loglevel for the script")
parser.add_argument("--only", help="Only apply the correction rule with the specified method name")
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
root = logging.getLogger()
root.setLevel(logging.DEBUG)
sh = logging.StreamHandler(sys.stdout)
fformatter = logging.Formatter('%(levelname)s: %(message)s')
sh.setFormatter(fformatter)
numeric_level = getattr(logging, args.loglevel.upper(), None)
assert isinstance(numeric_level, int)
sh.setLevel(numeric_level)
root.addHandler(sh)
config = ConfigParser()
config.read(join(dirname(abspath(__file__)), "config.ini"))
correctbot = CorrectBot(config, simulate=True)
apply_only_rule = None
if args.only is not None:
apply_only_rule = str(args.only)
for worksheet in correctbot.fortraininglib.get_worksheet_list():
correctbot.check_page(worksheet, args.language_code, apply_only_rule)
print(f"{worksheet}: {correctbot.get_correction_counter()} corrections")
if correctbot.get_correction_counter() > 0 or correctbot.get_suggestion_counter() > 0:
print(correctbot.get_correction_stats())
print(correctbot.get_correction_diff())
print(correctbot.get_suggestion_stats())
print(correctbot.get_suggestion_diff())
if correctbot.get_warning_counter() > 0:
print(correctbot.get_warnings())