def system_lookup(word, exe='./dict'): # works for '朗道英汉/汉英词典5.0' only if os.path.isfile(exe) and os.access(exe, os.X_OK): dict_file = 'langdao-ec-gb.dictionary' if is_english(word) else 'langdao-ce-gb.dictionary' dict_path = os.path.expanduser('~/Library/Dictionaries/{}'.format(dict_file)) proc = subprocess.Popen([exe, dict_path, word], stdout=subprocess.PIPE) definition = proc.stdout.read() if definition == '(null)\n': return None else: from DictionaryServices import DCSCopyTextDefinition unicode_word = word.decode('utf-8') word_range = (0, len(unicode_word)) definition = DCSCopyTextDefinition(None, unicode_word, word_range) if definition is None: return None definition = definition.encode('utf-8') definition = definition.split('\n相关词组:\n')[0] result = definition.split('\n') if is_english(word): if result[1].startswith('*['): phonetic = result[1][2:-1] result[0:2] = ['{} {}'.format(word, '/{}/'.format(phonetic) if phonetic else '')] else: result[1:2] = result[1].split('; ') return result
def main(): """ define.py Access the default OSX dictionary """ try: searchword = sys.argv[1] except IndexError: errmsg = 'You did not enter any terms to look up in the Dictionary.' print(errmsg) sys.exit() wordrange = (0, len(searchword)) dictresult = DCSCopyTextDefinition(None, searchword, wordrange) if not dictresult: errmsg = "'%result' not found in Dictionary." % (searchword) print(errmsg.encode('utf-8')) else: result = dictresult.encode("utf-8") arrow = colorize("\n\n\xe2\x96\xb6 ", "red") result = result.replace(b'\xe2\x96\xb6', arrow) bullet = colorize(u'\n\u2022', "bold") result = result.replace(b'\xe2\x80\xa2', bullet) phrases_header = colorize("\n\nPHRASES\n", "bold") result = result.replace(b'PHRASES', phrases_header) derivatives_header = colorize("\n\nDERIVATIVES\n", "bold") result = result.replace(b'DERIVATIVES', derivatives_header) origin_header = colorize("\n\nORIGIN\n", "bold") result = result.replace(b'ORIGIN', origin_header) result = result.decode("utf-8") for part in ["noun", "verb", "adverb", "adjective"]: result = result.replace(f"{part} 1", f"\n{Fore.GREEN + part + Style.RESET_ALL}\n1") for num in range(2, 10): result = result.replace(f" {num} ", f"\n{num} ") print("\n", result)
def main(): if len(sys.argv) == 1: exit(1) word = sys.argv[1].decode('utf-8') result = DCSCopyTextDefinition(None, word, (0, len(word))) print(result.encode('utf-8'))