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,107 +1,105 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Generate
6
- # Writes the llms.txt of one language, in the format described at llmstxt.org: the title and a quoted summary
7
- # of the site (from the home page), then one section per group of pages with its summary and its links. A
8
- # section with an index page is linked from its heading, and the index page is not listed again. The file ends
9
- # with links to the llms.txt of the other languages. The headings, summaries and link texts are the model's.
10
- # Without a model, headings are made from the addresses and the summary of a section is the description of its
11
- # index page. others: [language code, address] for each other language that has a file.
12
- class LlmsTxt
13
- MORE_PAGES = { "de" => "Weitere Seiten", "en" => "More pages", "es" => "Más páginas", "fr" => "Autres pages",
14
- "it" => "Altre pagine", "pt" => "Outras páginas" }.freeze
15
- DEFAULT_MORE = "More pages"
16
- OTHER_LANGUAGES = { "de" => "Andere Sprachen", "en" => "Other languages", "es" => "Otros idiomas",
17
- "fr" => "Autres langues", "it" => "Altre lingue", "pt" => "Outros idiomas",
18
- "nl" => "Andere talen" }.freeze
19
- DEFAULT_OTHER = "Other languages"
20
- NAMES = { "de" => "Deutsch", "en" => "English", "es" => "Español", "fr" => "Français", "it" => "Italiano",
21
- "pt" => "Português", "nl" => "Nederlands" }.freeze
22
-
23
- def self.call(**) = new(**).call
24
-
25
- def initialize(outline:, home:, wording:, language:, fallback_title:, others: [])
26
- @outline = outline
27
- @home = home
28
- @wording = wording
29
- @language = language
30
- @fallback_title = fallback_title
31
- @others = others
32
- end
33
-
34
- def call
35
- lines = ["# #{title}"]
36
- lines.push("", "> #{@home.description}") if @home && !@home.description.empty?
37
- @outline.sections.each { |section| lines.concat(section_lines(section, "##")) }
38
- lines.concat(more_pages_lines)
39
- lines.concat(other_language_lines)
40
- "#{lines.join("\n")}\n"
41
- end
42
-
43
- private
44
-
45
- def title = [@home&.title, @home&.name].find { |text| !text.to_s.empty? } || @fallback_title
46
-
47
- def base(code) = code.split("-").first
48
-
49
- def section_lines(section, mark)
50
- lines = ["", "#{mark} #{heading(section)}"]
51
- summary = summary_for(section)
52
- lines.push("", summary) if summary
53
- links = section.items.map { |item| link(item) }
54
- lines.push("", *links) unless links.empty?
55
- lines.concat(section.children.flat_map { |child| section_lines(child, "###") })
56
- end
57
-
58
- def heading(section)
59
- text = @wording.headings.fetch(section.id) { humanize(section.slug) }
60
- section.hub ? "[#{escape(text)}](#{address(section.hub.facts.url)})" : text
61
- end
62
-
63
- # The model's summary already carries what the index page says. Without one, the index page's own description.
64
- def summary_for(section)
65
- @wording.summaries[section.id] || (own_text(section.hub) if section.hub)
66
- end
67
-
68
- def own_text(hub)
69
- text = Describe.fallback(hub.facts)
70
- text unless text.empty?
71
- end
72
-
73
- def link(item)
74
- facts = item.facts
75
- line = "- [#{escape(Describe.label(facts.name))}](#{address(facts.url)})"
76
- text = @wording.text_for(item)
77
- text.empty? ? line : "#{line}: #{text}"
78
- end
79
-
80
- def more_pages_lines
81
- return [] if @outline.loose.empty?
82
-
83
- ["", "## #{MORE_PAGES.fetch(base(@language), DEFAULT_MORE)}", "", *@outline.loose.map { |item| link(item) }]
84
- end
85
-
86
- def other_language_lines
87
- return [] if @others.empty?
88
-
89
- ["", "## #{OTHER_LANGUAGES.fetch(base(@language), DEFAULT_OTHER)}", "",
90
- *@others.map { |code, url| "- [#{escape(language_name(code))}](#{url})" }]
91
- end
92
-
93
- # The name of a language in that language. A region such as pt-br stays visible, so two variants differ.
94
- def language_name(code)
95
- name = NAMES.fetch(base(code), code)
96
- code.include?("-") ? "#{name} (#{code})" : name
97
- end
98
-
99
- def humanize(slug) = slug.tr("-_", " ").split.map(&:capitalize).join(" ")
100
-
101
- def escape(text) = text.gsub(/[\[\]]/) { |bracket| "\\#{bracket}" }
102
-
103
- def address(url) = url.gsub(" ", "%20").gsub(")", "%29")
3
+ module Yuno::Proofreader
4
+ module Generate
5
+ # Writes the llms.txt of one language, in the format described at llmstxt.org: the title and a quoted summary
6
+ # of the site (from the home page), then one section per group of pages with its summary and its links. A
7
+ # section with an index page is linked from its heading, and the index page is not listed again. The file ends
8
+ # with links to the llms.txt of the other languages. The headings, summaries and link texts are the model's.
9
+ # Without a model, headings are made from the addresses and the summary of a section is the description of its
10
+ # index page. others: [language code, address] for each other language that has a file.
11
+ class LlmsTxt
12
+ MORE_PAGES = { "de" => "Weitere Seiten", "en" => "More pages", "es" => "Más páginas", "fr" => "Autres pages",
13
+ "it" => "Altre pagine", "pt" => "Outras páginas" }.freeze
14
+ DEFAULT_MORE = "More pages"
15
+ OTHER_LANGUAGES = { "de" => "Andere Sprachen", "en" => "Other languages", "es" => "Otros idiomas",
16
+ "fr" => "Autres langues", "it" => "Altre lingue", "pt" => "Outros idiomas",
17
+ "nl" => "Andere talen" }.freeze
18
+ DEFAULT_OTHER = "Other languages"
19
+ NAMES = { "de" => "Deutsch", "en" => "English", "es" => "Español", "fr" => "Français", "it" => "Italiano",
20
+ "pt" => "Português", "nl" => "Nederlands" }.freeze
21
+
22
+ def self.call(**) = new(**).call
23
+
24
+ def initialize(outline:, home:, wording:, language:, fallback_title:, others: [])
25
+ @outline = outline
26
+ @home = home
27
+ @wording = wording
28
+ @language = language
29
+ @fallback_title = fallback_title
30
+ @others = others
104
31
  end
