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,49 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # Says in a few words why a page ranks where it does.
7
- module Reasons
8
- TEMPLATES = {
9
- home: "the homepage", navigation: "in the homepage's navigation", footer: "in the homepage's footer",
10
- list: "%<rank>d. of %<size>d on %<from>s", cards: "card %<rank>d of %<size>d on %<from>s",
11
- text: "linked in the text of %<from>s", address: "under %<from>s"
12
- }.freeze
13
- ON_HOMEPAGE = %i[home navigation].freeze
14
- MIN_MENTIONS = 3
15
- MAX = 3
16
-
17
- module_function
18
-
19
- # home: the key of the homepage, so a route that starts there is not described twice.
20
- def of(item, home: nil) = [reached(item.path), *standing_out(item, home)].first(MAX)
21
-
22
- def reached(path)
23
- return "not reached from the homepage" unless path
24
-
25
- format(TEMPLATES.fetch(path.kind), rank: path.rank.to_i, size: path.size.to_i, from: shown(path.from))
26
- end
27
-
28
- def standing_out(item, home)
29
- notes = []
30
- notes << "shown on the homepage" if shown_on_homepage?(item, home)
31
- notes << "listed or linked on #{item.mentioned_by} pages" if item.mentioned_by >= MIN_MENTIONS
32
- notes << "changed #{item.changed.iso8601}" if item.changed
33
- notes
34
- end
35
-
36
- # Said only when the way the page is reached does not say it already.
37
- def shown_on_homepage?(item, home)
38
- return false unless item.values[:presence].to_f >= 1
39
-
40
- path = item.path
41
- !(path && (ON_HOMEPAGE.include?(path.kind) || (home && path.from == home)))
42
- end
43
-
44
- # A key such as "example.com/trips" as the path a person reads: "/trips".
45
- def shown(key) = key.to_s.sub(%r{\A[^/]*}, "").then { |path| path.empty? ? "/" : path }
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # Says in a few words why a page ranks where it does.
6
+ module Reasons
7
+ TEMPLATES = {
8
+ home: "the homepage", navigation: "in the homepage's navigation", footer: "in the homepage's footer",
9
+ list: "%<rank>d. of %<size>d on %<from>s", cards: "card %<rank>d of %<size>d on %<from>s",
10
+ text: "linked in the text of %<from>s", address: "under %<from>s"
11
+ }.freeze
12
+ ON_HOMEPAGE = %i[home navigation].freeze
13
+ MIN_MENTIONS = 3
14
+ MAX = 3
15
+
16
+ module_function
17
+
18
+ # home: the key of the homepage, so a route that starts there is not described twice.
19
+ def of(item, home: nil) = [reached(item.path), *standing_out(item, home)].first(MAX)
20
+
21
+ def reached(path)
22
+ return "not reached from the homepage" unless path
23
+
24
+ template = TEMPLATES.fetch(path.kind)
25
+ return template unless template.include?("%")
26
+
27
+ format(template, rank: path.rank.to_i, size: path.size.to_i, from: shown(path.from))
46
28
  end
29
+
30
+ def standing_out(item, home)
31
+ notes = []
32
+ notes << "shown on the homepage" if shown_on_homepage?(item, home)
33
+ notes << "listed or linked on #{item.mentioned_by} pages" if item.mentioned_by >= MIN_MENTIONS
34
+ notes << "changed #{item.changed.iso8601}" if item.changed
35
+ notes
36
+ end
37
+
38
+ # Said only when the way the page is reached does not say it already.
39
+ def shown_on_homepage?(item, home)
40
+ return false unless item.values[:presence].to_f >= 1
41
+
42
+ path = item.path
43
+ !(path && (ON_HOMEPAGE.include?(path.kind) || (home && path.from == home)))
44
+ end
45
+
46
+ # A key such as "example.com/trips" as the path a person reads: "/trips".
47
+ def shown(key) = key.to_s.sub(%r{\A[^/]*}, "").then { |path| path.empty? ? "/" : path }
47
48
  end
