-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
52 lines (41 loc) · 1.55 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
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end
namespace :compile do
desc "Compile substitutions.rb"
task :data do
data_path = File.expand_path(File.dirname(__FILE__) + "/data")
output_path = File.expand_path(File.dirname(__FILE__) + "/lib/attentive")
substitutions = {}
File.open(data_path + "/substitutions.tsv") do |file|
file.each do |line|
next if line.start_with?("#") # skip comments
next if line == "\n" # skip blank lines
# the file contains tab-separated values.
# the first value is the contraction.
# the remaining values are possible phrases that match it
phrases = line.downcase.chomp.split("\t")
raise "#{line.inspect} must have at least two values" unless phrases.length >= 2
substitutions[phrases.shift] = phrases
end
end
File.open(output_path + "/substitutions.rb", "w") do |file|
file.write <<-RUBY
require "attentive/trie"
module Attentive
SUBSTITUTIONS = #{substitutions.inspect}.each_with_object({}) do |(key, values), new_hash|
tokens = Attentive.tokenize(key, substitutions: false)
possibilities = values.map { |value| Attentive.tokenize(value, substitutions: false) }
value = possibilities.length == 1 ? possibilities[0] : Attentive::Phrase.new([Attentive::Tokens::AnyOf.new(key, possibilities, 0)])
new_hash[tokens] = value
end.freeze
end
RUBY
end
end
end
task :default => :spec