-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextnormalizer.cpp
More file actions
58 lines (50 loc) · 1.37 KB
/
Copy pathtextnormalizer.cpp
File metadata and controls
58 lines (50 loc) · 1.37 KB
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
#include "textnormalizer.h"
namespace {
// True if the last non-space character of `s` ends a sentence.
bool endsSentence(const QString& s)
{
for (int i = s.size() - 1; i >= 0; --i) {
const QChar c = s.at(i);
if (!c.isSpace())
return c == '.' || c == '!' || c == '?';
}
return false;
}
} // namespace
void TextNormalizer::reset()
{
m_atStart = true;
m_capNext = true;
}
QString TextNormalizer::applyFormat(const QString& seg, bool atStart, bool capitalize)
{
QString s = seg;
if (capitalize && !s.isEmpty())
s[0] = s.at(0).toUpper();
return atStart ? s : (QStringLiteral(" ") + s);
}
QString TextNormalizer::normalize(const QString& rawSegment)
{
if (!m_enabled) {
// Pass-through: keep the recognizer's own text/spacing.
if (!rawSegment.isEmpty())
m_atStart = false;
return rawSegment;
}
const QString seg = rawSegment.trimmed();
if (seg.isEmpty())
return {};
const QString out = applyFormat(seg, m_atStart, m_capNext);
m_atStart = false;
m_capNext = endsSentence(seg);
return out;
}
QString TextNormalizer::previewPartial(const QString& rawSegment) const
{
if (!m_enabled)
return rawSegment;
const QString seg = rawSegment.trimmed();
if (seg.isEmpty())
return {};
return applyFormat(seg, m_atStart, m_capNext);
}