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,127 +1,125 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # The importance of one page: a score from 0 to 100, its tier (1 is the most important), the reasons, and the
7
- # model's view of it when there is one.
8
- Score = Data.define(:key, :page, :score, :tier, :values, :notes, :ai)
9
-
10
- # How much a signal counted: its weight in weights.yml, the share of pages it was measured on, and the weight
11
- # it ended with. note says why a signal counted nothing.
12
- Usage = Data.define(:signal, :weight, :coverage, :effective, :note)
13
-
14
- # The pages from the most to the least important. confidence is the share of pages that the navigation or the
15
- # lists of the site lead to, and ai_share the part of each score the model's judgement made up.
16
- Ranking = Data.define(:scores, :usage, :confidence, :ai_share)
17
-
18
- # Combines the measurements of every page, and the model's judgement when there is one, into a score and a
19
- # tier. A signal counts by the share of pages it was measured on, a page without a value gets the middle value
20
- # of the others, and a signal that is the same on every page counts nothing. The model's share grows as the
21
- # structure of the site leads to fewer pages.
22
- class Scorer
23
- STRUCTURE = { home: 1.0, navigation: 1.0, footer: 1.0, list: 1.0, cards: 0.5 }.freeze
24
- UNMEASURED = "could not be measured on any page"
25
- NO_SPREAD = "the same on every page, so it says nothing"
26
-
27
- # views: the model's views of the pages, keyed like the signals. Without them the score comes from the
28
- # structure alone. adjustments: points a person added to pages, keyed the same way.
29
- def self.call(signals, weights, views: {}, adjustments: {}) = new(signals, weights, views, adjustments).call
30
-
31
- def initialize(signals, weights, views = {}, adjustments = {})
32
- @signals = signals
33
- @weights = weights
34
- @ai = views
35
- @adjustments = adjustments
36
- end
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # The importance of one page: a score from 0 to 100, its tier (1 is the most important), the reasons, and the
6
+ # model's view of it when there is one.
7
+ Score = Data.define(:key, :page, :score, :tier, :values, :notes, :ai)
8
+
9
+ # How much a signal counted: its weight in weights.yml, the share of pages it was measured on, and the weight
10
+ # it ended with. note says why a signal counted nothing.
11
+ Usage = Data.define(:signal, :weight, :coverage, :effective, :note)
12
+
13
+ # The pages from the most to the least important. confidence is the share of pages that the navigation or the
14
+ # lists of the site lead to, and ai_share the part of each score the model's judgement made up.
15
+ Ranking = Data.define(:scores, :usage, :confidence, :ai_share)
16
+
17
+ # Combines the measurements of every page, and the model's judgement when there is one, into a score and a
18
+ # tier. A signal counts by the share of pages it was measured on, a page without a value gets the middle value
19
+ # of the others, and a signal that is the same on every page counts nothing. The model's share grows as the
20
+ # structure of the site leads to fewer pages.
21
+ class Scorer
22
+ STRUCTURE = { home: 1.0, navigation: 1.0, footer: 1.0, list: 1.0, cards: 0.5 }.freeze
23
+ UNMEASURED = "could not be measured on any page"
24
+ NO_SPREAD = "the same on every page, so it says nothing"
25
+
26
+ # views: the model's views of the pages, keyed like the signals. Without them the score comes from the
27
+ # structure alone. adjustments: points a person added to pages, keyed the same way.
28
+ def self.call(signals, weights, views: {}, adjustments: {}) = new(signals, weights, views, adjustments).call
29
+
30
+ def initialize(signals, weights, views = {}, adjustments = {})
31
+ @signals = signals
32
+ @weights = weights
33
+ @ai = views
34
+ @adjustments = adjustments
35
+ end
37
36
 
