12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

辞書サーバーによる辞書環境の構築

Last updated at Posted at 2018-01-07

わたしが電子辞書を卒業してPCアプリによる辞書ファイル検索を始めたのは、Emacsのsdicが最初でした。その後、StarDictに移行しましたが、筐体、OSを変えるたびの移行が面倒くさく、アプリの開発も行われなくなったようで、やがて使わなくなり、最近はウェブ上のオンライン辞書サイト(Weblioや英辞郎)を専ら使っていました。それでも、昔購入した英辞郎のテキスト変換ファイルは持ち歩いていて、パッと調べたいときはgrepなどで使用してました。最近また「いちいちブラウザに切り替えず、Emacs内で辞書引きを完結させたいな」と思うようになり、以前使っていたsdicに戻るのも芸がないので、dictdによる辞書環境を作ってみました。なお辞書データベースには英辞郎(わたしが持っているのは英辞郎付属のテキスト変換ツールでテキストに変換済みのファイルです)を使用しました。

自宅で辞書サーバー

自宅の筐体に触れる機会はめっきり減ったのですが、それでも長期休暇などではちょいちょい弄ったりもします。メインのAndroid環境に導入する前に、こちらで辞書環境を作ってみましょう。自宅の環境は32ビットのLinux Mint18(Xfce-32bit)です。サーバーといっても大げさなものではなく、スタンドアロンでの使用しか予定してません。

辞書サーバー構築

dictdのインストール

apt-getで辞書サーバーdictdとコマンドライン辞書クライアントのdict、さらに辞書変換ツールdictfmtをインストールします。

$ sudo apt-get install dictd dict dictfmt
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  recode
提案パッケージ:
  dict-foldoc dict-gcide | dictd-dictionary dict-jargon dict-wn
以下のパッケージが新たにインストールされます:
  dict dictd dictfmt recode
アップグレード: 0 個、新規インストール: 4 個、削除: 0 個、保留: 707 個。
297 kB 中 32.1 kB のアーカイブを取得する必要があります。
この操作後に追加で 871 kB のディスク容量が消費されます。
続行しますか? [Y/n] y

.....

辞書ファイルの変換

ここの情報を参考にしました。

わかったことを要約すると、

  1. dictfmtというコマンドでdictd用の辞書ファイルに変換できるらしい。
  2. dictfmtでは、英辞郎フォーマットからの変換はサポートされてないが、英辞郎フォーマットからサポートされている簡単なフォーマットへの変換はワンライナーで行けそう。

です。

英辞郎テキスト辞書の準備

英辞郎のCD-ROM(最近のバージョンはオンライン購入してダウンロードできるらしいです)を用意しWindows機(またはWineなど)などで辞書をテキストに変換します(手順)。

次に英辞郎のテキスト辞書eijiro52.txtをUTF-8に変換します

$ nkf -w eijiro52.txt > eijiro52.txt.utf8

英辞郎辞書からjargon辞書への変換

ざっと見た感じでは、英辞郎テキスト辞書は見出し語と意味が空白+タブ+空白で区切られているようです。一方jargon辞書は見出し語がコロンで区切られていて、見出し語の前にもコロンが必要のようです。とりあえずこれを正式なフォーマットと仮定して変換しました。

$ cat eijiro52.txt.utf8 | perl -p -e 's/:/:/g;s/^/:/;s/ \t /:/' > eijiro52.txt.utf8.jargon

英辞郎txt.utf8.jargon形式からdict形式への変換

dictfmtで変換して、辞書用のディレクトリーにコピーします。

 $ dictfmt --utf8 -s "eiwa" -j eijiro < eijiro52.txt.utf8.jargon
     1016642 headwords
 $ ls -l eijiro.*
-rw-r--r-- 1 ayatakesi ayatakesi 62649476  1月  7 14:31 eijiro.dict
-rw-r--r-- 1 ayatakesi ayatakesi 27278900  1月  7 14:31 eijiro.index

$ sudo cp eijiro.* /usr/local/share/dict/

設定ファイルの修正

辞書サーバーの設定ファイルに英辞郎辞書を登録します。

dictd.conf
# /etc/dictd/dictd.conf

# This is the configuration file for /usr/sbin/dictd.  The access
# specification included in this file allows access only from the
# localhost.  If this machine is acting as a server for a network you
# will probably want to add additional access specifications in this
# file.  See the dictd manpage - man dictd.

# A sample database section is generated automatically upon
# installation or removal of the dictd package or any dictionary
# database package.  Replace the ``include /var/lib/dictd/db.list''
# line below if you wish to provide a custom database section.
# Customization may also be achieved via the optional dictdconfig
# order override file /etc/dictd/dictd.order.  See the dictdconfig
# manpage - man dictdconfig.