48
49
  end
49
50
  end
@@ -1,110 +1,108 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # Turns the result of an analysis into the lines the analyze command prints.
7
- class Report
8
- TOP = 15
9
- BOTTOM = 5
10
- MISSING = 10
11
- DISAGREEMENTS = 5
12
-
13
- def initialize(result)
14
- @result = result
15
- @ranking = result.ranking
16
- end
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # Turns the result of an analysis into the lines the analyze command prints.
6
+ class Report
7
+ TOP = 15
8
+ BOTTOM = 5
9
+ MISSING = 10
10
+ DISAGREEMENTS = 5
11
+
12
+ def initialize(result)
13
+ @result = result
14
+ @ranking = result.ranking
15
+ end
17
16
 
18
- def lines
19
- [*summary, "", *usage_lines, *model_lines, *note_lines, "", "Most important:", *rows(top), *bottom_lines,
20
- *missing_lines, "", footer]
21
- end
17
+ def lines
18
+ [*summary, "", *usage_lines, *model_lines, *note_lines, "", "Most important:", *rows(top), *bottom_lines,
19
+ *missing_lines, "", footer]
20
+ end
22
21
 
23
- private
22
+ private
24
23
 
25
- def scores = @ranking.scores
24
+ def scores = @ranking.scores
26
25
 
27
- def summary
28
- tiers = scores.map(&:tier).tally.sort.map { |tier, count| "tier #{tier}: #{count}" }.join(", ")
29
- ["#{'[dry run] ' if @result.dry_run}Analysed #{scores.size} pages, after reading #{@result.pages_read} " \
30
- "of them.",
31
- "The structure of the site leads to #{percent(@ranking.confidence)} of the pages, and the models' " \
32
- "judgement makes up #{percent(@ranking.ai_share)} of each score.",
33
- "Pages per tier: #{tiers}."]
34
- end
26
+ def summary
27
+ tiers = scores.map(&:tier).tally.sort.map { |tier, count| "tier #{tier}: #{count}" }.join(", ")
28
+ ["#{'[dry run] ' if @result.dry_run}Analysed #{scores.size} pages, after reading #{@result.pages_read} " \
29
+ "of them.",
30
+ "The structure of the site leads to #{percent(@ranking.confidence)} of the pages, and the models' " \
31
+ "judgement makes up #{percent(@ranking.ai_share)} of each score.",
32
+ "Pages per tier: #{tiers}."]
33
+ end
35
34
 
36
- def usage_lines
37
- counted = @ranking.usage.map do |use|
38
- note = use.note ? " (#{use.note})" : ""
39
- " #{use.signal}: weight #{use.weight.round}, measured on #{percent(use.coverage)} of the pages#{note}"
40
- end
41
- ["How each measurement counted:", *counted]
35
+ def usage_lines
36
+ counted = @ranking.usage.map do |use|
37
+ note = use.note ? " (#{use.note})" : ""
38
+ " #{use.signal}: weight #{use.weight.round}, measured on #{percent(use.coverage)} of the pages#{note}"
42
39
  end
40
+ ["How each measurement counted:", *counted]
41
+ end
43
42
 
44
- def model_lines
45
- judged = @result.judged
46
- return [] unless judged
43
+ def model_lines
44
+ judged = @result.judged
45
+ return [] unless judged
47
46
 
48
- calls = judged.calls.map do |call|
49
- " #{call.model}, #{call.seconds.round} s, tokens in/out/thinking " \
50
- "#{call.input_tokens} / #{call.output_tokens} / #{call.thinking_tokens}"
51
- end
52
- ["", "The models were asked #{calls.size} time(s):", *calls, *disagreement_lines(judged),
53
- *judged.problems.map { |problem| " - #{problem}" }]
47
+ calls = judged.calls.map do |call|
48
+ " #{call.model}, #{call.seconds.round} s, tokens in/out/thinking " \
49
+ "#{call.input_tokens} / #{call.output_tokens} / #{call.thinking_tokens}"
54
50
  end
51
+ ["", "The models were asked #{calls.size} time(s):", *calls, *disagreement_lines(judged),
52
+ *judged.problems.map { |problem| " - #{problem}" }]
53
+ end
55
54
 
56
- def disagreement_lines(judged)
57
- return [] if judged.disagreements.empty?
55
+ def disagreement_lines(judged)
56
+ return [] if judged.disagreements.empty?
58
57
 
59
- shown = judged.disagreements.first(DISAGREEMENTS).map do |view|
60
- " #{Reasons.shown(view.key)} (#{(view.spread * 100).round} points apart)"
61
- end
62
- ["The two models disagree most about:", *shown]
58
+ shown = judged.disagreements.first(DISAGREEMENTS).map do |view|
59
+ " #{Reasons.shown(view.key)} (#{(view.spread * 100).round} points apart)"
63
60
  end
61
+ ["The two models disagree most about:", *shown]
62
+ end
64
63
 
65
- def note_lines
66
- return [] if @result.notes.empty?
64
+ def note_lines
65
+ return [] if @result.notes.empty?
67
66
 
68
- ["", "Notes:", *@result.notes.map { |note| " - #{note}" }]
69
- end
67
+ ["", "Notes:", *@result.notes.map { |note| " - #{note}" }]
68
+ end
70
69
 
71
- def top = scores.first(TOP).each_with_index.map { |score, index| [score, index + 1] }
70
+ def top = scores.first(TOP).each_with_index.map { |score, index| [score, index + 1] }
72
71
 
73
- def bottom_lines
74
- count = [BOTTOM, scores.size - TOP].min
75
- return [] unless count.positive?
72
+ def bottom_lines
73
+ count = [BOTTOM, scores.size - TOP].min
74
+ return [] unless count.positive?
76
75
 
77
- shown = scores.last(count).each_with_index.map { |score, index| [score, scores.size - count + index + 1] }
78
- ["", "Least important:", *rows(shown)]
79
- end
76
+ shown = scores.last(count).each_with_index.map { |score, index| [score, scores.size - count + index + 1] }
77
+ ["", "Least important:", *rows(shown)]
78
+ end
80
79
 
81
- def rows(list)
82
- list.map do |score, place|
83
- format(" %3<place>d. %3<score>d T%<tier>d %-45<path>s %<notes>s", place:, score: score.score,
84
- tier: score.tier,
85
- path: Reasons.shown(score.key),
86
- notes: score.notes.join("; "))
87
- end
80
+ def rows(list)
81
+ list.map do |score, place|
82
+ format(" %3<place>d. %3<score>d T%<tier>d %-45<path>s %<notes>s", place:, score: score.score,
83
+ tier: score.tier,
84
+ path: Reasons.shown(score.key),
85
+ notes: score.notes.join("; "))
88
86
  end
87
+ end
89
88
 
90
- def missing_lines
91
- missing = @result.not_in_sheet
92
- return [] if missing.empty?
89
+ def missing_lines
90
+ missing = @result.not_in_sheet
91
+ return [] if missing.empty?
93
92
 
94
- more = missing.size > MISSING ? ", ..." : ""
95
- ["", "#{missing.size} page(s) are not in the Pages tab, so nothing was written for them " \
96
- "(run pages import): #{missing.first(MISSING).join(', ')}#{more}"]
97
- end
98
-
99
- def footer
100
- return "Dry run: nothing was written to the sheet." if @result.dry_run
93
+ more = missing.size > MISSING ? ", ..." : ""
94
+ ["", "#{missing.size} page(s) are not in the Pages tab, so nothing was written for them " \
95
+ "(run pages import): #{missing.first(MISSING).join(', ')}#{more}"]
96
+ end
101
97
 