38
- def call
39
- usage = @weights.signals.map { |signal, weight| usage_for(signal, weight) }
40
- medians = usage.to_h { |use| [use.signal, median(known(use.signal))] }
41
- valued = @signals.map { |item| [item, value_of(item, usage, medians)] }
42
- Ranking.new(scores: ranked(valued), usage:, confidence:, ai_share:)
43
- end
37
+ def call
38
+ usage = @weights.signals.map { |signal, weight| usage_for(signal, weight) }
39
+ medians = usage.to_h { |use| [use.signal, median(known(use.signal))] }
40
+ valued = @signals.map { |item| [item, value_of(item, usage, medians)] }
41
+ Ranking.new(scores: ranked(valued), usage:, confidence:, ai_share:)
42
+ end
44
43
 
45
- private
44
+ private
46
45
 
47
- def known(signal) = @signals.filter_map { |item| item.values[signal] }
46
+ def known(signal) = @signals.filter_map { |item| item.values[signal] }
48
47
 
49
- def usage_for(signal, weight)
50
- values = known(signal)
51
- coverage = @signals.empty? ? 0.0 : values.size.fdiv(@signals.size)
52
- note = UNMEASURED if values.empty?
53
- note ||= NO_SPREAD if values.any? && values.max - values.min < @weights.min_spread
54
- Usage.new(signal:, weight:, coverage:, effective: note ? 0.0 : weight * coverage, note:)
55
- end
48
+ def usage_for(signal, weight)
49
+ values = known(signal)
50
+ coverage = @signals.empty? ? 0.0 : values.size.fdiv(@signals.size)
51
+ note = UNMEASURED if values.empty?
52
+ note ||= NO_SPREAD if values.any? && values.max - values.min < @weights.min_spread
53
+ Usage.new(signal:, weight:, coverage:, effective: note ? 0.0 : weight * coverage, note:)
54
+ end
56
55
 
57
- def median(values)
58
- sorted = values.sort
59
- return 0.0 if sorted.empty?
56
+ def median(values)
57
+ sorted = values.sort
58
+ return 0.0 if sorted.empty?
60
59
 
61
- middle = sorted.size / 2
62
- sorted.size.odd? ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2.0
63
- end
60
+ middle = sorted.size / 2
61
+ sorted.size.odd? ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2.0
62
+ end
64
63
 
65
- def home_key = @home_key ||= @signals.find { |item| item.path&.kind == :home }&.key
64
+ def home_key = @home_key ||= @signals.find { |item| item.path&.kind == :home }&.key
66
65
 
67
- # The homepage leads to every other page, so it always comes first and is never adjusted.
68
- def value_of(item, usage, medians)
69
- return 1.0 if item.key == home_key
66
+ # The homepage leads to every other page, so it always comes first and is never adjusted.
67
+ def value_of(item, usage, medians)
68
+ return 1.0 if item.key == home_key
70
69
 
71
- points = @adjustments[item.key]&.points.to_f
72
- (blended(item, usage, medians) + (points / 100.0)).clamp(0.0, 1.0)
73
- end
70
+ points = @adjustments[item.key]&.points.to_f
71
+ (blended(item, usage, medians) + (points / 100.0)).clamp(0.0, 1.0)
72
+ end
74
73
 
75
- def blended(item, usage, medians)
76
- structural = combined(item, usage, medians)
77
- return structural if ai_share.zero?
74
+ def blended(item, usage, medians)
75
+ structural = combined(item, usage, medians)
76
+ return structural if ai_share.zero?
78
77
 
79
- ((1 - ai_share) * structural) + (ai_share * (@ai[item.key]&.value || ai_median))
80
- end
78
+ ((1 - ai_share) * structural) + (ai_share * (@ai[item.key]&.value || ai_median))
79
+ end
81
80
 
82
- def combined(item, usage, medians)
83
- total = usage.sum(&:effective)
84
- return 0.0 if total.zero?
81
+ def combined(item, usage, medians)
82
+ total = usage.sum(&:effective)
83
+ return 0.0 if total.zero?
85
84
 
86
- usage.sum { |use| use.effective * (item.values[use.signal] || medians[use.signal]) } / total
87
- end
85
+ usage.sum { |use| use.effective * (item.values[use.signal] || medians[use.signal]) } / total
86
+ end
88
87
 
