yuno-proofreader 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/lib/yuno/proofreader/analysis/adjustments.rb +30 -32
  4. data/lib/yuno/proofreader/analysis/crawler.rb +71 -73
  5. data/lib/yuno/proofreader/analysis/entries.rb +52 -54
  6. data/lib/yuno/proofreader/analysis/extractor.rb +162 -164
  7. data/lib/yuno/proofreader/analysis/graph.rb +86 -88
  8. data/lib/yuno/proofreader/analysis/judge.rb +80 -82
  9. data/lib/yuno/proofreader/analysis/judge_prompt.rb +50 -52
  10. data/lib/yuno/proofreader/analysis/judgments.rb +50 -52
  11. data/lib/yuno/proofreader/analysis/links.rb +44 -46
  12. data/lib/yuno/proofreader/analysis/list_order.rb +20 -22
  13. data/lib/yuno/proofreader/analysis/reasons.rb +44 -43
  14. data/lib/yuno/proofreader/analysis/report.rb +79 -81
  15. data/lib/yuno/proofreader/analysis/runner.rb +133 -135
  16. data/lib/yuno/proofreader/analysis/scorer.rb +97 -99
  17. data/lib/yuno/proofreader/analysis/settings.rb +65 -67
  18. data/lib/yuno/proofreader/analysis/signals.rb +77 -79
  19. data/lib/yuno/proofreader/analysis/structured_data.rb +40 -42
  20. data/lib/yuno/proofreader/analysis/url_tree.rb +34 -36
  21. data/lib/yuno/proofreader/analysis/weights.rb +67 -69
  22. data/lib/yuno/proofreader/audit/agenda.rb +63 -65
  23. data/lib/yuno/proofreader/audit/agenda_report.rb +44 -46
  24. data/lib/yuno/proofreader/audit/answers.rb +53 -55
  25. data/lib/yuno/proofreader/audit/attempt.rb +50 -52
  26. data/lib/yuno/proofreader/audit/batch.rb +49 -51
  27. data/lib/yuno/proofreader/audit/batch_report.rb +62 -64
  28. data/lib/yuno/proofreader/audit/cadence.rb +47 -49
  29. data/lib/yuno/proofreader/audit/instructions.rb +15 -17
  30. data/lib/yuno/proofreader/audit/log_fields.rb +16 -18
  31. data/lib/yuno/proofreader/audit/prompt.rb +118 -120
  32. data/lib/yuno/proofreader/audit/report.rb +116 -118
  33. data/lib/yuno/proofreader/audit/runner.rb +150 -152
  34. data/lib/yuno/proofreader/audit/schemas.rb +38 -40
  35. data/lib/yuno/proofreader/audit/steps.rb +46 -48
  36. data/lib/yuno/proofreader/cli/analysis_commands.rb +36 -38
  37. data/lib/yuno/proofreader/cli/audit_commands.rb +89 -91
  38. data/lib/yuno/proofreader/cli/command_log.rb +34 -36
  39. data/lib/yuno/proofreader/cli/flags.rb +33 -35
  40. data/lib/yuno/proofreader/cli/generate_commands.rb +99 -101
  41. data/lib/yuno/proofreader/cli/init_command.rb +123 -125
  42. data/lib/yuno/proofreader/cli/model_commands.rb +35 -37
  43. data/lib/yuno/proofreader/cli/queue_commands.rb +79 -81
  44. data/lib/yuno/proofreader/cli/sheet_commands.rb +42 -44
  45. data/lib/yuno/proofreader/cli.rb +197 -199
  46. data/lib/yuno/proofreader/config.rb +13 -17
  47. data/lib/yuno/proofreader/context.rb +45 -50
  48. data/lib/yuno/proofreader/env_file.rb +52 -56
  49. data/lib/yuno/proofreader/errors.rb +7 -9
  50. data/lib/yuno/proofreader/findings/finding.rb +73 -75
  51. data/lib/yuno/proofreader/findings/matching.rb +21 -23
  52. data/lib/yuno/proofreader/findings/merger.rb +35 -37
  53. data/lib/yuno/proofreader/findings/outline.rb +50 -52
  54. data/lib/yuno/proofreader/findings/plan.rb +14 -16
  55. data/lib/yuno/proofreader/findings/reconciler.rb +170 -172
  56. data/lib/yuno/proofreader/findings/source.rb +54 -56
  57. data/lib/yuno/proofreader/findings/text.rb +24 -26
  58. data/lib/yuno/proofreader/generate/cache.rb +54 -56
  59. data/lib/yuno/proofreader/generate/collector.rb +98 -100
  60. data/lib/yuno/proofreader/generate/describe.rb +18 -20
  61. data/lib/yuno/proofreader/generate/llm/prompt.rb +50 -52
  62. data/lib/yuno/proofreader/generate/llm/reader.rb +84 -86
  63. data/lib/yuno/proofreader/generate/llm/wording.rb +12 -14
  64. data/lib/yuno/proofreader/generate/llms_builder.rb +36 -38
  65. data/lib/yuno/proofreader/generate/llms_report.rb +82 -84
  66. data/lib/yuno/proofreader/generate/llms_txt.rb +99 -101
  67. data/lib/yuno/proofreader/generate/llms_writer.rb +43 -45
  68. data/lib/yuno/proofreader/generate/outline.rb +67 -69
  69. data/lib/yuno/proofreader/generate/passage.rb +41 -43
  70. data/lib/yuno/proofreader/generate/saved_answer.rb +42 -44
  71. data/lib/yuno/proofreader/http/client.rb +94 -96
  72. data/lib/yuno/proofreader/http/net_http_transport.rb +35 -37
  73. data/lib/yuno/proofreader/http/robots.rb +51 -53
  74. data/lib/yuno/proofreader/init/ask.rb +45 -47
  75. data/lib/yuno/proofreader/init/detector.rb +75 -77
  76. data/lib/yuno/proofreader/init/interview.rb +155 -157
  77. data/lib/yuno/proofreader/init/parsing.rb +37 -39
  78. data/lib/yuno/proofreader/init/web.rb +22 -24
  79. data/lib/yuno/proofreader/llm/client.rb +116 -118
  80. data/lib/yuno/proofreader/llm/defaults.rb +10 -12
  81. data/lib/yuno/proofreader/llm/errors.rb +17 -19
  82. data/lib/yuno/proofreader/llm/factory.rb +34 -36
  83. data/lib/yuno/proofreader/llm/handoff.rb +86 -88
  84. data/lib/yuno/proofreader/llm/http_transport.rb +25 -27
  85. data/lib/yuno/proofreader/llm/json_text.rb +12 -14
  86. data/lib/yuno/proofreader/llm/provider.rb +49 -51
  87. data/lib/yuno/proofreader/llm/providers/gemini.rb +111 -113
  88. data/lib/yuno/proofreader/llm/providers/manual.rb +34 -36
  89. data/lib/yuno/proofreader/llm/providers.rb +17 -19
  90. data/lib/yuno/proofreader/llm/types.rb +6 -8
  91. data/lib/yuno/proofreader/llm/usage.rb +8 -10
  92. data/lib/yuno/proofreader/log.rb +88 -96
  93. data/lib/yuno/proofreader/pages/fetcher.rb +53 -55
  94. data/lib/yuno/proofreader/pages/report.rb +37 -39
  95. data/lib/yuno/proofreader/pages/snapshot.rb +32 -34
  96. data/lib/yuno/proofreader/pages/trimmer.rb +23 -25
  97. data/lib/yuno/proofreader/settings_file.rb +35 -38
  98. data/lib/yuno/proofreader/site.rb +52 -54
  99. data/lib/yuno/proofreader/site_reader.rb +122 -124
  100. data/lib/yuno/proofreader/sitemap/aligner.rb +58 -60
  101. data/lib/yuno/proofreader/sitemap/importer.rb +19 -21
  102. data/lib/yuno/proofreader/sitemap/loader.rb +95 -97
  103. data/lib/yuno/proofreader/sitemap/mismatch.rb +53 -55
  104. data/lib/yuno/proofreader/sitemap/pairer.rb +171 -173
  105. data/lib/yuno/proofreader/sitemap/parser.rb +50 -52
  106. data/lib/yuno/proofreader/sitemap/report.rb +72 -74
  107. data/lib/yuno/proofreader/store/cells.rb +53 -57
  108. data/lib/yuno/proofreader/store/gateways/csv.rb +53 -56
  109. data/lib/yuno/proofreader/store/gateways/google.rb +152 -158
  110. data/lib/yuno/proofreader/store/memory.rb +60 -64
  111. data/lib/yuno/proofreader/store/records.rb +18 -20
  112. data/lib/yuno/proofreader/store/schema.rb +85 -89
  113. data/lib/yuno/proofreader/store/sheets.rb +132 -145
  114. data/lib/yuno/proofreader/store/tab_setup.rb +51 -54
  115. data/lib/yuno/proofreader/version.rb +1 -1
  116. data/lib/yuno/proofreader.rb +2 -4
  117. metadata +2 -2
