Skip to content

Instantly share code, notes, and snippets.

@tmatz
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmatz/e9441653c1b13c16a551 to your computer and use it in GitHub Desktop.
Save tmatz/e9441653c1b13c16a551 to your computer and use it in GitHub Desktop.
How To Use WnnEngine
package my.package.name;
import jp.co.omronsoft.openwnn.JAJP.OpenWnnEngineJAJP;
import jp.co.omronsoft.openwnn.JAJP.Romkan;
import jp.co.omronsoft.openwnn.ComposingText;
import jp.co.omronsoft.openwnn.StrSegment;
import jp.co.omronsoft.openwnn.WnnWord;
import jp.co.omronsoft.openwnn.LetterConverter;
public class MainActivity extends Activity
{
private OpenWnnEngineJAJP mConverter;
private LetterConverter mPreConverter;
private ComposingText mConposingText;
@Override
public void onCreate(Bunde savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// かな漢字変換の初期化
mConverter = new OpenWnnEngineJAJP(context, "/data/data/my.package.name/writableJAJP.dic");
mConverter.init();
// ローマ字・かな変換
mPreConverter = new Romkan();
// 変換結果が格納されます
mComposingText = new ComposingText();
insert("k");
insert("a");
insert("n");
insert("j");
insert("i");
insert("n");
insert("i");
insert("h");
insert("e");
insert("n");
insert("k");
insert("a");
insert("n");
insert("n"); // 'ん'をかなに変換させておかないと、nのまま独立した文節になってしまう?
// かなを漢字に変換します。変換結果はLAYER2に格納されます。
int res = mConverter.convert(mComposingText);
// 連文節変換の文節ごとに変換候補を取得します。
for (int i = 0; i < mComposingText.size(ComposingText.LAYER2); ++i)
{
// 文節の変換結果です。
String clause = mComposingText.toString(ComposingText.LAYER2, i, i);
// 文節のその他の候補を取得できるようにします。
if (0 < mConverter.makeCandidateListOf(i))
{
// 文節の変換候補を取得します。
WnnWord word;
while ((word = mConverter.getNextCandidate()) != null)
{
}
}
}
mConverter.close();
}
private bool insert(String str)
{
// 入力文字はLAYER0とLAYER1に追加します。
mComposingText.insertStrSegment(ComposingText.LAYER0, ComposingText.LAYER1, new StrSegment(str));
// 一文字入力するたびにかなに変換します。
// 入力された文字がかなに変換できた時は true を返ります。
return mPreConverter.convert(mComposingText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment