-
Notifications
You must be signed in to change notification settings - Fork 1
/
dict
executable file
·55 lines (46 loc) · 1.7 KB
/
dict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
require 'sqlite3'
DB = SQLite3::Database.new 'eo.db'
puts "TOTAL: %d words, %d phrases, %d facts" % [
DB.get_first_value("select count(*) from words"),
DB.get_first_value("select count(*) from phrases"),
DB.get_first_value("select count(*) from facts")
]
puts "TO EXPORT: %d words, %d phrases, %d facts" % [
DB.get_first_value("select count(*) from words where exported=0"),
DB.get_first_value("select count(*) from phrases where exported=0"),
DB.get_first_value("select count(*) from facts where exported=0")
]
exit unless ARGV[0] == 'export'
OUTFILE = '/tmp/anki.tab'
require 'erb'
def clean(txt)
ERB::Util.html_escape(txt.gsub("\t", ' ').strip).gsub("\n", '<br>')
end
def add2anki(q, a)
File.open(OUTFILE, 'a') do |f|
f.puts "%s\t%s" % [clean(q), clean(a)]
end
end
DB.execute("select en, eo from words where exported=0 order by id") do |row|
add2anki("WORD:\n#{row[0]}", row[1])
add2anki(row[1], row[0])
end
DB.execute("update words set exported=2 where exported=0")
DB.execute("select en, eo from phrases where exported=0 order by id") do |row|
add2anki("PHRASE:\n#{row[0]}", row[1])
add2anki(row[1], row[0])
end
DB.execute("update phrases set exported=2 where exported=0")
DB.execute("select q, a from facts where exported=0 order by id") do |row|
add2anki(row[0], row[1])
add2anki(row[1], row[0])
end
DB.execute("update facts set exported=2 where exported=0")
puts "Go import #{OUTFILE}."
print "TYPE ok WHEN DONE: "
raise 'go update sqlite manually' unless 'ok' == STDIN.gets.strip
DB.execute("update words set exported=1 where exported=2")
DB.execute("update phrases set exported=1 where exported=2")
DB.execute("update facts set exported=1 where exported=2")
puts "DONE."