@@ -1,76 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- # Reads the settings that live outside proofreader.yml (API keys, the id of the sheet, Google credentials) from
6
- # a .env file, so they do not have to be typed into the shell. A variable that is already set wins over the file
7
- module EnvFile
8
- DEFAULT = ".env"
9
- CHOICE = "PROOFREADER_ENV_FILE"
10
- LINE = /\A(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\z/
11
- DOUBLE = /\A"((?:[^"\\]|\\.)*)"\s*(?:#.*)?\z/
12
- SINGLE = /\A'([^']*)'\s*(?:#.*)?\z/
13
- ESCAPES = { "n" => "\n", "t" => "\t" }.freeze
3
+ module Yuno::Proofreader
4
+ # Reads the settings that live outside proofreader.yml (API keys, the id of the sheet, Google credentials) from
5
+ # a .env file. A variable that is already set wins over the file
6
+ module EnvFile
7
+ DEFAULT = ".env"
8
+ CHOICE = "PROOFREADER_ENV_FILE"
9
+ LINE = /\A(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\z/
10
+ DOUBLE = /\A"((?:[^"\\]|\\.)*)"\s*(?:#.*)?\z/
11
+ SINGLE = /\A'([^']*)'\s*(?:#.*)?\z/
12
+ ESCAPES = { "n" => "\n", "t" => "\t" }.freeze
14
13
 
