1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/input_method/text_utils.h"
// TODO(crbug/1223597) The rules to detect sentence end is not perfect, and we
// may want to use regex to improve readability.
namespace ash::input_method {
namespace {
const int kMaxSearchRange = 200;
const int kSpecialWordMaxLength = 6;
// The index difference between a sentence end and the next sentence start.
// Setting it to 1 is sufficient for grammar check model, but 2 is better since
// according to current rules, there is always a space or '\n' or '\r' after a
// sentence end.
const int kGapBetweenSentenceEndAndNextStart = 2;
bool IsSentenceEndCharacter(char16_t c) {
return (c == u'.' || c == u'?' || c == u'!' || c == u'。' || c == u'。' ||
c == u'.' || c == u'.' || c == u'?' || c == u'?' || c == u'!' ||
c == u'!' || c == u'…');
}
bool EndsInSpecialPeriodWord(const std::u16string& text, uint32_t pos) {
uint32_t idx = pos;
while (idx <= pos && pos - idx <= kSpecialWordMaxLength &&
text[idx] != u' ' && text[idx] != u'(') {
idx--;
}
if (idx > pos || pos - idx > kSpecialWordMaxLength) {
return false;
}
std::u16string last_word = text.substr(idx + 1, pos - idx);
return (last_word == u"c.f." || last_word == u"cf." || last_word == u"e.g." ||
last_word == u"eg." || last_word == u"i.e." || last_word == u"ie." ||
last_word == u"Mmes." || last_word == u"Mr." ||
last_word == u"Mrs." || last_word == u"Ms." ||
last_word == u"Mses." || last_word == u"Mssrs." ||
last_word == u"Prof." || last_word == u"n.b." || last_word == u"nb.");
}
bool IsSentenceEndSectionCharacter(char16_t c) {
return (c == u')' || c == u']' || c == u'}' || c == u'\'' || c == u'\"' ||
c == u'ʺ' || c == u'˝' || c == u'ˮ' || c == u'"' || c == u'″' ||
c == u'”' || c == u'»');
}
bool IsEmoticonEyes(char16_t c) {
return (c == ':' || c == ';');
}
bool IsEmoticonNose(char16_t c) {
return (c == u'-' || c == u'^' || c == u'{' || c == u'*');
}
bool IsEmoticonMouth(char16_t c) {
return (c == u')' || c == u'(' || c == u'\\' || c == u'|' || c == u'/');
}
bool EndsInEmoticon(const std::u16string& text, uint32_t pos) {
return ((pos >= 1 && IsEmoticonEyes(text[pos - 1]) &&
IsEmoticonMouth(text[pos])) ||
(pos >= 2 && IsEmoticonEyes(text[pos - 2]) &&
IsEmoticonNose(text[pos - 1]) && IsEmoticonMouth(text[pos])));
}
bool IsSentenceEnd(const std::u16string& text, uint32_t pos) {
if (pos < text.size() - 1 &&
(text[pos + 1] == '\n' || text[pos + 1] == '\r')) {
return true;
}
// The character after the sentence end must be a space or the end of the
// text.
if (pos < 2 || (pos < text.size() - 1 && text[pos + 1] != u' ')) {
return false;
}
if (IsSentenceEndCharacter(text[pos]) &&
!EndsInSpecialPeriodWord(text, pos)) {
return true;
}
if (IsSentenceEndCharacter(text[pos - 1]) &&
IsSentenceEndSectionCharacter(text[pos])) {
return true;
}
if (EndsInEmoticon(text, pos)) {
return true;
}
return false;
}
} // namespace
Sentence::Sentence() = default;
Sentence::Sentence(const gfx::Range& original_range, const std::u16string& text)
: original_range(original_range), text(text) {}
Sentence::Sentence(const Sentence& other) = default;
Sentence::~Sentence() = default;
uint32_t FindLastSentenceEnd(const std::u16string& text, uint32_t pos) {
if (pos == 0 || pos > text.size()) {
return kUndefined;
}
for (size_t i = pos - 1; i > 0 && pos - i <= kMaxSearchRange; i--) {
if (IsSentenceEnd(text, i)) {
return i;
}
}
return kUndefined;
}
uint32_t FindNextSentenceEnd(const std::u16string& text, uint32_t pos) {
if (pos >= text.size()) {
return kUndefined;
}
for (size_t i = pos; i < text.size() && i - pos <= kMaxSearchRange; i++) {
if (IsSentenceEnd(text, i)) {
return i;
}
}
return kUndefined;
}
Sentence FindLastSentence(const std::u16string& text, uint32_t pos) {
if (pos > text.size()) {
return Sentence();
}
if (pos > 0 &&
(pos == text.size() || text[pos] == '\n' || text[pos] == '\r')) {
pos--;
}
uint32_t end = FindLastSentenceEnd(text, pos);
if (end == kUndefined) {
return Sentence();
}
uint32_t start = FindLastSentenceEnd(text, end);
if (start == kUndefined) {
start = 0;
} else {
start = start + kGapBetweenSentenceEndAndNextStart;
}
if (start >= end || end - start > kMaxSearchRange) {
return Sentence();
}
return Sentence(gfx::Range(start, end + 1),
text.substr(start, end - start + 1));
}
Sentence FindCurrentSentence(const std::u16string& text, uint32_t pos) {
if (pos > text.size()) {
return Sentence();
}
if (pos > 0 &&
(pos == text.size() || text[pos] == '\n' || text[pos] == '\r')) {
pos--;
}
uint32_t start = FindLastSentenceEnd(text, pos);
if (start == kUndefined) {
start = 0;
} else {
start = start + kGapBetweenSentenceEndAndNextStart;
}
uint32_t end = FindNextSentenceEnd(text, pos);
if (end == kUndefined) {
end = text.length() - 1;
}
if (start >= end || end - start > kMaxSearchRange) {
return Sentence();
}
return Sentence(gfx::Range(start, end + 1),
text.substr(start, end - start + 1));
}
} // namespace ash::input_method
|