lita-wikipedia 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/lita/handlers/wikipedia.rb +18 -6
- data/lita-wikipedia.gemspec +1 -1
- data/spec/lita/handlers/wikipedia_spec.rb +24 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d7a4b0b1a9e02e1cd16d926732544161225143c
|
4
|
+
data.tar.gz: 7c58e5072db8990a2bbf1a992c6aa0747544a102
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f193331786e2a15fdf77aa2a8a9c0ca4dd3555a5c14025d36d113514cb9122aa496460acab713109b7cbe0f7a28b9c8ea6624a9c8f76055fc356ca9da242ef55
|
7
|
+
data.tar.gz: b3542a293b98b39fca8dd8fe6ce30b25ee0433739f4dc278064857e7ab3d20eb8221b31459af380bc0226b2a8abffca0663c096bc499ef422c037b3697a558db
|
@@ -1,7 +1,21 @@
|
|
1
|
-
require "cgi"
|
2
1
|
require "json"
|
3
2
|
require "open-uri"
|
4
3
|
|
4
|
+
def disambiguate(term)
|
5
|
+
url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info|links|pageprops&format=json&exintro=&explaintext=&inprop=url&ppprop=disambiguation&titles=#{term}&redirects="
|
6
|
+
result = JSON.parse(open(URI.parse(URI.encode(url.strip))).read)
|
7
|
+
page = result['query']['pages'].first[1]
|
8
|
+
extract = page['extract'].split("\n").first
|
9
|
+
if page.fetch('pageprops', {}).has_key? 'disambiguation'
|
10
|
+
links = page['links'].map {
|
11
|
+
|x| x['title'] if not x['title'].downcase.include? 'disambiguation'}
|
12
|
+
links = links.select {|x| x != nil}
|
13
|
+
return disambiguate(links.sample)
|
14
|
+
end
|
15
|
+
url = page['fullurl']
|
16
|
+
[extract, url]
|
17
|
+
end
|
18
|
+
|
5
19
|
module Lita
|
6
20
|
module Handlers
|
7
21
|
class Wikipedia < Handler
|
@@ -9,11 +23,9 @@ module Lita
|
|
9
23
|
help: { t("help.wikipedia_key") => t("help.wikipedia_value")})
|
10
24
|
|
11
25
|
def wikipedia(response)
|
12
|
-
url =
|
13
|
-
|
14
|
-
|
15
|
-
response.reply(page['extract'].split("\n").first)
|
16
|
-
response.reply("Source: #{page['fullurl']}")
|
26
|
+
extract, url = disambiguate(response.matches.first.first)
|
27
|
+
response.reply(extract)
|
28
|
+
response.reply("Source: #{url}")
|
17
29
|
end
|
18
30
|
end
|
19
31
|
|
data/lita-wikipedia.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "lita-wikipedia"
|
3
|
-
spec.version = "0.0.
|
3
|
+
spec.version = "0.0.2"
|
4
4
|
spec.authors = ["Tristan Chong"]
|
5
5
|
spec.email = ["ong@tristaneuan.ch"]
|
6
6
|
spec.description = %q{A Lita handler that returns a requested Wikipedia article.}
|
@@ -10,5 +10,29 @@ describe Lita::Handlers::Wikipedia, lita_handler: true do
|
|
10
10
|
expect(replies[0]).to match 'Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.'
|
11
11
|
expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/Ruby_(programming_language)'
|
12
12
|
end
|
13
|
+
|
14
|
+
it "responds with a random matching article when disambiguation occurs" do
|
15
|
+
send_command "wiki 12th man"
|
16
|
+
responses = [
|
17
|
+
"Source: http://en.wikipedia.org/wiki/The_Twelfth_Man",
|
18
|
+
"Source: http://en.wikipedia.org/wiki/The_12th_Man_(album)",
|
19
|
+
"Source: http://en.wikipedia.org/wiki/12th_man_(football)",
|
20
|
+
"Source: http://en.wikipedia.org/wiki/Glossary_of_cricket_terms"
|
21
|
+
]
|
22
|
+
expect(responses).to include(replies[1])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#disambiguate" do
|
27
|
+
it "resolves disambiguation by returning a random matching article" do
|
28
|
+
extract, url = disambiguate('12th man')
|
29
|
+
urls = [
|
30
|
+
"http://en.wikipedia.org/wiki/The_Twelfth_Man",
|
31
|
+
"http://en.wikipedia.org/wiki/The_12th_Man_(album)",
|
32
|
+
"http://en.wikipedia.org/wiki/12th_man_(football)",
|
33
|
+
"http://en.wikipedia.org/wiki/Glossary_of_cricket_terms"
|
34
|
+
]
|
35
|
+
expect(urls).to include(url)
|
36
|
+
end
|
13
37
|
end
|
14
38
|
end
|