Skip to content

Commit

Permalink
Update DateRewriter to be able to handle 3-digits.
Browse files Browse the repository at this point in the history
This CL updates DateRewriter to rewrite 3-digits segments to date and
time.

3-digits are splitted in two ways (DDD to D+DD and DD+D).  The
candidates are organized in the following order:
  1. description (date/time)
  2. split type (D+DD/DD+D)
  3. string format (XX:XX, etc)

BUG=
TEST=unittest
REF_BUG=20059084
REF_CL=90971460
  • Loading branch information
kohyatoh authored and yukawa committed Nov 15, 2015
1 parent 0a55c15 commit f2cc056
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/mozc_version_template.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAJOR=2
MINOR=17
BUILD=2238
BUILD=2239
REVISION=102
# NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
# downloaded by NaCl Mozc.
Expand Down
20 changes: 11 additions & 9 deletions src/rewriter/date_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2322,10 +2322,10 @@ bool IsNDigits(const string &value, int n) {
// - All the meta candidates are based on "cd" (e.g. "CD", "Cd").
// Therefore to get "2223" we should access the raw input.
// Prerequisit: |segments| has only one conversion segment.
const bool GetNDigits(const composer::Composer &composer,
const Segments &segments,
int n,
string *output) {
bool GetNDigits(const composer::Composer &composer,
const Segments &segments,
int n,
string *output) {
DCHECK(output);
DCHECK_EQ(1, segments.conversion_segments_size());
const Segment &segment = segments.conversion_segment(0);
Expand Down Expand Up @@ -2360,8 +2360,8 @@ const bool GetNDigits(const composer::Composer &composer,
}
} // namespace

bool DateRewriter::RewriteFourDigits(const composer::Composer &composer,
Segments *segments) const {
bool DateRewriter::RewriteConsecutiveDigits(const composer::Composer &composer,
Segments *segments) const {
if (segments->conversion_segments_size() != 1) {
// This method rewrites a segment only when the segments has only
// one conversion segment.
Expand All @@ -2371,8 +2371,10 @@ bool DateRewriter::RewriteFourDigits(const composer::Composer &composer,
}

string key;
if (!GetNDigits(composer, *segments, 4, &key)) {
// No four digit key is available.
// Currently three and four consecutive digits are converted
if (!GetNDigits(composer, *segments, 3, &key) &&
!GetNDigits(composer, *segments, 4, &key)) {
// No three or four digit key is available.
return false;
}

Expand Down Expand Up @@ -2470,7 +2472,7 @@ bool DateRewriter::Rewrite(const ConversionRequest &request,
}

if (request.has_composer()) {
modified |= RewriteFourDigits(request.composer(), segments);
modified |= RewriteConsecutiveDigits(request.composer(), segments);
}

return modified;
Expand Down
7 changes: 4 additions & 3 deletions src/rewriter/date_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ class DateRewriter : public RewriterInterface {
bool RewriteAd(Segment *segment) const;
bool RewriteWeekday(Segment *segment) const;

// When only one conversion segment has four number characters,
// When only one conversion segment has consecutive number characters,
// this function adds date and time candidates.
// e.g.)
// key -> candidates will be added
// ------------------------------------------------
// 0101 -> "1月1日、01/01、1時1分,午前1時1分、1:01"
// 2020 -> "20時20分、午後8時20分、20:20"
// 2930 -> "29時30分、29時半、午前5時30分、午前5時半"
bool RewriteFourDigits(const composer::Composer &composer,
Segments *segments) const;
// 123 -> "1月23日、01/23、1:23"
bool RewriteConsecutiveDigits(const composer::Composer &composer,
Segments *segments) const;

bool AdToEra(int year, vector<string> *results) const;

Expand Down
47 changes: 47 additions & 0 deletions src/rewriter/date_rewriter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,53 @@ TEST_F(DateRewriterTest, NumberRewriterTest) {
// "日付"
EXPECT_EQ(0, CountDescription(segments, "\xE6\x97\xA5\xE4\xBB\x98"));

// 123 is expected 3 time candidates and 2 date candidates
InitSegment("123", "123", &segments);
EXPECT_TRUE(rewriter.Rewrite(conversion_request, &segments));
// "時刻"
EXPECT_EQ(3, CountDescription(segments, "\xE6\x99\x82\xE5\x88\xBB"));
EXPECT_TRUE(ContainCandidate(segments, "1:23"));
// "1時23分"
EXPECT_TRUE(ContainCandidate(segments,
"\x31\xe6\x99\x82\x32\x33\xe5\x88\x86"));
// "午前1時23分"
EXPECT_TRUE(ContainCandidate(
segments,
"\xe5\x8d\x88\xe5\x89\x8d\x31\xe6\x99\x82\x32\x33\xe5\x88\x86"));

// "日付"
EXPECT_EQ(2, CountDescription(segments, "\xE6\x97\xA5\xE4\xBB\x98"));
EXPECT_TRUE(ContainCandidate(segments, "01/23"));
// "1月23日"
EXPECT_TRUE(ContainCandidate(segments,
"\x31\xe6\x9c\x88\x32\x33\xe6\x97\xa5"));

// 346 is expected 3 time candidates and 0 date candidate
InitSegment("346", "346", &segments);
EXPECT_TRUE(rewriter.Rewrite(conversion_request, &segments));
// "時刻"
EXPECT_EQ(3, CountDescription(segments, "\xE6\x99\x82\xE5\x88\xBB"));
EXPECT_TRUE(ContainCandidate(segments, "3:46"));
// "3時46分"
EXPECT_TRUE(ContainCandidate(segments,
"\x33\xe6\x99\x82\x34\x36\xe5\x88\x86"));
// "午前3時46分"
EXPECT_TRUE(ContainCandidate(
segments,
"\xe5\x8d\x88\xe5\x89\x8d\x33\xe6\x99\x82\x34\x36\xe5\x88\x86"));

// "日付"
EXPECT_EQ(0, CountDescription(segments, "\xE6\x97\xA5\xE4\xBB\x98"));

// 765 is expected 0 time candidate and 0 date candidate
InitSegment("765", "765", &segments);
EXPECT_FALSE(rewriter.Rewrite(conversion_request, &segments));
// "時刻"
EXPECT_EQ(0, CountDescription(segments, "\xE6\x99\x82\xE5\x88\xBB"));

// "日付"
EXPECT_EQ(0, CountDescription(segments, "\xE6\x97\xA5\xE4\xBB\x98"));

// Especially for mobile, look at meta candidates' value, too.
// "あかあか" on 12keys layout will be transliterated to "1212".
InitMetaSegment(
Expand Down

0 comments on commit f2cc056

Please sign in to comment.