ぼんやりDTP

DTPに関係したりしなかったりするぼんやりとした話をなんとなく。

コマンドラインで辞書を引くいくつかの方法

コマンドラインで辞書を引くいくつかの方法

主にMacで。

以前に書いた、「コマンドラインで辞書を引きたい - ぼんやりDTP」で導入した pyvoc は使えなくなったようなので他の方法を探す。

Translate Shell

Google Translate (default), Bing Translator, Yandex.Translate, and Apertium」をエンジンに使う翻訳、辞書ツール。

今回ググって見つけた。割といいけど、表示が冗長かも。-brief オプションもあるけど、逆に簡潔すぎるかも。でも割といいかも。

GitHub - soimort/translate-shell: Command-line translator using Google Translate, Bing Translator, Yandex.Translate, etc.

インストール

$ brew install translate-shell

辞書として引く

$ trans -d word

翻訳

$ trans word

参考ページ

  1. Translate Shell - A Tool To Use Google Translate From Command Line In Linux | 2daygeek.com
  2. Translate-shellを無限に動かして遊ぶ - Qiita

dictd

割といい。

Dictd - Using a Linux Command Line Dictionary - Putorius

インストール

↓間違えたのかなんなのか出来なかった(2021-02-16)

$ brew install dictd

↓これでインストールできた(2021-02-16)

$ brew install dict

使い方

$ dict word

使える辞書の確認

$ dict -D

辞書の指定

$ dict -d gcide word

参考ページ:

  1. Dictd - Using a Linux Command Line Dictionary - Putorius
  2. dict — Homebrew Formulae(2021-02-16)

sdct

StarDict のオフライン版らしい。

インストール

$ brew install sdct

辞書ファイルのダウンロード

  1. dictd-www.dict.org Dictionaries
  2. Collaborative International Dictionary of English - Wikipedia
    1. GCIDE_XML
  3. dictd-www.dict.org Dictionaries

辞書ファイルの設置

$ sudo mkdir -p /usr/share/stardict/dic/

ディレクトリを作るそうだがHigh-sierraでは作れない模様。

代わりに /usr/local/stardict/dic/ を作って環境変数STARDICT_DATA_DIR=/usr/local/stardict/ に設定

$ sudo mkdir -p /usr/local/stardict/dic/
$ export STARDICT_DATA_DIR=/usr/local/stardict/

すればいいと思ったがエラーが出る。