15
- # path: the file. names: the variables it set. skipped: the numbers of the lines that were not NAME=VALUE.
16
- # loose: whether other users can read the file.
17
- Loaded = Data.define(:path, :names, :skipped, :loose) do
18
- def warnings = [skipped_warning, loose_warning].compact
14
+ # path: the file. names: the variables it set. skipped: the numbers of the lines that were not NAME=VALUE.
15
+ # loose: whether other users can read the file.
16
+ Loaded = Data.define(:path, :names, :skipped, :loose) do
17
+ def warnings = [skipped_warning, loose_warning].compact
19
18
 
20
- def skipped_warning
21
- return if skipped.empty?
19
+ def skipped_warning
20
+ return if skipped.empty?
22
21
 
23
- "Ignored #{skipped.size} line(s) of #{path} that are not NAME=VALUE (line #{skipped.first(5).join(', ')})."
24
- end
25
-
26
- def loose_warning = ("#{path} can be read by other users. Run: chmod 600 #{path}" if loose)
22
+ "Ignored #{skipped.size} line(s) of #{path} that are not NAME=VALUE (line #{skipped.first(5).join(', ')})."
27
23
  end
28
24
 
29
- module_function
25
+ def loose_warning = ("#{path} can be read by other users. Run: chmod 600 #{path}" if loose)
26
+ end
30
27
 
31
- # Loads the file into env without replacing what is already set. Returns what was loaded, or nil when there
32
- # is no file to load. A file named in PROOFREADER_ENV_FILE that does not exist is an error.
33
- def load(env = ENV, path: nil)
34
- chosen = path || env[CHOICE].to_s.strip
35
- file = chosen.empty? ? DEFAULT : chosen
36
- unless File.file?(file)
37
- raise ConfigError, "#{CHOICE} points to #{file}, which is not a file." unless chosen.empty?
28
+ module_function
38
29
 
39
- return
40
- end
30
+ # Loads the file into env without replacing what is already set
31
+ def load(env = ENV, path: nil)
32
+ chosen = path || env[CHOICE].to_s.strip
33
+ file = chosen.empty? ? DEFAULT : chosen
34
+ unless File.file?(file)
35
+ raise ConfigError, "#{CHOICE} points to #{file}, which is not a file." unless chosen.empty?
41
36
 
42
- pairs, skipped = parse(File.read(file))
43
- names = pairs.keys.select { |name| env[name].to_s.empty? }
44
- names.each { |name| env[name] = pairs[name] }
45
- Loaded.new(path: file, names:, skipped:, loose: loose?(file))
37
+ return
46
38
  end
47
39
 
48
- # [the NAME=VALUE pairs, the numbers of the lines that were neither a pair, empty nor a comment]
49
- def parse(text)
50
- pairs = {}
51
- skipped = []
52
- text.each_line.with_index(1) do |raw, number|
53
- line = raw.strip
54
- next if line.empty? || line.start_with?("#")
40
+ pairs, skipped = parse(File.read(file))
41
+ names = pairs.keys.select { |name| env[name].to_s.empty? }
42
+ names.each { |name| env[name] = pairs[name] }
43
+ Loaded.new(path: file, names:, skipped:, loose: loose?(file))
44
+ end
45
+
46
+ def parse(text)
47
+ pairs = {}
48
+ skipped = []
49
+ text.each_line.with_index(1) do |raw, number|
50
+ line = raw.strip
51
+ next if line.empty? || line.start_with?("#")
55
52
 
