-
Notifications
You must be signed in to change notification settings - Fork 9
/
Rakefile
169 lines (140 loc) · 4.07 KB
/
Rakefile
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
require 'yaml'
desc 'Clean the working directory'
task :clean do
system 'git clean -df'
end
desc 'Parse the dictionary and check for errors'
task :check do
puts 'Parsing dictionary...'
YAML.parse_file 'dictionary.yml'
puts 'Done.'
end
desc 'Count the entries in the dictionary'
task :count do
puts "Number of entries: #{load_dictionary.count}"
end
desc 'List entries with missing field. If `all\' is specified, checks for empty entires'
task :with_missing, :field do |task, args|
args.with_defaults field: 'all'
dictionary = load_dictionary
entries = dictionary.select do |key, value|
if args[:field] == 'all'
value.nil?
else
value.nil? or value[args[:field]].nil?
end
end
print "Entries with missing field `#{args[:field]}': "
puts entries.any? ? entries.keys.join(', ') : 'none'
end
desc 'List entries with translations without accent'
task :without_accent do
dictionary = load_dictionary
entries = dictionary.reject do |key, value|
next true if value.nil?
case value['превод']
when NilClass
true
when String
value['превод'].include? "\u0300"
when Array
value['превод'].all? { |translation| translation.include? "\u0300" }
end
end
print 'Entries without accented translation: '
puts entries.any? ? entries.keys.join(', ') : 'none'
end
desc 'Get a list of broken references'
task :check_links do
broken = Hash.new { |hash, key| hash[key] = [] }
dictionary = load_dictionary
dictionary.each do |key, value|
unless value.nil?
case value['препратка']
when String
link = value['препратка']
broken[link] |= [key] unless dictionary.has_key? link
when Array
value['препратка'].each { |link| broken[link] |= [key] unless dictionary.has_key? link }
end
end
end
broken.each do |key, value|
puts "Missing `#{key}' is reffered to by:"
value.each { |term| puts " #{term}" }
end
end
desc 'Sort the dictionary'
task :sort do
store_dictionary sort_dictionary load_dictionary
end
desc 'Convert the dictionary to JSON'
task :json do
require 'json'
dictionary = load_dictionary
groups = Hash.new { |hash, key| hash[key] = {} }
dictionary.each do |key, value|
group = key[0..1].downcase.gsub /[^a-zA-Z0-9]/, '_'
groups[group][key] = value
end
groups.each do |group, entries|
print "Creating `#{group}.json'... "
File.open("#{group}.json", 'w') { |file| file.write entries.to_json }
puts 'Done.'
end
end
desc 'List dictionary contributors'
task :contributors do
system 'git shortlog -s -n'
end
desc 'List the bibliography references used in the dictionary'
task :bibliography do
dictionary = load_dictionary
puts 'Extracting references...'
references = dictionary.map { |key, value| value['справка'] unless value.nil? }
puts 'References:'
references.flatten.uniq.compact.each do |reference|
puts ' ' + reference
end
end
desc 'Search for an entry'
task :search, :term do |task, args|
dictionary = load_dictionary
term = dictionary[args[:term]]
unless term.nil?
puts args[:term]
term.each do |key, value|
puts " #{key}: #{value}"
end
else
puts 'Term not found.'
end
end
desc 'Add an untranslated term to the dictionary'
task :add, :term do |task, args|
raise 'Term not specified' unless args[:term]
dictionary = load_dictionary
raise 'Term already exists' if dictionary.has_key? args[:term]
dictionary[args[:term]] = nil
store_dictionary sort_dictionary dictionary
end
def sort_dictionary(dictionary)
sorted_dictionary = {}
puts 'Sorting...'
dictionary.sort_by { |key, value| key.downcase }.each do |key, value|
sorted_dictionary[key] = value
end
sorted_dictionary
end
def load_dictionary(file = 'dictionary.yml')
print 'Loading dictionary... '
dictionary = YAML.load_file file
puts 'Done.'
dictionary
end
def store_dictionary(dictionary, file = 'dictionary.yml')
print "Writing dictionary in #{file}... "
File.open(file, 'w') { |f| f.write dictionary.to_yaml }
puts 'Done.'
end