1*993b0882SAndroid Build Coastguard Worker /*
2*993b0882SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*993b0882SAndroid Build Coastguard Worker *
4*993b0882SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*993b0882SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*993b0882SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*993b0882SAndroid Build Coastguard Worker *
8*993b0882SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*993b0882SAndroid Build Coastguard Worker *
10*993b0882SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*993b0882SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*993b0882SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*993b0882SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*993b0882SAndroid Build Coastguard Worker * limitations under the License.
15*993b0882SAndroid Build Coastguard Worker */
16*993b0882SAndroid Build Coastguard Worker
17*993b0882SAndroid Build Coastguard Worker #ifndef NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_SCRIPT_APPROX_SCRIPT_H_
18*993b0882SAndroid Build Coastguard Worker #define NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_SCRIPT_APPROX_SCRIPT_H_
19*993b0882SAndroid Build Coastguard Worker
20*993b0882SAndroid Build Coastguard Worker #include "lang_id/common/utf8.h"
21*993b0882SAndroid Build Coastguard Worker #include "lang_id/script/script-detector.h"
22*993b0882SAndroid Build Coastguard Worker
23*993b0882SAndroid Build Coastguard Worker namespace libtextclassifier3 {
24*993b0882SAndroid Build Coastguard Worker namespace mobile {
25*993b0882SAndroid Build Coastguard Worker
26*993b0882SAndroid Build Coastguard Worker // Returns script for the UTF-8 character that starts at address |s| and has
27*993b0882SAndroid Build Coastguard Worker // |num_bytes| bytes. Note: behavior is unspecified if s points to a UTF-8
28*993b0882SAndroid Build Coastguard Worker // character that has a different number of bytes. If you don't know
29*993b0882SAndroid Build Coastguard Worker // |num_bytes|, call GetApproxScript(const char *s).
30*993b0882SAndroid Build Coastguard Worker //
31*993b0882SAndroid Build Coastguard Worker // NOTE: to keep BUILD deps small, this function returns an int, but you can
32*993b0882SAndroid Build Coastguard Worker // assume it's an enum UScriptCode (unicode/uscript.h)
33*993b0882SAndroid Build Coastguard Worker //
34*993b0882SAndroid Build Coastguard Worker // If unable to determine the script, this function returns kUnknownUscript, the
35*993b0882SAndroid Build Coastguard Worker // int value of USCRIPT_UNKNOWN from enum UScriptCode.
36*993b0882SAndroid Build Coastguard Worker int GetApproxScript(const unsigned char *s, int num_bytes);
37*993b0882SAndroid Build Coastguard Worker
38*993b0882SAndroid Build Coastguard Worker // See comments for GetApproxScript() above.
39*993b0882SAndroid Build Coastguard Worker extern const int kUnknownUscript;
40*993b0882SAndroid Build Coastguard Worker
41*993b0882SAndroid Build Coastguard Worker // Same as before, but s is a const char *pointer (no unsigned). Internally, we
42*993b0882SAndroid Build Coastguard Worker // prefer "unsigned char" (the signed status of char is ambiguous), so we cast
43*993b0882SAndroid Build Coastguard Worker // and call the previous version (with const unsigned char *).
GetApproxScript(const char * s,int num_bytes)44*993b0882SAndroid Build Coastguard Worker inline int GetApproxScript(const char *s, int num_bytes) {
45*993b0882SAndroid Build Coastguard Worker return GetApproxScript(reinterpret_cast<const unsigned char *>(s), num_bytes);
46*993b0882SAndroid Build Coastguard Worker }
47*993b0882SAndroid Build Coastguard Worker
48*993b0882SAndroid Build Coastguard Worker // Returns script for the UTF-8 character that starts at address |s|. NOTE:
49*993b0882SAndroid Build Coastguard Worker // UTF-8 is a var-length encoding, taking between 1 and 4 bytes per Unicode
50*993b0882SAndroid Build Coastguard Worker // character. We infer the number of bytes based on s[0]. If that number is k,
51*993b0882SAndroid Build Coastguard Worker // we expect to be able to read k bytes starting from address |s|. I.e., do not
52*993b0882SAndroid Build Coastguard Worker // call this function on broken UTF-8.
GetApproxScript(const char * s)53*993b0882SAndroid Build Coastguard Worker inline int GetApproxScript(const char *s) {
54*993b0882SAndroid Build Coastguard Worker return GetApproxScript(s, utils::OneCharLen(s));
55*993b0882SAndroid Build Coastguard Worker }
56*993b0882SAndroid Build Coastguard Worker
57*993b0882SAndroid Build Coastguard Worker // Returns max value returned by the GetApproxScript() functions.
58*993b0882SAndroid Build Coastguard Worker int GetMaxApproxScriptResult();
59*993b0882SAndroid Build Coastguard Worker
60*993b0882SAndroid Build Coastguard Worker class ApproxScriptDetector : public ScriptDetector {
61*993b0882SAndroid Build Coastguard Worker public:
62*993b0882SAndroid Build Coastguard Worker ~ApproxScriptDetector() override = default;
63*993b0882SAndroid Build Coastguard Worker
64*993b0882SAndroid Build Coastguard Worker // Note: the int result of this method is actually a UScriptCode enum value.
65*993b0882SAndroid Build Coastguard Worker // We return int to match the general case from the base class ScriptDetector
66*993b0882SAndroid Build Coastguard Worker // (some script detectors do not use UScriptCode).
GetScript(const char * s,int num_bytes)67*993b0882SAndroid Build Coastguard Worker int GetScript(const char *s, int num_bytes) const override {
68*993b0882SAndroid Build Coastguard Worker return GetApproxScript(s, num_bytes);
69*993b0882SAndroid Build Coastguard Worker }
70*993b0882SAndroid Build Coastguard Worker
GetMaxScript()71*993b0882SAndroid Build Coastguard Worker int GetMaxScript() const override {
72*993b0882SAndroid Build Coastguard Worker return GetMaxApproxScriptResult();
73*993b0882SAndroid Build Coastguard Worker }
74*993b0882SAndroid Build Coastguard Worker
75*993b0882SAndroid Build Coastguard Worker SAFTM_DEFINE_REGISTRATION_METHOD("approx-unicode-script-detector",
76*993b0882SAndroid Build Coastguard Worker ApproxScriptDetector);
77*993b0882SAndroid Build Coastguard Worker };
78*993b0882SAndroid Build Coastguard Worker
79*993b0882SAndroid Build Coastguard Worker } // namespace mobile
80*993b0882SAndroid Build Coastguard Worker } // namespace nlp_saft
81*993b0882SAndroid Build Coastguard Worker
82*993b0882SAndroid Build Coastguard Worker #endif // NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_SCRIPT_APPROX_SCRIPT_H_
83