| 56 | static const int32_t kMaxInputChars = 300; |
| 57 | |
| 58 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
| 59 | tflite::StringRef input = tflite::GetString(GetInput(context, node, 0), 0); |
| 60 | |
| 61 | string result(absl::AsciiStrToLower(absl::string_view(input.str, input.len))); |
| 62 | absl::StripAsciiWhitespace(&result); |
| 63 | // Do not remove commas, semi-colons or colons from the sentences as they can |
| 64 | // indicate the beginning of a new clause. |
| 65 | RE2::GlobalReplace(&result, kPunctuationsRegex, ""); |
| 66 | RE2::GlobalReplace(&result, "\\s('t|'nt|n't|'d|'ll|'s|'m|'ve|'re)([\\s,;:/])", |
| 67 | "\\1\\2"); |
| 68 | RE2::GlobalReplace(&result, "\\s('t|'nt|n't|'d|'ll|'s|'m|'ve|'re)$", "\\1"); |
| 69 | for (auto iter = kRegexTransforms->begin(); iter != kRegexTransforms->end(); |
| 70 | iter++) { |
| 71 | RE2::GlobalReplace(&result, iter->first, iter->second); |
| 72 | } |
| 73 | |
| 74 | // Treat questions & interjections as special cases. |
| 75 | RE2::GlobalReplace(&result, "([?])+", "\\1"); |
| 76 | RE2::GlobalReplace(&result, "([!])+", "\\1"); |
| 77 | RE2::GlobalReplace(&result, "([^?!]+)([?!])", "\\1 \\2 "); |
| 78 | RE2::GlobalReplace(&result, "([?!])([?!])", "\\1 \\2"); |
| 79 | |
| 80 | RE2::GlobalReplace(&result, "[\\s,:;\\-&'\"]+$", ""); |
| 81 | RE2::GlobalReplace(&result, "^[\\s,:;\\-&'\"]+", ""); |
| 82 | absl::StripAsciiWhitespace(&result); |
| 83 | |
| 84 | // Add start and end token. |
| 85 | // Truncate input to maximum allowed size. |
| 86 | if (result.length() <= kMaxInputChars) { |
| 87 | absl::StrAppend(&result, " ", kEndToken); |
| 88 | } else { |
| 89 | result = result.substr(0, kMaxInputChars); |
| 90 | } |
| 91 | result = absl::StrCat(kStartToken, " ", result); |
| 92 | |
| 93 | tflite::DynamicBuffer buf; |
| 94 | buf.AddString(result.data(), result.length()); |
| 95 | buf.WriteToTensorAsVector(GetOutput(context, node, 0)); |
| 96 | return kTfLiteOk; |
| 97 | } |
| 98 | |
| 99 | } // namespace normalize |
| 100 |
nothing calls this directly
no test coverage detected