def get_random_articles_v1(number_of_articles_wanted):
"""Given the wanted number of articles returned, get random wikipedia articles"""
if number_of_articles_wanted == 1:
print(wikipedia.summary(wikipedia.random()))
else:
list_of_articles = wikipedia.random(number_of_articles_wanted)
try:
for a in list_of_articles:
article = a[:]
if ('disambiguation' in wikipedia.page(a).title) or ('it may refer to' in wikipedia.page(a).title):
list_of_articles.remove(a)
list_of_articles.append(wikipedia.random())
print(list_of_articles.index(a)+1," - "+wikipedia.summary(a))
print()
except wikipedia.exceptions.DisambiguationError:
list_of_articles.remove(article)
list_of_articles.append(wikipedia.random(article))
python类summary()的实例源码
def get_pages(query):
pages = list()
if len(query.strip()) <= 0:
raise ValueError
response = requests.get(SEARCH_URL + str(query))
soup = BeautifulSoup(markup=response.text, features="lxml")
if soup is None:
raise Exception
if "search" in str(soup.title).lower():
result_ul = soup.find(name="ul", attrs={"class": "mw-search-results"})
results_list = result_ul.find_all("li")
for li in results_list:
li_div = li.find(name="div", attrs={"class": "mw-search-result-heading"})
a = li_div.find("a")
link = "https://en.wikipedia.org" + a["href"]
heading = str(a.text)
pages.append((link, heading))
return pages
else:
return wikipedia.summary(query)
def getWikipedia(args):
__name__ = "getWikipedia"
#syntax:
#wikipedia random
#wikipedia article
try:
if args[0].lower() == "article":
return wikipedia.summary(args[1],sentences=2)
except wikipedia.exceptions.DisambiguationError as e:
return str(e)
if args[0].lower() == "random":
desiredArticleSummary = ""
while len(desiredArticleSummary) <150:
try:
desiredArticleSummary = wikipedia.summary(wikipedia.random())
except wikipedia.exceptions.DisambiguationError as e:
print("Tried; disambiguation hitch; trying again")
return desiredArticleSummary
def wikipedia_search_slow(query, lang="en", max_result=1):
import wikipedia
#wikification
query = any2unicode(query)
items = []
ret = {"query":query, "itemList":items}
wikipedia.set_lang(lang)
wikiterm = wikipedia.search(query)
#logging.info(wikiterm)
for idx, term in enumerate(wikiterm[0:max_result]):
wikipage = wikipedia.page(term)
item = {
"name": wikipage.title,
"description": wikipedia.summary(term, sentences=1),
"url": wikipage.url,
}
items.append(item)
return ret
def wikipedia(cmd, message, args):
if args:
q = ' '.join(args).lower()
try:
result = wp.summary(q)
if len(result) >= 650:
result = result[:650] + '...'
response = discord.Embed(color=0xF9F9F9)
response.add_field(name=f'?? Wikipedia: `{q.upper()}`', value=result)
except wp.PageError:
response = discord.Embed(color=0x696969, title='?? No results.')
except wp.DisambiguationError:
response = discord.Embed(color=0xBE1931, title='? Search too broad, please be more specific.')
else:
response = discord.Embed(color=0xBE1931, title='? Nothing inputted.')
await message.channel.send(None, embed=response)
def OnEnter(self, event):
input = self.txt.GetValue()
# input = input.lower()
app_id = '2V3684-LXELTTTJ9J'
try:
# wolframalpha
client = wolframalpha.Client(app_id)
res = client.query(input)
ans = next(res.results).text
print(ans)
speak(ans)
except:
# wikipedia
input = input.split(' ')
input = ' '.join(input[2:])
print(wikipedia.summary(input))
speak('Searched wikipedia for '+input)
def wikipedia_summary(msg, lang = 'en'):
try:
if lang == 'en':
wikipedia.set_lang('en')
else:
wikipedia.set_lang(lang)
url = wikipedia.page(msg).url
msg = wikipedia.summary(msg)
fliter = []
for i in msg:
if i != '\n':
fliter.append(i)
else:
break
msg = "".join(fliter)
return msg + '\n' + url
except:
return "Not Found Page or LANG"
def define_subject(speech_text):
words_of_message = speech_text.split()
words_of_message.remove('define')
cleaned_message = ' '.join(words_of_message)
try:
wiki_data = wikipedia.summary(cleaned_message, sentences=5)
regEx = re.compile(r'([^\(]*)\([^\)]*\) *(.*)')
m = regEx.match(wiki_data)
while m:
wiki_data = m.group(1) + m.group(2)
m = regEx.match(wiki_data)
wiki_data = wiki_data.replace("'", "")
tts(wiki_data)
except wikipedia.exceptions.DisambiguationError as e:
tts('Can you please be more specific? You may choose something from the following.')
print("Can you please be more specific? You may choose something from following.; {0}".format(e))
def search_engine (self, search_input=""):
try:
client = wolframalpha.Client(
app_id="23XUAT-H2875HHEEX") # this is the client App id specification for the PYSHA
results = client.query(search_input) # this searchs the input from the client side
answer = next(
results.results).text # this gets the String Answered from the Search Engine . so that the answer spoken out by Pysha
print(answer)
return answer
except:
try:
results = wikipedia.summary(search_input,
sentences=2) # this searches for the wikipedia summary input and then parse the input
print(results)
return results
except:
webbrowser.open(search_input) # this will open the web browser
return
def wikipediaAction(message):
"""Makes the appropriate calls to the wikipedia API for answer wiki queries.
Args:
message: An incoming text message
processer: Instance of NLProcessor class
Returns:
A message indicating what action was taking with the wikipedia API
"""
# tokenize input
tokens = tokenize.wordpunct_tokenize(message)
# filter stopwords, additionally, remove 'wiki' or 'wikipedia'
tokens_filtered = remove_stopwords(tokens)
tokens_filtered = [token for token in tokens_filtered if token != 'wiki' and token != 'wikipedia']
# join filtered message
message = ' '.join(tokens_filtered)
# for debugging/testing
print("(Highly) processed input: ", message)
# Get the wikipedia summary for the request
try:
summary = wikipedia.summary(message, sentences = 1)
url = wikipedia.page(message).url
answer = summary + "\nSee more here: " + url
if len(answer) > 500:
answer = answer[0:500] + "\nSee wikipedia for more..."
except:
# handle all errors
answer = "Request was not found using Wikipedia. Be more specific?"
return answer
def download_single(wiki_page_name, only_summary=False, language='en'):
"""
Download the content of a wikipedia page
:param wiki_page_name: the name
:param only_summary:
:return:
"""
wikipedia.set_lang(language)
if only_summary:
page = wikipedia.page(wiki_page_name)
return page.content
else:
return wikipedia.summary(wiki_page_name)
def summary(query, sentences=0, chars=0):
"""Returns a plain text summary from the query's page."""
try:
return wikipedia.summary(query, sentences, chars)
except wikipedia.exceptions.PageError:
return "No page matches, try another item."
except wikipedia.exceptions.DisambiguationError as error:
return error.options[:5]
def wiki(query):
#wiki search and return top 3 sentences
para = wikipedia.summary(query, sentences=3)
return para
def shorten_news(url, n = 5):
from bs4 import BeautifulSoup as bs
from summarizer import FrequencySummarizer as fs
response = _req.get(url)
if not response.ok:
return False
page = response.content
soup = bs(page, "lxml")
summary = fs().summarize("\n".join([x.text for x in soup.findAll("p") if len(x.text.split()) > 1]), n)
summary.insert(0, soup.title.text)
return ' '.join(summary)
def get_gkg(query):
try:
s = _wk.summary(query, sentences = 5)
for x in _findall("\(.*\)", s):
s = s.replace(x, "")
return s
except _wk.DisambiguationError, e:
return False
def help_message():
"""
:return:
"""
help_text = """
**RERO**
*The multipurpose utility bot for Discord.*
Commands
```ruby
. ?names : List of detected name changes
?pm [on, off, 24/7] : Sends you PM if you get mentioned
?8ball question : Answers a question 8 ball style
?sr subreddit : Grab random image from the subreddit
?anime name : Grab a anime from MAL
?manga name : Grab a manga from MAL
?ud query : Urban Dictionary definition
?wiki query : Wikipedia summary of querry
?giphy query : Gif matching querry
?xkcd [number] : Random xkcd or specify a number
?weather city : Get weather information
For a complete list of functions (*too many to send by PM*),
Want Rero in your server too?
<https://discordapp.com/oauth2/authorize?client_id=314796406948757504&scope=bot&permissions=8>
Visit RERO's Server:
https://discord.gg/nSHt53W
"""
return help_text
```
def wikipedia_parser(ctx, message):
"""
Returns a wikipedia definition
:param ctx:
:param message:
:return:
"""
try:
querry = message.content[6:]
search = wikipedia.summary(str(querry), sentences=4)
await ctx.send_message(message.channel, "```{}```".format(search))
except wikipedia.DisambiguationError as e:
length = len(e.options)
if length == 1:
await ctx.send_message(message.channel, "Did you mean? `{}`".format(e.options[0]))
elif length == 2:
await ctx.send_message(message.channel, "Did you mean? `{}` or `{}`"
.format(e.options[0], e.options[1]))
else:
await ctx.send_message(message.channel,
"Disambiguation in you query. It can mean `{}`, `{}` and {} more."
.format(e.options[0], e.options[1], str(length)))
except wikipedia.PageError:
await ctx.send_message(message.channel, "No pages matched your querry :cry:")
except wikipedia.HTTPTimeoutError:
await ctx.send_message(message.channel, "Hey there, slow down you searches for a bit!")
except wikipedia.RedirectError:
await ctx.send_message(message.channel,
"Error: page title unexpectedly resolves to a redirect. "
"Please re-check your query.")
except wikipedia.WikipediaException:
await ctx.send_message(message.channel, "Error: The search parameter must be set.")
def whatIs(query,sessionID="general"):
try:
return wikipedia.summary(query)
except:
for newquery in wikipedia.search(query):
try:
return wikipedia.summary(newquery)
except:
pass
return about(query)
def whoIs(query,sessionID="general"):
try:
return wikipedia.summary(query)
except:
for newquery in wikipedia.search(query):
try:
return wikipedia.summary(newquery)
except:
pass
return "I don't know about "+query
def wikipedia_query(message, match):
query = match.group("query")
print("QUERY: " + query)
result = wikipedia.summary(str(query), sentences=1)
return TextMessageProtocolEntity(result, to=message.getFrom())
def do_activate(self, args, argv):
print(wikipedia.summary(' '.join(args), sentences=2))
def do_activate(self, args, argv):
wikipedia.set_lang("de")
print(wikipedia.summary(' '.join(args), sentences=2))
def randwiki(irc, source, msgtarget, args):
rand = wikipedia.random(pages=1)
url = wikipedia.page(rand).url
irc.msg(msgtarget, "Random Article: {} - \x1d{}\x1d".format(rand, url))
irc.msg(msgtarget, wikipedia.summary(rand, sentences=2, chars=250, auto_suggest=True))
def wiki(irc, source, msgtarget, args):
try:
url = wikipedia.page(args).url
page = wikipedia.summary(wikipedia.search(args)[0], sentences=2, auto_suggest=True)
irc.msg(msgtarget, page)
irc.msg(msgtarget, "More at \x1d"+url)
except wikipedia.exceptions.DisambiguationError as e:
bot_commands["wiki"](irc, source, msgtarget, e.options[0])
except wikipedia.exceptions.PageError:
irc.msg(msgtarget, "No page could be found")
def help_message():
"""
:return:
"""
help_text = """
**LAPZBOT**
*The multipurpose utility bot for Discord.*
Commands
```ruby
. ?names : List of detected name changes
?pm [on, off, 24/7] : Sends you PM if you get mentioned
?8ball question : Answers a question 8 ball style
?sr subreddit : Grab random image from the subreddit
?anime name : Grab a anime from MAL
?manga name : Grab a manga from MAL
?ud query : Urban Dictionary definition
?wiki query : Wikipedia summary of querry
?giphy query : Gif matching querry
?xkcd [number] : Random xkcd or specify a number
?weather city : Get weather information
For a complete list of functions (*too many to send by PM*), visit
https://lapoozza.me/commands-list/
Want L.A.P.Z.B.O.T in your server too?
https://lapoozza.me/add
Visit LAPZBOT's Server:
https://lapoozza.me/discord
"""
return help_text
```
def wikipedia_parser(ctx, message):
"""
Returns a wikipedia definition
:param ctx:
:param message:
:return:
"""
try:
querry = message.content[6:]
search = wikipedia.summary(str(querry), sentences=4)
await ctx.send_message(message.channel, "```{}```".format(search))
except wikipedia.DisambiguationError as e:
length = len(e.options)
if length == 1:
await ctx.send_message(message.channel, "Did you mean? `{}`".format(e.options[0]))
elif length == 2:
await ctx.send_message(message.channel, "Did you mean? `{}` or `{}`"
.format(e.options[0], e.options[1]))
else:
await ctx.send_message(message.channel,
"Disambiguation in you query. It can mean `{}`, `{}` and {} more."
.format(e.options[0], e.options[1], str(length)))
except wikipedia.PageError:
await ctx.send_message(message.channel, "No pages matched your querry :cry:")
except wikipedia.HTTPTimeoutError:
await ctx.send_message(message.channel, "Hey there, slow down you searches for a bit!")
except wikipedia.RedirectError:
await ctx.send_message(message.channel,
"Error: page title unexpectedly resolves to a redirect. "
"Please re-check your query.")
except wikipedia.WikipediaException:
await ctx.send_message(message.channel, "Error: The search parameter must be set.")
def whoIs(query,sessionID="general"):
try:
return wikipedia.summary(query)
except:
for newquery in wikipedia.search(query):
try:
return wikipedia.summary(newquery)
except:
pass
return "I don't know about "+query
def whoIs(query,sessionID="general"):
try:
return wikipedia.summary(query)
except:
for newquery in wikipedia.search(query):
try:
return wikipedia.summary(newquery)
except:
pass
return "I don't know about "+query
def get_random_articles_v2():
"""Retrieves random articles until the user types 'stop' """
ans = input('Press enter to continue or stop to stop: ')
while ans != 'stop':
try:
print(wikipedia.summary(wikipedia.random()))
print()
ans = input('Press enter to continue or stop to stop: ')
except wikipedia.exceptions.DisambiguationError:
print(wikipedia.summary(wikipedia.random()))
print()
ans = input('Press enter to continue or stop to stop: ')
def get_wanted_article(search_term):
"""Given a search term, find the associated article"""
search_term = " ".join(search_term)
try:
list_of_associated_articles = wikipedia.search(search_term)
wanted_article = list_of_associated_articles[0]
print(wikipedia.summary(wanted_article))
except wikipedia.exceptions.DisambiguationError as disambiguation:
sys.exit("Unfortunately your request has led to a disambiguation, "
"please refine your search further:\n{}".format(disambiguation))