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,102 +1,100 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # The model's view of a page. value is between 0 and 1, or nil when the model could not tell. spread is the
7
- # difference between the two models' values when both judged the page.
8
- AiView = Data.define(:key, :value, :kind, :place, :certainty, :reason, :spread)
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # The model's view of a page. value is between 0 and 1, or nil when the model could not tell. spread is the
6
+ # difference between the two models' values when both judged the page.
7
+ AiView = Data.define(:key, :value, :kind, :place, :certainty, :reason, :spread)
9
8
 
10
- # Asks a model, and then a second one, how much each page matters to the site's readers, and combines the
11
- # answers. Pages about the same place share one fame for it, so a volcano is as famous on its tour page as on
12
- # its region page.
13
- class Judge
14
- BATCH = 60
15
- Opinion = Data.define(:judgments, :calls, :problems, :models)
16
- Result = Data.define(:views, :calls, :problems, :disagreements)
9
+ # Asks a model, and then a second one, how much each page matters to the site's readers, and combines the
10
+ # answers. Pages about the same place share one fame for it, so a volcano is as famous on its tour page as on
11
+ # its region page.
12
+ class Judge
13
+ BATCH = 60
14
+ Opinion = Data.define(:judgments, :calls, :problems, :models)
15
+ Result = Data.define(:views, :calls, :problems, :disagreements)
17
16
 
18
- def initialize(site:, client:, weights:, hint: "", batch: BATCH, progress: ->(_message) {})
19
- @client = client
20
- @prompt = JudgePrompt.new(site, hint:)
21
- @limit = weights.disagreement / 100.0
22
- @batch = batch
23
- @progress = progress
24
- end
17
+ def initialize(site:, client:, weights:, hint: "", batch: BATCH, progress: ->(_message) {})
18
+ @client = client
19
+ @prompt = JudgePrompt.new(site, hint:)
20
+ @limit = weights.disagreement / 100.0
21
+ @batch = batch
22
+ @progress = progress
23
+ end
25
24
 
26
- # views maps each page's key to its AiView. Raises an LlmError when no model can give the first opinion.
27
- def call(entries)
28
- @notes = []
29
- first = opinion(entries, avoid: [], label: "first opinion")
30
- opinions = [first, second_opinion(entries, first)].compact
31
- views = combine(entries, opinions)
32
- disagreements = views.values.select { |view| view.spread && view.spread >= @limit }
33
- Result.new(views:, calls: opinions.flat_map(&:calls), problems: opinions.flat_map(&:problems) + @notes,
34
- disagreements: disagreements.sort_by { |view| -view.spread })
35
- end
25
+ # views maps each page's key to its AiView. Raises an LlmError when no model can give the first opinion.
26
+ def call(entries)
27
+ @notes = []
28
+ first = opinion(entries, avoid: [], label: "first opinion")
29
+ opinions = [first, second_opinion(entries, first)].compact
30
+ views = combine(entries, opinions)
31
+ disagreements = views.values.select { |view| view.spread && view.spread >= @limit }
32
+ Result.new(views:, calls: opinions.flat_map(&:calls), problems: opinions.flat_map(&:problems) + @notes,
33
+ disagreements: disagreements.sort_by { |view| -view.spread })
34
+ end
36
35
 
37
- private
36
+ private
38
37
 
39
- def opinion(entries, avoid:, label:)
40
- judgments = {}
41
- calls = []
42
- problems = []
43
- entries.each_slice(@batch).with_index(1) do |batch, number|
44
- @progress.call("Asking a model how much the pages matter (#{label}, part #{number})...")
45
- answer = @client.generate(system: @prompt.system, user: @prompt.user(batch), avoid:)
46
- parsed = Judgments.parse(answer.data, ids: batch.map(&:id))
47
- judgments.merge!(parsed.judgments)
48
- problems.concat(parsed.problems.map { |problem| "#{label}: #{problem}" })
49
- calls << answer.with(data: nil)
50
- end
51
- Opinion.new(judgments:, calls:, problems:, models: calls.map(&:model).uniq)
38
+ def opinion(entries, avoid:, label:)
39
+ judgments = {}
40
+ calls = []
41
+ problems = []
42
+ entries.each_slice(@batch).with_index(1) do |batch, number|
43
+ @progress.call("Asking a model how much the pages matter (#{label}, part #{number})...")
44
+ answer = @client.generate(system: @prompt.system, user: @prompt.user(batch), avoid:)
45
+ parsed = Judgments.parse(answer.data, ids: batch.map(&:id))
46
+ judgments.merge!(parsed.judgments)
47
+ problems.concat(parsed.problems.map { |problem| "#{label}: #{problem}" })
48
+ calls << answer.with(data: nil)
52
49
  end
