Skip to content

Commit 67634f6

Browse files
committed
Use safe_print everywhere
1 parent 4b04609 commit 67634f6

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

pythainlp/cli/benchmark.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pythainlp import cli
1313
from pythainlp.benchmarks import word_tokenization
14+
from pythainlp.tools import safe_print
1415

1516

1617
def _read_file(path):
@@ -81,7 +82,7 @@ def __init__(self, name, argv):
8182
expected
8283
), "Input and test files do not have the same number of samples"
8384

84-
print(
85+
safe_print(
8586
"Benchmarking %s against %s with %d samples in total"
8687
% (args.input_file, args.test_file, len(actual))
8788
)
@@ -121,12 +122,12 @@ def __init__(self, name, argv):
121122
/ statistics["word_level:total_words_in_ref_sample"]
122123
)
123124

124-
print("============== Benchmark Result ==============")
125+
safe_print("============== Benchmark Result ==============")
125126

126127
for c in ["tp", "fn", "tn", "fp", "precision", "recall"]:
127128
c = f"char_level:{c}"
128129
v = statistics[c]
129-
print(f"{c:>40s} {v:.4f}")
130+
safe_print(f"{c:>40s} {v:.4f}")
130131

131132
for c in [
132133
"total_words_in_sample",
@@ -137,20 +138,20 @@ def __init__(self, name, argv):
137138
]:
138139
c = f"word_level:{c}"
139140
v = statistics[c]
140-
print(f"{c:>40s} {v:.4f}")
141+
safe_print(f"{c:>40s} {v:.4f}")
141142

142143
if args.save_details:
143144
dir_name = os.path.dirname(args.input_file)
144145
file_name = args.input_file.split("/")[-1].split(".")[0]
145146

146147
res_path = "%s/eval-%s.yml" % (dir_name, file_name)
147-
print("Evaluation result is saved to %s" % res_path)
148+
safe_print("Evaluation result is saved to %s" % res_path)
148149

149150
with open(res_path, "w", encoding="utf-8") as outfile:
150151
yaml.dump(statistics, outfile, default_flow_style=False)
151152

152153
res_path = "%s/eval-details-%s.json" % (dir_name, file_name)
153-
print("Details of comparisons is saved to %s" % res_path)
154+
safe_print("Details of comparisons is saved to %s" % res_path)
154155

155156
with open(res_path, "w", encoding="utf-8") as f:
156157
samples = []
@@ -160,7 +161,12 @@ def __init__(self, name, argv):
160161
del r["actual"]
161162

162163
samples.append(
163-
{"metrics": r, "expected": expected, "actual": actual, "id": i}
164+
{
165+
"metrics": r,
166+
"expected": expected,
167+
"actual": actual,
168+
"id": i,
169+
}
164170
)
165171

166172
details = {"metrics": statistics, "samples": samples}

pythainlp/cli/data.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"""
55
Command line for PyThaiNLP's dataset/corpus management.
66
"""
7+
78
import argparse
89

910
from pythainlp import corpus
10-
from pythainlp.tools import get_pythainlp_data_path
11+
from pythainlp.tools import get_pythainlp_data_path, safe_print
1112

1213

1314
class App:
@@ -55,9 +56,9 @@ def get(self, argv):
5556
)
5657
args = parser.parse_args(argv[3:])
5758
if corpus.download(args.dataset_name):
58-
print("Downloaded successfully.")
59+
safe_print("Downloaded successfully.")
5960
else:
60-
print("Not found.")
61+
safe_print("Not found.")
6162

6263
def rm(self, argv):
6364
parser = argparse.ArgumentParser(
@@ -71,9 +72,9 @@ def rm(self, argv):
7172
)
7273
args = parser.parse_args(argv[3:])
7374
if corpus.remove(args.dataset_name):
74-
print("Removed successfully.")
75+
safe_print("Removed successfully.")
7576
else:
76-
print("Not found.")
77+
safe_print("Not found.")
7778

7879
def info(self, argv):
7980
parser = argparse.ArgumentParser(
@@ -88,29 +89,29 @@ def info(self, argv):
8889
args = parser.parse_args(argv[3:])
8990
info = corpus.get_corpus_db_detail(args.dataset_name)
9091
if info:
91-
print(info)
92+
safe_print(info)
9293
else:
93-
print("Not found.")
94+
safe_print("Not found.")
9495

9596
def catalog(self, argv):
9697
"""Print dataset/corpus available for download."""
9798
corpus_db = corpus.get_corpus_db(corpus.corpus_db_url())
9899
corpus_db = corpus_db.json()
99100
corpus_names = sorted(corpus_db.keys())
100-
print("Dataset/corpus available for download:")
101+
safe_print("Dataset/corpus available for download:")
101102
for name in corpus_names:
102-
print(f"- {name} {corpus_db[name]['latest_version']}", end="")
103+
safe_print(f"- {name} {corpus_db[name]['latest_version']}", end="")
103104
corpus_info = corpus.get_corpus_db_detail(name)
104105
if corpus_info:
105-
print(f" (Local: {corpus_info['version']})")
106+
safe_print(f" (Local: {corpus_info['version']})")
106107
else:
107-
print()
108+
safe_print()
108109

109-
print(
110+
safe_print(
110111
"\nUse subcommand 'get' to download a dataset.\n\n"
111112
"Example: thainlp data get crfcut\n"
112113
)
113114

114115
def path(self, argv):
115116
"""Print path of local dataset."""
116-
print(get_pythainlp_data_path())
117+
safe_print(get_pythainlp_data_path())

pythainlp/cli/tag.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"""
55
Command line for PyThaiNLP's taggers.
66
"""
7+
78
import argparse
89

910
from pythainlp import cli
1011
from pythainlp.tag import pos_tag
12+
from pythainlp.tools import safe_print
1113

1214

1315
class SubAppBase:
@@ -34,7 +36,7 @@ def __init__(self, name, argv):
3436
result = self.run(tokens)
3537

3638
for word, tag in result:
37-
print(word, "/", tag)
39+
safe_print(word, "/", tag)
3840

3941

4042
class POSTaggingApp(SubAppBase):
@@ -73,4 +75,4 @@ def __init__(self, argv):
7375
if tag_type == "pos":
7476
POSTaggingApp("Part-of-Speech tagging", argv)
7577
else:
76-
print(f"Tag type not available: {tag_type}")
78+
safe_print(f"Tag type not available: {tag_type}")

0 commit comments

Comments
 (0)