nakiwiki 0.0.0

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 (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/lib/nakiwiki.rb +49 -0
  4. data/nakiwiki.gemspec +14 -0
  5. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3a57e37e37b29c1134064d261c32e09d3b403380ae145075df4c80a96662bc25
4
+ data.tar.gz: 48f66fbabba73c9ff002b19046b0c267d290806a346fca179e373301e492edb6
5
+ SHA512:
6
+ metadata.gz: e29ff633867e3f82e162f054c4df9c46f0776798fa45ae78c72eab202d8a4edf2d54241c92f1d5a91f36a917e8ce13927a7569c89e10fb9cd491b40656e031f0
7
+ data.tar.gz: 2324a3bb442999bacc363a50b58db235e6d29c28418a0d93780ec9a5c42ded1ba3894fc9c362bc879e9e486da535728a23b9db71d28e3f65ca4b1ae2441b7154
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Victor Maslov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/lib/nakiwiki.rb ADDED
@@ -0,0 +1,49 @@
1
+ module NakiWiki
2
+ require "infoboxer"
3
+
4
+ def self.get_rescue_nil wiki, query, &block
5
+ wiki.get query, &block
6
+ rescue ::RuntimeError # Infoboxer raises RuntimeError
7
+ end
8
+ private_class_method :get_rescue_nil
9
+
10
+ def self.page_summary page
11
+ page.paragraphs.map do |par|
12
+ par if par.children.any?{ |_| _.is_a?(::Infoboxer::Tree::Text) && !_.to_s.empty? }
13
+ end.find(&:itself).text.strip.gsub(/\n+/, " ")
14
+ end
15
+ private_class_method :page_summary
16
+
17
+ def self.wiki query, lang, not_found, see_also
18
+ # https://en.wikipedia.org/wiki/List_of_Wikipedias
19
+ wikipedia = ::Infoboxer.wikipedia lang
20
+ return not_found unless page = get_rescue_nil(wikipedia, query){ |_| _.prop :pageterms } ||
21
+ wikipedia.search(query, limit: 1){ |_| _.prop(:pageterms) }.first ||
22
+ wikipedia.search(query, limit: 1){ |_| _.prop(:pageterms).what(:text) }.first
23
+ "#{
24
+ if about = page.templates(name: "About").first
25
+ _, _, alt, *_ = about.unwrap.map(&:text)
26
+ # TODO: propose the (disambiguation) page
27
+ "(#{see_also}: #{alt}) " if alt
28
+ end
29
+ }#{
30
+ label, description = page.source.fetch("terms").values_at("label", "description")
31
+ unless description
32
+ if short = page.templates(name: "Short description").first
33
+ short.unwrap.text
34
+ else
35
+ page_summary page
36
+ end
37
+ else
38
+ fail unless 1 == description.size
39
+ fail unless 1 == label&.size
40
+ if {"ru" => "страница значений в проекте Викимедиа"}[lang] == description[0] && !page.lookup(:Template, name: "Неоднозначность").empty?
41
+ "что вы имели в виду? #{page.sections.map{ |_| [_.heading.to_s, _.lookup(:Wikilink).map(&:name)] }.to_h.keep_if{ |_, __| !__.empty? }.flat_map{ |k, v| v.map(&:inspect) }.uniq.join ", " }"
42
+ else
43
+ [label, description].join(" -- ")
44
+ end
45
+ end
46
+ }"
47
+ end
48
+
49
+ end
data/nakiwiki.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "nakiwiki"
3
+ spec.version = "0.0.0"
4
+ spec.summary = "Wikipedia functionality suitable for chat bots"
5
+
6
+ spec.author = "Victor Maslov aka Nakilon"
7
+ spec.email = "nakilon@gmail.com"
8
+ spec.license = "MIT"
9
+ spec.metadata = {"source_code_uri" => "https://github.com/nakilon/nakiwiki"}
10
+
11
+ spec.add_dependency "infoboxer"
12
+
13
+ spec.files = %w{ LICENSE nakiwiki.gemspec lib/nakiwiki.rb }
14
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakiwiki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Maslov aka Nakilon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: infoboxer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: nakilon@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - lib/nakiwiki.rb
35
+ - nakiwiki.gemspec
36
+ homepage:
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ source_code_uri: https://github.com/nakilon/nakiwiki
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.1.6
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Wikipedia functionality suitable for chat bots
60
+ test_files: []