50
+ Opinion.new(judgments:, calls:, problems:, models: calls.map(&:model).uniq)
51
+ end
53
52
 
54
- # A second model judges the same pages without seeing the first answer.
55
- def second_opinion(entries, first)
56
- if (@client.models - first.models).empty?
57
- @notes << "Only one model is available, so there is no second opinion."
58
- return
59
- end
60
- opinion(entries, avoid: first.models, label: "second opinion")
61
- rescue LlmError => e
62
- @notes << "The second opinion failed (#{e.message}), so the first stands alone."
63
- nil
53
+ # A second model judges the same pages without seeing the first answer.
54
+ def second_opinion(entries, first)
55
+ if (@client.models - first.models).empty?
56
+ @notes << "Only one model is available, so there is no second opinion."
57
+ return
64
58
  end
59
+ opinion(entries, avoid: first.models, label: "second opinion")
60
+ rescue LlmError => e
61
+ @notes << "The second opinion failed (#{e.message}), so the first stands alone."
62
+ nil
63
+ end
65
64
 
66
- def combine(entries, opinions)
67
- fame = place_fame(opinions)
68
- entries.to_h do |entry|
69
- found = opinions.filter_map { |opinion| opinion.judgments[entry.id] }
70
- [entry.key, view(entry, found, fame)]
71
- end
65
+ def combine(entries, opinions)
66
+ fame = place_fame(opinions)
67
+ entries.to_h do |entry|
68
+ found = opinions.filter_map { |opinion| opinion.judgments[entry.id] }
69
+ [entry.key, view(entry, found, fame)]
72
70
  end
71
+ end
73
72
 
74
- # One fame per place: the average over every page and every model that named it.
75
- def place_fame(opinions)
76
- named = opinions.flat_map { |opinion| opinion.judgments.values }.reject { |judgment| judgment.place.empty? }
77
- named.group_by { |judgment| place_key(judgment.place) }
78
- .transform_values { |group| group.sum(&:place_fame).fdiv(group.size) }
79
- end
73
+ # One fame per place: the average over every page and every model that named it.
74
+ def place_fame(opinions)
75
+ named = opinions.flat_map { |opinion| opinion.judgments.values }.reject { |judgment| judgment.place.empty? }
76
+ named.group_by { |judgment| place_key(judgment.place) }
77
+ .transform_values { |group| group.sum(&:place_fame).fdiv(group.size) }
78
+ end
80
79
 
81
- def view(entry, found, fame)
82
- values = found.filter_map { |judgment| value_of(judgment, fame) }
83
- first = found.first
84
- AiView.new(key: entry.key, value: values.empty? ? nil : values.sum / values.size, kind: first&.kind,
85
- place: first&.place.to_s, certainty: first&.certainty || "unknown", reason: first&.reason.to_s,
86
- spread: values.size == 2 ? (values[0] - values[1]).abs : nil)
87
- end
80
+ def view(entry, found, fame)
81
+ values = found.filter_map { |judgment| value_of(judgment, fame) }
82
+ first = found.first
83
+ AiView.new(key: entry.key, value: values.empty? ? nil : values.sum / values.size, kind: first&.kind,
84
+ place: first&.place.to_s, certainty: first&.certainty || "unknown", reason: first&.reason.to_s,
85
+ spread: values.size == 2 ? (values[0] - values[1]).abs : nil)
86
+ end
88
87
 
89
- # A page about a place counts its interest and the place's fame equally. Unknown means no value at all.
90
- def value_of(judgment, fame)
91
- return if judgment.certainty == "unknown"
92
- return judgment.interest / 100.0 if judgment.place.empty?
88
+ # A page about a place counts its interest and the place's fame equally. Unknown means no value at all.
89
+ def value_of(judgment, fame)
90
+ return if judgment.certainty == "unknown"
91
+ return judgment.interest / 100.0 if judgment.place.empty?
93
92
 
94
- (judgment.interest + fame.fetch(place_key(judgment.place))) / 200.0
95
- end
93
+ (judgment.interest + fame.fetch(place_key(judgment.place))) / 200.0
94
+ end
96
95
 