g_mkdir failed: No such file or directory
Nothing similar to word, sorry :(

色々適切に設定すれば使えそう。

使い方

$ sdcv word

参考ページ

software recommendation - Is there an offline command-line dictionary? - Ask Ubuntu

myougiden

EDICT (JMdict)をベースにした Japanese/English、English/Japanese 辞書

割と良さげ

GitHub - melissaboiko/myougiden: A Japanese/English dictionary for the command line, colorful and full of features.

インストール

$ pip3 install myougiden

辞書のアップデート

$ updatedb-myougiden -f

引き方

$ myougiden word

English Dictionary CLI

GitHub - joegesualdo/dictionary-cli: English dictionary from the command line

なんかうまくインストールできなかった。

npm のインストール

$ brew install npm

インストール

$ npm install -g english-dictionary-cli

引き方

$ definition <word> [options]

GENE95 辞書」を grep で引く

1998年に再配布の許諾を得て公開されているKurumiさんによる「GENE95 辞書」のテキストファイルを利用する方法。

  1. GENE95 DictionaryからGENE95 辞書をダウンロードして解凍。
  2. 解凍してできたgene.txtのテキストファイルをエディターなどで文字コードutf-8、改行文字をLFに変更して保存する。
  3. 変更したgene.txtgrepコマンドで任意の単語を検索して表示する。
  4. 任意の関数名で.bashrcなどに関数を定義して使用する。

gene.txtの構成としては英語表現の次の行にその英語表現の複数の和訳、説明が記述されているようなので、英和辞書として使用するのは比較的利用しやすいが、和英辞書として利用する場合はそれなりの工夫が必要と思われる。

関数の定義例

function 任意の関数名() {
    grep $1 gene.txtのパス -オプション
}

grep のコマンド例

「word」と完全一致の行と後の1行を検索する

$ grep -E ^"word"$ gene_utf.txt -A 1 --color
#word
#言葉,ワード,語,ひと言,約束,口論,合言葉,神の言葉,を言葉で表す,便り,話,表わす

「言葉」で始まる行と前の1行を表示する

$ grep -E ^"言葉" gene_utf.txt -B 1 --color
#beyond description
#言葉で言い表せられないほど
#--
〜中略〜
#--
#language
#言葉,言語,ことば,国語
#--
〜中略〜
#--
#weigh one's words
#言葉を選ぶ
#--
#word
#言葉,ワード,語,ひと言,約束,口論,合言葉,神の言葉,を言葉で表す,便り,話,表わす

参考ページ

  1. Linux のコマンドラインでオフライン英和辞書 - Qiita
    1. 英和辞書として使用する方法が解説されている。
  2. シェルから使うオフラインの英和・和英辞書 - Qiita
    1. 英和辞書、和英辞書として使用する方法が解説されている。

macOS付属の辞書を引く

ターミナルからmacOS付属の辞書を起動して引く。

ターミナル内の表示ではないが一応。

引き方例

$ open dict://word
$ open dict://"word search"

macOS付属の辞書をスクリプトなどを経由して引く

macOS付属の辞書をCLIで利用するためのスクリプトがいくつか存在し、また、自分で作成することもできる模様。

applescript - look up a word in Dictionary.app in Terminal - Ask Differentにいくつかサンプルコード等があるので利用できる。

Python、Swift 4等でOSのプライベートAPIを利用するようだ。

上記ページ記載のpythonスクリプトを使用した出力例

$ python dict.py word
#ワード 1word① 単語。語。ことば。多く,他の外来語と複合して用いられる。「キー-―」「クロス―」② 英文タイプでの単位。五文字を一ワードとする。③ コンピューターの内部で一度に処理される情報量の単位。1ワードを構成するビット数でその大きさを表す。1ワードは8・16・32・64ビットなどコンピューターによって異なる。語。〈子項目〉ワードプロセッサー

その他参考ページ

  1. 標準入力で読み込んだ単語の意味をMac内蔵辞書で調べ、単語帳を作る。 - Qiita
  2. Python Tips: Mac の辞書アプリを Python から利用したい - Life with Python

OSX Dictionary CLI

macOS付属の辞書をCLIで引けるアプリケーションだが、表示は見ずらいかも。

  1. GitHub - odlp/dictionary-osx: Access the Mac OSX Dictionary app via the command line
    1. Releases · odlp/dictionary-osx · GitHub
    2. GitHub - mattt/DictionaryKit: An Objective-C Wrapper for Private Dictionary Services on Mac OS X

インストール

  1. Releases · odlp/dictionary-osx · GitHubからdictionaryをダウンロード。
  2. dictionaryに実行権を付与
    1. chmod u+x dictionary
  3. /usr/local/bin/dictionary等パスの通ったディレクトリに置く。

引き方

$ dictionary word

表示例

word | wəːd | noun 1 a single distinct meaningful element of speech or writing, used with others (or sometimes alone) to form a sentence and typically shown with a space on either side when written or printed: I don't like the word ‘unofficial’ | why so many words for so few ideas? • a single distinct conceptual unit of language, comprising inflected and variant forms. • (usually words) something spoken or written; a remark or statement: his grandfather's words had been meant kindly | a word of warning. • [with negative] (a word) even the smallest amount of something spoken or written: don't believe a word of it. • (words) angry talk: her father would have had words with her about that. • [mass noun] speech as distinct from action: he conforms in word and deed to the values of a society that he rejects. 2 a command, password, or signal: someone gave me the word to start playing. • [mass noun] communication; news: I was afraid to leave Edinburgh in case there was word from the War Office. 
〜省略〜

オプション

-f text|html|speech-part
  1. text:デフォルト。上記のようにテキストのみ。
  2. htmlAPIから帰ってくるHTMLそのまま。
  3. speech-part:最初の定義の品詞を返すっぽいのだが試した限りでは何も出なかった。

wkdict

WiktionaryCLIで引く

インストール

$ pip3 install wkdict

インストール時にエラーが出た。

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/readme'
Consider using the `--user` option or check the permissions.

--userオプションをつけてインストール

pip3 install --user wkdict

インストール先はパスが通っていないとの警告。

  WARNING: The script wkdict is installed in '/Users/hoge/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

パスを通す。

$ echo 'export PATH=$PATH:/Users/hoge/Library/Python/3.8/bin' >> ~/.bash_profile

引き方

$ wkdict word

表示例。ちょっと派手かな。なんか「This application is powered by Wiktionary's API」が点滅する。

🔅 This application is powered by Wiktionary's API 🔅

1) word (Received Pronunciation) IPA: /wɜːd/
   🏷  noun

    📗 Definitions
    ☞ word (countable and uncountable, plural words)
    ☞ The smallest unit of language that has a particular meaning
      and can be expressed by itself; the smallest discrete,
      meaningful unit of language. (contrast morpheme.)
    ☞ Something like such a unit of language:

    📘 Examples
    ➡ But every word, whether written or spoken, which urges
      the woman to antagonism against the man, every word which is
      written or spoken to try and make of her a hybrid, self-
      contained opponent of men, makes a rift in the lute to which
      the world looks for its sweetest music.
    ➡ The word, whether written or spoken, does not look like
      or sound like its meaning — it does not resemble its
      signified. We only connect the two because we have learnt the
      code — language. Without such knowledge, 'Maggie' would just
      be a meaningless pattern of shapes or sounds.
    ➡ Brian and Abby signed the word clothing, in which the
      thumbs brush down the chest as though something is hanging
      there. They both spoke the word clothing. Brian then signed
      the word for change,  […]
   🏷  verb