89
- def ai_values = @ai_values ||= @ai.values.filter_map(&:value)
88
+ def ai_values = @ai_values ||= @ai.values.filter_map(&:value)
90
89
 
91
- def ai_median = @ai_median ||= median(ai_values)
90
+ def ai_median = @ai_median ||= median(ai_values)
92
91
 
93
- # ai_share_min when the structure leads to every page, growing to ai_share_max as it leads to fewer.
94
- # Nothing when the model judged no page.
95
- def ai_share
96
- @ai_share ||= begin
97
- low = @weights.ai_share_min
98
- ai_values.empty? ? 0.0 : low + ((1 - confidence) * (@weights.ai_share_max - low))
99
- end
92
+ # ai_share_min when the structure leads to every page, growing to ai_share_max as it leads to fewer.
93
+ # Nothing when the model judged no page.
94
+ def ai_share
95
+ @ai_share ||= begin
96
+ low = @weights.ai_share_min
97
+ ai_values.empty? ? 0.0 : low + ((1 - confidence) * (@weights.ai_share_max - low))
100
98
  end
99
+ end
101
100
 
102
- # Highest value first. The values are not rounded here, so only pages that really are equal share a place
103
- # and the better tier. The score shown is rounded.
104
- def ranked(valued)
105
- order = valued.each_with_index.sort_by { |(_item, value), index| [-value, index] }.map(&:first)
106
- first_place = {}
107
- order.each_with_index.map do |(item, value), index|
108
- first_place[value] ||= index
109
- score_for(item, value, tier_at(first_place[value], order.size))
110
- end
101
+ # Highest value first. The values are not rounded here, so only pages that really are equal share a place
102
+ # and the better tier. The score shown is rounded.
103
+ def ranked(valued)
104
+ order = valued.each_with_index.sort_by { |(_item, value), index| [-value, index] }.map(&:first)
105
+ first_place = {}
106
+ order.each_with_index.map do |(item, value), index|
107
+ first_place[value] ||= index
108
+ score_for(item, value, tier_at(first_place[value], order.size))
111
109
  end
110
+ end
112
111
 
113
- def score_for(item, value, tier)
114
- Score.new(key: item.key, page: item.page, score: (100 * value).round, tier:, values: item.values,
115
- notes: Reasons.of(item, home: home_key) + Array(@adjustments[item.key]&.notes), ai: @ai[item.key])
116
- end
112
+ def score_for(item, value, tier)
113
+ Score.new(key: item.key, page: item.page, score: (100 * value).round, tier:, values: item.values,
114
+ notes: Reasons.of(item, home: home_key) + Array(@adjustments[item.key]&.notes), ai: @ai[item.key])
115
+ end
117
116
 
118
- def tier_at(index, count) = @weights.tiers.count { |share| index.fdiv(count) >= share } + 1
117
+ def tier_at(index, count) = @weights.tiers.count { |share| index.fdiv(count) >= share } + 1
119
118
 
120
- def confidence
121
- return 0.0 if @signals.empty?
119
+ def confidence
120
+ return 0.0 if @signals.empty?
122
121
 
123
- @confidence ||= @signals.sum { |item| STRUCTURE.fetch(item.path&.kind, 0.0) }.fdiv(@signals.size)
124
- end
122
+ @confidence ||= @signals.sum { |item| STRUCTURE.fetch(item.path&.kind, 0.0) }.fdiv(@signals.size)
125
123
  end
126
124
  end
127
125
  end
