Last active
March 12, 2016 15:44
-
-
Save scarfacedeb/3286d54f4ce0b71496da to your computer and use it in GitHub Desktop.
Import from kindle vocabulary to anki
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/env python | |
""" | |
Modified from | |
http://macscripter.net/viewtopic.php?id=26675 | |
http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal | |
HowTo | |
chmod 755 define.py # make file executable | |
alias define="'/Users/me/Script/define.py'" # => add to .emacs_profile | |
# then (from command line) | |
define recursive # or any other word | |
# .. definition follows .. | |
""" | |
import sys | |
import argparse | |
try: | |
from DictionaryServices import * | |
except: | |
print "WARNING: The pyobjc Python library was not found. You can install it by typing: 'pip install -U pyobjc'" | |
print "..................\n" | |
try: | |
from colorama import Fore, Back, Style | |
except: | |
print "WARNING: The colorama Python library was not found. You can install it by typing: 'pip install colorama'" | |
print "..................\n" | |
def main(): | |
""" | |
define.py | |
Access the default OSX dictionary | |
2015-11-27 | |
-added colors via colorama | |
""" | |
try: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('searchword', nargs='+', help='word to be defined') | |
parser.add_argument('--no-color', dest='nocolor', action='store_true', default=False) | |
parser.add_argument('--data-only', dest='data_only', action='store_true', default=False) | |
parser.add_argument('--html', dest='html', action='store_true', default=False) | |
args = parser.parse_args() | |
searchword = " ".join(args.searchword).decode('utf-8') | |
# searchword = sys.argv[1].decode('utf-8') | |
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: | |
if not args.data_only: | |
errmsg = "'%s' not found in Dictionary." % (searchword) | |
print errmsg.encode('utf-8') | |
else: | |
s = dictresult.encode("utf-8") | |
arrow = doColor("\n\n\xe2\x96\xb6 ", "red", args.nocolor) | |
s = s.replace('\xe2\x96\xb6', arrow) # arrow | |
bullet = doColor("\n\xe2\x80\xa2 ", "red", args.nocolor) | |
s = s.replace('\xe2\x80\xa2', bullet) # bullet | |
header = wrapHeader("PHRASES", args.html) | |
header = doColor(header, "important", args.nocolor) | |
s = s.replace('PHRASES', header) | |
header = wrapHeader("PHRASAL VERBS", args.html) | |
header = doColor(header, "important", args.nocolor) | |
s = s.replace('PHRASAL VERBS', header) | |
header = wrapHeader("DERIVATIVES", args.html) | |
header = doColor(header, "important", args.nocolor) | |
s = s.replace('DERIVATIVES', header) | |
header = wrapHeader("ORIGIN", args.html) | |
header = doColor(header, "important", args.nocolor) | |
s = s.replace('ORIGIN', header) | |
if not args.data_only: | |
print doColor("Dictionary entry for:\n", "red", args.nocolor) | |
print s | |
if not args.data_only: | |
print "\n---------------------------------" | |
def wrapHeader(s, html=False): | |
if html: | |
return '<h3>' + s + '</h3>' | |
else: | |
return '\n\n' + s + '\n' | |
def doColor(s, style=None, no_color=False): | |
""" | |
util for returning a colored string | |
if colorama is not installed, FAIL SILENTLY | |
""" | |
if no_color: | |
return s | |
try: | |
if style == "comment": | |
s = Style.DIM + s + Style.RESET_ALL | |
elif style == "important": | |
s = Style.BRIGHT + s + Style.RESET_ALL | |
elif style == "normal": | |
s = Style.RESET_ALL + s + Style.RESET_ALL | |
elif style == "red": | |
s = Fore.RED + s + Style.RESET_ALL | |
elif style == "green": | |
s = Fore.GREEN + s + Style.RESET_ALL | |
except: | |
pass | |
return s | |
if __name__ == '__main__': | |
main() |
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
require "csv" | |
dump = `sqlite3 -csv /Volumes/Kindle/system/vocabulary/vocab.db "SELECT w.word, w.stem, l.usage, bi.title FROM words w JOIN lookups l ON w.id = l.word_key JOIN book_info bi ON l.book_key = bi.id;"` | |
CSV.open("dump.csv", "w", col_sep: ";", force_quotes: true) do |csv| | |
orig = CSV.parse(dump) | |
total = orig.count | |
orig.each_with_index do |row, i| | |
word, stem, usage, book = row | |
definition = `./define.py --no-color --html --data-only #{row.first}` | |
usage.gsub! word, "<b>#{word}</b>" | |
definition.gsub! "\n", "<br>" | |
csv << [ stem, usage, definition, book ] | |
puts "Added #{stem} (#{i + 1} out of #{total})" | |
end | |
puts "Finished: #{total} cards" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment