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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.!
#include "builder.h"
#include "common.h"
#include "filesystem.h"
#include "flags.h"
#include "normalizer.h"
#include "sentencepiece.pb.h"
#include "sentencepiece_model.pb.h"
#include "sentencepiece_processor.h"
#include "sentencepiece_trainer.h"
DEFINE_string(model, "", "Model file name");
DEFINE_bool(use_internal_normalization, false,
"Use NormalizerSpec \"as-is\" to run the normalizer "
"for SentencePiece segmentation");
DEFINE_string(normalization_rule_name, "",
"Normalization rule name. "
"Choose from nfkc or identity");
DEFINE_string(normalization_rule_tsv, "", "Normalization rule TSV file. ");
DEFINE_bool(remove_extra_whitespaces, true, "Remove extra whitespaces");
DEFINE_bool(decompile, false,
"Decompile compiled charamap and output it as TSV.");
DEFINE_string(output, "", "Output filename");
using sentencepiece::ModelProto;
using sentencepiece::NormalizerSpec;
using sentencepiece::SentencePieceProcessor;
using sentencepiece::SentencePieceTrainer;
using sentencepiece::normalizer::Builder;
using sentencepiece::normalizer::Normalizer;
int main(int argc, char *argv[]) {
std::vector<std::string> rest_args;
sentencepiece::flags::ParseCommandLineFlags(argc, argv, &rest_args);
NormalizerSpec spec;
if (!FLAGS_model.empty()) {
ModelProto model_proto;
SentencePieceProcessor sp;
CHECK_OK(sp.Load(FLAGS_model));
spec = sp.model_proto().normalizer_spec();
} else if (!FLAGS_normalization_rule_tsv.empty()) {
spec.set_normalization_rule_tsv(FLAGS_normalization_rule_tsv);
CHECK_OK(SentencePieceTrainer::PopulateNormalizerSpec(&spec));
} else if (!FLAGS_normalization_rule_name.empty()) {
spec.set_name(FLAGS_normalization_rule_name);
CHECK_OK(SentencePieceTrainer::PopulateNormalizerSpec(&spec));
} else {
LOG(FATAL) << "Sets --model, normalization_rule_tsv, or "
"normalization_rule_name flag.";
}
// Uses the normalizer spec encoded in the model_pb.
if (!FLAGS_use_internal_normalization) {
spec.set_add_dummy_prefix(false); // do not add dummy prefix.
spec.set_escape_whitespaces(false); // do not output meta symbol.
spec.set_remove_extra_whitespaces(FLAGS_remove_extra_whitespaces);
}
if (FLAGS_decompile) {
Builder::CharsMap chars_map;
CHECK_OK(
Builder::DecompileCharsMap(spec.precompiled_charsmap(), &chars_map));
CHECK_OK(Builder::SaveCharsMap(FLAGS_output, chars_map));
} else {
const Normalizer normalizer(spec);
auto output = sentencepiece::filesystem::NewWritableFile(FLAGS_output);
CHECK_OK(output->status());
if (rest_args.empty()) {
rest_args.push_back(""); // empty means that read from stdin.
}
std::string line;
for (const auto &filename : rest_args) {
auto input = sentencepiece::filesystem::NewReadableFile(filename);
CHECK_OK(input->status());
while (input->ReadLine(&line)) {
output->WriteLine(normalizer.Normalize(line));
}
}
}
return 0;
}
|