@@ -1,90 +1,88 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Analysis
6
- # The analysis part of the settings file. Everything in it is optional.
7
- # importance_hint what the model should know about the business, such as which offers matter most
8
- # priority_paths points added to the score of the pages at or below a path, from -50 to 50
9
- # hubs index pages to read even if nothing else leads to them
10
- # hub_order auto, editorial or neutral: whether the order of the lists says what matters
11
- # max_pages how many pages the crawl reads at most
12
- class Settings < Data.define(:importance_hint, :priority_paths, :hubs, :hub_order, :max_pages)
13
- KEYS = %w[importance_hint priority_paths hubs hub_order max_pages].freeze
14
- ORDERS = %w[auto editorial neutral].freeze
15
- MAX_POINTS = 50
16
- PAGES = (1..500)
17
- DEFAULT_PAGES = 80
18
-
19
- def self.default
20
- new(importance_hint: "", priority_paths: {}, hubs: [], hub_order: "auto", max_pages: DEFAULT_PAGES)
21
- end
22
-
23
- def self.from_hash(value)
24
- return default if value.nil?
25
- raise ConfigError, "Site: analysis must be a list of key: value pairs" unless value.is_a?(Hash)
3
+ module Yuno::Proofreader
4
+ module Analysis
5
+ # The analysis part of the settings file. Everything in it is optional.
6
+ # importance_hint what the model should know about the business, such as which offers matter most
7
+ # priority_paths points added to the score of the pages at or below a path, from -50 to 50
8
+ # hubs index pages to read even if nothing else leads to them
9
+ # hub_order auto, editorial or neutral: whether the order of the lists says what matters
10
+ # max_pages how many pages the crawl reads at most
11
+ class Settings < Data.define(:importance_hint, :priority_paths, :hubs, :hub_order, :max_pages)
12
+ KEYS = %w[importance_hint priority_paths hubs hub_order max_pages].freeze
13
+ ORDERS = %w[auto editorial neutral].freeze
14
+ MAX_POINTS = 50
15
+ PAGES = (1..500)
16
+ DEFAULT_PAGES = 80
17
+
18
+ def self.default
19
+ new(importance_hint: "", priority_paths: {}, hubs: [], hub_order: "auto", max_pages: DEFAULT_PAGES)
20
+ end
26
21
 
27
- raw = value.transform_keys(&:to_s)
28
- unknown = raw.keys - KEYS
29
- unless unknown.empty?
30
- raise ConfigError, "Site: analysis: unknown setting(s): #{unknown.join(', ')}. Allowed: #{KEYS.join(', ')}"
31
- end
22
+ def self.from_hash(value)
23
+ return default if value.nil?
24
+ raise ConfigError, "Site: analysis must be a list of key: value pairs" unless value.is_a?(Hash)
32
25
 
33
- new(importance_hint: Context.text(raw["importance_hint"], "analysis: importance_hint", Context::MAX_TEXT),
34
- priority_paths: priority_paths(raw["priority_paths"]), hubs: hubs(raw["hubs"]),
35
- hub_order: hub_order(raw["hub_order"]), max_pages: max_pages(raw["max_pages"]))
26
+ raw = value.transform_keys(&:to_s)
27
+ unknown = raw.keys - KEYS
28
+ unless unknown.empty?
29
+ raise ConfigError, "Site: analysis: unknown setting(s): #{unknown.join(', ')}. Allowed: #{KEYS.join(', ')}"
36
30
  end
37
31
 
38
- def self.priority_paths(value)
39
- return {} if value.nil?
40
- unless value.is_a?(Hash)
41
- raise ConfigError, "Site: analysis: priority_paths must list paths with points, for example /trips: 10"
42
- end
32
+ new(importance_hint: Context.text(raw["importance_hint"], "analysis: importance_hint", Context::MAX_TEXT),
33
+ priority_paths: priority_paths(raw["priority_paths"]), hubs: hubs(raw["hubs"]),
34
+ hub_order: hub_order(raw["hub_order"]), max_pages: max_pages(raw["max_pages"]))
35
+ end
43
36
 
44
- value.to_h { |path, points| [path_of(path), points_for(points, path)] }
37
+ def self.priority_paths(value)
38
+ return {} if value.nil?
39
+ unless value.is_a?(Hash)
40
+ raise ConfigError, "Site: analysis: priority_paths must list paths with points, for example /trips: 10"
45
41
  end
46
42
 