32
+
33
+ def call
34
+ lines = ["# #{title}"]
35
+ lines.push("", "> #{@home.description}") if @home && !@home.description.empty?
36
+ @outline.sections.each { |section| lines.concat(section_lines(section, "##")) }
37
+ lines.concat(more_pages_lines)
38
+ lines.concat(other_language_lines)
39
+ "#{lines.join("\n")}\n"
40
+ end
41
+
42
+ private
43
+
44
+ def title = [@home&.title, @home&.name].find { |text| !text.to_s.empty? } || @fallback_title
45
+
46
+ def base(code) = code.split("-").first
47
+
48
+ def section_lines(section, mark)
49
+ lines = ["", "#{mark} #{heading(section)}"]
50
+ summary = summary_for(section)
51
+ lines.push("", summary) if summary
52
+ links = section.items.map { |item| link(item) }
53
+ lines.push("", *links) unless links.empty?
54
+ lines.concat(section.children.flat_map { |child| section_lines(child, "###") })
55
+ end
56
+
57
+ def heading(section)
58
+ text = @wording.headings.fetch(section.id) { humanize(section.slug) }
59
+ section.hub ? "[#{escape(text)}](#{address(section.hub.facts.url)})" : text
60
+ end
61
+
62
+ # The model's summary already carries what the index page says. Without one, the index page's own description.
63
+ def summary_for(section)
64
+ @wording.summaries[section.id] || (own_text(section.hub) if section.hub)
65
+ end
66
+
67
+ def own_text(hub)
68
+ text = Describe.fallback(hub.facts)
69
+ text unless text.empty?
70
+ end
71
+
72
+ def link(item)
73
+ facts = item.facts
74
+ line = "- [#{escape(Describe.label(facts.name))}](#{address(facts.url)})"
75
+ text = @wording.text_for(item)
76
+ text.empty? ? line : "#{line}: #{text}"
77
+ end
78
+
79
+ def more_pages_lines
80
+ return [] if @outline.loose.empty?
81
+
82
+ ["", "## #{MORE_PAGES.fetch(base(@language), DEFAULT_MORE)}", "", *@outline.loose.map { |item| link(item) }]
83
+ end
84
+
85
+ def other_language_lines
86
+ return [] if @others.empty?
87
+
88
+ ["", "## #{OTHER_LANGUAGES.fetch(base(@language), DEFAULT_OTHER)}", "",
89
+ *@others.map { |code, url| "- [#{escape(language_name(code))}](#{url})" }]
90
+ end
91
+
92
+ # The name of a language in that language. A region such as pt-br stays visible, so two variants differ.
93
+ def language_name(code)
94
+ name = NAMES.fetch(base(code), code)
95
+ code.include?("-") ? "#{name} (#{code})" : name
96
+ end
97
+
98
+ def humanize(slug) = slug.tr("-_", " ").split.map(&:capitalize).join(" ")
99
+
100
+ def escape(text) = text.gsub(/[\[\]]/) { |bracket| "\\#{bracket}" }
101
+
102
+ def address(url) = url.gsub(" ", "%20").gsub(")", "%29")
105
103
  end
