-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikidefine.py
65 lines (45 loc) · 1.48 KB
/
wikidefine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List, Dict, Optional
import random
import subprocess
from subprocess import signal
import os
from time import sleep
import numpy as np
import math
import wikipedia
from automaton import VoiceControlledAutomaton, State, Exit, DEBUG
class WikiBotState(State):
pass
class WikiBot(VoiceControlledAutomaton):
def __init__(self, **kwargs):
super().__init__(name="wikibot", **kwargs)
self.keywords = [
"define",
]
self.state_keywords = {
WikiBotState.enter: self.keywords,
WikiBotState.exit: [],
}
self.SideEffectTransitionMatrix = {
WikiBotState.enter: self.define,
WikiBotState.exit: self.exit,
}
def define(self, query: str, must_understand=False) -> Optional[WikiBotState]:
query = query.replace("define", "", 1).strip()
print("Wikibot query:", query)
# via https://alanhylands.com/how-to-web-scrape-wikipedia-python-urllib-beautiful-soup-pandas/
err = ""
results = wikipedia.search(query)
if not results:
err = "Couldnt find an article matching "+ query
self.exit(err)
try:
abstract = wikipedia.page(results[0]).summary
except Exception as e:
err = str(e)
self.exit(err)
print(abstract)
self.speak(abstract)
return WikiBotState.exit
def _exit_effects(self, text: str):
self.speak(text)