47
- def self.points_for(value, path)
48
- number = Integer(value.to_s.strip, 10, exception: false)
49
- return number if number&.between?(-MAX_POINTS, MAX_POINTS)
43
+ value.to_h { |path, points| [path_of(path), points_for(points, path)] }
44
+ end
50
45
 
51
- raise ConfigError, "Site: analysis: priority_paths #{path} needs whole points from -#{MAX_POINTS} to " \
52
- "#{MAX_POINTS} (got #{value.inspect})"
53
- end
46
+ def self.points_for(value, path)
47
+ number = Integer(value.to_s.strip, 10, exception: false)
48
+ return number if number&.between?(-MAX_POINTS, MAX_POINTS)
54
49
 
55
- def self.hubs(value)
56
- items = Array(value)
57
- raise ConfigError, "Site: analysis: hubs must be a list of paths, written as text" unless items.all?(String)
50
+ raise ConfigError, "Site: analysis: priority_paths #{path} needs whole points from -#{MAX_POINTS} to " \
51
+ "#{MAX_POINTS} (got #{value.inspect})"
52
+ end
58
53
 
59
- items.map { |item| path_of(item) }.reject { |path| path == "/" }.uniq
60
- end
54
+ def self.hubs(value)
55
+ items = Array(value)
56
+ raise ConfigError, "Site: analysis: hubs must be a list of paths, written as text" unless items.all?(String)
61
57
 
62
- # A path as it appears in the address: with a leading slash and without a trailing one. "/" stays "/".
63
- def self.path_of(value)
64
- path = value.to_s.strip
65
- path = "/#{path}" unless path.start_with?("/")
66
- path.length > 1 ? path.chomp("/") : path
67
- end
58
+ items.map { |item| path_of(item) }.reject { |path| path == "/" }.uniq
59
+ end
68
60
 
69
- def self.hub_order(value)
70
- return "auto" if value.nil?
61
+ # A path as it appears in the address: with a leading slash and without a trailing one. "/" stays "/".
62
+ def self.path_of(value)
63
+ path = value.to_s.strip
64
+ path = "/#{path}" unless path.start_with?("/")
65
+ path.length > 1 ? path.chomp("/") : path
66
+ end
71
67
 
72
- order = value.to_s.strip.downcase
73
- return order if ORDERS.include?(order)
68
+ def self.hub_order(value)
69
+ return "auto" if value.nil?
74
70
 
75
- raise ConfigError, "Site: analysis: hub_order must be one of #{ORDERS.join(', ')} (got #{value.inspect})"
76
- end
71
+ order = value.to_s.strip.downcase
72
+ return order if ORDERS.include?(order)
77
73
 
78
- def self.max_pages(value)
79
- return DEFAULT_PAGES if value.nil?
74
+ raise ConfigError, "Site: analysis: hub_order must be one of #{ORDERS.join(', ')} (got #{value.inspect})"
75
+ end
80
76
 
81
- number = Integer(value.to_s.strip, 10, exception: false)
82
- return number if number && PAGES.cover?(number)
77
+ def self.max_pages(value)
78
+ return DEFAULT_PAGES if value.nil?
83
79
 
84
- raise ConfigError, "Site: analysis: max_pages must be a whole number from 1 to #{PAGES.last}"
85
- end
86
- private_class_method :priority_paths, :points_for, :hubs, :path_of, :hub_order, :max_pages
80
+ number = Integer(value.to_s.strip, 10, exception: false)
81
+ return number if number && PAGES.cover?(number)
82
+
83
+ raise ConfigError, "Site: analysis: max_pages must be a whole number from 1 to #{PAGES.last}"
87
84
  end
85
+ private_class_method :priority_paths, :points_for, :hubs, :path_of, :hub_order, :max_pages
88
86
  end
89
87
  end
90
88
  end
@@ -2,101 +2,99 @@
2
2
 
3
3
  require "date"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Analysis
