Debian Codegraph @

lab/kcdict/kcdictejrtotsv

raw
1 #! /usr/bin/ruby
2 # -*- coding: utf-8 -*-
3
4 BASEDIR = ARGV.length > 0 ? ARGV[0] : "."
5
6 if !File::directory?(BASEDIR)
7 printf("%s is not a directory\n", BASEDIR)
8 exit(1)
9 end
10
11 pattern = sprintf("%s/*.TXT", BASEDIR)
12 Dir.glob(pattern) do |path|
13 dictname = path.sub(/.*\//, "")
14 dictname = dictname.sub(/[-_0-9]*\.TXT$/, "").downcase
15 File::open(path) do |infile|
16 outpath = sprintf("eijiro-%s.tsv", dictname)
17 File::open(outpath, "w") do |outfile|
18 seq = 0
19 infile.each do |line|
20 begin
21 line = line.encode('UTF-8', 'Windows-31J')
22 rescue => e
23 p e
24 next
25 end
26 line = line.gsub(/\s+/, " ")
27 line = line.strip
28 line = line.sub(/^■/, "")
29 face = line.sub(/ +:.*/, "")
30 text = line.sub(/[^:]* +: +/, "")
31 part = ""
32 if face.match(/ *{[^}]+}$/)
33 part = face.sub(/.*{([^}]+)}$/, '\1')
34 face = face.sub(/ *{[^}]+}$/, "")
35 end
36 key = face.downcase
37 text = text.gsub(/{[^}]+}/, "")
38 text = text.sub(/ *◆([a-z]+:|【URL】|【出典】).*$/, "")
39 seq += 1
40 printf(outfile, "%s\t%d\t%s\t%s\t%s\n", key, seq, face, part, text)
41 printf("%s: %s: %d records done\n", $0, dictname, seq) if seq % 1000 == 0
42 end
43 end
44 end
45 end