Created
May 29, 2016 01:03
-
-
Save gh640/e6ccde3abfc936b85eaadb933088e7ad to your computer and use it in GitHub Desktop.
Python: Mac の辞書アプリをターミナルから利用するためのスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
# coding: utf-8 | |
"""Mac に備え付けの辞書アプリをコマンドラインで利用する | |
Usage: | |
/usr/bin/python2.7 __file__ < [検索したい単語を格納したファイル.txt] | |
See: | |
- Accessing Dictionaries | |
https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/DictionaryServicesProgGuide/access/access.html#//apple_ref/doc/uid/TP40006152-CH5-SW1 | |
""" | |
import sys | |
# Mac の辞書サービス | |
from DictionaryServices import DCSGetTermRangeInString, DCSCopyTextDefinition | |
# 定数 | |
# 標準入出力に使用するもの | |
ENCODING = 'utf-8' | |
# 辞書サービスに使用するもの | |
DICTIONARY = None | |
OFFSET = 0 | |
# 設定値 | |
config = { | |
# 入力された単語そのものを出力するかどうか | |
'print_word': True, | |
} | |
def get_stdin_with_encoding(encoding=None): | |
"""ジェネレータ: 標準入力を 1 行ずつ取得する取得する。 | |
標準入力を 1 行ずつ取得し前処理をした後に返す。 | |
- 先頭と末尾の空白、改行コードは削除する。 | |
- 指定された文字コードでのデコード | |
Args: | |
encoding: 標準入力をデコードする際の文字コード。 | |
Yields: | |
前処理(空白の削除、デコード)済の標準入力。 | |
""" | |
while True: | |
# 標準入力から 1 行取得する。 | |
# 前処理として空白の削除とデコードを行う。 | |
line_raw = sys.stdin.readline().strip() | |
line = line_raw.decode(encoding) if encoding else line_raw | |
if not line: | |
break | |
yield line | |
def get_dict_definition(word): | |
"""指定した単語に対する定義を取得する。 | |
Args: | |
word: 辞書で定義を取得したい単語。 | |
Returns: | |
辞書で検索した単語の定義。該当するアイテムが存在しない場合は空文字列を返す。 | |
""" | |
word_range = DCSGetTermRangeInString(DICTIONARY, word, OFFSET) | |
try: | |
word_definition = DCSCopyTextDefinition(DICTIONARY, word, word_range) | |
# 該当する単語が見つからなかった場合は空文字列を返す | |
except IndexError as e: | |
word_definition = '' | |
return word_definition | |
if __name__ == "__main__": | |
# 日本語の単語の場合はデコードしないと辞書で正しく引けないので utf-8 でデコードする | |
for word in get_stdin_with_encoding(ENCODING): | |
if config['print_word']: | |
print(word.encode(ENCODING)) | |
print(get_dict_definition(word).encode(ENCODING)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@planeticus, thank you, too!