56
- match = LINE.match(line)
57
- match ? pairs[match[1]] = value_of(match[2]) : skipped << number
58
- end
59
- [pairs, skipped]
53
+ match = LINE.match(line)
54
+ match ? pairs[match[1]] = value_of(match[2]) : skipped << number
60
55
  end
56
+ [pairs, skipped]
57
+ end
61
58
 
62
- def value_of(raw)
63
- text = raw.strip
64
- if (match = DOUBLE.match(text)) then unescape(match[1])
65
- elsif (match = SINGLE.match(text)) then match[1]
66
- else text.sub(/\s+#.*\z/, "")
67
- end
59
+ def value_of(raw)
60
+ text = raw.strip
61
+ if (match = DOUBLE.match(text)) then unescape(match[1])
62
+ elsif (match = SINGLE.match(text)) then match[1]
63
+ else text.sub(/\s+#.*\z/, "")
68
64
  end
65
+ end
69
66
 
70
- def unescape(text) = text.gsub(/\\(.)/) { ESCAPES.fetch(Regexp.last_match(1), Regexp.last_match(1)) }
67
+ def unescape(text) = text.gsub(/\\(.)/) { ESCAPES.fetch(Regexp.last_match(1), Regexp.last_match(1)) }
71
68
 
72
- # Whether other users can read the file. Not checked where there are no unix permissions.
73
- def loose?(file) = File::ALT_SEPARATOR.nil? && File.stat(file).mode.anybits?(0o077)
74
- end
69
+ # Whether other users can read the file
70
+ def loose?(file) = File::ALT_SEPARATOR.nil? && File.stat(file).mode.anybits?(0o077)
75
71
  end
76
72
  end
@@ -1,17 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- class Error < StandardError; end
3
+ module Yuno::Proofreader
4
+ class Error < StandardError; end
6
5
 
7
- class ConfigError < Error; end
6
+ class ConfigError < Error; end
8
7
 
9
- class SitemapError < Error; end
8
+ class SitemapError < Error; end
10
9
 
11
- class FetchError < Error; end
10
+ class FetchError < Error; end
12
11
 
13
- class StoreError < Error; end
12
+ class StoreError < Error; end
14
13
 
15
- class LlmError < Error; end
16
- end
14
+ class LlmError < Error; end
17
15
  end
@@ -1,95 +1,93 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Findings
6
- # One suggested change, as the model reports it: where it is, what it quotes and what it suggests.
7
- class Finding < Data.define(:language, :category, :effort, :original, :suggested, :reason)
8
- CATEGORIES = %w[accent typo grammar repetition html alt_text parity seo stale style other].freeze
9
- MAX_QUOTE = 2_000
10
- MAX_SUGGESTION = 2_000
11
- MAX_REASON = 500
12
-
13
- # findings: the usable ones. discarded: a line for each item that was not usable, saying why.
14
- Parsed = Data.define(:findings, :discarded)
15
-
16
- # items: the list of findings from a model answer. An item that cannot be used is discarded with a
17
- # reason, so one bad item never costs the others. categories: the ones allowed for this list.
18
- # fallback: the one used for anything else.
19
- def self.parse_all(items, languages:, categories: CATEGORIES, fallback: "other")
20
- return Parsed.new(findings: [], discarded: []) if items.nil?
21
- return Parsed.new(findings: [], discarded: ["the findings are not a list"]) unless items.is_a?(Array)
22
-
23
- findings = []
24
- discarded = []
25
- items.each_with_index do |item, index|
26
- finding, problem = build(item, languages:, categories:, fallback:)
27
- if finding
28
- findings << finding
29
- else
30
- discarded << "finding #{index + 1} #{problem}"
31
- end
3
+ module Yuno::Proofreader
4
+ module Findings
5
+ # One suggested change, as the model reports it: where it is, what it quotes and what it suggests.
6
+ class Finding < Data.define(:language, :category, :effort, :original, :suggested, :reason)
7
+ CATEGORIES = %w[accent typo grammar repetition html alt_text parity seo stale style other].freeze
8
+ MAX_QUOTE = 2_000
9
+ MAX_SUGGESTION = 2_000
10
+ MAX_REASON = 500
11
+
12
+ # findings: the usable ones. discarded: a line for each item that was not usable, saying why.
13
+ Parsed = Data.define(:findings, :discarded)
14
+
15
+ # items: the list of findings from a model answer. An item that cannot be used is discarded with a
16
+ # reason, so one bad item never costs the others. categories: the ones allowed for this list.
17
+ # fallback: the one used for anything else.
18
+ def self.parse_all(items, languages:, categories: CATEGORIES, fallback: "other")
19
+ return Parsed.new(findings: [], discarded: []) if items.nil?
20
+ return Parsed.new(findings: [], discarded: ["the findings are not a list"]) unless items.is_a?(Array)
21
+
22
+ findings = []
23
+ discarded = []
24
+ items.each_with_index do |item, index|
25
+ finding, problem = build(item, languages:, categories:, fallback:)
26
+ if finding
27
+ findings << finding
28
+ else
29
+ discarded << "finding #{index + 1} #{problem}"
32
30
  end
33
- Parsed.new(findings:, discarded:)
34
31
  end
32
+ Parsed.new(findings:, discarded:)
33
+ end
35
34
 
36
- # Returns [finding, nil], or [nil, what is wrong with the item].
37
- def self.build(item, languages:, categories:, fallback:)
38
- return [nil, "is not an object"] unless item.is_a?(Hash)
35
+ # Returns [finding, nil], or [nil, what is wrong with the item].
36
+ def self.build(item, languages:, categories:, fallback:)
37
+ return [nil, "is not an object"] unless item.is_a?(Hash)
39
38
 
40
- raw = item.transform_keys(&:to_s)
41
- language = language_for(raw["language"], languages)
42
- problem = problem_with(raw, language)
43
- return [nil, problem] if problem
39
+ raw = item.transform_keys(&:to_s)
40
+ language = language_for(raw["language"], languages)
41
+ problem = problem_with(raw, language)
42
+ return [nil, problem] if problem
44
43
 
45
- category = category_for(raw["category"], categories, fallback)
46
- [new(language:, category:, effort: effort_for(raw["effort"], category), original: clean(raw["original"]),
47
- suggested: cut(clean(raw["suggested"]), MAX_SUGGESTION), reason: cut(clean(raw["reason"]), MAX_REASON)),
48
- nil]
49
- end
44
+ category = category_for(raw["category"], categories, fallback)
45
+ [new(language:, category:, effort: effort_for(raw["effort"], category), original: clean(raw["original"]),
46
+ suggested: cut(clean(raw["suggested"]), MAX_SUGGESTION), reason: cut(clean(raw["reason"]), MAX_REASON)),
47
+ nil]
48
+ end
50
49
 
51
- def self.problem_with(raw, language)
52
- original = clean(raw["original"])
53
- suggested = clean(raw["suggested"])
54
- return "has an unknown language #{raw['language'].inspect}" unless language
55
- return "quotes no text" if original.empty?
56
- return "quotes more than #{MAX_QUOTE} characters" if original.length > MAX_QUOTE
57
- return "suggests nothing" if suggested.empty?
50
+ def self.problem_with(raw, language)
51
+ original = clean(raw["original"])
52
+ suggested = clean(raw["suggested"])
53
+ return "has an unknown language #{raw['language'].inspect}" unless language
54
+ return "quotes no text" if original.empty?
55
+ return "quotes more than #{MAX_QUOTE} characters" if original.length > MAX_QUOTE
56
+ return "suggests nothing" if suggested.empty?
58
57
 
59
- "suggests the text it quotes" if suggested == original
60
- end
58
+ "suggests the text it quotes" if suggested == original
59
+ end
61
60
 
62
- # An exact code wins. Otherwise a regional code (de-AT) counts as its language (de).
63
- def self.language_for(value, languages)
64
- code = clean(value).downcase
65
- languages.find { |language| language == code } ||
66
- languages.sort_by { |language| -language.length }.find { |language| code.start_with?("#{language}-") }
67
- end
61
+ # An exact code wins. Otherwise a regional code (de-AT) counts as its language (de).
62
+ def self.language_for(value, languages)
63
+ code = clean(value).downcase
64
+ languages.find { |language| language == code } ||
65
+ languages.sort_by { |language| -language.length }.find { |language| code.start_with?("#{language}-") }
66
+ end
68
67
 
69
- def self.category_for(value, categories, fallback)
70
- category = clean(value).downcase
71
- categories.include?(category) ? category : fallback
72
- end
68
+ def self.category_for(value, categories, fallback)
69
+ category = clean(value).downcase
70
+ categories.include?(category) ? category : fallback
71
+ end
73
72
 
74
- # Quick means a fix that needs no thinking. Anything unclear is bigger, and alt text is always bigger.
75
- def self.effort_for(value, category)
76
- return "bigger" if category == "alt_text"
73
+ # Quick means a fix that needs no thinking. Anything unclear is bigger, and alt text is always bigger.
74
+ def self.effort_for(value, category)
75
+ return "bigger" if category == "alt_text"
77
76
 
78
- clean(value).downcase == "quick" ? "quick" : "bigger"
79
- end
77
+ clean(value).downcase == "quick" ? "quick" : "bigger"
78
+ end
80
79
 
81
- # Text and numbers become trimmed text. Anything else (a nested structure, nothing) becomes empty.
82
- def self.clean(value)
83
- case value
84
- when String, Numeric then value.to_s.strip
85
- else ""
86
- end
80
+ # Text and numbers become trimmed text. Anything else (a nested structure, nothing) becomes empty.
81
+ def self.clean(value)
82
+ case value
83
+ when String, Numeric then value.to_s.strip
84
+ else ""
87
85
  end
86
+ end
88
87
 
89
- def self.cut(text, max) = text[0, max]
88
+ def self.cut(text, max) = text[0, max]
90
89
 
91
- private_class_method :build, :problem_with, :language_for, :category_for, :effort_for, :clean, :cut
92
- end
90
+ private_class_method :build, :problem_with, :language_for, :category_for, :effort_for, :clean, :cut
93
91
  end
94
92
  end
95
93
  end
@@ -1,34 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Findings
6
- # When two findings, or a finding and a suggestion in the sheet, are about the same place.
7
- module Matching
8
- MIN_OVERLAP = 6
9
- FAMILIES = { "accent" => "spelling", "typo" => "spelling", "grammar" => "wording", "style" => "wording" }.freeze
3
+ module Yuno::Proofreader
4
+ module Findings
5
+ # When two findings, or a finding and a suggestion in the sheet, are about the same place.
6
+ module Matching
7
+ MIN_OVERLAP = 6
8
+ FAMILIES = { "accent" => "spelling", "typo" => "spelling", "grammar" => "wording", "style" => "wording" }.freeze
10
9
 
11
- module_function
10
+ module_function
12
11
 
13
- # Spelling and wording issues match across their categories, because the model files one issue under either.
14
- def family(category) = FAMILIES.fetch(category.to_s, category.to_s)
12
+ # Spelling and wording issues match across their categories, because the model files one issue under either.
13
+ def family(category) = FAMILIES.fetch(category.to_s, category.to_s)
15
14
 
16
- # Same language, and one quoted text contains the other (ignoring spaces, capitals and entities).
17
- # A short quote has to match exactly, so a word like "the" does not match half the page.
18
- def same_spot?(first, second)
19
- return false unless first.language == second.language
15
+ # Same language, and one quoted text contains the other (ignoring spaces, capitals and entities).
16
+ # A short quote has to match exactly, so a word like "the" does not match half the page.
17
+ def same_spot?(first, second)
18
+ return false unless first.language == second.language
20
19
 
21
- one = key(first.original)
22
- other = key(second.original)
23
- return false if one.empty? || other.empty?
24
- return true if one == other
20
+ one = key(first.original)
21
+ other = key(second.original)
22
+ return false if one.empty? || other.empty?
23
+ return true if one == other
25
24
 
26
- shorter, longer = [one, other].sort_by(&:length)
27
- shorter.length >= MIN_OVERLAP && longer.include?(shorter)
28
- end
29
-
30
- def key(text) = Text.squash(text).downcase
25
+ shorter, longer = [one, other].sort_by(&:length)
26
+ shorter.length >= MIN_OVERLAP && longer.include?(shorter)
31
27
  end
28
+
29
+ def key(text) = Text.squash(text).downcase
32
30
  end
33
31
  end
34
32
  end
@@ -1,42 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Findings
6
- # Combines the findings of the audit and of the independent second review. A finding both found counts as
7
- # confirmed. Where both found it, the audit's wording is kept, because its categories are the more reliable.
8
- class Merger
9
- Merged = Data.define(:finding, :found_by)
10
-
11
- BOTH = "both"
12
- AUDIT = "audit"
13
- REVIEW = "review"
14
- UNREVIEWED = "audit (unreviewed)"
15
-
16
- # REVIEW: is nil when the second review failed. An empty list means it ran and found nothing.
17
- def self.call(audit, review) = new(audit, review).call
18
-
19
- def initialize(audit, review)
20
- @audit = audit
21
- @review = review
22
- end
23
-
24
- def call
25
- return @audit.map { |finding| Merged.new(finding:, found_by: UNREVIEWED) } if @review.nil?
26
-
27
- confirmed = Array.new(@review.size, false)
28
- merged = @audit.map { |finding| compare(finding, confirmed) }
29
- merged + @review.each_index.reject { |index| confirmed[index] }
30
- .map { |index| Merged.new(finding: @review[index], found_by: REVIEW) }
31
- end
32
-
33
- private
34
-
35
- def compare(finding, confirmed)
36
- hits = @review.each_index.select { |index| Matching.same_spot?(finding, @review[index]) }
37
- hits.each { |index| confirmed[index] = true }
38
- Merged.new(finding:, found_by: hits.empty? ? AUDIT : BOTH)
39
- end
3
+ module Yuno::Proofreader
4
+ module Findings
5
+ # Combines the findings of the audit and of the independent second review. A finding both found counts as
6
+ # confirmed. Where both found it, the audit's wording is kept, because its categories are the more reliable.
7
+ class Merger
8
+ Merged = Data.define(:finding, :found_by)
9
+
10
+ BOTH = "both"
11
+ AUDIT = "audit"
12
+ REVIEW = "review"
13
+ UNREVIEWED = "audit (unreviewed)"
14
+
15
+ # REVIEW: is nil when the second review failed. An empty list means it ran and found nothing.
16
+ def self.call(audit, review) = new(audit, review).call
17
+
18
+ def initialize(audit, review)
19
+ @audit = audit
20
+ @review = review
21
+ end
22
+
23
+ def call
24
+ return @audit.map { |finding| Merged.new(finding:, found_by: UNREVIEWED) } if @review.nil?
25
+
26
+ confirmed = Array.new(@review.size, false)
27
+ merged = @audit.map { |finding| compare(finding, confirmed) }
28
+ merged + @review.each_index.reject { |index| confirmed[index] }
29
+ .map { |index| Merged.new(finding: @review[index], found_by: REVIEW) }
30
+ end
31
+
32
+ private
33
+
34
+ def compare(finding, confirmed)
35
+ hits = @review.each_index.select { |index| Matching.same_spot?(finding, @review[index]) }
36
+ hits.each { |index| confirmed[index] = true }
37
+ Merged.new(finding:, found_by: hits.empty? ? AUDIT : BOTH)
40
38
  end
41
39
  end
42
40
  end
@@ -1,72 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Findings
6
- # The structure of a page's HTML: its header, navigation and footer areas, and its headings.
7
- # It answers one question: which headings is a position in the page under?
8
- # It reads the markup with regular expressions, so it is made for the cleaned HTML the tool works with.
9
- class Outline
10
- Region = Data.define(:name, :start, :finish)
11
- Heading = Data.define(:level, :start, :inner, :region)
3
+ module Yuno::Proofreader
4
+ module Findings
5
+ # The structure of a page's HTML: its header, navigation and footer areas, and its headings.
6
+ # It answers one question: which headings is a position in the page under?
7
+ # It reads the markup with regular expressions, so it is made for the cleaned HTML the tool works with.
8
+ class Outline
9
+ Region = Data.define(:name, :start, :finish)
10
+ Heading = Data.define(:level, :start, :inner, :region)
12
11
 
13
- REGION = %r{<(head|header|nav|footer)\b.*?</\1\s*>}mi
14
- HEADING = %r{<h([1-6])\b[^>]*>(.*?)</h\1\s*>}mi
15
- MAX_LABEL = 60
12
+ REGION = %r{<(head|header|nav|footer)\b.*?</\1\s*>}mi
13
+ HEADING = %r{<h([1-6])\b[^>]*>(.*?)</h\1\s*>}mi
14
+ MAX_LABEL = 60
16
15
 
17
- def initialize(html)
18
- @regions = matches(REGION, html).map do |match|
19
- Region.new(name: match[1].downcase, start: match.begin(0), finish: match.end(0))
20
- end
21
- @headings = matches(HEADING, html).map { |match| build_heading(match) }
16
+ def initialize(html)
17
+ @regions = matches(REGION, html).map do |match|
18
+ Region.new(name: match[1].downcase, start: match.begin(0), finish: match.end(0))
22
19
  end
20
+ @headings = matches(HEADING, html).map { |match| build_heading(match) }
21
+ end
23
22
 
24
- # The headings as [level, text, region name or nil], for looking at the structure.
25
- def headings = @headings.map { |heading| [heading.level, label(heading), heading.region&.name] }
23
+ # The headings as [level, text, region name or nil], for looking at the structure.
24
+ def headings = @headings.map { |heading| [heading.level, label(heading), heading.region&.name] }
26
25
 
27
- # "header", "footer" or "head" for those areas. Anywhere else the headings above the position, from the
28
- # first h2 down, such as "What we offer > Tours". The h1 is left out, because everything is under it.
29
- def path_at(position)
30
- region = region_at(position)
31
- return "head" if region&.name == "head"
26
+ # "header", "footer" or "head" for those areas. Anywhere else the headings above the position, from the
27
+ # first h2 down, such as "What we offer > Tours". The h1 is left out, because everything is under it.
28
+ def path_at(position)
29
+ region = region_at(position)
30
+ return "head" if region&.name == "head"
32
31
 
33
- parts = chain_at(position, region).select { |heading| heading.level > 1 }.map { |heading| label(heading) }
34
- path = [region&.name, *parts].compact.join(" > ")
35
- path.empty? ? "(before first h2)" : path
36
- end
32
+ parts = chain_at(position, region).select { |heading| heading.level > 1 }.map { |heading| label(heading) }
33
+ path = [region&.name, *parts].compact.join(" > ")
34
+ path.empty? ? "(before first h2)" : path
35
+ end
37
36
 
38
- private
37
+ private
39
38
 
40
- def matches(regexp, html)
41
- html.to_enum(:scan, regexp).map { Regexp.last_match }
42
- end
39
+ def matches(regexp, html)
40
+ html.to_enum(:scan, regexp).map { Regexp.last_match }
41
+ end
43
42
 
44
- def build_heading(match)
45
- start = match.begin(0)
46
- Heading.new(level: match[1].to_i, start:, inner: match[2], region: region_at(start))
47
- end
43
+ def build_heading(match)
44
+ start = match.begin(0)
45
+ Heading.new(level: match[1].to_i, start:, inner: match[2], region: region_at(start))
46
+ end
48
47
 
49
- def region_at(position)
50
- @regions.find { |region| position >= region.start && position < region.finish }
51
- end
48
+ def region_at(position)
49
+ @regions.find { |region| position >= region.start && position < region.finish }
50
+ end
52
51
 
53
- # The headings that are open at the position: a new heading closes every heading of the same or a lower
54
- # level. Headings in other areas (a footer, say) do not count.
55
- def chain_at(position, region)
56
- @headings.each_with_object([]) do |heading, chain|
57
- next if heading.start > position || heading.region != region
52
+ # The headings that are open at the position: a new heading closes every heading of the same or a lower
53
+ # level. Headings in other areas (a footer, say) do not count.
54
+ def chain_at(position, region)
55
+ @headings.each_with_object([]) do |heading, chain|
56
+ next if heading.start > position || heading.region != region
58
57
 
59
- chain.pop while chain.any? && chain.last.level >= heading.level
60
- chain << heading
61
- end
58
+ chain.pop while chain.any? && chain.last.level >= heading.level
59
+ chain << heading
62
60
  end
61
+ end
63
62
 
64
- def label(heading)
65
- text = Text.collapse(Text.decode(Text.strip_tags(heading.inner)))
66
- return "(empty h#{heading.level})" if text.empty?
63
+ def label(heading)
64
+ text = Text.collapse(Text.decode(Text.strip_tags(heading.inner)))
65
+ return "(empty h#{heading.level})" if text.empty?
67
66
 
68
- text.length > MAX_LABEL ? "#{text[0, MAX_LABEL - 1]}…" : text
69
- end
67
+ text.length > MAX_LABEL ? "#{text[0, MAX_LABEL - 1]}…" : text
70
68
  end
71
69
  end
72
70
  end
@@ -1,24 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Findings
6
- # One thing the reconciliation decided. kind is :added, :reappeared, :seen_again, :suppressed, :duplicate,
7
- # :ignored, :closed or :close_skipped.
8
- Decision = Data.define(:kind, :summary, :reason)
3
+ module Yuno::Proofreader
4
+ module Findings
5
+ # One thing the reconciliation decided. kind is :added, :reappeared, :seen_again, :suppressed, :duplicate,
6
+ # :ignored, :closed or :close_skipped.
7
+ Decision = Data.define(:kind, :summary, :reason)
9
8
 
10
- # What to write: the rows to add, the changes to rows that exist (row number => fields), and why.
11
- Plan = Data.define(:additions, :updates, :decisions) do
12
- def apply(store)
13
- store.update_suggestions(updates)
14
- store.add_suggestions(additions)
15
- self
16
- end
9
+ # What to write: the rows to add, the changes to rows that exist (row number => fields), and why.
10
+ Plan = Data.define(:additions, :updates, :decisions) do
11
+ def apply(store)
12
+ store.update_suggestions(updates)
13
+ store.add_suggestions(additions)
14
+ self
15
+ end
17
16
 
18
- def counts = decisions.map(&:kind).tally
17
+ def counts = decisions.map(&:kind).tally
19
18
 
20
- def empty? = additions.empty? && updates.empty?
21
- end
19
+ def empty? = additions.empty? && updates.empty?
22
20
  end
23
21
  end
24
22
  end