102
- "Written: importance, tier and notes for #{@result.written} page(s) in the Pages tab, and the " \
103
- "measurements of every page in the Analysis tab."
104
- end
98
+ def footer
99
+ return "Dry run: nothing was written to the sheet." if @result.dry_run
105
100
 
106
- def percent(share) = "#{(share * 100).round}%"
101
+ "Written: importance, tier and notes for #{@result.written} page(s) in the Pages tab, and the " \
102
+ "measurements of every page in the Analysis tab."
107
103
  end
104
+
105
+ def percent(share) = "#{(share * 100).round}%"
108
106
  end
109
107
  end
110
108
  end
@@ -2,169 +2,167 @@
2
2
 
3
3
  require "date"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Analysis
8
- # Runs the whole importance analysis: reads the structure of the site, asks the models, scores every page and
9
- # writes the results to the store. Everything it needs is passed in, so it runs with fakes in tests.
10
- class Runner
11
- # judged: what the models answered (a Judge::Result), or nil when they were not asked. not_in_sheet: the
12
- # pages that have a score but no row in the Pages tab. written: how many rows of Pages were written.
13
- Result = Data.define(:ranking, :judged, :pages_read, :notes, :not_in_sheet, :written, :dry_run)
14
-
15
- # client: the model client, needed only when the models are asked.
16
- def initialize(site:, store:, http:, client: nil, weights: Weights.default, clock: -> { Time.now.utc },
17
- progress: ->(_message) {}, log: Log.null)
18
- @site = site
19
- @store = store
20
- @http = http
21
- @client = client
22
- @weights = weights
23
- @clock = clock
24
- @progress = progress
25
- @log = log
26
- @language = site.default_language
27
- end
5
+ module Yuno::Proofreader
6
+ module Analysis
7
+ # Runs the whole importance analysis: reads the structure of the site, asks the models, scores every page and
8
+ # writes the results to the store. Everything it needs is passed in, so it runs with fakes in tests.
9
+ class Runner
10
+ # judged: what the models answered (a Judge::Result), or nil when they were not asked. not_in_sheet: the
11
+ # pages that have a score but no row in the Pages tab. written: how many rows of Pages were written.
12
+ Result = Data.define(:ranking, :judged, :pages_read, :notes, :not_in_sheet, :written, :dry_run)
13
+
14
+ # client: the model client, needed only when the models are asked.
15
+ def initialize(site:, store:, http:, client: nil, weights: Weights.default, clock: -> { Time.now.utc },
16
+ progress: ->(_message) {}, log: Log.null)
17
+ @site = site
18
+ @store = store
19
+ @http = http
20
+ @client = client
21
+ @weights = weights
22
+ @clock = clock
23
+ @progress = progress
24
+ @log = log
25
+ @language = site.default_language
26
+ end
28
27
 
29
- # judgement: :fresh (ask the models), :reuse (take their judgement from the last run, kept in the Analysis
30
- # tab) or :none (the structure alone).
31
- def run(judgement: :fresh, dry_run: false)
32
- @notes = []
33
- now = @clock.call
34
- pairing = load_pairing
35
- inventory, signals = measure(pairing, now)
36
- views, judged = views_for(judgement, inventory, pairing)
37
- records = @store.pages
38
- ranking = Scorer.call(signals, @weights, views:, adjustments: adjustments(pairing, records))
39
- written = dry_run ? 0 : write(ranking, signals, records, now)
40
- result = Result.new(ranking:, judged:, pages_read: inventory.facts.size, notes: @notes,
41
- not_in_sheet: not_in_sheet(ranking, records), written:, dry_run:)
42
- log_result(result)
43
- result
44
- end
28
+ # judgement: :fresh (ask the models), :reuse (take their judgement from the last run, kept in the Analysis
29
+ # tab) or :none (the structure alone).
30
+ def run(judgement: :fresh, dry_run: false)
31
+ @notes = []
32
+ now = @clock.call
33
+ pairing = load_pairing
34
+ inventory, signals = measure(pairing, now)
35
+ views, judged = views_for(judgement, inventory, pairing)
36
+ records = @store.pages
37
+ ranking = Scorer.call(signals, @weights, views:, adjustments: adjustments(pairing, records))
38
+ written = dry_run ? 0 : write(ranking, signals, records, now)
39
+ result = Result.new(ranking:, judged:, pages_read: inventory.facts.size, notes: @notes,
40
+ not_in_sheet: not_in_sheet(ranking, records), written:, dry_run:)
41
+ log_result(result)
42
+ result
43
+ end
45
44
 