8
- # The measurements of one page. values holds each signal from 0 to 1, or nil when it could not be measured.
9
- # mentioned_by is the number of pages that list or link it, and changed the date it last changed, if known.
10
- PageSignals = Data.define(:key, :page, :path, :values, :mentioned_by, :changed)
11
-
12
- # Measures every page of the pairing: how well the homepage reaches it, whether the homepage shows it, how many
13
- # pages list or link it, and how recently it changed.
14
- class Signals
15
- FOOTER_PRESENCE = 0.4
16
-
17
- def self.call(**inputs) = new(**inputs).call
18
-
19
- def initialize(inventory:, pages:, weights:, language:, today: Date.today, order: "editorial")
20
- @inventory = inventory
21
- @pages = pages
22
- @language = language
23
- @today = today
24
- @half_life = weights.half_life
25
- @graph = Graph.new(inventory, weights, order:)
26
- @home = Links.key(inventory.home)
27
- @shown, @footer = homepage_keys
28
- @mentions = count_mentions
29
- end
5
+ module Yuno::Proofreader
6
+ module Analysis
7
+ # The measurements of one page. values holds each signal from 0 to 1, or nil when it could not be measured.
8
+ # mentioned_by is the number of pages that list or link it, and changed the date it last changed, if known.
9
+ PageSignals = Data.define(:key, :page, :path, :values, :mentioned_by, :changed)
10
+
11
+ # Measures every page of the pairing: how well the homepage reaches it, whether the homepage shows it, how many
12
+ # pages list or link it, and how recently it changed.
13
+ class Signals
14
+ FOOTER_PRESENCE = 0.4
15
+
16
+ def self.call(**inputs) = new(**inputs).call
17
+
18
+ def initialize(inventory:, pages:, weights:, language:, today: Date.today, order: "editorial")
19
+ @inventory = inventory
20
+ @pages = pages
21
+ @language = language
22
+ @today = today
23
+ @half_life = weights.half_life
24
+ @graph = Graph.new(inventory, weights, order:)
25
+ @home = Links.key(inventory.home)
26
+ @shown, @footer = homepage_keys
27
+ @mentions = count_mentions
28
+ end
30
29
 
31
- def call
32
- counts = @pages.map { |page| @mentions.fetch(Links.key(url_of(page)), 0) }
33
- ranks = percentile_ranks(counts)
34
- @pages.each_with_index.map { |page, index| measure(page, counts[index], ranks[index]) }
35
- end
30
+ def call
31
+ counts = @pages.map { |page| @mentions.fetch(Links.key(url_of(page)), 0) }
32
+ ranks = percentile_ranks(counts)
33
+ @pages.each_with_index.map { |page, index| measure(page, counts[index], ranks[index]) }
34
+ end
36
35
 
37
- private
36
+ private
38
37
 
39
- def url_of(page) = page.urls[@language] || page.urls.values.first
38
+ def url_of(page) = page.urls[@language] || page.urls.values.first
40
39
 
41
- def measure(page, count, rank)
42
- url = url_of(page)
43
- key = Links.key(url)
44
- changed = changed_on(page, url)
45
- values = { reach: @graph.paths[key]&.value || 0.0, presence: presence(key), mentions: rank,
46
- freshness: changed && freshness(changed) }
47
- PageSignals.new(key:, page:, path: @graph.paths[key], values:, mentioned_by: count, changed:)
48
- end
40
+ def measure(page, count, rank)
41
+ url = url_of(page)
42
+ key = Links.key(url)
43
+ changed = changed_on(page, url)
44
+ values = { reach: @graph.paths[key]&.value || 0.0, presence: presence(key), mentions: rank,
45
+ freshness: changed && freshness(changed) }
46
+ PageSignals.new(key:, page:, path: @graph.paths[key], values:, mentioned_by: count, changed:)
47
+ end
49
48
 
