-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
77 lines (61 loc) · 2.31 KB
/
util.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
65
66
67
68
69
70
71
72
73
74
75
import os
import argparse
import logging
import re
import regex
import sys
# variables used by the no_escaping function
replacements = {"&": "&",
"|": "|",
"<": "<",
">": ">",
"&apos": "'",
""": '"',
"[": "[",
"]": "]"}
substrs = sorted(replacements, key=len, reverse=True)
nrregexp = re.compile('|'.join(map(re.escape, substrs)))
regex_alpha = regex.compile("^[[:alpha:]]+$")
# Back-replacements of strings mischanged by the Moses tokenizer
def no_escaping(text):
global nrregexp, replacements
return nrregexp.sub(lambda match: replacements[match.group(0)], text)
# Check if the argument of a program (argparse) is positive or zero
def check_positive_between_zero_and_one(value):
ivalue = float(value)
if ivalue < 0 or ivalue > 1:
raise argparse.ArgumentTypeError("%s is an invalid float value between 0 and 1" % value)
return ivalue
# Check if the argument of a program (argparse) is positive or zero
def check_positive_or_zero(value):
ivalue = int(value)
if ivalue < 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
# Check if the argument of a program (argparse) is strictly positive
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
# Check if the argument of a program (argparse) is strictly positive
def check_if_folder(path):
if not os.path.isdir(path):
raise argparse.ArgumentTypeError("%s is not a directory" % path)
return path
# Logging config
def logging_setup(args = None):
logger = logging.getLogger()
logger.handlers = [] # Removing default handler to avoid duplication of log messages
logger.setLevel(logging.ERROR)
h = logging.StreamHandler(sys.stderr)
if args != None:
h = logging.StreamHandler(args.logfile)
h.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(h)
logger.setLevel(logging.INFO)
if args != None:
if not args.quiet:
logger.setLevel(logging.INFO)
if args.debug:
logger.setLevel(logging.DEBUG)