97
- def place_key(place)
98
- place.unicode_normalize(:nfd).gsub(/\p{Mn}/, "").downcase.gsub(/[^a-z0-9]+/, " ").strip
99
- end
96
+ def place_key(place)
97
+ place.unicode_normalize(:nfd).gsub(/\p{Mn}/, "").downcase.gsub(/[^a-z0-9]+/, " ").strip
100
98
  end
101
99
  end
102
100
  end
@@ -2,70 +2,68 @@
2
2
 
3
3
  require "yaml"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Analysis
8
- # Builds the request that asks a model how much each page matters. The wording comes from judge.yml, the site
9
- # part from the settings, and the JSON format is here, because the code that reads the answer depends on it.
10
- class JudgePrompt
11
- PATH = File.join(__dir__, "judge.yml")
12
- KEYS = %w[role what_to_judge rules].freeze
13
- FORMAT = "Return ONLY valid JSON, with no commentary and no markdown fences, in exactly this shape:\n" \
14
- '{"pages":[{"id":"p1","kind":"place|lodging|product|information|service|other",' \
15
- '"place":"<place, or empty>","place_fame":0,"interest":0,"certainty":"known|partial|unknown",' \
16
- '"reason":"<one short sentence in English>"}]}'
5
+ module Yuno::Proofreader
6
+ module Analysis
7
+ # Builds the request that asks a model how much each page matters. The wording comes from judge.yml, the site
8
+ # part from the settings, and the JSON format is here, because the code that reads the answer depends on it.
9
+ class JudgePrompt
10
+ PATH = File.join(__dir__, "judge.yml")
11
+ KEYS = %w[role what_to_judge rules].freeze
12
+ FORMAT = "Return ONLY valid JSON, with no commentary and no markdown fences, in exactly this shape:\n" \
13
+ '{"pages":[{"id":"p1","kind":"place|lodging|product|information|service|other",' \
14
+ '"place":"<place, or empty>","place_fame":0,"interest":0,"certainty":"known|partial|unknown",' \
15
+ '"reason":"<one short sentence in English>"}]}'
17
16
 
18
- def self.texts = @texts ||= load
17
+ def self.texts = @texts ||= load
19
18
 
20
- def self.load(path = PATH)
21
- data = YAML.safe_load_file(path)
22
- raise Error, "#{path} must hold the texts of the request" unless data.is_a?(Hash)
19
+ def self.load(path = PATH)
20
+ data = YAML.safe_load_file(path)
21
+ raise Error, "#{path} must hold the texts of the request" unless data.is_a?(Hash)
23
22
 
24
- missing = KEYS.select { |key| data[key].to_s.strip.empty? }
25
- raise Error, "#{path} has no text for: #{missing.join(', ')}" unless missing.empty?
23
+ missing = KEYS.select { |key| data[key].to_s.strip.empty? }
24
+ raise Error, "#{path} has no text for: #{missing.join(', ')}" unless missing.empty?
26
25
 
27
- KEYS.to_h { |key| [key, data[key].to_s.strip] }
28
- end
26
+ KEYS.to_h { |key| [key, data[key].to_s.strip] }
27
+ end
29
28
 
30
- # hint: what the site owner wants the model to know, such as which offers matter most.
31
- def initialize(site, hint: "", texts: self.class.texts)
32
- @site = site
33
- @hint = hint.to_s
34
- @texts = texts
35
- end
29
+ # hint: what the site owner wants the model to know, such as which offers matter most.
30
+ def initialize(site, hint: "", texts: self.class.texts)
31
+ @site = site
32
+ @hint = hint.to_s
33
+ @texts = texts
34
+ end
36
35
 
37
- def system
38
- [section("ROLE", @texts["role"]), site_section, section("WHAT TO JUDGE", @texts["what_to_judge"]),
39
- section("RULES", @texts["rules"]), section("OUTPUT FORMAT", FORMAT)].join("\n\n")
40
- end
36
+ def system
37
+ [section("ROLE", @texts["role"]), site_section, section("WHAT TO JUDGE", @texts["what_to_judge"]),
38
+ section("RULES", @texts["rules"]), section("OUTPUT FORMAT", FORMAT)].join("\n\n")
39
+ end
41
40
 
42
- def user(entries)
43
- ["Pages (id | address | names | listed on | types):", *entries.map { |entry| line(entry) }].join("\n")
44
- end
41
+ def user(entries)
42
+ ["Pages (id | address | names | listed on | types):", *entries.map { |entry| line(entry) }].join("\n")
43
+ end
45
44
 
