Applied fPIE, pie compier option
[platform/core/uifw/anthy.git] / src-ordering / candswap.c
1 /*
2  * ¸õÊä¤Î¸ò´¹¤Î¥Ò¥¹¥È¥ê¤ò´ÉÍý¤¹¤ë¡£
3  *
4  * anthy_swap_cand_ent() ¤Ç³Ø½¬¤¹¤ë
5  * anthy_proc_swap_candidate() ¤Ç³Ø½¬·ë²Ì¤òÍѤ¤¤ë
6  *
7  *  ¡ÖÅÄü¤¬¡×¤È¤¤¤¦¸õÊä¤ò¥È¥Ã¥×¤Ë½Ð¤·¤Æ¡ÖÅÄȪ¤¬¡×¤Ç³ÎÄꤵ¤ì¤¿¾ì¹ç¤Ï
8  *  ¼«Î©¸ìÉô:¡ÖÅÄü¡×->¡ÖÅÄȪ¡×
9  *    ¤ÎÆó¤Ä¤Î¥¨¥ó¥È¥ê¤òÄɲ乤ë
10  *
11  */
12 #include <stdlib.h>
13
14 #include <anthy/record.h>
15 #include <anthy/segment.h>
16 /* for OCHAIRE_SCORE */
17 #include <anthy/splitter.h>
18 #include "sorter.h"
19
20 #define MAX_INDEP_PAIR_ENTRY 100
21
22 /* ¸õÊä¤Î¼«Î©¸ìÉô¤ò³Ø½¬¤¹¤ë */
23 static void
24 learn_swap_cand_indep(struct cand_ent *o, struct cand_ent *n)
25 {
26   xstr os, ns;
27   int res;
28   int o_idx = o->core_elm_index;
29   int n_idx = n->core_elm_index;
30
31   /* ¼«Î©¸ìÉô¤ò´Þ¤àʸÀᤷ¤«³Ø½¬¤·¤Ê¤¤ */
32   if (o_idx < 0 || n_idx < 0) {
33     return ;
34   }
35   if (o->elm[o_idx].str.len != n->elm[n_idx].str.len) {
36     return ;
37   }
38   if (o->elm[o_idx].nth == -1 || n->elm[n_idx].nth == -1) {
39     return ;
40   }
41   res = anthy_get_nth_dic_ent_str(o->elm[o_idx].se, &o->elm[o_idx].str,
42                                   o->elm[o_idx].nth, &os);
43   if (res) {
44     return ;
45   }
46   res = anthy_get_nth_dic_ent_str(n->elm[n_idx].se, &n->elm[n_idx].str,
47                                   n->elm[n_idx].nth, &ns);
48   if (res) {
49     free(os.str);
50     return ;
51   }
52   if (anthy_select_section("INDEPPAIR", 1) == 0) {
53     if (anthy_select_row(&os, 1) == 0) {
54       anthy_set_nth_xstr(0, &ns);
55     }
56   }
57   free(os.str);
58   free(ns.str);
59 }
60
61 /*
62  * ¸õÊäo ¤ò½Ð¤·¤¿¤én ¤¬¥³¥ß¥Ã¥È¤µ¤ì¤¿¤Î¤Ç
63  * o -> n ¤òrecord¤Ë¥»¥Ã¥È¤¹¤ë
64  */
65 void
66 anthy_swap_cand_ent(struct cand_ent *o, struct cand_ent *n)
67 {
68   if (o == n) {
69     /* Æ±¤¸¸õÊä */
70     return ;
71   }
72   if (n->flag & CEF_USEDICT) {
73     /* ÍÑÎã¼­½ñ¤«¤é½Ð¤Æ¤­¤¿¸õÊä */
74     return ;
75   }
76   /* ¼«Î©¸ìÉô */
77   learn_swap_cand_indep(o, n);
78 }
79
80
81 /*
82  * ÊÑ´¹»þ¤ËÀ¸À®¤·¤¿¸õÊä¤òʤ٤¿¾õÂ֤ǺÇÍ¥Àè¤Î¸õÊä¤ò·è¤á¤ë
83  * ¥ë¡¼¥×¤Î½üµî¤Ê¤É¤â¹Ô¤¦
84  */
85 static xstr *
86 prepare_swap_candidate(xstr *target)
87 {
88   xstr *xs, *n;
89   if (anthy_select_row(target, 0) == -1) {
90     return NULL;
91   }
92   xs = anthy_get_nth_xstr(0);
93   if (!xs) {
94     return NULL;
95   }
96   /* Âè°ì¸õÊä -> xs ¤È¤Ê¤ë¤Î¤òȯ¸« */
97   anthy_mark_row_used();
98   if (anthy_select_row(xs, 0) != 0){
99     /* xs -> ¢Ý */
100     return xs;
101   }
102   /* xs -> n */
103   n = anthy_get_nth_xstr(0);
104   if (!n) {
105     return NULL;
106   }
107
108   if (!anthy_xstrcmp(target, n)) {
109     /* Âè°ì¸õÊä -> xs -> n ¤Ç n = Âè°ì¸õÊä¤Î¥ë¡¼¥× */
110     anthy_select_row(target, 0);
111     anthy_release_row();
112     anthy_select_row(xs, 0);
113     anthy_release_row();
114     /* Âè°ì¸õÊä -> xs ¤ò¾Ã¤·¤Æ¡¢¸ò´¹¤ÎɬÍפÏ̵¤· */
115     return NULL;
116   }
117   /* Âè°ì¸õÊä -> xs -> n ¤Ç n != Âè°ì¸õÊä¤Ê¤Î¤Ç
118    * Âè°ì¸õÊä -> n¤òÀßÄê
119    */
120   if (anthy_select_row(target, 0) == 0){
121     anthy_set_nth_xstr(0, n);
122   }
123   return n;
124 }
125
126 #include <src-worddic/dic_ent.h>
127
128 /*
129  * ¼«Î©¸ì¤Î¤ß
130  */
131 static void
132 proc_swap_candidate_indep(struct seg_ent *se)
133 {
134   xstr *xs;
135   xstr key;
136   int i;
137   int core_elm_idx;
138   int res;
139   struct cand_elm *core_elm;
140
141   core_elm_idx = se->cands[0]->core_elm_index;
142   if (core_elm_idx < 0) {
143     return ;
144   }
145
146   /* 0ÈÖÌܤθõÊä¤Îʸ»úÎó¤ò¼è¤ê½Ð¤¹ */
147   core_elm = &se->cands[0]->elm[core_elm_idx];
148   if (core_elm->nth < 0) {
149     return ;
150   }
151   res = anthy_get_nth_dic_ent_str(core_elm->se,
152                                   &core_elm->str,
153                                   core_elm->nth,
154                                   &key);
155   if (res) {
156     return ;
157   }
158
159   /**/
160   anthy_select_section("INDEPPAIR", 1);
161   xs = prepare_swap_candidate(&key);
162   free(key.str);
163   if (!xs) {
164     return ;
165   }
166
167   /* Âè°ì¸õÊä -> xs ¤Ê¤Î¤Ç xs¤Î¸õÊä¤òõ¤¹ */
168   for (i = 1; i < se->nr_cands; i++) {
169     if (se->cands[i]->nr_words == se->cands[0]->nr_words &&
170         se->cands[i]->core_elm_index == core_elm_idx) {
171       xstr cand;
172       res = anthy_get_nth_dic_ent_str(se->cands[i]->elm[core_elm_idx].se,
173                                       &se->cands[i]->elm[core_elm_idx].str,
174                                       se->cands[i]->elm[core_elm_idx].nth,
175                                       &cand);
176       if (res == 0 &&
177           !anthy_xstrcmp(&cand, xs)) {
178         free(cand.str);
179         /* ¤ß¤Ä¤±¤¿¤Î¤Ç¤½¤Î¸õÊä¤Î¥¹¥³¥¢¤ò¥¢¥Ã¥× */
180         se->cands[i]->score = se->cands[0]->score + 1;
181         return ;
182       }
183       free(cand.str);
184     }
185   }
186 }
187
188 /*
189  * ÊÑ´¹»þ¤ËÀ¸À®¤·¤¿¸õÊä¤òʤ٤¿¾õÂ֤ǺÇÍ¥Àè¤Î¸õÊä¤ò·è¤á¤ë
190  */
191 void
192 anthy_proc_swap_candidate(struct seg_ent *seg)
193 {
194   if (NULL == seg->cands) { /* ¼­½ñ¤â¤·¤¯¤Ï³Ø½¬¥Ç¡¼¥¿¤¬²õ¤ì¤Æ¤¤¤¿»þ¤ÎÂкö */
195     return;
196   }
197
198   if (seg->cands[0]->score >= OCHAIRE_SCORE) {
199     /* cands[0] ¤ÏÆÃÊ̤ÊÅÀ¿ô¤ò»ý¤Ã¤Æ¤¤¤ë */
200     return ;
201   }
202   if (seg->cands[0]->flag & CEF_USEDICT) {
203     return ;
204   }
205   /**/
206   proc_swap_candidate_indep(seg);
207 }
208
209 /* ¸õÊä¸ò´¹¤Î¸Å¤¤¥¨¥ó¥È¥ê¤ò¾Ã¤¹ */
210 void
211 anthy_cand_swap_ageup(void)
212 {
213   if (anthy_select_section("INDEPPAIR", 0) == 0) {
214     anthy_truncate_section(MAX_INDEP_PAIR_ENTRY);
215   }
216 }