106
104
  end
107
105
  end
@@ -3,51 +3,49 @@
3
3
  require "fileutils"
4
4
  require "uri"
5
5
 
6
- module Yuno
7
- module Proofreader
8
- module Generate
9
- # One llms.txt written to the data folder, with the address it is meant to be uploaded to.
10
- LlmsFile = Data.define(:language, :path, :address, :built)
11
-
12
- # Writes the llms.txt of each language to the data folder, with links to the files of the other languages.
13
- class LlmsWriter
14
- def initialize(site:, builder:)
15
- @site = site
16
- @builder = builder
17
- end
18
-
19
- # Returns the files that were written. A language none of whose pages could be read has no file.
20
- def call(collected, languages)
21
- languages.filter_map { |language| write(collected, language) }
22
- end
23
-
24
- private
25
-
26
- def write(collected, language)
27
- return unless collected.readable?(language)
28
-
29
- built = @builder.call(collected, language:, fallback_title: URI.parse(@site.website).host,
30
- others: others(collected, language))
31
- path = File.join(File.expand_path(@site.data_dir), "llms-#{language}.txt")
32
- write_text(path, built.text)
33
- LlmsFile.new(language:, path:, address: address(language), built:)
34
- end
35
-
36
- # The llms.txt of the other languages, as [code, address].
37
- # Only languages whose pages could be read have one.
38
- def others(collected, language)
39
- (@site.languages - [language]).select { |code| collected.readable?(code) }
40
- .map { |code| [code, address(code)] }
41
- end
42
-
43
- def address(language) = "#{@site.website}#{@site.prefixes.fetch(language)}/llms.txt"
44
-
45
- def write_text(path, text)
46
- FileUtils.mkdir_p(File.dirname(path))
47
- File.write(path, text, encoding: "UTF-8")
48
- rescue SystemCallError => e
49
- raise ConfigError, "Could not write #{path}: #{e.message}"
50
- end
6
+ module Yuno::Proofreader
7
+ module Generate
8
+ # One llms.txt written to the data folder, with the address it is meant to be uploaded to.
9
+ LlmsFile = Data.define(:language, :path, :address, :built)
10
+
11
+ # Writes the llms.txt of each language to the data folder, with links to the files of the other languages.
12
+ class LlmsWriter
13
+ def initialize(site:, builder:)
14
+ @site = site
15
+ @builder = builder
16
+ end
17
+
18
+ # Returns the files that were written. A language none of whose pages could be read has no file.
19
+ def call(collected, languages)
20
+ languages.filter_map { |language| write(collected, language) }
21
+ end
22
+
23
+ private
24
+
25
+ def write(collected, language)
26
+ return unless collected.readable?(language)
27
+
28
+ built = @builder.call(collected, language:, fallback_title: URI.parse(@site.website).host,
29
+ others: others(collected, language))
30
+ path = File.join(File.expand_path(@site.data_dir), "llms-#{language}.txt")
31
+ write_text(path, built.text)
32
+ LlmsFile.new(language:, path:, address: address(language), built:)
33
+ end
34
+
35
+ # The llms.txt of the other languages, as [code, address].
36
+ # Only languages whose pages could be read have one.
37
+ def others(collected, language)
38
+ (@site.languages - [language]).select { |code| collected.readable?(code) }
39
+ .map { |code| [code, address(code)] }
40
+ end
41
+
42
+ def address(language) = "#{@site.website}#{@site.prefixes.fetch(language)}/llms.txt"
43
+
44
+ def write_text(path, text)
45
+ FileUtils.mkdir_p(File.dirname(path))
46
+ File.write(path, text, encoding: "UTF-8")
47
+ rescue SystemCallError => e
48
+ raise ConfigError, "Could not write #{path}: #{e.message}"
51
49
  end
