Codebase list mozc / debian/0.11.svn13-1 composer / composer.cc
debian/0.11.svn13-1

Tree @debian/0.11.svn13-1 (Download .tar.gz)

composer.cc @debian/0.11.svn13-1raw · history · blame

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// Copyright 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Interactive composer from a Roman string to a Hiragana string

#include "composer/composer.h"

#include "base/util.h"
#include "converter/character_form_manager.h"
#include "composer/internal/composition.h"
#include "composer/internal/mode_switching_handler.h"
#include "composer/internal/transliterators_ja.h"
#include "composer/table.h"
#include "session/commands.pb.h"
#include "session/config_handler.h"
#include "session/config.pb.h"

namespace mozc {
namespace composer {

namespace {

const TransliteratorInterface *GetTransliterator(
    const transliteration::TransliterationType comp_mode) {
  const TransliteratorInterface *kNullTransliterator = NULL;
  switch (comp_mode) {
    case transliteration::HALF_ASCII:
    case transliteration::HALF_ASCII_UPPER:
    case transliteration::HALF_ASCII_LOWER:
    case transliteration::HALF_ASCII_CAPITALIZED:
      VLOG(2) << "GetTransliterator: GetHalfAsciiTransliterator";
      return TransliteratorsJa::GetHalfAsciiTransliterator();

    case transliteration::FULL_ASCII:
    case transliteration::FULL_ASCII_UPPER:
    case transliteration::FULL_ASCII_LOWER:
    case transliteration::FULL_ASCII_CAPITALIZED:
      VLOG(2) << "GetTransliterator: GetFullAsciiTransliterator";
      return TransliteratorsJa::GetFullAsciiTransliterator();

    case transliteration::HALF_KATAKANA:
      VLOG(2) << "GetTransliterator: GetHalfKatakanaTransliterator";
      return TransliteratorsJa::GetHalfKatakanaTransliterator();

    case transliteration::FULL_KATAKANA:
      VLOG(2) << "GetTransliterator: GetFullKatakanaTransliterator";
      return TransliteratorsJa::GetFullKatakanaTransliterator();

    case transliteration::HIRAGANA:
      VLOG(2) << "GetTransliterator: kNullTransliterator";
      return TransliteratorsJa::GetHiraganaTransliterator();

    default:
      VLOG(2) << "GetTransliterator: kNullTransliterator";
      LOG(ERROR) << "Unknown TransliterationType: " << comp_mode;
      return kNullTransliterator;
  }
  VLOG(2) << "GetTransliterator: kNullTransliterator ";
  return kNullTransliterator;  // Just in case
}

const transliteration::TransliterationType GetTransliterationType(
    const TransliteratorInterface *transliterator,
    const transliteration::TransliterationType default_type) {
  if (transliterator == TransliteratorsJa::GetHiraganaTransliterator()) {
    return transliteration::HIRAGANA;
  }
  if (transliterator == TransliteratorsJa::GetHalfAsciiTransliterator()) {
    return transliteration::HALF_ASCII;
  }
  if (transliterator == TransliteratorsJa::GetFullAsciiTransliterator()) {
    return transliteration::FULL_ASCII;
  }
  if (transliterator == TransliteratorsJa::GetFullKatakanaTransliterator()) {
    return transliteration::FULL_KATAKANA;
  }
  if (transliterator == TransliteratorsJa::GetHalfKatakanaTransliterator()) {
    return transliteration::HALF_KATAKANA;
  }
  return default_type;
}

void Transliterate(const transliteration::TransliterationType mode,
                   const string &input,
                   string *output) {
  // When the mode is HALF_KATAKANA, Full width ASCII is also
  // transformed.
  if (mode == transliteration::HALF_KATAKANA) {
    string tmp_input;
    Util::HiraganaToKatakana(input, &tmp_input);
    Util::FullWidthToHalfWidth(tmp_input, output);
    return;
  }

  switch (mode) {
    case transliteration::HALF_ASCII:
      Util::FullWidthAsciiToHalfWidthAscii(input, output);
      break;
    case transliteration::HALF_ASCII_UPPER:
      Util::FullWidthAsciiToHalfWidthAscii(input, output);
      Util::UpperString(output);
      break;
    case transliteration::HALF_ASCII_LOWER:
      Util::FullWidthAsciiToHalfWidthAscii(input, output);
      Util::LowerString(output);
      break;
    case transliteration::HALF_ASCII_CAPITALIZED:
      Util::FullWidthAsciiToHalfWidthAscii(input, output);
      Util::CapitalizeString(output);
      break;

    case transliteration::FULL_ASCII:
      Util::HalfWidthAsciiToFullWidthAscii(input, output);
      break;
    case transliteration::FULL_ASCII_UPPER:
      Util::HalfWidthAsciiToFullWidthAscii(input, output);
      Util::UpperString(output);
      break;
    case transliteration::FULL_ASCII_LOWER:
      Util::HalfWidthAsciiToFullWidthAscii(input, output);
      Util::LowerString(output);
      break;
    case transliteration::FULL_ASCII_CAPITALIZED:
      Util::HalfWidthAsciiToFullWidthAscii(input, output);
      Util::CapitalizeString(output);
      break;

    case transliteration::FULL_KATAKANA:
      Util::HiraganaToKatakana(input, output);
      break;
    case transliteration::HIRAGANA:
      *output = input;
      break;
    default:
      LOG(ERROR) << "Unknown TransliterationType: " << mode;
      *output = input;
      break;
  }
}

transliteration::TransliterationType GetTransliterationTypeFromCompositionMode(
    const commands::CompositionMode mode) {
  switch (mode) {
    case commands::HIRAGANA:
      return transliteration::HIRAGANA;
    case commands::FULL_KATAKANA:
      return transliteration::FULL_KATAKANA;
    case commands::HALF_ASCII:
      return transliteration::HALF_ASCII;
    case commands::FULL_ASCII:
      return transliteration::FULL_ASCII;
    case commands::HALF_KATAKANA:
      return transliteration::HALF_KATAKANA;
    default:
      // commands::DIRECT or invalid mode.
      LOG(ERROR) << "Invalid CompositionMode: " << mode;
      return transliteration::HIRAGANA;
  }
}

}  // namespace

static const size_t kMaxPreeditLength = 256;
Composer::Composer(CompositionInterface *composition)
    : position_(0),
      input_mode_(transliteration::HIRAGANA),
      output_mode_(transliteration::HIRAGANA),
      comeback_input_mode_(transliteration::HIRAGANA),
      capital_sequence_count_(0),
      max_length_(kMaxPreeditLength) {
  composition_.reset(composition);
  SetInputMode(transliteration::HIRAGANA);
  Reset();
}

Composer::~Composer() {}

void Composer::Reset() {
  EditErase();
  SetInputMode(comeback_input_mode_);
}

void Composer::ReloadConfig() {
  // Do nothing at this moment.
}

bool Composer::Empty() const {
  return (GetLength() == 0);
}

void Composer::SetTable(const Table *table) {
  composition_->SetTable(table);
}

void Composer::SetInputMode(transliteration::TransliterationType mode) {
  comeback_input_mode_ = mode;
  input_mode_ = mode;
  capital_sequence_count_ = 0;
  composition_->SetInputMode(GetTransliterator(mode));
}

void Composer::SetTemporaryInputMode(
    transliteration::TransliterationType mode) {
  // Set comeback_input_mode_ to revert back the current input mode.
  comeback_input_mode_ = input_mode_;
  input_mode_ = mode;
  capital_sequence_count_ = 0;
  composition_->SetInputMode(GetTransliterator(mode));
}

void Composer::UpdateInputMode() {
  if (position_ == 0) {
    // Set the default input mode.
    SetInputMode(comeback_input_mode_);
    return;
  }

  if (position_ == composition_->GetLength()) {
    // Do nothing, if the cursor is in the end of the composition.
    return;
  }

  const TransliteratorInterface *lhs_t12r =
      composition_->GetTransliterator(position_);
  const TransliteratorInterface *rhs_t12r =
      composition_->GetTransliterator(position_ + 1);
  if (lhs_t12r == rhs_t12r) {
    // If the current cursor is between the same character type like
    // "A|B" and "あ|い", the input mode follows the character type.
    input_mode_ = GetTransliterationType(lhs_t12r, comeback_input_mode_);
    capital_sequence_count_ = 0;
    composition_->SetInputMode(GetTransliterator(input_mode_));
    return;
  }

  // Set the default input mode.
  SetInputMode(comeback_input_mode_);
}

transliteration::TransliterationType Composer::GetInputMode() const {
  return input_mode_;
}

transliteration::TransliterationType Composer::GetOutputMode() const {
  return output_mode_;
}

void Composer::ToggleInputMode() {
  if (input_mode_ == transliteration::HIRAGANA) {
    // TODO(komatsu): Refer user's perference.
    SetInputMode(transliteration::HALF_ASCII);
  } else {
    SetInputMode(transliteration::HIRAGANA);
  }
}

void Composer::SetOutputMode(transliteration::TransliterationType mode) {
  output_mode_ = mode;
  composition_->SetTransliterator(
      0, composition_->GetLength(), GetTransliterator(mode));
  MoveCursorToEnd();
}

void Composer::InsertCharacterAt(size_t pos, const string &input) {
  if (!EnableInsert()) {
    return;
  }
  position_ = composition_->InsertAt(pos, input);
}

void Composer::InsertCharacter(const string &input) {
  if (!EnableInsert()) {
    return;
  }
  position_ = composition_->InsertAt(position_, input);
}

void Composer::InsertCharacterPreedit(const string &input) {
  InsertCharacterKeyAndPreedit(input, input);
}

void Composer::InsertCharacterKeyAndPreedit(const string &key,
                                       const string &preedit) {
  if (!EnableInsert()) {
    return;
  }
  position_ = composition_->InsertKeyAndPreeditAt(position_, key, preedit);
}

bool Composer::InsertCharacterKeyEvent(const commands::KeyEvent &key) {
  if (key.has_mode()) {
    const transliteration::TransliterationType new_input_mode =
        GetTransliterationTypeFromCompositionMode(key.mode());
    if (new_input_mode != input_mode_) {
      // Only when the new input mode is different from the current
      // input mode, SetInputMode is called.  Otherwise the value of
      // comeback_input_mode_ is lost.
      SetInputMode(new_input_mode);
    }
  }

  if (!key.has_key_code()) {
    // If only SHIFT is pressed, this is used to revert back to the
    // previous input mode.
    for (size_t i = 0; key.modifier_keys_size(); ++i) {
      if (key.modifier_keys(i) == commands::KeyEvent::SHIFT) {
        // TODO(komatsu): Enable to customize the behavior.
        SetInputMode(comeback_input_mode_);
        return true;
      }
    }
    LOG(WARNING) << "key_code is empty";
    return false;
  }

  const uint32 key_code = key.key_code();
  if ((key_code >> 8) > 0) {
    LOG(ERROR) << "key_code is not ASCII: " << key_code;
    return false;
  }
  const char key_char = static_cast<char>(key_code);
  char input[2] = " ";
  input[0] = key_char;

  if (key.has_key_string()) {
    if (key.input_style() == commands::KeyEvent::AS_IS ||
        key.input_style() == commands::KeyEvent::DIRECT_INPUT) {
      composition_->SetInputMode(
          Transliterators::GetConversionStringSelector());
      InsertCharacterKeyAndPreedit(input, key.key_string());
      SetInputMode(comeback_input_mode_);
    } else {
      // Kana input usually has key_string.  Note that, the existence of
      // key_string never determine if the input mode is Kana or Romaji.
      InsertCharacterKeyAndPreedit(input, key.key_string());
    }
  } else {
    // Romaji input usually does not has key_string.  Note that, the
    // existence of key_string never determine if the input mode is
    // Kana or Romaji.
    if ('A' <= key_char && key_char <= 'Z') {
      if (GET_CONFIG(shift_key_mode_switch) ==
          config::Config::ASCII_INPUT_MODE) {
        if (input_mode_ == transliteration::HALF_ASCII ||
            input_mode_ == transliteration::FULL_ASCII) {
          // Do nothing.
        } else {
          SetTemporaryInputMode(transliteration::HALF_ASCII);
        }
      } else if (GET_CONFIG(shift_key_mode_switch) ==
                 config::Config::KATAKANA_INPUT_MODE) {
        if (input_mode_ == transliteration::FULL_KATAKANA) {
          // Do nothing.
        } else {
          SetTemporaryInputMode(transliteration::FULL_KATAKANA);
        }
      }
      ++capital_sequence_count_;
    }
    // When capital input continues, the next lower input is the end
    // of temporary half-width Ascii input.
    if ('a' <= key_char && key_char <= 'z') {
      if (capital_sequence_count_ > 1 &&
          GET_CONFIG(shift_key_mode_switch) ==
          config::Config::ASCII_INPUT_MODE) {
        SetInputMode(comeback_input_mode_);
      }
      if (GET_CONFIG(shift_key_mode_switch) ==
          config::Config::KATAKANA_INPUT_MODE) {
        SetInputMode(comeback_input_mode_);
      }
      capital_sequence_count_ = 0;
    }
    InsertCharacter(input);
  }

  if (comeback_input_mode_ == input_mode_) {
    AutoSwitchMode();
  }
  return true;
}

void Composer::DeleteAt(size_t pos) {
  position_ = composition_->DeleteAt(pos);
  UpdateInputMode();
}

void Composer::Delete() {
  position_ = composition_->DeleteAt(position_);
  UpdateInputMode();
}

void Composer::EditErase() {
  composition_->Erase();
  position_ = 0;
  UpdateInputMode();
}

void Composer::BackspaceAt(size_t pos) {
  if (pos == 0) {
    return;
  }
  position_ = composition_->DeleteAt(pos - 1);
  UpdateInputMode();
}

void Composer::Backspace() {
  BackspaceAt(position_);
  UpdateInputMode();
}

void Composer::MoveCursorLeft() {
  if (position_ > 0) {
    --position_;
    UpdateInputMode();
  }
}

void Composer::MoveCursorRight() {
  if (position_ < composition_->GetLength()) {
    ++position_;
    UpdateInputMode();
  }
}

void Composer::MoveCursorToBeginning() {
  if (position_ != 0) {
    position_ = 0;
    UpdateInputMode();
  }
}

void Composer::MoveCursorToEnd() {
  const size_t cursor_end = composition_->GetLength();
  if (position_ != cursor_end) {
    position_ = cursor_end;
    UpdateInputMode();
  }
}

void Composer::GetPreedit(string *left, string *focused, string *right) const {
  DCHECK(left);
  DCHECK(focused);
  DCHECK(right);
  composition_->GetPreedit(position_, left, focused, right);

  // TODO(komatsu): This function can be obsolete.
  string preedit = *left + *focused + *right;
  if (TransformCharactersForNumbers(&preedit)) {
    const size_t left_size = Util::CharsLen(*left);
    const size_t focused_size = Util::CharsLen(*focused);
    *left = Util::SubString(preedit, 0, left_size);
    *focused = Util::SubString(preedit, left_size, focused_size);
    *right = Util::SubString(preedit, left_size + focused_size, string::npos);
  }
}

void Composer::GetStringForPreedit(string *output) const {
  composition_->GetString(output);
  TransformCharactersForNumbers(output);
}

void Composer::GetStringForSubmission(string *output) const {
  // TODO(komatsu): We should make sure if we can integrate this
  // function to GetStringForPreedit after a while.
  GetStringForPreedit(output);
}

void Composer::GetQueryForConversion(string *output) const {
  string base_output;
  composition_->GetStringWithTrimMode(FIX, &base_output);
  TransformCharactersForNumbers(&base_output);
  Util::FullWidthAsciiToHalfWidthAscii(base_output, output);
}

namespace {
// Determine which query is suitable for a prediction query and return
// its pointer.
// Exmaple:
// = Romanji Input =
// ("もz", "も") -> "も"  // a part of romanji should be trimed.
// ("z", "") -> "z"      // ditto.
// ("もzky", "もz") -> "もzky"  // a user might intentionally typed them.
// = Kana Input =
// ("か", "") -> "か"  // a part of kana (it can be "が") should not be trimed.
string *GetBaseQueryForPrediction(string *asis_query,
                                  string *trimed_query) {
  // If the sizes are equal, there is no matter.
  if (asis_query->size() == trimed_query->size()) {
    return asis_query;
  }

  // Get the different part between asis_query and trimed_query.  For
  // example, "ky" is the different part where asis_query is "もzky"
  // and trimed_query is "もz".
  DCHECK_GT(asis_query->size(), trimed_query->size());
  const string asis_tail = asis_query->substr(trimed_query->size());
  DCHECK(!asis_tail.empty());

  // If the different part is not an alphabet, asis_query is used.
  // This check is mainly used for Kana Input.
  const Util::ScriptType asis_tail_type = Util::GetScriptType(asis_tail);
  if (asis_tail_type != Util::ALPHABET) {
    return asis_query;
  }

  // If the trimed_query is emapy and asis_query is alphabet, an empty
  // string is used because the query is probably a part of romanji.
  if (trimed_query->empty()) {  // alphabet???
    return trimed_query;
  }

  // Now there are two patterns: ("もzk", "もz") and ("もずk", "もず").
  // We assume "もzk" is user's intentional query, but "もずk" is not.
  // So our results are:
  // ("もzk", "もz") => "もzk" and ("もずk", "もず") => "もず".
  const string trimed_tail = Util::SubString(*trimed_query,
                                             Util::CharsLen(*trimed_query) - 1,
                                             string::npos);
  DCHECK(!trimed_tail.empty());
  const Util::ScriptType trimed_tail_type = Util::GetScriptType(trimed_tail);
  if (trimed_tail_type == Util::ALPHABET) {
    return asis_query;
  } else {
    return trimed_query;
  }
}
}  // anonymous namespace

void Composer::GetQueryForPrediction(string *output) const {
  string asis_query;
  composition_->GetStringWithTrimMode(ASIS, &asis_query);

  string trimed_query;
  composition_->GetStringWithTrimMode(TRIM, &trimed_query);

  // NOTE(komatsu): This is a hack to go around the difference
  // expectation between Romanji-Input and Kana-Input.  "かn" in
  // Romaji-Input should be "か" while "あか" in Kana-Input should be
  // "あか", although "かn" and "あか" have the same properties.  An
  // ideal solution is to expand the ambguity and pass all of them to
  // the converter. (e.g. "かn" -> ["かな",..."かの", "かん", ...] /
  // "あか" -> ["あか", "あが"])
  string *base_query = GetBaseQueryForPrediction(&asis_query, &trimed_query);
  TransformCharactersForNumbers(base_query);
  Util::FullWidthAsciiToHalfWidthAscii(*base_query, output);
}

size_t Composer::GetLength() const {
  return composition_->GetLength();
}

size_t Composer::GetCursor() const {
  return position_;
}


void Composer::GetTransliterations(
    transliteration::Transliterations *t13ns) const {
  GetSubTransliterations(0, GetLength(), t13ns);
}

void Composer::GetSubTransliteration(
    const transliteration::TransliterationType type,
    const size_t position,
    const size_t size,
    string *transliteration) const {
  const TransliteratorInterface *t12r = GetTransliterator(type);
  const TransliteratorInterface *kNullT12r = NULL;

  string full_base;
  composition_->GetStringWithTransliterator(t12r, &full_base);

  const size_t t13n_start =
    composition_->ConvertPosition(position, kNullT12r, t12r);
  const size_t t13n_end =
    composition_->ConvertPosition(position + size, kNullT12r, t12r);
  const size_t t13n_size = t13n_end - t13n_start;

  const string sub_base = Util::SubString(full_base, t13n_start, t13n_size);
  transliteration->clear();
  Transliterate(type, sub_base, transliteration);
}

void Composer::GetSubTransliterations(
    const size_t position,
    const size_t size,
    transliteration::Transliterations *transliterations) const {
  string t13n;
  for (size_t i = 0; i < transliteration::NUM_T13N_TYPES; ++i) {
    const transliteration::TransliterationType t13n_type =
      transliteration::TransliterationTypeArray[i];
    GetSubTransliteration(t13n_type, position, size, &t13n);
    transliterations->push_back(t13n);
  }
}

bool Composer::EnableInsert() const {
  if (GetLength() >= max_length_) {
    // do not accept long chars to prevent DOS attack.
    LOG(WARNING) << "The length is too long.";
    return false;
  }
  return true;
}

void Composer::set_max_length(size_t length) {
  max_length_ = length;
}

size_t Composer::max_length() const {
  return max_length_;
}

void Composer::AutoSwitchMode() {
  if (!GET_CONFIG(use_auto_ime_turn_off)) {
    return;
  }

  // AutoSwitchMode is only available on Roma input
  if (GET_CONFIG(preedit_method) != config::Config::ROMAN) {
    return;
  }

  string key;
  // Key should be in half-width alphanumeric.
  composition_->GetStringWithTransliterator(
      GetTransliterator(transliteration::HALF_ASCII), &key);

  ModeSwitchingHandler::ModeSwitching display_mode =
      ModeSwitchingHandler::NO_CHANGE;
  ModeSwitchingHandler::ModeSwitching input_mode =
      ModeSwitchingHandler::NO_CHANGE;
  if (!ModeSwitchingHandler::GetModeSwitchingHandler()->GetModeSwitchingRule(
          key, &display_mode, &input_mode)) {
    // If the key is not a pattern of mode switch rule, the procedure
    // stops here.
    return;
  }

  // |display_mode| affects the existing composition the user typed.
  switch (display_mode) {
    case ModeSwitchingHandler::NO_CHANGE:
      // Do nothing.
      break;
    case ModeSwitchingHandler::REVERT_TO_PREVIOUS_MODE:
      // Invalid value for display_mode
      LOG(ERROR) << "REVERT_TO_PREVIOUS_MODE is an invalid value "
                 << "for display_mode.";
      break;
    case ModeSwitchingHandler::PREFERRED_ALPHANUMERIC:
      if (input_mode_ == transliteration::FULL_ASCII) {
        SetOutputMode(transliteration::FULL_ASCII);
      } else {
        SetOutputMode(transliteration::HALF_ASCII);
      }
      break;
    case ModeSwitchingHandler::HALF_ALPHANUMERIC:
      SetOutputMode(transliteration::HALF_ASCII);
      break;
    case ModeSwitchingHandler::FULL_ALPHANUMERIC:
      SetOutputMode(transliteration::FULL_ASCII);
      break;
    default:
      LOG(ERROR) << "Unkown value: " << display_mode;
      break;
  }

  // |input_mode| affects the current input mode used for the user's
  // new typing.
  switch (input_mode) {
    case ModeSwitchingHandler::NO_CHANGE:
      // Do nothing.
      break;
    case ModeSwitchingHandler::REVERT_TO_PREVIOUS_MODE:
      SetInputMode(comeback_input_mode_);
      break;
    case ModeSwitchingHandler::PREFERRED_ALPHANUMERIC:
      if (input_mode_ != transliteration::HALF_ASCII &&
          input_mode_ != transliteration::FULL_ASCII) {
        SetTemporaryInputMode(transliteration::HALF_ASCII);
      }
      break;
    case ModeSwitchingHandler::HALF_ALPHANUMERIC:
      if (input_mode_ != transliteration::HALF_ASCII) {
        SetTemporaryInputMode(transliteration::HALF_ASCII);
      }
      break;
    case ModeSwitchingHandler::FULL_ALPHANUMERIC:
      if (input_mode_ != transliteration::FULL_ASCII) {
        SetTemporaryInputMode(transliteration::FULL_ASCII);
      }
      break;
    default:
      LOG(ERROR) << "Unkown value: " << display_mode;
      break;
  }
}

// static
Composer *Composer::Create(const Table *table) {
  Composition *composition = new Composition;
  composition->SetTable(table);
  Composer *composer = new Composer(composition);
  return composer;
}

namespace {
enum Script {
  ALPHANUM,     // 0 - 9, "0" - "9", alphabet characters.
  JA_HYPHEN,  // "ー"
  JA_COMMA,   // "、"
  JA_PERIOD,  // "。"
  OTHERS,
};

static const char *kNumberSymbols[] = {
  "+", "*", "/", "=", "(", ")", "<", ">",
  // "+", "*", "/", "=",
  "\xEF\xBC\x8B", "\xEF\xBC\x8A", "\xEF\xBC\x8F", "\xEF\xBC\x9D",
  // "(", ")", "<", ">",
  "\xEF\xBC\x88", "\xEF\xBC\x89", "\xEF\xBC\x9C", "\xEF\xBC\x9E",
};
}  // anonymous namespace

// static
bool Composer::TransformCharactersForNumbers(string *query) {
  if (query == NULL) {
    LOG(ERROR) << "query is NULL";
    return false;
  }

  // Create a vector of scripts of query characters to avoid
  // processing query string many times.
  const size_t chars_len = Util::CharsLen(*query);
  vector<Script> char_scripts;
  // flags to determine whether continue to the next step.
  bool has_symbols = false;
  bool has_numbers = false;
  for (size_t i = 0; i < chars_len; ++i) {
    Script script = OTHERS;
    const string one_char = Util::SubString(*query, i, 1);
    if (one_char == "\xE3\x83\xBC") {  // "ー"
      has_symbols = true;
      script = JA_HYPHEN;
    } else if (one_char == "\xE3\x80\x81") {  // "、"
      has_symbols = true;
      script = JA_COMMA;
    } else if (one_char == "\xE3\x80\x82") {  // "。"
      has_symbols = true;
      script = JA_PERIOD;
    } else if (Util::IsScriptType(one_char, Util::NUMBER)) {
      has_numbers = true;
      script = ALPHANUM;
    } else if (Util::IsScriptType(one_char, Util::ALPHABET)) {
      has_numbers = true;
      script = ALPHANUM;
    } else {
      for (size_t j = 0; j < arraysize(kNumberSymbols); ++j) {
        if (one_char == kNumberSymbols[j]) {
          script = ALPHANUM;
          break;
        }
      }
    }
    char_scripts.push_back(script);
  }
  DCHECK_EQ(chars_len, char_scripts.size());
  if (!has_numbers || !has_symbols) {
    VLOG(1) << "The query does not contain number, symbol or neither.";
    return false;
  }

  string transformed_query;
  bool transformed = false;
  for (size_t i = 0; i < chars_len; ++i) {
    const Script script = char_scripts[i];
    if (script == OTHERS || script == ALPHANUM) {
      // Append one character.
      Util::SubString(*query, i, 1, &transformed_query);
      continue;
    }

    // "ー" should be "−" if the previous character is a digit, or the
    // previous character is empty and the next character is a digit.
    if (script == JA_HYPHEN) {
      bool check = false;
      // If there is no previous char, the next character decides the
      // transform.
      if (i == 0) {
        // Next char should exist and be a number.
        if (i+1 < chars_len && char_scripts[i+1] == ALPHANUM) {
          check = true;
        } else if (i+2 < chars_len &&
                   char_scripts[i+1] == JA_HYPHEN &&
                   char_scripts[i+2] == ALPHANUM) {
          // Otherwise, the next character should also be JA_HYPHEN
          // and the i+2 th characters should be an alphanumeric (eg. 1--2).
          check = true;
        }
      } else {
        // If previous character(s) is an Alphanumeric or sequence of
        // JA_HYPHEN terminated with an Alphanumeric, the target
        // JA_HYPHEN should be transformed (ex. 0-, 0-----, etc).
        for (size_t j = i; j > 0; --j) {
          if (char_scripts[j - 1] == JA_HYPHEN) {
            continue;
          }
          if (char_scripts[j - 1] == ALPHANUM) {
            check = true;
          }
          break;
        }
      }

      // JA_HYPHEN should be transformed to MINUS.
      if (check) {
        string append_char;
        CharacterFormManager::GetCharacterFormManager()->
            ConvertPreeditString("\xE2\x88\x92", &append_char);  // "−"
        transformed_query.append(append_char);
        transformed = true;
      } else {
        // Append one character.
        Util::SubString(*query, i, 1, &transformed_query);
      }
      continue;
    }

    // "、" should be "," if the previous character and the next
    // character are both digits.
    if (script == JA_COMMA) {
      // Previous char should exist and be a number.
      const bool lhs_check = (i > 0 && char_scripts[i-1] == ALPHANUM);
      // Next three characters should exist and be all digits.
      const bool rhs_check = (i+1 < chars_len && char_scripts[i+1] == ALPHANUM);
      // JA_COMMA should be transformed to COMMA.
      if (lhs_check && rhs_check) {
        string append_char;
        CharacterFormManager::GetCharacterFormManager()->
            ConvertPreeditString("\xEF\xBC\x8C", &append_char);  // ","
        transformed_query.append(append_char);
        transformed = true;
      } else {
        // Append one character.
        Util::SubString(*query, i, 1, &transformed_query);
      }
      continue;
    }

    // "。" should be "." if the previous character and the next
    // character are both digits.
    if (script == JA_PERIOD) {
      // Previous char should exist and be a number.
      const bool lhs_check = (i > 0 && char_scripts[i-1] == ALPHANUM);
      // Next three characters should exist and be a digit.
      const bool rhs_check = (i+1 < chars_len && char_scripts[i+1] == ALPHANUM);

      // JA_PRERIOD should be transformed to PRERIOD.
      if (lhs_check && rhs_check) {
        string append_char;
        CharacterFormManager::GetCharacterFormManager()->
            ConvertPreeditString("\xEF\xBC\x8E", &append_char);  // "."
        transformed_query.append(append_char);
        transformed = true;
      } else {
        // Append one character.
        Util::SubString(*query, i, 1, &transformed_query);
      }
      continue;
    }
    DCHECK(false) << "Should not come here.";
  }
  if (!transformed) {
    return false;
  }

  // It is possible that the query's size in byte differs from the
  // orig_query's size in byte.
  DCHECK_EQ(Util::CharsLen(*query), Util::CharsLen(transformed_query));
  *query = transformed_query;
  return true;
}

}  // namespace composer
}  // namespace mozc