Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wikipedia package #112

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bridges/python/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ requests = "==2.21.0"
pytube = "==9.5.0"
tinydb = "==3.9.0"
beautifulsoup4 = "==4.7.1"
wikipedia = "*"

[dev-packages]
13 changes: 9 additions & 4 deletions bridges/python/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/wikipedia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Wikipedia Package

The Wikipedia package allows your personal assistant to gather pieces of information from Wikipedia.

#### Requirements
- wikipedia

#### Usage

1. Duplicate the file `packages/wikipedia/config/config.sample.json` and rename it `config.json`.
2. Set your language and other settings.
3. This package uses wikipedia. To install it: from inside leon directory `cd bridges/python`
4. `pipenv install wikipedia`
5. Done!

```
(en-US)
- "Search on Wikipedia GitHub"

```

#### Options
- `lang`: Choose your language. Default: `en`
- `sentences`: Number of sentences to report. No greater than 10.

#### Links

- [MediaWiki API](https://www.mediawiki.org/wiki/API:Main_page)
- [Wikipedia GitHub](https://github.com/goldsmith/Wikipedia)
- [Wikipedia Docs](https://wikipedia.readthedocs.io/en/latest/)
Empty file.
7 changes: 7 additions & 0 deletions packages/wikipedia/config/config.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"wikipedia": {
"lang": "en",
"sentences": 5,
"options": {}
}
}
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions packages/wikipedia/data/answers/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"wikipedia": {
"acquiring": [
"Right away!",
"One second please.",
"Just a second...",
"Browsing Wikipedia right now..."
],
"summary": [
"%summary%"
],
"disambiguation_error": [
"Disambiguation error."
],
"page_error": [
"Your request does not match any page."
],
"no_entities_error": [
"Your request was not well formatted. Please, retry."
],
"connection_error": [
"I'm having issuse reaching the server."
]
}
}
Empty file.
56 changes: 56 additions & 0 deletions packages/wikipedia/data/expressions/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"wikipedia": {
"summary": {
"expressions": [
"Search on Wikipedia",
"Search on Wikipedia for",
"Search for ",
"Search",
"Look for",
"On wikipedia look for"
],
"entities": [
{
"type": "trim",
"name": "page",
"conditions": [
{
"type": "after_last",
"from": "wikipedia"
},
{
"type": "between",
"from": "wikipedia",
"to": "?"
},
{
"type": "after_last",
"from": "for"
},
{
"type": "between",
"from": "for",
"to": "?"
},
{
"type": "between",
"from": "search",
"to": "on"
},
{
"type": "after_last",
"from": "search"
}
]
}
]
},
"random": {
"expressions": [
"Search a random page on Wikipedia",
"Search a random Wikipedia page",
"Give me a random page on Wikipedia"
]
}
}
}
1 change: 1 addition & 0 deletions packages/wikipedia/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
80 changes: 80 additions & 0 deletions packages/wikipedia/wikipedia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import wikipedia
from wikipedia.exceptions import DisambiguationError, PageError

import utils

wikipedia.set_lang(utils.config("lang"))


def summary(string, entities):
"""
Get the summary of the requested Wikipedia page.
"""

for entity in entities:
if entity["entity"] == "page":
utils.output(
"inter",
"acquiring",
utils.translate("acquiring")
)

try:
summary = wikipedia.summary(
entity["sourceText"],
sentences=utils.config("sentences")
)
return utils.output(
"end",
"summary",
utils.translate(
"summary",
{"summary": summary}
)
)

except DisambiguationError as pages:
# TODO: ask which page to visit
return utils.output(
"end",
"disambiguation_error",
utils.translate("disambiguation_error")
)

except PageError:
return utils.output(
"end",
"page_error",
utils.translate("page_error")
)

return utils.output(
"end",
"no_entities_error",
utils.translate("no_entities_error")
)


def random(string, entities):
"""
Get the summary of a random Wikipedia page.
"""
utils.output(
"inter",
"acquiring",
utils.translate("acquiring")
)

summary = wikipedia.summary(title=wikipedia.random())

return utils.output(
"end",
"summary",
utils.translate(
"summary",
{"summary": summary}
)
)