Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "common/utils.h" |
| 18 | |
| 19 | #include <ctype.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | namespace libtextclassifier { |
| 23 | namespace nlp_core { |
| 24 | namespace utils { |
| 25 | |
| 26 | std::string TrimString(const std::string &text) { |
| 27 | int start = 0; |
| 28 | while ((start < text.size()) && isspace(text[start])) { |
| 29 | start++; |
| 30 | } |
| 31 | int end = text.size() - 1; |
| 32 | while ((end > start) && isspace(text[end])) { |
| 33 | end--; |
| 34 | } |
| 35 | return text.substr(start, end - start + 1); |
| 36 | } |
| 37 | |
| 38 | std::string Lowercase(const std::string &s) { |
| 39 | std::string result(s); |
| 40 | for (char &c : result) { |
| 41 | c = tolower(c); |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | void NormalizeDigits(std::string *form) { |
| 47 | for (size_t i = 0; i < form->size(); ++i) { |
| 48 | if ((*form)[i] >= '0' && (*form)[i] <= '9') { |
| 49 | (*form)[i] = '9'; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | } // namespace utils |
| 55 | } // namespace nlp_core |
| 56 | } // namespace libtextclassifier |