-
Notifications
You must be signed in to change notification settings - Fork 2
/
suggestlang.rb
90 lines (72 loc) · 2.29 KB
/
suggestlang.rb
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
require 'optparse'
require "yaml_converters"
require 'languagetool'
ignoreRuleIds = [ "UPPERCASE_SENTENCE_START" ]
options = {
"language" => "en",
"path" => "../ifme",
"languagetool" => "http://localhost:8081/v2"
}
OptionParser.new do |opts|
opts.banner = "Usage: suggestlang.rb [options]\nMake sure a languagetool.org instance is running.\nSee: http://wiki.languagetool.org/http-server\n\n"
opts.on("-l", "--language LANGUAGE", "Language code (default: en)") { |v| options["language"] = v }
opts.on("-p", "--path PATH", "Path to if me checkout (default: ../ifme)") { |v| options["path"] = v }
opts.on("-t", "--tool URI", "Url to languagetool.org instance (default: http://localhost:8081/v2)") { |v| options["languagetool"] = v }
end.parse!
localePath = File.join(options["path"], "config", "locales")
unless File.directory?(localePath)
puts "Error: #{localePath} isn't a valid folder"
exit
end
lt = LanguageTool::API.new base_uri: options["languagetool"]
Dir.glob(localePath + "/*" + options["language"] + ".yml").each do |lf|
f = lf.dup
f = f.sub! localePath + '/', ''
unless File.zero?(lf)
puts "Processing: " + f
puts "----"
yaml_reader = YamlConverters::YamlFileReader.new(lf)
converter = YamlConverters::YamlToSegmentsConverter.new(
yaml_reader, YamlConverters::SegmentToHashWriter.new
)
ff = converter.convert
i = 0
ff.each do |k,v|
i += 1
if v.split.size > 3
begin
check = lt.check text: v, language: options["language"]
if check.matches.any?
ignoreMatches = []
check.matches.each do |m|
if m.rule.issue_type == "misspelling"
if m.context.offset > 1
localText = m.context.text.to_s
if localText[m.context.offset-2,m.context.offset] == "%{"
ignoreMatches.push(m)
end
end
elsif ignoreRuleIds.include?(m.rule.id)
ignoreMatches.push(m)
end
end
if ignoreMatches.length != check.matches.length
puts ""
puts f + "@" + i.to_s + ": " + k
puts "Original: " + v
puts "Proposed: " + check.auto_fix
check.matches.each do |m|
unless ignoreMatches.include?(m)
puts " - " + m.message
end
end
end
end
rescue LanguageTool::APIError => e
puts e.exception
exit
end
end
end
end
end