46
- private
45
+ private
47
46
 
48
- def settings = @site.analysis
47
+ def settings = @site.analysis
49
48
 
50
- def load_pairing
51
- @progress.call("Reading the sitemap...")
52
- pairing = Sitemap::Loader.call(@site, @http).pairing
53
- return pairing if pairing.resolved?
49
+ def load_pairing
50
+ @progress.call("Reading the sitemap...")
51
+ pairing = Sitemap::Loader.call(@site, @http).pairing
52
+ return pairing if pairing.resolved?
54
53
 
55
- raise SitemapError, "The pages could not be paired by language. Run the sitemap command to see why."
56
- end
54
+ raise SitemapError, "The pages could not be paired by language. Run the sitemap command to see why."
55
+ end
57
56
 
58
- def measure(pairing, now)
59
- inventory = Crawler.new(site: @site, pages: pairing.pages, fetcher: Pages::Fetcher.new(@site, @http),
60
- extra_hubs: settings.hubs, max_pages: settings.max_pages, progress: @progress).call
61
- unless inventory.unread.empty?
62
- @notes << "#{inventory.unread.size} index page(s) were not read, because max_pages " \
63
- "(#{settings.max_pages}) was reached."
64
- end
65
- signals = Signals.call(inventory:, pages: pairing.pages, weights: @weights, language: @language,
66
- today: now.to_date, order: settings.hub_order)
67
- [inventory, signals]
57
+ def measure(pairing, now)
58
+ inventory = Crawler.new(site: @site, pages: pairing.pages, fetcher: Pages::Fetcher.new(@site, @http),
59
+ extra_hubs: settings.hubs, max_pages: settings.max_pages, progress: @progress).call
60
+ unless inventory.unread.empty?
61
+ @notes << "#{inventory.unread.size} index page(s) were not read, because max_pages " \
62
+ "(#{settings.max_pages}) was reached."
68
63
  end
64
+ signals = Signals.call(inventory:, pages: pairing.pages, weights: @weights, language: @language,
65
+ today: now.to_date, order: settings.hub_order)
66
+ [inventory, signals]
67
+ end
69
68
 
70
- # ---- the models ----
69
+ # ---- the models ----
71
70
 
72
- def views_for(mode, inventory, pairing)
73
- case mode
74
- when :fresh then fresh_views(inventory, pairing)
75
- when :reuse then [reused_views, nil]
76
- else [{}, nil]
77
- end
71
+ def views_for(mode, inventory, pairing)
72
+ case mode
73
+ when :fresh then fresh_views(inventory, pairing)
74
+ when :reuse then [reused_views, nil]
75
+ else [{}, nil]
78
76
  end
77
+ end
79
78
 
80
- def fresh_views(inventory, pairing)
81
- entries = Entries.call(inventory:, pages: pairing.pages, language: @language)
82
- judged = Judge.new(site: @site, client: @client, weights: @weights, hint: settings.importance_hint,
83
- progress: @progress).call(entries)
84
- [judged.views, judged]
85
- rescue LlmError => e
86
- @notes << "The models could not be asked (#{e.message}). The scores come from the structure alone."
87
- [{}, nil]
88
- end
79
+ def fresh_views(inventory, pairing)
80
+ entries = Entries.call(inventory:, pages: pairing.pages, language: @language)
81
+ judged = Judge.new(site: @site, client: @client, weights: @weights, hint: settings.importance_hint,
82
+ progress: @progress).call(entries)
83
+ [judged.views, judged]
84
+ rescue LlmError => e
85
+ @notes << "The models could not be asked (#{e.message}). The scores come from the structure alone."
86
+ [{}, nil]
87
+ end
89
88
 
