• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 Huawei Technologies Co., Ltd
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 "minddata/dataset/text/ir/validators.h"
18 
19 namespace mindspore {
20 namespace dataset {
21 /* ####################################### Validator Functions ############################################ */
22 
23 // Helper function to validate tokenizer directory parameter
ValidateTokenizerDirParam(const std::string & tokenizer_name,const std::string & tokenizer_file)24 Status ValidateTokenizerDirParam(const std::string &tokenizer_name, const std::string &tokenizer_file) {
25   if (tokenizer_file.empty()) {
26     std::string err_msg = tokenizer_name + ": tokenizer_file is not specified.";
27     MS_LOG(ERROR) << err_msg;
28     RETURN_STATUS_SYNTAX_ERROR(err_msg);
29   }
30 
31   Path file(tokenizer_file);
32   if (!file.Exists()) {
33     std::string err_msg = tokenizer_name + ": tokenizer_file: [" + tokenizer_file + "] is an invalid directory path.";
34     MS_LOG(ERROR) << err_msg;
35     RETURN_STATUS_SYNTAX_ERROR(err_msg);
36   }
37 
38   if (access(tokenizer_file.c_str(), R_OK) == -1) {
39     std::string err_msg = tokenizer_name + ": No access to specified tokenizer path: " + tokenizer_file;
40     MS_LOG(ERROR) << err_msg;
41     RETURN_STATUS_SYNTAX_ERROR(err_msg);
42   }
43 
44   return Status::OK();
45 }
46 
47 // Helper functions to help validate data type passed by user
IsTypeNumeric(const std::string & data_type)48 bool IsTypeNumeric(const std::string &data_type) {
49   if (data_type == "int8" || data_type == "uint8" || data_type == "int16" || data_type == "uint16" ||
50       data_type == "int32" || data_type == "uint32" || data_type == "int64" || data_type == "uint64" ||
51       data_type == "float16" || data_type == "float32" || data_type == "float64")
52     return true;
53   return false;
54 }
55 
IsTypeBoolean(const std::string & data_type)56 bool IsTypeBoolean(const std::string &data_type) { return data_type == "bool"; }
57 
IsTypeString(const std::string & data_type)58 bool IsTypeString(const std::string &data_type) { return data_type == "string"; }
59 }  // namespace dataset
60 }  // namespace mindspore
61