Debian Codegraph @

src/conversion.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2005 Takuro Ashie
4 * Copyright (C) 2012 CSSlayer
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <string.h>
22 #include <fcitx-utils/log.h>
23 #include "conversion.h"
24 #include "utils.h"
25 #include "imengine.h"
26
27 static void rotate_case (std::string &str);
28
29
30 //
31 // ConversionSegment Class
32 //
33 ConversionSegment::ConversionSegment (std::string str, int cand_id,
34 unsigned int reading_len)
35 : m_string (str),
36 m_cand_id (cand_id),
37 m_reading_len (reading_len)
38 {
39 }
40
41 ConversionSegment::~ConversionSegment ()
42 {
43 }
44
45 std::string &
46 ConversionSegment::get_string (void)
47 {
48 return m_string;
49 }
50
51
52 int
53 ConversionSegment::get_candidate_id (void)
54 {
55 return m_cand_id;
56 }
57
58 unsigned int
59 ConversionSegment::get_reading_length (void)
60 {
61 return m_reading_len;
62 }
63
64 void
65 ConversionSegment::set (std::string str, int cand_id)
66 {
67 m_string = str;
68 m_cand_id = cand_id;
69 }
70
71 void
72 ConversionSegment::set_reading_length (unsigned int len)
73 {
74 m_reading_len = len;
75 }
76
77
78
79 //
80 // Conversion Class
81 //
82 Conversion::Conversion (AnthyInstance &anthy, Reading &reading)
83 : m_anthy (anthy),
84 m_reading (reading),
85 m_anthy_context (anthy_create_context()),
86 m_start_id (0),
87 m_cur_segment (-1),
88 m_predicting (false)
89 {
90 anthy_context_set_encoding (m_anthy_context, ANTHY_UTF8_ENCODING);
91 }
92
93 Conversion::~Conversion ()
94 {
95 anthy_release_context (m_anthy_context);
96 }
97
98
99
100 //
101 // starting and finishing
102 //
103 void
104 Conversion::convert (std::string source, CandidateType ctype,
105 bool single_segment)
106 {
107 if (is_converting ())
108 return;
109
110 clear ();
111
112 std::string dest;
113
114 // convert
115 struct anthy_conv_stat conv_stat;
116 anthy_get_stat (m_anthy_context, &conv_stat);
117 if (conv_stat.nr_segment <= 0) {
118 dest = source;
119 anthy_set_string (m_anthy_context, dest.c_str ());
120 }
121
122 if (single_segment)
123 join_all_segments ();
124
125 // get information about conversion string
126 anthy_get_stat (m_anthy_context, &conv_stat);
127 if (conv_stat.nr_segment <= 0)
128 return;
129
130 // select first segment
131 m_cur_segment = 0;
132
133 // create segments
134 m_segments.clear();
135 for (int i = m_start_id; i < conv_stat.nr_segment; i++) {
136 struct anthy_segment_stat seg_stat;
137 anthy_get_segment_stat (m_anthy_context, i, &seg_stat);
138 m_segments.push_back (
139 ConversionSegment (get_segment_string (i, ctype), ctype,
140 seg_stat.seg_len));
141 }
142 }
143
144 void
145 Conversion::convert (CandidateType ctype, bool single_segment)
146 {
147 convert (m_reading.get_by_char (), ctype, single_segment);
148 }
149
150 void
151 Conversion::convert (const std::string &source, bool single_segment)
152 {
153 convert (source, FCITX_ANTHY_CANDIDATE_DEFAULT, single_segment);
154 }
155
156 void
157 Conversion::predict (void)
158 {
159 clear ();
160
161 #ifdef HAS_ANTHY_PREDICTION
162 std::string str;
163
164 str = m_reading.get_by_char ();
165 anthy_set_prediction_string (m_anthy_context, str.c_str ());
166
167 struct anthy_prediction_stat ps;
168 anthy_get_prediction_stat (m_anthy_context, &ps);
169 if (ps.nr_prediction > 0)
170 m_predicting = true;
171 else
172 anthy_reset_context (m_anthy_context);
173 #endif /* HAS_ANTHY_PREDICTION */
174 }
175
176 void
177 Conversion::clear (int segment_id)
178 {
179 if (segment_id < 0 || m_segments.size () <= 0 ||
180 segment_id >= (int) m_segments.size () - 1)
181 {
182 // complete clear
183
184 anthy_reset_context (m_anthy_context);
185
186 m_segments.clear ();
187
188 m_start_id = 0;
189 m_cur_segment = -1;
190 m_predicting = false;
191
192 } else {
193 // partial clear
194
195 // remove stored segments
196 ConversionSegments::iterator it = m_segments.begin ();
197 m_segments.erase (it, it + segment_id + 1);
198
199 // adjust selected segment
200 int new_start_segment_id = m_start_id + segment_id + 1;
201 if (m_cur_segment >= 0) {
202 m_cur_segment -= new_start_segment_id - m_start_id;
203 if (m_cur_segment < 0)
204 m_cur_segment = 0;
205 }
206
207 // adjust offset
208 unsigned int clear_len = 0;
209 for (int i = m_start_id; i < new_start_segment_id; i++) {
210 struct anthy_segment_stat seg_stat;
211 anthy_get_segment_stat (m_anthy_context, i, &seg_stat);
212 clear_len += seg_stat.seg_len;
213 }
214 m_reading.erase (0, clear_len, true);
215 m_start_id = new_start_segment_id;
216 }
217 }
218
219 void
220 Conversion::commit (int segment_id, bool learn)
221 {
222 if (!is_converting ()) return;
223
224 // learn
225 for (unsigned int i = m_start_id;
226 learn &&
227 i < m_segments.size () &&
228 (segment_id < 0 || (int) i <= segment_id);
229 i++)
230 {
231 if (m_segments[i].get_candidate_id () >= 0)
232 anthy_commit_segment (m_anthy_context, i,
233 m_segments[i].get_candidate_id ());
234 }
235
236 clear (segment_id);
237 }
238
239
240
241 //
242 // getting status
243 //
244 bool
245 Conversion::is_converting (void)
246 {
247 if (m_segments.size () > 0)
248 return true;
249 else
250 return false;
251 }
252
253 bool
254 Conversion::is_predicting (void)
255 {
256 return m_predicting;
257 }
258
259 std::string
260 Conversion::get (void)
261 {
262 std::string str;
263 ConversionSegments::iterator it;
264 for (it = m_segments.begin (); it != m_segments.end (); it++)
265 str += it->get_string ();
266 return str;
267 }
268
269 unsigned int
270 Conversion::get_length (void)
271 {
272 unsigned int len = 0;
273 ConversionSegments::iterator it;
274 for (it = m_segments.begin (); it != m_segments.end (); it++)
275 len += it->get_string().length ();
276 return len;
277 }
278
279 unsigned int
280 Conversion::get_length_by_char (void)
281 {
282 unsigned int len = 0;
283 ConversionSegments::iterator it;
284 for (it = m_segments.begin (); it != m_segments.end (); it++)
285 len += util_utf8_string_length(it->get_string());
286 return len;
287 }
288
289 void
290 Conversion::update_preedit (void)
291 {
292 FcitxMessages* preedit;
293 if (m_anthy.support_client_preedit())
294 preedit = m_anthy.get_client_preedit();
295 else
296 preedit = m_anthy.get_preedit();
297 unsigned int seg_id;
298 ConversionSegments::iterator it;
299 for (it = m_segments.begin (), seg_id = 0;
300 it != m_segments.end ();
301 it++, seg_id++)
302 {
303 // create attribute for this segment
304 if (it->get_string().length () <= 0) {
305 continue;
306 }
307
308 FcitxMessageType type;
309 if ((int) seg_id == m_cur_segment) {
310 type = (FcitxMessageType) (MSG_HIGHLIGHT | MSG_FIRSTCAND);
311 } else {
312 type = (FcitxMessageType) MSG_INPUT;
313 }
314 FcitxMessagesAddMessageAtLast(preedit, type, "%s", it->get_string().c_str());
315 }
316 }
317
318
319
320 //
321 // segments of the converted sentence
322 //
323 int
324 Conversion::get_nr_segments (void)
325 {
326 if (!is_converting ()) return 0;
327
328 struct anthy_conv_stat conv_stat;
329 anthy_get_stat (m_anthy_context, &conv_stat);
330
331 return conv_stat.nr_segment - m_start_id;
332 }
333
334 std::string
335 Conversion::get_segment_string (int segment_id, int candidate_id)
336 {
337 if (segment_id < 0) {
338 if (m_cur_segment < 0)
339 return std::string ();
340 else
341 segment_id = m_cur_segment;
342 }
343
344 struct anthy_conv_stat conv_stat;
345 anthy_get_stat (m_anthy_context, &conv_stat);
346
347 if (conv_stat.nr_segment <= 0)
348 return std::string ();
349
350 if (m_start_id < 0 ||
351 m_start_id >= conv_stat.nr_segment)
352 {
353 return std::string (); // error
354 }
355
356 if (segment_id < 0 ||
357 segment_id + m_start_id >= conv_stat.nr_segment)
358 {
359 return std::string (); //error
360 }
361
362 // character position of the head of segment.
363 unsigned int real_seg_start = 0;
364 for (int i = m_start_id; i < m_start_id + segment_id; i++) {
365 struct anthy_segment_stat seg_stat;
366 anthy_get_segment_stat (m_anthy_context, i, &seg_stat);
367 real_seg_start += seg_stat.seg_len;
368 }
369
370 int real_seg = segment_id + m_start_id;
371 int cand;
372 if (candidate_id <= FCITX_ANTHY_LAST_SPECIAL_CANDIDATE)
373 cand = m_segments[segment_id].get_candidate_id ();
374 else
375 cand = candidate_id;
376
377 // get information of this segment
378 struct anthy_segment_stat seg_stat;
379 anthy_get_segment_stat (m_anthy_context, real_seg, &seg_stat);
380
381 // get string of this segment
382 std::string segment_str;
383 if (cand < 0) {
384 get_reading_substr (segment_str, segment_id, cand,
385 real_seg_start, seg_stat.seg_len);
386 } else {
387 int len = anthy_get_segment (m_anthy_context, real_seg, cand, NULL, 0);
388 if (len > 0) {
389 char buf[len + 1];
390 anthy_get_segment (m_anthy_context, real_seg, cand, buf, len + 1);
391 buf[len] = '\0';
392 segment_str = buf;
393 }
394 }
395
396 return segment_str;
397 }
398
399 int
400 Conversion::get_selected_segment (void)
401 {
402 return m_cur_segment;
403 }
404
405 void
406 Conversion::select_segment (int segment_id)
407 {
408 if (!is_converting ()) return;
409
410 if (segment_id < 0) {
411 m_cur_segment = -1;
412 m_anthy.reset_cursor(0);
413 return;
414 }
415
416 struct anthy_conv_stat conv_stat;
417 anthy_get_stat (m_anthy_context, &conv_stat);
418
419 int real_segment_id = segment_id + m_start_id;
420
421 if (segment_id >= 0 && real_segment_id < conv_stat.nr_segment) {
422 if (m_cur_segment != segment_id) {
423 if (segment_id < m_segments.size()) {
424 m_anthy.reset_cursor(m_segments[segment_id].get_candidate_id());
425 }
426 m_cur_segment = segment_id;
427 }
428 }
429 }
430
431 int
432 Conversion::get_segment_size (int segment_id)
433 {
434 if (!is_converting ()) return -1;
435
436 struct anthy_conv_stat conv_stat;
437 anthy_get_stat (m_anthy_context, &conv_stat);
438
439 if (segment_id < 0) {
440 if (m_cur_segment < 0)
441 return -1;
442 else
443 segment_id = m_cur_segment;
444 }
445 int real_segment_id = segment_id + m_start_id;
446
447 if (real_segment_id >= conv_stat.nr_segment)
448 return -1;
449
450 struct anthy_segment_stat seg_stat;
451 anthy_get_segment_stat (m_anthy_context, real_segment_id, &seg_stat);
452
453 return seg_stat.seg_len;
454 }
455
456 void
457 Conversion::resize_segment (int relative_size, int segment_id)
458 {
459 if (is_predicting ()) return;
460 if (!is_converting ()) return;
461
462 struct anthy_conv_stat conv_stat;
463 anthy_get_stat (m_anthy_context, &conv_stat);
464
465 int real_segment_id;
466
467 if (segment_id < 0) {
468 if (m_cur_segment < 0)
469 return;
470 else
471 segment_id = m_cur_segment;
472 real_segment_id = segment_id + m_start_id;
473 } else {
474 real_segment_id = segment_id + m_start_id;
475 if (m_cur_segment > segment_id)
476 m_cur_segment = segment_id;
477 }
478
479 if (real_segment_id >= conv_stat.nr_segment)
480 return;
481
482 // do resize
483 anthy_resize_segment (m_anthy_context, real_segment_id, relative_size);
484
485 // reset candidates of trailing segments
486 anthy_get_stat (m_anthy_context, &conv_stat);
487 ConversionSegments::iterator start_iter = m_segments.begin();
488 ConversionSegments::iterator end_iter = m_segments.end();
489 m_segments.erase (start_iter + segment_id, end_iter);
490 for (int i = real_segment_id; i < conv_stat.nr_segment; i++) {
491 struct anthy_segment_stat seg_stat;
492 anthy_get_segment_stat (m_anthy_context, i, &seg_stat);
493 m_segments.push_back (
494 ConversionSegment (get_segment_string (i - m_start_id, 0), 0,
495 seg_stat.seg_len));
496 }
497 }
498
499 unsigned int
500 Conversion::get_segment_position (int segment_id)
501 {
502 if (segment_id < 0) {
503 if (m_cur_segment < 0)
504 return get_length ();
505 else
506 segment_id = m_cur_segment;
507 }
508
509 unsigned int pos = 0;
510
511 for (unsigned int i = 0;
512 (int) i < m_cur_segment && i < m_segments.size ();
513 i++)
514 {
515 pos += m_segments[i].get_string().length ();
516 }
517
518 return pos;
519 }
520
521 INPUT_RETURN_VALUE
522 get_candidate(void* arg, struct _FcitxCandidateWord* candWord)
523 {
524 int* i = (int*) candWord->priv;
525 AnthyInstance* anthy = (AnthyInstance*) candWord->owner;
526 anthy->action_select_candidate(*i);
527 return IRV_DO_NOTHING;
528 }
529
530 //
531 // candidates for a segment or prediction
532 //
533 void
534 Conversion::get_candidates (FcitxCandidateWordList *table, int segment_id)
535 {
536 FcitxCandidateWordReset(table);
537 FcitxCandidateWordSetLayoutHint(table, m_anthy.get_config()->m_candidate_layout);
538 int selected = get_selected_candidate();
539
540 if (is_predicting ()) {
541 #ifdef HAS_ANTHY_PREDICTION
542 std::string str;
543 struct anthy_prediction_stat ps;
544
545 anthy_get_prediction_stat (m_anthy_context, &ps);
546
547 for (int i = 0; i < ps.nr_prediction; i++) {
548 int len = anthy_get_prediction (m_anthy_context, i, NULL, 0);
549 if (len <= 0)
550 continue;
551
552 char buf[len + 1];
553 anthy_get_prediction (m_anthy_context, i, buf, len + 1);
554 buf[len] = '\0';
555
556 FcitxCandidateWord candWord;
557 int *p = fcitx_utils_new(int);
558 *p = i;
559 candWord.callback = get_candidate;
560 candWord.extraType = MSG_OTHER;
561 candWord.owner = (void*) &m_anthy;
562 candWord.priv = (void*) p;
563 candWord.strExtra = NULL;
564 candWord.strWord = strdup(buf);
565 if (i == selected)
566 candWord.wordType = MSG_CANDIATE_CURSOR;
567 else
568 candWord.wordType = MSG_OTHER;
569
570 FcitxCandidateWordAppend(table, &candWord);
571 }
572 #endif /* HAS_ANTHY_PREDICTION */
573 } else if (is_converting ()) {
574 struct anthy_conv_stat conv_stat;
575 anthy_get_stat (m_anthy_context, &conv_stat);
576
577 if (conv_stat.nr_segment <= 0)
578 return;
579
580 if (segment_id < 0) {
581 if (m_cur_segment < 0)
582 return;
583 else
584 segment_id = m_cur_segment;
585 }
586 int real_segment_id = segment_id + m_start_id;
587
588 if (real_segment_id >= conv_stat.nr_segment)
589 return;
590
591 struct anthy_segment_stat seg_stat;
592 anthy_get_segment_stat (m_anthy_context, real_segment_id, &seg_stat);
593
594 for (int i = 0; i < seg_stat.nr_candidate; i++) {
595 int len = anthy_get_segment (m_anthy_context, real_segment_id, i,
596 NULL, 0);
597 if (len <= 0)
598 continue;
599
600 char buf[len + 1];
601 anthy_get_segment (m_anthy_context, real_segment_id,
602 i, buf, len + 1);
603
604 FcitxCandidateWord candWord;
605 int *p = fcitx_utils_new(int);
606 *p = i;
607 candWord.callback = get_candidate;
608 candWord.extraType = MSG_OTHER;
609 candWord.owner = (void*) &m_anthy;
610 candWord.priv = (void*) p;
611 candWord.strExtra = NULL;
612 candWord.strWord = strdup(buf);
613 if (i == selected)
614 candWord.wordType = MSG_CANDIATE_CURSOR;
615 else
616 candWord.wordType = MSG_OTHER;
617
618 FcitxCandidateWordAppend(table, &candWord);
619 }
620 }
621 }
622
623 int
624 Conversion::get_selected_candidate (int segment_id)
625 {
626 if (is_predicting ()) {
627 #ifdef HAS_ANTHY_PREDICTION
628 struct anthy_prediction_stat ps;
629
630 anthy_get_prediction_stat (m_anthy_context, &ps);
631
632 if (ps.nr_prediction <= 0)
633 return -1;
634
635 if (segment_id < 0) {
636 if (m_cur_segment < 0)
637 return -1;
638 else
639 segment_id = m_cur_segment;
640 } else if (segment_id >= ps.nr_prediction) {
641 return -1;
642 }
643
644 return m_segments[segment_id].get_candidate_id ();
645 #endif /* HAS_ANTHY_PREDICTION */
646
647 } else if (is_converting ()) {
648 struct anthy_conv_stat cs;
649
650 anthy_get_stat (m_anthy_context, &cs);
651
652 if (cs.nr_segment <= 0)
653 return -1;
654
655 if (segment_id < 0) {
656 if (m_cur_segment < 0)
657 return -1;
658 else
659 segment_id = m_cur_segment;
660 } else if (segment_id >= cs.nr_segment) {
661 return -1;
662 }
663
664 return m_segments[segment_id].get_candidate_id ();
665 }
666
667 return -1;
668 }
669
670 void
671 Conversion::select_candidate (int candidate_id, int segment_id)
672 {
673 if (is_predicting ()) {
674 #ifdef HAS_ANTHY_PREDICTION
675 if (candidate_id < FCITX_ANTHY_CANDIDATE_DEFAULT)
676 return;
677
678 struct anthy_prediction_stat ps;
679 anthy_get_prediction_stat (m_anthy_context, &ps);
680
681 if (ps.nr_prediction <= 0)
682 return;
683
684 if (!is_converting ()) {
685 m_cur_segment = 0;
686 m_segments.push_back (ConversionSegment (
687 get_prediction_string (0), 0,
688 m_reading.get_length ()));
689 }
690
691 if (candidate_id < ps.nr_prediction) {
692 m_segments[0].set (get_prediction_string (candidate_id),
693 candidate_id);
694 }
695 #endif /* HAS_ANTHY_PREDICTION */
696
697 } else if (is_converting ()) {
698 if (candidate_id <= FCITX_ANTHY_LAST_SPECIAL_CANDIDATE)
699 return;
700
701 struct anthy_conv_stat conv_stat;
702 anthy_get_stat (m_anthy_context, &conv_stat);
703
704 if (conv_stat.nr_segment <= 0)
705 return;
706
707 if (segment_id < 0) {
708 if (m_cur_segment < 0)
709 return;
710 else
711 segment_id = m_cur_segment;
712 }
713 int real_segment_id = segment_id + m_start_id;
714
715 if (segment_id >= conv_stat.nr_segment)
716 return;
717
718 struct anthy_segment_stat seg_stat;
719 anthy_get_segment_stat (m_anthy_context, real_segment_id, &seg_stat);
720
721 if (candidate_id == FCITX_ANTHY_CANDIDATE_HALF) {
722 switch (m_segments[segment_id].get_candidate_id ()) {
723 case FCITX_ANTHY_CANDIDATE_LATIN:
724 case FCITX_ANTHY_CANDIDATE_WIDE_LATIN:
725 candidate_id = FCITX_ANTHY_CANDIDATE_LATIN;
726 break;
727 default:
728 candidate_id = FCITX_ANTHY_CANDIDATE_HALF_KATAKANA;
729 break;
730 }
731 }
732
733 if (candidate_id < seg_stat.nr_candidate) {
734 m_segments[segment_id].set (get_segment_string (segment_id,
735 candidate_id),
736 candidate_id);
737 }
738 }
739 }
740
741
742
743 //
744 // Utilities
745 //
746 void
747 Conversion::get_reading_substr (std::string &string,
748 int segment_id,
749 int candidate_id,
750 int seg_start,
751 int seg_len)
752 {
753 int prev_cand = 0;
754
755 if (segment_id < (int) m_segments.size ())
756 prev_cand = m_segments[segment_id].get_candidate_id ();
757
758 switch ((CandidateType) candidate_id) {
759 case FCITX_ANTHY_CANDIDATE_LATIN:
760 if (prev_cand == FCITX_ANTHY_CANDIDATE_LATIN) {
761 std::string str = m_segments[segment_id].get_string ();
762 rotate_case (str);
763 string = str;
764 } else {
765 string = m_reading.get_by_char (seg_start, seg_len,
766 FCITX_ANTHY_STRING_LATIN);
767 }
768 break;
769
770 case FCITX_ANTHY_CANDIDATE_WIDE_LATIN:
771 if (prev_cand == FCITX_ANTHY_CANDIDATE_WIDE_LATIN) {
772 std::string str;
773 util_convert_to_half (str, m_segments[segment_id].get_string ());
774 rotate_case (str);
775 util_convert_to_wide (string, str);
776 } else {
777 string = m_reading.get_by_char (seg_start, seg_len,
778 FCITX_ANTHY_STRING_WIDE_LATIN);
779 }
780 break;
781
782 case FCITX_ANTHY_CANDIDATE_KATAKANA:
783 string = m_reading.get_by_char (seg_start, seg_len,
784 FCITX_ANTHY_STRING_KATAKANA);
785 break;
786
787 case FCITX_ANTHY_CANDIDATE_HALF_KATAKANA:
788 string = m_reading.get_by_char (seg_start, seg_len,
789 FCITX_ANTHY_STRING_HALF_KATAKANA);
790 break;
791
792 case FCITX_ANTHY_CANDIDATE_HALF:
793 // shouldn't reach to this entry
794 string = m_reading.get_by_char (seg_start, seg_len,
795 FCITX_ANTHY_STRING_HALF_KATAKANA);
796 break;
797
798 case FCITX_ANTHY_CANDIDATE_HIRAGANA:
799 default:
800 string = m_reading.get_by_char (seg_start, seg_len,
801 FCITX_ANTHY_STRING_HIRAGANA);
802 break;
803 }
804 }
805
806 std::string
807 Conversion::get_prediction_string (int candidate_id)
808 {
809 #ifdef HAS_ANTHY_PREDICTION
810 if (!is_predicting ())
811 return std::string ();
812
813 struct anthy_prediction_stat ps;
814 anthy_get_prediction_stat (m_anthy_context, &ps);
815
816 if (ps.nr_prediction <= 0)
817 return std::string ();
818
819 int len = anthy_get_prediction (m_anthy_context, candidate_id, NULL, 0);
820 if (len <= 0)
821 return std::string ();
822
823 char buf[len + 1];
824 anthy_get_prediction (m_anthy_context, candidate_id, buf, len + 1);
825 buf[len] = '\0';
826
827 std::string cand = buf;
828
829 return cand;
830 #else /* HAS_ANTHY_PREDICTION */
831 return std::string ();
832 #endif /* HAS_ANTHY_PREDICTION */
833 }
834
835 void
836 Conversion::join_all_segments (void)
837 {
838 do {
839 struct anthy_conv_stat conv_stat;
840 anthy_get_stat (m_anthy_context, &conv_stat);
841 int nr_seg = conv_stat.nr_segment - m_start_id;
842
843 if (nr_seg > 1)
844 anthy_resize_segment (m_anthy_context, m_start_id, 1);
845 else
846 break;
847 } while (true);
848 }
849
850 static void
851 rotate_case (std::string &str)
852 {
853 bool is_mixed = false;
854 for (unsigned int i = 1; i < str.length (); i++) {
855 if ((isupper (str[0]) && islower (str[i])) ||
856 (islower (str[0]) && isupper (str[i])))
857 {
858 is_mixed = true;
859 break;
860 }
861 }
862
863 if (is_mixed) {
864 // Anthy -> anthy, anThy -> anthy
865 for (unsigned int i = 0; i < str.length (); i++)
866 str[i] = tolower (str[i]);
867 } else if (isupper (str[0])) {
868 // ANTHY -> Anthy
869 for (unsigned int i = 1; i < str.length (); i++)
870 str[i] = tolower (str[i]);
871 } else {
872 // anthy -> ANTHY
873 for (unsigned int i = 0; i < str.length (); i++)
874 str[i] = toupper (str[i]);
875 }
876 }