〜略〜

参考ページ

  1. wkdict: A CLI dictionary app in your terminal app - Wenhao’s Page

wikit

WikipediaCLIで引く。

デフォルトでは概要だけ表示する。

-a オプションをつけるとすべてのセクションを表示するので、その場合はパイプで less 等につなげるのが推奨とのこと。

インストール

$ sudo npm install wikit -g

引き方

$ wikit word
# In linguistics, a word of a spoken language can be defined as the smallest sequence
# of phonemes that can be uttered in isolation with objective or practical meaning.
# 〜省略〜

-l または --lang オプションで言語ごとのWikipediaを引ける。

言語のコードは「HTML ISO Language Code Reference」等参照。

日本語ならja、フランス語ならfr、中国語ならzh など。

$ wikit -l ja word
# Microsoft Word(マイクロソフト・ワード)は、マイクロソフトがWindows、Android、macOS及びiOS向けに販売している文書作成ソフトウェア.
# Microsoft Excelとともに、同社のオフィススイート、Microsoft Officeの中核をなすアプリケーションである. 一般的にはワード(Wor
# dまたはMS-Wordとも)と呼ばれることが多いが、「ワード」と名称が付く商品名や商標名は他にもある.

wikit だけ実行すると、オプションなど、使い方が参照できる。

参考ページ:

  1. How To Search And Use Wikipedia In Linux Terminal?

その他辞書APIを利用してスクリプトを自作する

Oxford Dictionaries APIなどの、Webで辞書を利用できるAPIを利用してPython等のスクリプトで自作する。

JSON形式で返してくれるようなAPIが色々あるので自作しようとすればできるが、それなりのものを作るとなると、それなりの労力がかかる。

Oxford Dictionaries API しか試してないが、オブジェクトモデルの構成や各オブジェクトの意味を理解するのも骨だし、各オブジェクト、キーが全部入りのサンプルファイルがあるわけではないのでまあ、結構大変。

また、無料で利用できる回数は上限があったり、APIの仕様がアップデートされるなどして、そのままでは使用できなくなったり、また、急にサービスが終了する場合もあるだろう。

参考ページ

  1. Oxford Dictionaries API
  2. Merriam-Webster Dictionary API
  3. 13 Dictionary APIs such as Oxford, WordsAPI APIs (like: Oxford Dictionaries) | Rakuten RapidAPI

辞書の出力をパイプでlessにつなげる

上記のような辞書の出力が度々、一面をはみ出るのでパイプでlessにつなげたくなるが、そのままだと色や体裁が崩れる。

expect というソフトウェアパッケージの unbuffer というツールを使うといいらしい。

expect のインストール

$ brew install expect

使い方の例

$ unbuffer trans -d word | less -R

参考ページ

  1. less にパイプしても色が消えないようにする方法 - Qiita

その他参考ページ

  1. software recommendation - Is there an offline command-line dictionary? - Ask Ubuntu
  2. How to Customize the macOS Dictionary App - Apple Gazette
  3. GitHub - takumakei/osx-dictionary: CLI for OSX Dictionary.app
  4. How To Look Up Dictionary Definitions Via Terminal | Unixmen
  5. Take advantage of the built-in word list - Mac OS X Hints
  6. Mac で EPWING など辞書あれこれ… – 澍法雨(2021-02-16)