2023年7月30日日曜日

PPvでEPWING形式の辞書を引く

PPvで、カーソル位置の/範囲選択した単語をEPWING形式の辞書で検索し、PPv[X]にその内容を表示する。

必要なもの

EBWIN4には、EPWING形式の辞書をあらかじめ登録しておこう。

準備

以下のファイルをScriptフォルダに保存する。dic.luaのEBWIN4のパスは環境に応じて読み替えてください。

--EBWIN4で検索した結果をファイルに出力
--第一変数:検索語
--第二変数:出力ファイルパス
ebwin="D:\\bin\\EBWIN4\\ebwinc.exe /C=1 /E=u"
nyagos.exec(ebwin .. " " .. arg[1] .. " > " .. arg[2])
view raw dic.lua hosted with ❤ by GitHub
//!*script
// PPvのカーソル位置にある文字を返す
// 単語が空白区切りであることが前提
// 例: *linemessage %*script(%0\Script\selectword.js)
var linetext = PPx.Extract('%*selecttext');
var lh = PPx.Extract('%lH');
// var blankpos = linetext.indexOf( ' ' );
// 文字列を左右に分割し、空白前/後を削除
var lstr = linetext.substr( 0, lh ).replace(/.*\s(.*?)$/, '$1');
var rstr = linetext.substr( lh ).replace(/^(.*?)\s.*/, '$1');
// 文字列を再結合
var str = lstr+rstr;
// 句読点等を取り除く
var result = str.replace(/(\.|\,|\"|\'|\!|\?|\;|\:)/g, '');
// ’以前を取り除く。フランス語の単語抽出用
var result = result.replace(/.*’(.*)$/, '$1');
// 文字数が45を超えていれば以降のコマンド実行を中止
if (result.length > 45){
PPx.SetPopLineMessage("文字数が多すぎます");
PPx.Quit(-1);
}
PPx.Result = result;
PPx.Quit();
view raw selectword.js hosted with ❤ by GitHub

カーソル位置の単語を検索

単語が空白区切りになっている英文等の場合はこちらが便利。 [I]でキャレットモードにして、辞書を引きたい単語上にカーソルを置いてから、以下のコマンドを実行する。

*string o,tempfile=%*temp(name,f) %: %Oi nyagos -f %0Script\dic.lua %*script(%0Script\selectword.js) %so"tempfile" %: *ppv -utf8 -bootid:x -r %so"tempfile" -k *selectppx %n

一時ファイルが一々つくられるのが嫌な場合は、以下を適当なキーに登録して実行する。

*ifmatch "!o:e,a:d-",%si"tempfile" %: *string i,tempfile=%*temp(name,f)
%Oi nyagos -f %0Script\dic.lua %*script(%0Script\selectword.js) %si"tempfile"
*ppv -utf8 -bootid:x -r %si"tempfile" -k *selectppx %n

単語を範囲選択して検索

日本語で辞書を引くとき等は、単語の自動取得が難しいので、おそらくこちらの方法を取ることになる。

*string o,tempfile=%*temp(name,f) %: %Oi nyagos -f %0Script\dic.lua %*selecttext %so"tempfile" %: *ppv -utf8 -bootid:x -r %so"tempfile" -k *selectppx %n

参考

0 件のコメント:

コメントを投稿