52
50
  end
53
51
  end
@@ -1,90 +1,88 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Generate
6
- # One page in the outline, with the id the model refers to it by.
7
- Item = Data.define(:id, :facts)
8
-
9
- # A group of pages that share the start of their address. hub is the page at the address of the group itself
10
- # (its index page), when there is one. items are the other pages directly in the group, children the groups
11
- # below it. Ids are given in the order the file lists things.
12
- Section = Data.define(:id, :slug, :hub, :items, :children) do
13
- def all_items = [hub, *items, *children.flat_map(&:all_items)].compact
14
-
15
- def listed_items = [*items, *children.flat_map(&:listed_items)]
16
- end
3
+ module Yuno::Proofreader
4
+ module Generate
5
+ # One page in the outline, with the id the model refers to it by.
6
+ Item = Data.define(:id, :facts)
7
+
8
+ # A group of pages that share the start of their address. hub is the page at the address of the group itself
9
+ # (its index page), when there is one. items are the other pages directly in the group, children the groups
10
+ # below it. Ids are given in the order the file lists things.
11
+ Section = Data.define(:id, :slug, :hub, :items, :children) do
12
+ def all_items = [hub, *items, *children.flat_map(&:all_items)].compact
13
+
14
+ def listed_items = [*items, *children.flat_map(&:listed_items)]
15
+ end
17
16
 
18
- # The sections in order of their most important page, and the pages that belong to no section.
19
- Outline = Data.define(:sections, :loose) do
20
- def all_sections = sections.flat_map { |section| [section, *section.children] }
17
+ # The sections in order of their most important page, and the pages that belong to no section.
18
+ Outline = Data.define(:sections, :loose) do
19
+ def all_sections = sections.flat_map { |section| [section, *section.children] }
21
20
 
22
- def items = [*sections.flat_map(&:all_items), *loose]
21
+ def items = [*sections.flat_map(&:all_items), *loose]
23
22
 
24
- def listed = [*sections.flat_map(&:listed_items), *loose]
25
- end
23
+ def listed = [*sections.flat_map(&:listed_items), *loose]
24
+ end
26
25
 
27
- # Groups pages by the start of their address. The first part of the path makes a section and the second a
28
- # subsection, and a group needs at least two pages to get a heading of its own. Nothing here asks a model:
29
- # the addresses of a site already say what belongs together.
30
- class Grouper
31
- MIN_GROUP = 2
32
- Row = Data.define(:facts, :rank, :segments)
33
-
34
- def self.call(entries) = new(entries).call
35
-
36
- # entries: Facts, most important first.
37
- def initialize(entries)
38
- rows = entries.each_with_index.map do |facts, index|
39
- Row.new(facts:, rank: index, segments: segments_of(facts.path))
40
- end
41
- @rows = rows.reject { |row| row.segments.empty? }.uniq(&:segments)
42
- @counters = Hash.new(0)
26
+ # Groups pages by the start of their address. The first part of the path makes a section and the second a
27
+ # subsection, and a group needs at least two pages to get a heading of its own. Nothing here asks a model:
28
+ # the addresses of a site already say what belongs together.
29
+ class Grouper
30
+ MIN_GROUP = 2
31
+ Row = Data.define(:facts, :rank, :segments)
32
+
33
+ def self.call(entries) = new(entries).call
34
+
35
+ # entries: Facts, most important first.
36
+ def initialize(entries)
37
+ rows = entries.each_with_index.map do |facts, index|
38
+ Row.new(facts:, rank: index, segments: segments_of(facts.path))
43
39
  end