90
- def reused_views
91
- views = @store.analysis_rows.to_h { |row| view_from(row) }
92
- return views if views.values.any?(&:value)
89
+ def reused_views
90
+ views = @store.analysis_rows.to_h { |row| view_from(row) }
91
+ return views if views.values.any?(&:value)
93
92
 
94
- raise Error, "The Analysis tab holds no judgements from the models to reuse. " \
95
- "Run analyze without --reuse-ai first."
96
- end
93
+ raise Error, "The Analysis tab holds no judgements from the models to reuse. " \
94
+ "Run analyze without --reuse-ai first."
95
+ end
97
96
 
98
- def view_from(row)
99
- key = Links.key(row[:address].to_s)
100
- [key, AiView.new(key:, value: share(row[:ai_value]), kind: row[:ai_kind].to_s, place: row[:ai_place].to_s,
101
- certainty: row[:ai_certainty].to_s, reason: row[:ai_reason].to_s,
102
- spread: share(row[:ai_spread]))]
103
- end
97
+ def view_from(row)
98
+ key = Links.key(row[:address].to_s)
99
+ [key, AiView.new(key:, value: share(row[:ai_value]), kind: row[:ai_kind].to_s, place: row[:ai_place].to_s,
100
+ certainty: row[:ai_certainty].to_s, reason: row[:ai_reason].to_s,
101
+ spread: share(row[:ai_spread]))]
102
+ end
104
103
 
105
- def share(value) = (value.nil? || value.to_s.strip.empty? ? nil : value.to_f / 100)
104
+ def share(value) = (value.nil? || value.to_s.strip.empty? ? nil : value.to_f / 100)
106
105
 
107
- # ---- the sheet ----
106
+ # ---- the sheet ----
108
107
 
109
- def keyed(records) = records.to_h { |record| [Links.key(record.key(@language).to_s), record] }
108
+ def keyed(records) = records.to_h { |record| [Links.key(record.key(@language).to_s), record] }
110
109
 
111
- def adjustments(pairing, records)
112
- boosts = records.to_h { |record| [Links.key(record.key(@language).to_s), record.boost] }.compact
113
- Adjustments.call(pages: pairing.pages, language: @language, settings:, boosts:)
114
- end
110
+ def adjustments(pairing, records)
111
+ boosts = records.to_h { |record| [Links.key(record.key(@language).to_s), record.boost] }.compact
112
+ Adjustments.call(pages: pairing.pages, language: @language, settings:, boosts:)
113
+ end
115
114
 
116
- def write(ranking, signals, records, now)
117
- by_key = keyed(records)
118
- updates = ranking.scores.each_with_object({}) do |score, found|
119
- record = by_key[score.key]
120
- found[record.row] = importance_of(score, now) if record
121
- end
122
- @store.record_importance(updates)
123
- @store.replace_analysis(analysis_rows(ranking, signals, now))
124
- updates.size
115
+ def write(ranking, signals, records, now)
116
+ by_key = keyed(records)
117
+ updates = ranking.scores.each_with_object({}) do |score, found|
118
+ record = by_key[score.key]
119
+ found[record.row] = importance_of(score, now) if record
125
120
  end
121
+ @store.record_importance(updates)
122
+ @store.replace_analysis(analysis_rows(ranking, signals, now))
123
+ updates.size
124
+ end
126
125
 
127
- def importance_of(score, now)
128
- { importance: score.score, tier: score.tier, importance_notes: score.notes.join("; "), analyzed_at: now }
129
- end
126
+ def importance_of(score, now)
127
+ { importance: score.score, tier: score.tier, importance_notes: score.notes.join("; "), analyzed_at: now }
128
+ end
130
129
 