46
- private
45
+ private
47
46
 
48
- def section(title, text) = "## #{title}\n#{text}"
47
+ def section(title, text) = "## #{title}\n#{text}"
49
48
 
50
- def clean(text) = Findings::Text.collapse(text).tr("|", "/")
49
+ def clean(text) = Findings::Text.collapse(text).tr("|", "/")
51
50
 
52
- def site_section
53
- context = @site.context
54
- lines = ["Website: #{@site.website}"]
55
- lines << "What the site is and offers: #{clean(context.business)}" unless context.business.empty?
56
- lines << "Audience: #{clean(context.audience)}" unless context.audience.empty?
57
- lines << "Note from the site owner: #{clean(@hint)}" unless @hint.strip.empty?
58
- lines << "The names are in: #{@site.default_language}"
59
- section("SITE", lines.join("\n"))
60
- end
51
+ def site_section
52
+ context = @site.context
53
+ lines = ["Website: #{@site.website}"]
54
+ lines << "What the site is and offers: #{clean(context.business)}" unless context.business.empty?
55
+ lines << "Audience: #{clean(context.audience)}" unless context.audience.empty?
56
+ lines << "Note from the site owner: #{clean(@hint)}" unless @hint.strip.empty?
57
+ lines << "The names are in: #{@site.default_language}"
58
+ section("SITE", lines.join("\n"))
59
+ end
61
60
 
62
- def line(entry)
63
- names = entry.names.map { |name| %("#{clean(name)}") }.join(" / ")
64
- listed = entry.listed.map { |where| clean(where) }.join("; ")
65
- [entry.id, entry.path, names, listed, entry.types.join(", ")].map do |part|
66
- part.empty? ? "-" : part
67
- end.join(" | ")
68
- end
61
+ def line(entry)
62
+ names = entry.names.map { |name| %("#{clean(name)}") }.join(" / ")
63
+ listed = entry.listed.map { |where| clean(where) }.join("; ")
64
+ [entry.id, entry.path, names, listed, entry.types.join(", ")].map do |part|
65
+ part.empty? ? "-" : part
66
+ end.join(" | ")
69
67
  end
70
68
  end
71
69
  end
@@ -1,70 +1,68 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # What a model said about one page. place_fame and interest are from 0 to 100.
7
- Judgment = Data.define(:id, :kind, :place, :place_fame, :interest, :certainty, :reason)
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # What a model said about one page. place_fame and interest are from 0 to 100.
6
+ Judgment = Data.define(:id, :kind, :place, :place_fame, :interest, :certainty, :reason)
8
7
 
9
- # Reads a model's answer into judgements, keeping what is usable and saying what is not.
10
- module Judgments
11
- KINDS = %w[place lodging product information service other].freeze
12
- CERTAINTIES = %w[known partial unknown].freeze
13
- Parsed = Data.define(:judgments, :problems)
8
+ # Reads a model's answer into judgements, keeping what is usable and saying what is not.
9
+ module Judgments
10
+ KINDS = %w[place lodging product information service other].freeze
11
+ CERTAINTIES = %w[known partial unknown].freeze
12
+ Parsed = Data.define(:judgments, :problems)
14
13
 
15
- module_function
14
+ module_function
16
15
 
17
- # ids: the ids that were asked about. judgments maps each id to its Judgment.
18
- def parse(data, ids:)
19
- items = data.is_a?(Hash) ? data["pages"] : nil
20
- return Parsed.new(judgments: {}, problems: ["the answer has no list of pages"]) unless items.is_a?(Array)
16
+ # ids: the ids that were asked about. judgments maps each id to its Judgment.
17
+ def parse(data, ids:)
18
+ items = data.is_a?(Hash) ? data["pages"] : nil
19
+ return Parsed.new(judgments: {}, problems: ["the answer has no list of pages"]) unless items.is_a?(Array)
21
20
 
22
- judgments = {}
23
- problems = []
24
- items.each_with_index do |item, index|
25
- judgment, problem = build(item, ids, judgments)
26
- if problem
27
- problems << "item #{index + 1} #{problem}"
28
- else
29
- judgments[judgment.id] = judgment
30
- end
21
+ judgments = {}
22
+ problems = []
23
+ items.each_with_index do |item, index|
24
+ judgment, problem = build(item, ids, judgments)
25
+ if problem
26
+ problems << "item #{index + 1} #{problem}"
27
+ else
28
+ judgments[judgment.id] = judgment
31
29
  end
