Created
January 20, 2016 06:43
-
-
Save Ma233/27cc4c6aa99be4a9e580 to your computer and use it in GitHub Desktop.
pip install pyobjc-framework-DictionaryServices && ln -s ./dict.py /usr/bin/dict
This file contains hidden or 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/python | |
import re | |
import sys | |
from DictionaryServices import DCSCopyTextDefinition | |
split_re = re.compile(r"\s+(?=[A-Z]\.\s)") | |
_split_re = re.compile(r"(?=" + u"[\u2460-\u2473]" + r")") | |
def search(word): | |
wordrange = (0, len(word)) | |
search_result = DCSCopyTextDefinition(None, word, wordrange) | |
if not search_result: | |
errmsg = "'%s' not found in system dict." % (word) | |
print errmsg.encode('utf-8') | |
sys.exit(2) | |
else: | |
return search_result.encode('utf-8') | |
def format(search_result): | |
_fresult = split_re.sub('\n\n', search_result) | |
fresult = _split_re.sub('\n ', _fresult.decode('utf-8')) | |
return fresult | |
def main(): | |
if not len(sys.argv) == 2: | |
print 'Usage: dict <word>' | |
sys.exit(1) | |
word = sys.argv[1].decode('utf-8') | |
search_result = search(word) | |
print '---------------' | |
print format(search_result) | |
print '---------------' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment