static void load_seg() { clear_seg_label(); struct anthy_conv_stat acs; anthy_get_stat(ac, &acs); segN = 0; if (acs.nr_segment > 0) { char buf[256]; int i; for(i=0; i < acs.nr_segment; i++) { anthy_get_segment(ac, i, 0, buf, sizeof(buf)); seg[i].selidx = 0; gtk_label_set_text(GTK_LABEL(seg[i].label), buf); struct anthy_segment_stat ss; anthy_get_segment_stat(ac, i, &ss); seg[i].selN = ss.nr_candidate; segN++; } state=STATE_CONVERT; // cursor = 0; if (cursor >= acs.nr_segment) cursor = acs.nr_segment - 1; disp_convert(); } keysN=0; }
static gboolean select_idx(int c) { int idx = pageidx + c; if (idx < seg[cursor].selN) { char buf[256]; anthy_get_segment(ac, cursor, idx, buf, sizeof(buf)); struct anthy_segment_stat ss; anthy_get_segment_stat(ac, cursor, &ss); int len = ss.seg_len; gtk_label_set_text(GTK_LABEL(seg[cursor].label), buf); seg[cursor].selidx = idx; int sidx = get_sel_seg_with_ofs(seg[cursor].ofs); if (sidx==sel_segN) { sel_segN++; } if (sel_seg[sidx].sel_str) free(sel_seg[sidx].sel_str); dbg("select_idx idx:%d sidx:%d %s\n", idx, sidx, buf); sel_seg[sidx].sel_str = strdup(buf); sel_seg[sidx].ofs = seg[cursor].ofs; sel_seg[sidx].len = len; state = STATE_CONVERT; gmf.mf_hide_selections_win(); return (segN==1); } return FALSE; }
int main(int argc,char **argv){ if(anthy_init()) { printf("anthy_init() is failed.\n"); return 1; } anthy_set_personality(""); ac = anthy_create_context(); if(!ac) { printf("anthy_create_context() is failed.\n"); return 1; } //準備完了! for(k=0; k<1000; k++){ anthy_set_string(ac, "わたしのなまえはおくのです。"); //anthy_commit_segment(ac); anthy_get_stat(ac, &cs); //printf("segment:%d\n",cs.nr_segment); for(i=0; i<cs.nr_segment; i++){ struct anthy_segment_stat ss; anthy_get_segment_stat(ac,i,&ss); //printf("can:%d,len:%d |", ss.nr_candidate, ss.seg_len); for(j=0; j<ss.nr_candidate; j++){ char buffer[256]; anthy_get_segment(ac,i,j,buffer,256); //printf(" %s ", buffer); } //printf("\n"); } //printf("\n"); //anthy_print_context(ac); } anthy_release_context(ac); anthy_quit(); return 0; }
static void load_seg() { dbg("load_seg sel_segN:%d\n", sel_segN); clear_seg_label(); struct anthy_conv_stat acs; anthy_get_stat(ac, &acs); segN = 0; if (acs.nr_segment > 0) { char buf[256]; int i; int ofs = 0; for(i=0; i < acs.nr_segment; i++) { struct anthy_segment_stat ss; anthy_get_segment_stat(ac, i, &ss); int len = ss.seg_len; int idx = get_sel_seg_with_ofs(ofs); dbg("%d] sel idx:%d ofs:%d\n",i, idx, ofs); int selN = seg[i].selN = ss.nr_candidate; char *old_str = NULL; seg[i].selidx = 0; if (idx < sel_segN && sel_seg[idx].len==len) { int j; for(j=0;j<selN;j++) { anthy_get_segment(ac, i, j, buf, sizeof(buf)); if (!strcmp(buf, sel_seg[idx].sel_str) ) { dbg("old found %s", buf); seg[i].selidx = j; break; } } } anthy_get_segment(ac, i, seg[i].selidx, buf, sizeof(buf)); gtk_label_set_text(GTK_LABEL(seg[i].label), buf); dbg("seg len:%d\n", len); seg[i].ofs = ofs; seg[i].len = len; segN++; ofs += len; } state=STATE_CONVERT; // cursor = 0; if (cursor >= acs.nr_segment) cursor = acs.nr_segment - 1; disp_convert(); } keysN=0; }
static uim_lisp get_segment_length(uim_lisp ac_, uim_lisp seg_) { anthy_context_t ac; int seg, err; struct anthy_segment_stat ss; ac = get_anthy_context(ac_); seg = C_INT(seg_); validate_segment_index(ac, seg); err = anthy_get_segment_stat(ac, seg, &ss); if (err) uim_fatal_error("anthy_get_segment_stat() failed"); return MAKE_INT(ss.seg_len); }
void Conversion::resize_segment (int relative_size, int segment_id) { if (is_predicting ()) return; if (!is_converting ()) return; struct anthy_conv_stat conv_stat; anthy_get_stat (m_anthy_context, &conv_stat); int real_segment_id; if (segment_id < 0) { if (m_cur_segment < 0) return; else segment_id = m_cur_segment; real_segment_id = segment_id + m_start_id; } else { real_segment_id = segment_id + m_start_id; if (m_cur_segment > segment_id) m_cur_segment = segment_id; } if (real_segment_id >= conv_stat.nr_segment) return; // do resize anthy_resize_segment (m_anthy_context, real_segment_id, relative_size); // reset candidates of trailing segments anthy_get_stat (m_anthy_context, &conv_stat); ConversionSegments::iterator start_iter = m_segments.begin(); ConversionSegments::iterator end_iter = m_segments.end(); m_segments.erase (start_iter + segment_id, end_iter); for (int i = real_segment_id; i < conv_stat.nr_segment; i++) { struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, i, &seg_stat); m_segments.push_back ( ConversionSegment (get_segment_string (i - m_start_id, 0), 0, seg_stat.seg_len)); } }
void Conversion::clear (int segment_id) { if (segment_id < 0 || m_segments.size () <= 0 || segment_id >= (int) m_segments.size () - 1) { // complete clear anthy_reset_context (m_anthy_context); m_segments.clear (); m_start_id = 0; m_cur_segment = -1; m_predicting = false; } else { // partial clear // remove stored segments ConversionSegments::iterator it = m_segments.begin (); m_segments.erase (it, it + segment_id + 1); // adjust selected segment int new_start_segment_id = m_start_id + segment_id + 1; if (m_cur_segment >= 0) { m_cur_segment -= new_start_segment_id - m_start_id; if (m_cur_segment < 0) m_cur_segment = 0; } // adjust offset unsigned int clear_len = 0; for (int i = m_start_id; i < new_start_segment_id; i++) { struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, i, &seg_stat); clear_len += seg_stat.seg_len; } m_reading.erase (0, clear_len, true); m_start_id = new_start_segment_id; } }
// // starting and finishing // void Conversion::convert (std::string source, CandidateType ctype, bool single_segment) { if (is_converting ()) return; clear (); std::string dest; // convert struct anthy_conv_stat conv_stat; anthy_get_stat (m_anthy_context, &conv_stat); if (conv_stat.nr_segment <= 0) { dest = source; anthy_set_string (m_anthy_context, dest.c_str ()); } if (single_segment) join_all_segments (); // get information about conversion string anthy_get_stat (m_anthy_context, &conv_stat); if (conv_stat.nr_segment <= 0) return; // select first segment m_cur_segment = 0; // create segments m_segments.clear(); for (int i = m_start_id; i < conv_stat.nr_segment; i++) { struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, i, &seg_stat); m_segments.push_back ( ConversionSegment (get_segment_string (i, ctype), ctype, seg_stat.seg_len)); } }
int Conversion::get_segment_size (int segment_id) { if (!is_converting ()) return -1; struct anthy_conv_stat conv_stat; anthy_get_stat (m_anthy_context, &conv_stat); if (segment_id < 0) { if (m_cur_segment < 0) return -1; else segment_id = m_cur_segment; } int real_segment_id = segment_id + m_start_id; if (real_segment_id >= conv_stat.nr_segment) return -1; struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, real_segment_id, &seg_stat); return seg_stat.seg_len; }
std::string Conversion::get_segment_string (int segment_id, int candidate_id) { if (segment_id < 0) { if (m_cur_segment < 0) return std::string (); else segment_id = m_cur_segment; } struct anthy_conv_stat conv_stat; anthy_get_stat (m_anthy_context, &conv_stat); if (conv_stat.nr_segment <= 0) return std::string (); if (m_start_id < 0 || m_start_id >= conv_stat.nr_segment) { return std::string (); // error } if (segment_id < 0 || segment_id + m_start_id >= conv_stat.nr_segment) { return std::string (); //error } // character position of the head of segment. unsigned int real_seg_start = 0; for (int i = m_start_id; i < m_start_id + segment_id; i++) { struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, i, &seg_stat); real_seg_start += seg_stat.seg_len; } int real_seg = segment_id + m_start_id; int cand; if (candidate_id <= FCITX_ANTHY_LAST_SPECIAL_CANDIDATE) cand = m_segments[segment_id].get_candidate_id (); else cand = candidate_id; // get information of this segment struct anthy_segment_stat seg_stat; anthy_get_segment_stat (m_anthy_context, real_seg, &seg_stat); // get string of this segment std::string segment_str; if (cand < 0) { get_reading_substr (segment_str, segment_id, cand, real_seg_start, seg_stat.seg_len); } else { int len = anthy_get_segment (m_anthy_context, real_seg, cand, NULL, 0); if (len > 0) { char buf[len + 1]; anthy_get_segment (m_anthy_context, real_seg, cand, buf, len + 1); buf[len] = '\0'; segment_str = buf; } } return segment_str; }