32
- Parsed.new(judgments:, problems: problems + missing(ids, judgments))
33
30
  end
31
+ Parsed.new(judgments:, problems: problems + missing(ids, judgments))
32
+ end
34
33
 
35
- def build(item, ids, seen)
36
- return [nil, "is not an object"] unless item.is_a?(Hash)
37
-
38
- id = item["id"].to_s.strip
39
- return [nil, "has an unknown id #{id.inspect}"] unless ids.include?(id)
40
- return [nil, "repeats the id #{id}"] if seen.key?(id)
34
+ def build(item, ids, seen)
35
+ return [nil, "is not an object"] unless item.is_a?(Hash)
41
36
 
42
- judgment = Judgment.new(
43
- id:, kind: choice(item["kind"], KINDS, "other"), place: text(item["place"], 80),
44
- place_fame: score(item["place_fame"]), interest: score(item["interest"]),
45
- certainty: choice(item["certainty"], CERTAINTIES, "unknown"), reason: text(item["reason"], 200)
46
- )
47
- [judgment, nil]
48
- end
37
+ id = item["id"].to_s.strip
38
+ return [nil, "has an unknown id #{id.inspect}"] unless ids.include?(id)
39
+ return [nil, "repeats the id #{id}"] if seen.key?(id)
49
40
 
50
- def missing(ids, judgments)
51
- left = ids - judgments.keys
52
- left.empty? ? [] : ["no judgement for #{left.size} page(s): #{left.first(10).join(', ')}"]
53
- end
41
+ judgment = Judgment.new(
42
+ id:, kind: choice(item["kind"], KINDS, "other"), place: text(item["place"], 80),
43
+ place_fame: score(item["place_fame"]), interest: score(item["interest"]),
44
+ certainty: choice(item["certainty"], CERTAINTIES, "unknown"), reason: text(item["reason"], 200)
45
+ )
46
+ [judgment, nil]
47
+ end
54
48
 
55
- def choice(value, allowed, fallback)
56
- word = value.to_s.strip.downcase
57
- allowed.include?(word) ? word : fallback
58
- end
49
+ def missing(ids, judgments)
50
+ left = ids - judgments.keys
51
+ left.empty? ? [] : ["no judgement for #{left.size} page(s): #{left.first(10).join(', ')}"]
52
+ end
59
53
 
60
- def score(value)
61
- number = Float(value, exception: false)
62
- number ? number.clamp(0, 100).round : 50
63
- end
54
+ def choice(value, allowed, fallback)
55
+ word = value.to_s.strip.downcase
56
+ allowed.include?(word) ? word : fallback
57
+ end
64
58
 
65
- def text(value, max) = value.is_a?(String) ? Findings::Text.collapse(value)[0, max] : ""
66
- private_class_method :build, :missing, :choice, :score, :text
59
+ def score(value)
60
+ number = Float(value, exception: false)
61
+ number ? number.clamp(0, 100).round : 50
67
62
  end
63
+
64
+ def text(value, max) = value.is_a?(String) ? Findings::Text.collapse(value)[0, max] : ""
65
+ private_class_method :build, :missing, :choice, :score, :text
68
66
  end
69
67
  end
70
68
  end
@@ -2,52 +2,50 @@
2
2
 
3
3
  require "uri"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Analysis
