1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "lang_id/common/fel/task-context.h"
18
19 #include <string>
20
21 #include "lang_id/common/lite_strings/numbers.h"
22
23 namespace libtextclassifier3 {
24 namespace mobile {
25
GetInputPath(const std::string & name) const26 std::string TaskContext::GetInputPath(const std::string &name) const {
27 auto it = inputs_.find(name);
28 if (it != inputs_.end()) {
29 return it->second;
30 }
31 return "";
32 }
33
SetInputPath(const std::string & name,const std::string & path)34 void TaskContext::SetInputPath(const std::string &name,
35 const std::string &path) {
36 inputs_[name] = path;
37 }
38
Get(const std::string & name,const char * defval) const39 std::string TaskContext::Get(const std::string &name,
40 const char *defval) const {
41 auto it = parameters_.find(name);
42 if (it != parameters_.end()) {
43 return it->second;
44 }
45 return defval;
46 }
47
Get(const std::string & name,int defval) const48 int TaskContext::Get(const std::string &name, int defval) const {
49 const std::string s = Get(name, "");
50 int value = defval;
51 if (LiteAtoi(s, &value)) {
52 return value;
53 }
54 return defval;
55 }
56
Get(const std::string & name,float defval) const57 float TaskContext::Get(const std::string &name, float defval) const {
58 const std::string s = Get(name, "");
59 float value = defval;
60 if (LiteAtof(s, &value)) {
61 return value;
62 }
63 return defval;
64 }
65
Get(const std::string & name,bool defval) const66 bool TaskContext::Get(const std::string &name, bool defval) const {
67 std::string value = Get(name, "");
68 return value.empty() ? defval : value == "true";
69 }
70
SetParameter(const std::string & name,const std::string & value)71 void TaskContext::SetParameter(const std::string &name,
72 const std::string &value) {
73 parameters_[name] = value;
74 }
75
76 } // namespace mobile
77 } // namespace nlp_saft
78