131
- def not_in_sheet(ranking, records)
132
- known = keyed(records)
133
- ranking.scores.reject { |score| known.key?(score.key) }.map { |score| Reasons.shown(score.key) }
134
- end
130
+ def not_in_sheet(ranking, records)
131
+ known = keyed(records)
132
+ ranking.scores.reject { |score| known.key?(score.key) }.map { |score| Reasons.shown(score.key) }
133
+ end
135
134
 
136
- def analysis_rows(ranking, signals, now)
137
- measured = signals.to_h { |item| [item.key, item] }
138
- ranking.scores.map { |score| analysis_row(score, measured[score.key], now) }
139
- end
135
+ def analysis_rows(ranking, signals, now)
136
+ measured = signals.to_h { |item| [item.key, item] }
137
+ ranking.scores.map { |score| analysis_row(score, measured[score.key], now) }
138
+ end
140
139
 
141
- def analysis_row(score, item, now)
142
- { page_name: score.page.name, address: score.page.urls[@language] || score.page.urls.values.first,
143
- importance: score.score, tier: score.tier, changed: item&.changed&.iso8601.to_s,
144
- route: Reasons.reached(item&.path), analyzed_at: now, **measurements(score), **judgement(score.ai) }
145
- end
140
+ def analysis_row(score, item, now)
141
+ { page_name: score.page.name, address: score.page.urls[@language] || score.page.urls.values.first,
142
+ importance: score.score, tier: score.tier, changed: item&.changed&.iso8601.to_s,
143
+ route: Reasons.reached(item&.path), analyzed_at: now, **measurements(score), **judgement(score.ai) }
144
+ end
146
145
 
147
- def measurements(score)
148
- score.values.slice(:reach, :presence, :mentions, :freshness).transform_values { |value| value&.round(3) }
149
- end
146
+ def measurements(score)
147
+ score.values.slice(:reach, :presence, :mentions, :freshness).transform_values { |value| value&.round(3) }
148
+ end
150
149
 
151
- def judgement(view)
152
- return {} unless view
150
+ def judgement(view)
151
+ return {} unless view
153
152
 
154
- { ai_value: view.value && (view.value * 100).round, ai_kind: view.kind.to_s, ai_place: view.place,
155
- ai_certainty: view.certainty, ai_reason: view.reason, ai_spread: view.spread && (view.spread * 100).round }
156
- end
153
+ { ai_value: view.value && (view.value * 100).round, ai_kind: view.kind.to_s, ai_place: view.place,
154
+ ai_certainty: view.certainty, ai_reason: view.reason, ai_spread: view.spread && (view.spread * 100).round }
155
+ end
157
156
 
158
- def log_result(result)
159
- @notes.each { |note| @log.warn("analysis note", message: note) }
160
- ranking = result.ranking
161
- @log.info("analysis finished", pages: ranking.scores.size, pages_read: result.pages_read,
162
- written: result.written, dry_run: result.dry_run,
163
- not_in_sheet: result.not_in_sheet.size,
164
- structure_confidence: ranking.confidence.round(2),
165
- ai_share: ranking.ai_share.round(2),
166
- **Llm::Usage.of(Array(result.judged&.calls)))
167
- end
157
+ def log_result(result)
158
+ @notes.each { |note| @log.warn("analysis note", message: note) }
159
+ ranking = result.ranking
160
+ @log.info("analysis finished", pages: ranking.scores.size, pages_read: result.pages_read,
161
+ written: result.written, dry_run: result.dry_run,
162
+ not_in_sheet: result.not_in_sheet.size,
163
+ structure_confidence: ranking.confidence.round(2),
164
+ ai_share: ranking.ai_share.round(2),
165
+ **Llm::Usage.of(Array(result.judged&.calls)))
168
166
  end
169
167
  end
170
168
  end