40
+ @rows = rows.reject { |row| row.segments.empty? }.uniq(&:segments)
41
+ @counters = Hash.new(0)
42
+ end
44
43
 
45
- def call
46
- sections = []
47
- loose = []
48
- @rows.group_by { |row| row.segments.first }.each do |slug, group|
49
- group.size >= MIN_GROUP ? sections << top(slug, group) : loose.concat(group)
50
- end
51
- Outline.new(sections:, loose: loose.map { |row| item(row) })
44
+ def call
45
+ sections = []
46
+ loose = []
47
+ @rows.group_by { |row| row.segments.first }.each do |slug, group|
48
+ group.size >= MIN_GROUP ? sections << top(slug, group) : loose.concat(group)
52
49
  end
50
+ Outline.new(sections:, loose: loose.map { |row| item(row) })
51
+ end
53
52
 
54
- private
53
+ private
55
54
 
56
- def segments_of(path) = path.split("/").reject(&:empty?)
55
+ def segments_of(path) = path.split("/").reject(&:empty?)
57
56
 
58
- def next_id(prefix) = "#{prefix}#{@counters[prefix] += 1}"
57
+ def next_id(prefix) = "#{prefix}#{@counters[prefix] += 1}"
59
58
 
60
- def item(row) = Item.new(id: next_id("p"), facts: row.facts)
59
+ def item(row) = Item.new(id: next_id("p"), facts: row.facts)
61
60
 
62
- def split_hub(rows, depth)
63
- hub = rows.find { |row| row.segments.size == depth }
64
- [hub, rows - [hub]]
65
- end
61
+ def split_hub(rows, depth)
62
+ hub = rows.find { |row| row.segments.size == depth }
63
+ [hub, rows - [hub]]
64
+ end
66
65
 
67
- def top(slug, group)
68
- hub, rest = split_hub(group, 1)
69
- nested, flat = subgroups(rest)
70
- id = next_id("s")
71
- Section.new(id:, slug:, hub: (item(hub) if hub), items: items_of(flat.flat_map(&:last)),
72
- children: nested.map { |name, rows| leaf(name, rows) })
73
- end
66
+ def top(slug, group)
67
+ hub, rest = split_hub(group, 1)
68
+ nested, flat = subgroups(rest)
69
+ id = next_id("s")
70
+ Section.new(id:, slug:, hub: (item(hub) if hub), items: items_of(flat.flat_map(&:last)),
71
+ children: nested.map { |name, rows| leaf(name, rows) })
72
+ end
74
73
 
75
- # The pages below a section: the groups that get a heading of their own, and the single pages that do not.
76
- def subgroups(rows)
77
- rows.group_by { |row| row.segments[1] }.partition { |_name, group| group.size >= MIN_GROUP }
78
- end
74
+ # The pages below a section: the groups that get a heading of their own, and the single pages that do not.
75
+ def subgroups(rows)
76
+ rows.group_by { |row| row.segments[1] }.partition { |_name, group| group.size >= MIN_GROUP }
77
+ end
79
78
 
80
- def items_of(rows) = rows.sort_by(&:rank).map { |row| item(row) }
79
+ def items_of(rows) = rows.sort_by(&:rank).map { |row| item(row) }
81
80
 
82
- def leaf(slug, rows)
83
- hub, rest = split_hub(rows, 2)
84
- id = next_id("s")
85
- hub_item = hub && item(hub)
86
- Section.new(id:, slug:, hub: hub_item, items: rest.sort_by(&:rank).map { |row| item(row) }, children: [])
87
- end
81
+ def leaf(slug, rows)
82
+ hub, rest = split_hub(rows, 2)
83
+ id = next_id("s")
84
+ hub_item = hub && item(hub)
85
+ Section.new(id:, slug:, hub: hub_item, items: rest.sort_by(&:rank).map { |row| item(row) }, children: [])
88
86
  end
89
87
  end
90
88
  end
@@ -2,52 +2,50 @@
2
2
 
3
3
  require "nokogiri"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Generate
