forked from inukshuk/anystyle-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
86 lines (72 loc) · 2.26 KB
/
Copy pathRakefile
File metadata and controls
86 lines (72 loc) · 2.26 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'bundler'
begin
Bundler.setup
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle` to install missing gems!"
exit e.status_code
end
require 'rake'
require 'rake/clean'
DATA = 'lib/anystyle/data'
CLEAN.include('*.gem')
CLEAN.include("#{DATA}/dict.txt")
CLEAN.include("#{DATA}/dict.txt.gz")
task :compile => ["#{DATA}/dict.txt.gz"]
rule '.txt.gz' => '.txt' do |t|
require 'zlib'
Zlib::GzipWriter.open(t.name) do |zip|
zip.mtime = File.mtime(t.source)
zip.orig_name = t.source
zip.write IO.binread(t.source)
end
puts "#{File.stat(t.name).size / 1024}k compressed"
end
file "#{DATA}/dict.txt" => FileList['res/*.txt'] do |t|
dict = {}
puts "Compiling dictionary from #{t.prerequisites.size} sources ..."
t.prerequisites.each do |file|
cat = ''
puts " <- #{File.basename(file)}"
File.foreach(file, encoding: 'UTF-8') do |line|
case line
when /^#! (\w+)/
cat = $1
dict[cat] ||= []
when /^#/
# discard comments...
else
# Only first word per line is token
token = line.split(/\s+(\d+\.\d+)\s*$/)[0]
# Strip punctuation and whitespace at start and end
token.gsub!(/^[\p{P}\p{S}\s]+|[\p{P}\p{S}\s]+$/, '')
# Replace characters with diacritic marks
# with their base equivalent
token.unicode_normalize!(:nfkd)
token.gsub!(/\p{M}/, '')
# Get rid of special characters like apostrophes
token.gsub!(/\p{Lm}/, '')
token.downcase!
# Split words on punctuation and keep all pieces
# that are at least 3 characters long as well as
# the original word with all punctuation removed
pieces = token.split(/[\p{P}\p{S}]/)
pieces << token.gsub(/[\p{P}\p{S}]/, '') if pieces.count > 1
pieces.each do |piece|
dict[cat] << piece if piece.length >= 3 && piece =~ /\p{L}/
end
end
end
end
puts "Writing dictionary to #{t.name} ..."
File.open(t.name, 'w') do |f|
dict.each do |cat, words|
words = words.uniq.sort
next if words.empty?
puts " <- %6d #! #{cat}" % [words.size]
f.puts "#! #{cat}"
words.each { |word| f.puts word }
end
end
puts "#{File.stat(t.name).size / 1024}k written"
end