Python wikipedia 模块,summary() 实例源码

我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用wikipedia.summary()

项目:WikiSummary    作者:Mikerah    | 项目源码 | 文件源码
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))
项目:AcronymExpansion    作者:adityathakker    | 项目源码 | 文件源码
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)
项目:Seams-Bot    作者:keithstellyes    | 项目源码 | 文件源码
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
项目:cdata    作者:cnschema    | 项目源码 | 文件源码
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
项目:apex-sigma-plugins    作者:lu-ci    | 项目源码 | 文件源码
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)
项目:Automation-Bots    作者:ab-anand    | 项目源码 | 文件源码
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)
项目:eatradish_haoyebot    作者:eatradish    | 项目源码 | 文件源码
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"
项目:MUSTani-Robot    作者:swayam01    | 项目源码 | 文件源码
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))
项目:PYSHA    作者:shafaypro    | 项目源码 | 文件源码
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
项目:Alfred    作者:JohnGiorgi    | 项目源码 | 文件源码
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
项目:LDA-REST    作者:valentinarho    | 项目源码 | 文件源码
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)
项目:Jarvis    作者:sukeesh    | 项目源码 | 文件源码
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]
项目:raspberrypi_AI-NO-GUI    作者:Comm4nd0    | 项目源码 | 文件源码
def wiki(query):
    #wiki search and return top 3 sentences
    para = wikipedia.summary(query, sentences=3)
    return para
项目:delbot    作者:shaildeliwala    | 项目源码 | 文件源码
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)
项目:delbot    作者:shaildeliwala    | 项目源码 | 文件源码
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
项目:rerobot    作者:voqz    | 项目源码 | 文件源码
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

```

项目:rerobot    作者:voqz    | 项目源码 | 文件源码
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.")
项目:Microsoft-chatbot    作者:ahmadfaizalbh    | 项目源码 | 文件源码
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)
项目:Microsoft-chatbot    作者:ahmadfaizalbh    | 项目源码 | 文件源码
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
项目:Chat-Bot    作者:FredLoh    | 项目源码 | 文件源码
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())
项目:sarah    作者:semicode-ltd    | 项目源码 | 文件源码
def do_activate(self, args, argv):
        print(wikipedia.summary(' '.join(args), sentences=2))
项目:sarah    作者:semicode-ltd    | 项目源码 | 文件源码
def do_activate(self, args, argv):
        wikipedia.set_lang("de")
        print(wikipedia.summary(' '.join(args), sentences=2))
项目:falco    作者:nathan0    | 项目源码 | 文件源码
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))
项目:falco    作者:nathan0    | 项目源码 | 文件源码
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")
项目:Lapzbot_Beta    作者:lap00zza    | 项目源码 | 文件源码
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

```

项目:Lapzbot_Beta    作者:lap00zza    | 项目源码 | 文件源码
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.")
项目:Chatbot    作者:ahmadfaizalbh    | 项目源码 | 文件源码
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
项目:Chatbot    作者:ahmadfaizalbh    | 项目源码 | 文件源码
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
项目:WikiSummary    作者:Mikerah    | 项目源码 | 文件源码
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: ')
项目:WikiSummary    作者:Mikerah    | 项目源码 | 文件源码
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))
项目:cnschema    作者:cnschema    | 项目源码 | 文件源码
def wikify2(phrase, description=None):
    #wikification
    ret = {}
    wikiterm = wikipedia.search(phrase)
    for idx, term in enumerate(wikiterm[0:MAX_RESULT]):
        wikipage = wikipedia.page(term)
        ret["wikipedia_{}_url".format(idx)] = wikipage.url
        ret["wikipedia_{}_desc".format(idx)] = wikipedia.summary(term, sentences=1)

    return ret
项目:jasper-modules    作者:mattcurrycom    | 项目源码 | 文件源码
def sayDefinition(text):
            mic.say(mic.say(wikipedia.summary(text, sentences=2)))
项目:OrarioTreniBot    作者:MarcoBuster    | 项目源码 | 文件源码
def getWikipediaSummary(station: str):
    try:
        result = wikipedia.summary("Stazione di {station}".format(station=station))
    except PageError:
        return "Nessuna informazione aggiuntiva disponibile"
    return cleanHTML(result) + " (da Wikipedia, l'enciclopedia libera)"
项目:Onyx    作者:OnyxProject    | 项目源码 | 文件源码
def get_summary(self):
        try:
            wikipedia.set_lang(self.lang)
            summary = wikipedia.summary(self.search)
            return summary
        except:
            logger.error('Getting wiki summary error : ' + str(e))
            raise WikiException(str(e))
项目:friday-bot    作者:dhruvramdev    | 项目源码 | 文件源码
def find_wiki(msg_list):
    if len(msg_list)== 1 :
        return "Search Query Not Given"
    try :
        return wikipedia.summary(' '.join(msg_list[1:]), sentences=3)
    except wikipedia.exceptions.DisambiguationError :
        return "Please Be More Specific."
    except IndexError:
        return "Incorrect Usage /wiki <query>"
    except:
        return "Failed to Get Query :P"