8
- # Addresses as the analysis compares them: absolute, without a fragment, and keyed so that
9
- # https://www.example.com/a/ and https://example.com/a count as the same page.
10
- module Links
11
- SKIPPED = ["#", "mailto:", "tel:", "javascript:"].freeze
12
-
13
- module_function
14
-
15
- def absolute(base, href)
16
- text = href.to_s.strip
17
- return if text.empty? || text.start_with?(*SKIPPED)
18
-
19
- uri = URI.join(base.to_s, text)
20
- return unless %w[http https].include?(uri.scheme) && !uri.host.to_s.empty?
21
-
22
- uri.fragment = nil
23
- uri.to_s
24
- rescue URI::Error
25
- nil
26
- end
27
-
28
- def host(url)
29
- URI.parse(url.to_s).host.to_s.downcase.delete_prefix("www.")
30
- rescue URI::Error
31
- ""
32
- end
33
-
34
- def same_site?(url, other) = !host(url).empty? && host(url) == host(other)
35
-
36
- # The page an address stands for: host without www., path without a trailing slash, and the query.
37
- def key(url)
38
- uri = URI.parse(url.to_s)
39
- path = uri.path.to_s.chomp("/")
40
- query = uri.query ? "?#{uri.query}" : ""
41
- "#{uri.host.to_s.downcase.delete_prefix('www.')}#{path.empty? ? '/' : path}#{query}"
42
- rescue URI::Error
43
- url.to_s
44
- end
45
-
46
- def segments(url)
47
- URI.parse(url.to_s).path.to_s.split("/").reject(&:empty?)
48
- rescue URI::Error
49
- []
50
- end
5
+ module Yuno::Proofreader
6
+ module Analysis
7
+ # Addresses as the analysis compares them: absolute, without a fragment, and keyed so that
8
+ # https://www.example.com/a/ and https://example.com/a count as the same page.
9
+ module Links
10
+ SKIPPED = ["#", "mailto:", "tel:", "javascript:"].freeze
11
+
12
+ module_function
13
+
14
+ def absolute(base, href)
15
+ text = href.to_s.strip
16
+ return if text.empty? || text.start_with?(*SKIPPED)
17
+
18
+ uri = URI.join(base.to_s, text)
19
+ return unless %w[http https].include?(uri.scheme) && !uri.host.to_s.empty?
20
+
21
+ uri.fragment = nil
22
+ uri.to_s
23
+ rescue URI::Error
24
+ nil
25
+ end
26
+
27
+ def host(url)
28
+ URI.parse(url.to_s).host.to_s.downcase.delete_prefix("www.")
29
+ rescue URI::Error
30
+ ""
31
+ end
32
+
33
+ def same_site?(url, other) = !host(url).empty? && host(url) == host(other)
34
+
35
+ # The page an address stands for: host without www., path without a trailing slash, and the query.
36
+ def key(url)
37
+ uri = URI.parse(url.to_s)
38
+ path = uri.path.to_s.chomp("/")
39
+ query = uri.query ? "?#{uri.query}" : ""
40
+ "#{uri.host.to_s.downcase.delete_prefix('www.')}#{path.empty? ? '/' : path}#{query}"
41
+ rescue URI::Error
42
+ url.to_s
43
+ end
44
+
45
+ def segments(url)
46
+ URI.parse(url.to_s).path.to_s.split("/").reject(&:empty?)
47
+ rescue URI::Error
48
+ []
51
49
  end
52
50
  end
53
51
  end
@@ -1,33 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # Whether the order of a list says which entries matter most.
7
- module ListOrder
8
- MIN_ENTRIES = 3
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # Whether the order of a list says which entries matter most.
6
+ module ListOrder
7
+ MIN_ENTRIES = 3
9
8
 
10
- module_function
9
+ module_function
11
10
 
12
- # mode: "editorial" (always), "neutral" (never) or "auto" (unless the list is in alphabetical order, which
13
- # is how a list sorted by a machine rather than by a person usually looks).
14
- def editorial?(list, mode)
15
- case mode
16
- when "editorial" then true
17
- when "neutral" then false
18
- else !alphabetical?(list)
19
- end
11
+ # mode: "editorial" (always), "neutral" (never) or "auto" (unless the list is in alphabetical order, which
12
+ # is how a list sorted by a machine rather than by a person usually looks).
13
+ def editorial?(list, mode)
14
+ case mode
15
+ when "editorial" then true
16
+ when "neutral" then false
17
+ else !alphabetical?(list)
20
18
  end
19
+ end
21
20
 
22
- def alphabetical?(list)
23
- names = list.list_members.map { |entry| sortable(entry.name) }
24
- names.size >= MIN_ENTRIES && names.none?(&:empty?) && names.each_cons(2).all? do |first, second|
25
- first <= second
26
- end
21
+ def alphabetical?(list)
22
+ names = list.list_members.map { |entry| sortable(entry.name) }
23
+ names.size >= MIN_ENTRIES && names.none?(&:empty?) && names.each_cons(2).all? do |first, second|
24
+ first <= second
27
25
  end
28
-
29
- def sortable(name) = name.to_s.unicode_normalize(:nfd).gsub(/\p{Mn}/, "").downcase.strip
30
26
  end
27
+
28
+ def sortable(name) = name.to_s.unicode_normalize(:nfd).gsub(/\p{Mn}/, "").downcase.strip
31
29
  end
32
30
  end
33
31
  end