# Older dictionary database packages did not automatically
# invoke /usr/sbin/dictdconfig upon installation and removal,
# so you may need to do so manually.

# Site section here:

global {
listen_to 127.0.0.1
# bind to local interfacea only
}

# Access section here:

access {
allow localhost
allow 127.0.0.1
# this allows access only from local host
allow inetd
# this allows access from inetd server
}

# Database section here:

include /var/lib/dictd/db.list

# User section here
database eijiro {
data  "/usr/local/share/dict/eijiro.dict"
index "/usr/local/share/dict/eijiro.index" }

検証用のコマンドライン辞書クライアントの設定ファイルから検証しやすいように外部ホストをコメントアウトします。

dict.conf
#  /etc/dictd/dict.conf 

#  Written by Bob Hilliard <hilliard@debian.org>
#  1998/03/20.  Last revised Sun, 22 Nov 1998 18:10:04 -0500 This is
#  the configuration file for /usr/bin/dict.  In most cases only the
#  server keyword need be specified.  

#  This default configuration will try to access a dictd server on the
#  local host, failing that, it will try the public server.  In many
#  cases this will be slow, so you should comment out the line for the
#  server that you don't want to use. To use any other server, enter
#  its IP address in place of "dict.org".  

#  Refer to the dict manpage (man dict) for other options that could
#  be inserted in here.

server localhost 
# server dict.org
# server dict0.us.dict.org
# server alt0.dict.org

ここまで終わったらdictdを起動(または再起動)して、動作確認してみます。

$ systemctl restart dictd
$ dict -I
  dictd 1.12.1/rf on Linux 4.4.0-21-generic
  On crow: up 9.000, 1 fork (400.0/hour)

  Database      Headwords         Index          Data  Uncompressed
  dummy           0          0 kB          0 kB          0 kB
  eijiro    1016642         26 MB         59 MB         59 MB

辞書クライアントで使用してみる

dict

$ dict hello
2 definitions found

From eiwa [eijiro]:

  hello
  【@】ヘロー、ヘロウ、【変化】《複》hellos、 / 【間投】こんにちは、あのう、やあ

From eiwa [eijiro]:

  Hello
  【人名】エロ

Mintデスクトップ辞書

gnomeを使ってるときも、類似のアプリがあったのですが、フリーな日本語辞書サーバーを見つけられず自分にとっては無用の長物でした。今回は自分で辞書サーバーを準備して使用します(もちろん英辞郎辞書はフリーではありませんが、localhostからの使用だけですからね)。

辞書サーバーにlocalhostを設定すると、辞書が見えるようになります。

設定画面

アプリ画面

モバイル環境

実は自宅のPCはほぼ置物化しており、本当の目的はスマホ(Android)上での辞書サーバー+辞書クライアントの運用です。スマホではGNURoot Debianとtermuxを使い分けてます。Emacsなど、普段使っているアプリはAndroidの操作性と親和性が高いtermux、termuxで提供されていないパッケージを使うときはGNURootを使用してます。

PCで作成した辞書をスマホに持っていけばインストールが必要なのはdictdだけですが、これはtermuxにパッケージがないのでGNURootで辞書サーバーを動かし、それをtermuxやスマホアプリから引くことにします。

なお特に手順は貼りませんが、PCと同じ手順でGNURootでのdictd、dict、dictfmtのインストール、dictfmtによる辞書変換はできました。ちょっとハマったのが辞書ファイルのパーミッションで、コピー後にchmod a+rとかしてやらないと辞書サーバーが起動してくれません。あと、systemctlが使用できなかったので、直接/etc/init.d/dictd restartとか叩いてます。

(2018/01/22加筆)
上記のような手順でスマホ·タブレット上でTermuxとGNURootを併用していたんですが、GNURootの常時起動はかなりバッテリーを消費するため、しょうがないのでTermux上でdictdをビルド·インストールしました。興味のあるかたはこちらを参照してください。

辞書クライアントで使用してみる

Emacs

Melpaからdictionary.elをインストールしました。init.elの設定はここを参照しました。なおdictionary-serverの値は"localhost"にカスタマイズしてます。
Screenshot_20180107-153328.png

ここでは完全一致しか拾ってないようですがMatching Strategyの選択次第で熟語?なども引けるようになります。

スマホアプリ

Google Playで検索したらDICTクライアントが見つかったので試してみました。
Screenshot_20180107-153606.png

結び

最初にfreedict.orgで辞書フォーマットを調べたときは面倒くさそうで「やっぱsdicにするか」とか思いましたが、dictfmtを知ってからはスイスイ行けました。これでいちいちブラウザに切り替えずに辞書が引けます。おわり

12
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?