8
- # The text of a page as the model gets it: its paragraphs, whole and in order, outside the header, the
9
- # navigation and the footer. Nothing is cut inside a paragraph. With a budget, paragraphs are taken from the
10
- # start while they fit, and the first one is always taken, however long it is. cut says whether some were left.
11
- module Passage
12
- Body = Data.define(:paragraphs, :cut)
13
-
14
- MIN_PARAGRAPH = 40
15
- # Characters of text handed over for a page of each tier. It keeps the request small and says nothing about
16
- # how much the model may write. A page of a lower tier, or one the analysis has not ranked, gets the smallest.
17
- BUDGETS = { 1 => 4_000, 2 => 2_500, 3 => 1_500 }.freeze
18
- DEFAULT_BUDGET = 800
19
- MAIN = "main, [role=main]"
20
- OUTSIDE_CHROME = "not(ancestor::header or ancestor::nav or ancestor::footer or ancestor::aside or " \
21
- "ancestor::*[@role='banner' or @role='navigation' or @role='contentinfo'])"
22
-
23
- module_function
24
-
25
- def budget(tier) = BUDGETS.fetch(tier, DEFAULT_BUDGET)
26
-
27
- # budget: characters to hand over, or nil for all of the text.
28
- def call(html, budget: nil)
29
- paragraphs = paragraphs_of(html)
30
- return Body.new(paragraphs:, cut: false) unless budget
31
-
32
- taken = []
33
- used = 0
34
- paragraphs.each do |paragraph|
35
- break if !taken.empty? && used + paragraph.length > budget
36
-
37
- taken << paragraph
38
- used += paragraph.length
39
- end
40
- Body.new(paragraphs: taken, cut: taken.size < paragraphs.size)
5
+ module Yuno::Proofreader
6
+ module Generate
7
+ # The text of a page as the model gets it: its paragraphs, whole and in order, outside the header, the
8
+ # navigation and the footer. Nothing is cut inside a paragraph. With a budget, paragraphs are taken from the
9
+ # start while they fit, and the first one is always taken, however long it is. cut says whether some were left.
10
+ module Passage
11
+ Body = Data.define(:paragraphs, :cut)
12
+
13
+ MIN_PARAGRAPH = 40
14
+ # Characters of text handed over for a page of each tier. It keeps the request small and says nothing about
15
+ # how much the model may write. A page of a lower tier, or one the analysis has not ranked, gets the smallest.
16
+ BUDGETS = { 1 => 4_000, 2 => 2_500, 3 => 1_500 }.freeze
17
+ DEFAULT_BUDGET = 800
18
+ MAIN = "main, [role=main]"
19
+ OUTSIDE_CHROME = "not(ancestor::header or ancestor::nav or ancestor::footer or ancestor::aside or " \
20
+ "ancestor::*[@role='banner' or @role='navigation' or @role='contentinfo'])"
21
+
22
+ module_function
23
+
24
+ def budget(tier) = BUDGETS.fetch(tier, DEFAULT_BUDGET)
25
+
26
+ # budget: characters to hand over, or nil for all of the text.
27
+ def call(html, budget: nil)
28
+ paragraphs = paragraphs_of(html)
29
+ return Body.new(paragraphs:, cut: false) unless budget
30
+
31
+ taken = []
32
+ used = 0
33
+ paragraphs.each do |paragraph|
34
+ break if !taken.empty? && used + paragraph.length > budget
35
+
36
+ taken << paragraph
37
+ used += paragraph.length
41
38
  end
39
+ Body.new(paragraphs: taken, cut: taken.size < paragraphs.size)
40
+ end
42
41
 
43
- def paragraphs_of(html)
44
- doc = Nokogiri::HTML(html.to_s)
45
- scope = doc.at_css(MAIN) || doc.at_css("body")
46
- return [] unless scope
42
+ def paragraphs_of(html)
43
+ doc = Nokogiri::HTML(html.to_s)
44
+ scope = doc.at_css(MAIN) || doc.at_css("body")
45
+ return [] unless scope
47
46
 
48
- texts = scope.xpath(".//p[#{OUTSIDE_CHROME}]").map { |node| Findings::Text.collapse(node.text) }
49
- texts.select { |text| text.length >= MIN_PARAGRAPH }.uniq
50
- end
47
+ texts = scope.xpath(".//p[#{OUTSIDE_CHROME}]").map { |node| Findings::Text.collapse(node.text) }
48
+ texts.select { |text| text.length >= MIN_PARAGRAPH }.uniq
51
49
  end
52
50
  end
53
51
  end