50
- # The pages the homepage shows in its navigation or lists, and the ones only its footer links.
51
- def homepage_keys
52
- facts = @inventory.facts_for(@inventory.home)
53
- return [Set.new, Set.new] unless facts
49
+ # The pages the homepage shows in its navigation or lists, and the ones only its footer links.
50
+ def homepage_keys
51
+ facts = @inventory.facts_for(@inventory.home)
52
+ return [Set.new, Set.new] unless facts
54
53
 
55
- links = facts.chrome_links
56
- listed = facts.lists.flat_map { |list| list.list_members.map(&:url) }
57
- shown = (links[:header] + links[:nav] + listed).to_set { |url| Links.key(url) }
58
- [shown, links[:footer].to_set { |url| Links.key(url) }]
59
- end
54
+ links = facts.chrome_links
55
+ listed = facts.lists.flat_map { |list| list.list_members.map(&:url) }
56
+ shown = (links[:header] + links[:nav] + listed).to_set { |url| Links.key(url) }
57
+ [shown, links[:footer].to_set { |url| Links.key(url) }]
58
+ end
60
59
 
61
- def presence(key)
62
- return 1.0 if key == @home || @shown.include?(key)
60
+ def presence(key)
61
+ return 1.0 if key == @home || @shown.include?(key)
63
62
 
64
- @footer.include?(key) ? FOOTER_PRESENCE : 0.0
65
- end
66
-
67
- # How many of the pages read list a page or link to it in their text. Each page counts once.
68
- def count_mentions
69
- counts = Hash.new(0)
70
- @inventory.facts.each_value do |facts|
71
- listed = facts.lists.flat_map { |list| list.list_members.map(&:url) }
72
- (listed + facts.main_links).map { |url| Links.key(url) }.uniq.each { |key| counts[key] += 1 }
73
- end
74
- counts
75
- end
63
+ @footer.include?(key) ? FOOTER_PRESENCE : 0.0
64
+ end
76
65
 
77
- # Where each value stands among the others, from 0 to 1. Equal values share the middle of their places.
78
- def percentile_ranks(values)
79
- values.map do |value|
80
- below = values.count { |other| other < value }
81
- (below + (0.5 * values.count(value))).fdiv(values.size)
82
- end
66
+ # How many of the pages read list a page or link to it in their text. Each page counts once.
67
+ def count_mentions
68
+ counts = Hash.new(0)
69
+ @inventory.facts.each_value do |facts|
70
+ listed = facts.lists.flat_map { |list| list.list_members.map(&:url) }
71
+ (listed + facts.main_links).map { |url| Links.key(url) }.uniq.each { |key| counts[key] += 1 }
83
72
  end
73
+ counts
74
+ end
84
75
 
85
- def changed_on(page, url)
86
- [page.lastmod, @inventory.facts_for(url)&.date_modified].filter_map { |value| date_of(value) }.max
76
+ # Where each value stands among the others, from 0 to 1. Equal values share the middle of their places.
77
+ def percentile_ranks(values)
78
+ values.map do |value|
79
+ below = values.count { |other| other < value }
80
+ (below + (0.5 * values.count(value))).fdiv(values.size)
87
81
  end
82
+ end
88
83
 
89
- def date_of(value)
90
- return value if value.is_a?(Date)
84
+ def changed_on(page, url)
85
+ [page.lastmod, @inventory.facts_for(url)&.date_modified].filter_map { |value| date_of(value) }.max
86
+ end
91
87
 
92
- text = value.to_s.strip[/\A\d{4}-\d{2}-\d{2}/]
93
- text && Date.iso8601(text)
94
- rescue ArgumentError
95
- nil
96
- end
88
+ def date_of(value)
89
+ return value if value.is_a?(Date)
97
90
 
98
- def freshness(date) = 0.5**([(@today - date).to_i, 0].max / @half_life)
91
+ text = value.to_s.strip[/\A\d{4}-\d{2}-\d{2}/]
92
+ text && Date.iso8601(text)
93
+ rescue ArgumentError
94
+ nil
99
95
  end
96
+
97
+ def freshness(date) = 0.5**([(@today - date).to_i, 0].max / @half_life)
100
98
  end
101
99
  end
102
100
  end