11
2+ /* MIT License
3+ Copyright (c) 2016 KIHARA, Hideto
4+
5+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6+ this software and associated documentation files (the "Software"), to deal in
7+ the Software without restriction, including without limitation the rights to
8+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+ the Software, and to permit persons to whom the Software is furnished to do so,
10+ subject to the following conditions:
11+
12+ The above copyright notice and this permission notice shall be included in all
13+ copies or substantial portions of the Software.
14+
15+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+ */
22+
223#include " PostMazeContext.h"
324#include " moji.h"
425
526const int MAX_SUFFIX = 4 ; // 活用語尾の最大文字数 TODO:imtutcnfで設定可能にする
27+ /* *
28+ * 活用する語の読みに付加する文字。
29+ * 交ぜ書き変換辞書の読みには、活用する語はこの文字付きで登録されている。
30+ */
31+ const std::wstring STR_INFLECTION_MARK = L" ―" ;
32+ const wchar_t CHAR_INFLECTION_MARK = L' ―' ;
633
734CPostMazeContext::CPostMazeContext ()
835{
@@ -23,24 +50,22 @@ void CPostMazeContext::Deactivate()
2350 postyomied = 0 ;
2451 postyomiResizing = PYR_NO;
2552 resizeWithInflection = false ;
53+ isInflection = false ;
2654}
2755
2856/* *
2957 * 後置型交ぜ書き変換状態を有効化
3058 * @param yomi 読み
31- * @param isKatuyo 活用する語として変換を開始するか
59+ * @param _isInflection 活用する語として変換を開始するか
3260 * @param startResizing yomiが変換できなかった場合、縮めながらの変換を試みるか
3361 * @param _resizeWithInflection 活用しない語を縮めた時に、活用する語としての変換を試みるか
3462 */
35- void CPostMazeContext::Activate (const std::wstring& yomi, bool isKatuyo , bool startResizing, bool _resizeWithInflection)
63+ void CPostMazeContext::Activate (const std::wstring& yomi, bool _isInflection , bool startResizing, bool _resizeWithInflection)
3664{
3765 postyomi.assign (yomi);
3866 postyomist = 0 ;
3967 postyomied = postyomi.size ();
40- if (isKatuyo)
41- {
42- postyomi.append (L" ―" );
43- }
68+ isInflection = _isInflection;
4469 if (startResizing)
4570 {
4671 postyomiResizing = PYR_SHRINKING;
@@ -70,27 +95,24 @@ void CPostMazeContext::EndResizing()
7095 */
7196bool CPostMazeContext::IsYomiInflection ()
7297{
73- return (IsActive () && postyomi[postyomi. size () - 1 ] == L ' ― ' );
98+ return (IsActive () && isInflection );
7499}
75100
76101/* *
77102 * 読みを取得する
103+ * @param withInflection 活用する語の場合、活用する語を示す「―」を付けるかどうか
78104 * @param [out] yomi 読み
79105 * @return true:読みがあった(=IsActive())場合。false:読みが無い場合
80106 */
81- bool CPostMazeContext::GetYomi (std::wstring *yomi)
107+ bool CPostMazeContext::GetYomi (bool withInflection, std::wstring *yomi)
82108{
83109 yomi->clear ();
84110 if (IsActive ())
85111 {
86- if (postyomi[postyomi.size () - 1 ] == L' ―' )
87- {
88- // TODO:ユーザが入力した'―'はそのままにする
89- yomi->assign (postyomi, 0 , postyomi.size () - 1 );
90- }
91- else
112+ yomi->assign (postyomi);
113+ if (withInflection && isInflection)
92114 {
93- yomi->assign (postyomi );
115+ yomi->append (STR_INFLECTION_MARK );
94116 }
95117 return true ;
96118 }
@@ -105,9 +127,9 @@ bool CPostMazeContext::GetYomi(std::wstring *yomi)
105127bool CPostMazeContext::GetGobi (std::wstring *gobi)
106128{
107129 gobi->clear ();
108- if (IsYomiInflection () && postyomied < postyomi.size () - 1 ) // -1:'―'
130+ if (IsYomiInflection () && postyomied < postyomi.size ())
109131 {
110- gobi->assign (postyomi, postyomied, postyomi.size () - 1 - postyomied);
132+ gobi->assign (postyomi, postyomied, postyomi.size () - postyomied);
111133 return true ;
112134 }
113135 return false ;
@@ -175,16 +197,15 @@ bool CPostMazeContext::Shrink(std::wstring *yomi)
175197 // 語幹の長さは保持したまま読みを縮める。対象読みを右にずらしたものに
176198 // 例: 「あおい」に対し、「あお」→「おい」
177199 size_t ed = ForwardMoji (postyomi, postyomied, 1 );
178- if (ed > postyomied && ed < postyomi. size () )
200+ if (ed > postyomied)
179201 {
180- // (postyomi末尾は'―'なのでed==postyomi.size()は不可)
181202 size_t st = ForwardMoji (postyomi, postyomist, 1 );
182203 if (st > postyomist)
183204 {
184205 postyomist = st;
185206 postyomied = ed;
186207 yomi->assign (postyomi, st, ed - st);
187- yomi->append (L" ― " );
208+ yomi->append (STR_INFLECTION_MARK );
188209 postyomiResizing = PYR_SHRINKING;
189210 return true ;
190211 }
@@ -194,7 +215,7 @@ bool CPostMazeContext::Shrink(std::wstring *yomi)
194215 // 例: 「あおい」に対し、「おい」→「あ」
195216 if (curlen > 1 )
196217 {
197- size_t alllen = CountMoji (postyomi) - 1 ; // -1:'―'
218+ size_t alllen = CountMoji (postyomi);
198219 size_t newlen = curlen - 1 ;
199220 size_t st = 0 ;
200221 // 語尾が長くなりすぎて、余分な候補が表示されるのを回避
@@ -204,12 +225,12 @@ bool CPostMazeContext::Shrink(std::wstring *yomi)
204225 st = ForwardMoji (postyomi, st, n);
205226 }
206227 ed = ForwardMoji (postyomi, st, newlen);
207- if (ed > st && ed < postyomi. size () )
228+ if (ed > st)
208229 {
209230 postyomist = st;
210231 postyomied = ed;
211232 yomi->assign (postyomi, st, ed - st);
212- yomi->append (L" ― " );
233+ yomi->append (STR_INFLECTION_MARK );
213234 postyomiResizing = PYR_SHRINKING;
214235 return true ;
215236 }
@@ -221,21 +242,23 @@ bool CPostMazeContext::Shrink(std::wstring *yomi)
221242 if (st > postyomist && st < postyomied)
222243 {
223244 std::wstring s (postyomi.substr (st));
224- if (s != L" ― " ) // 活用する語を示すマーカだけ ?
245+ if (s != STR_INFLECTION_MARK ) // 活用する語を示すマーカだけでない ?
225246 {
226247 postyomist = st;
227248 yomi->assign (s);
228249 postyomiResizing = PYR_SHRINKING;
229250 return true ;
230251 }
231252 }
232- if (resizeWithInflection)
253+ // 活用しない語として変換できなかったので、活用する語として変換を試みる。
254+ // (ユーザが入力した'―'がある場合は、
255+ // 既に活用する語として変換試行済。2重に'―'は付けない)
256+ if (resizeWithInflection && postyomi[postyomi.size () - 1 ] != CHAR_INFLECTION_MARK)
233257 {
234- // 活用しない語として変換できなかったので、
235- // 活用する語として変換を試みる
236258 postyomist = 0 ;
237- postyomi. append ( L" ― " ) ;
259+ isInflection = true ;
238260 yomi->assign (postyomi);
261+ yomi->append (STR_INFLECTION_MARK);
239262 postyomiResizing = PYR_SHRINKING;
240263 return true ;
241264 }
@@ -253,7 +276,7 @@ bool CPostMazeContext::Extend(std::wstring *yomi)
253276 yomi->clear ();
254277 if (IsYomiInflection ()) // 活用する語
255278 {
256- size_t suffixlen = CountMoji (postyomi.substr (postyomied)) - 1 ; // -1:'―'
279+ size_t suffixlen = CountMoji (postyomi.substr (postyomied));
257280 // 語尾を長くしすぎて、余分な候補が表示されるのを回避
258281 if (suffixlen < MAX_SUFFIX)
259282 {
@@ -268,34 +291,34 @@ bool CPostMazeContext::Extend(std::wstring *yomi)
268291 postyomist = st;
269292 postyomied = ed;
270293 yomi->assign (postyomi, st, ed - st);
271- yomi->append (L" ― " );
294+ yomi->append (STR_INFLECTION_MARK );
272295 postyomiResizing = PYR_EXTENDING;
273296 return true ;
274297 }
275298 }
276299 }
277- size_t alllen = CountMoji (postyomi) - 1 ; // -1:'―'
300+ size_t alllen = CountMoji (postyomi);
278301 size_t curlen = CountMoji (postyomi.substr (postyomist, postyomied - postyomist));
279302 // ずらせない場合、語幹を伸ばして、postyomiの末尾から試行
280303 // 例: 「あおい」に対し、「あ」→「おい」
281304 if (curlen < alllen)
282305 {
283- size_t ed = postyomi.size () - 1 ; // -1:'―'
306+ size_t ed = postyomi.size ();
284307 size_t st = BackwardMoji (postyomi, ed, curlen + 1 );
285308 if (st < ed)
286309 {
287310 postyomist = st;
288311 postyomied = ed;
289312 yomi->assign (postyomi, st, ed - st);
290- yomi->append (L" ― " );
313+ yomi->append (STR_INFLECTION_MARK );
291314 postyomiResizing = PYR_EXTENDING;
292315 return true ;
293316 }
294317 }
295318 if (resizeWithInflection)
296319 {
297320 // さらに伸ばす場合、活用しない語として変換を試みる
298- postyomi. erase (postyomi. size () - 1 ) ;
321+ isInflection = false ;
299322 postyomied = postyomi.size ();
300323 size_t st = BackwardMoji (postyomi, postyomied, 1 );
301324 if (st < postyomied)
0 commit comments