本文整理汇总了Python中wikipedia.search函数的典型用法代码示例。如果您正苦于以下问题:Python search函数的具体用法?Python search怎么用?Python search使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了search函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict_movie_filter
def predict_movie_filter(movie, year):
"""Predict movie title and return that title."""
my_searches = set(wikipedia.search('%s (%i film)' % (movie, year)) +
wikipedia.search('%s (film)' % movie))
#list Comprehesions: [expression for item in list if conditional]
searches = [element for element in list(my_searches)
if (movie.lower() in element.lower()) &
('List of accolades' not in element)]
#if list of searches (searches) is empty, return None, otherwise
# convert everyhting to lower case
if len(searches) == 0:
return None
else:
searches = [search.lower() for search in searches]
#return movie titles in preference of:
# 1. movie (year film)
# 2. movie (film)
# 3. movie
#otherwise: return searches[0]
#note: must movie.lower because wikipedia package returns lower
lmovie = movie.lower()
first = '%s (%i film)' % (lmovie, year)
second = '%s (film)' % lmovie
if first in searches:
return '%s (%i film)' % (movie, year)
if second in searches:
return '%s (film' % movie
if lmovie in searches:
return movie
return searches[0]
示例2: info
def info():
query = "USA"
#query = None
query = query if query is not None else default_query
print("Searching Wikipedia")
results = wikipedia.search(query, results=5)
if results is None:
results = [wikipedia.search(default_query, results=3, suggestion=True)]
print("Found searches: {}".format(results))
wiki_page = random.choice(results)
wiki = wikipedia.page(wiki_page, auto_suggest=True, redirect=True)
print("Loading wikipedia page: " + wiki_page)
html = u"""<center><b>{}</b></center>
<hr>
<center>{}</center>
<br>
{}"""
images = wiki.images
random.shuffle(images)
images_html = ["<img src={} width='200px'/>".format(image)
for image in images[:10]]
images_html = "".join(images_html)
#html = html.format(wiki.title, wiki.html())
html = html.format(wiki.title, images_html, wiki.summary)
return html
示例3: search
def search(self, args):
"""::search::
used to search for a certain word.
example:
1. search wikipedia -> return words refer to 'wikipedia'
2. search wiki pedia -> return words refer to 'wiki' & 'pedia'
3. search wiki pedia 10 -> return 10 of the results refer to 'wiki'
& 'pedia' """
res, key_words = [], []
num = 0
if len(args) < 1:
raise AssertionError(colored("function search needs at least one argument!",
"red", attrs=["bold"]))
# there may be more than 2 arguments, for example: search wiki pedia 10
elif len(args) >= 2:
try:
num = int(args[-1])
except ValueError:
raise AssertionError("num should be a decimal number")
res = wikipedia.search(args[0: len(args) - 1], results=num)
key_words = args[0: len(args) - 1]
else:
res = wikipedia.search(args[0])
key_words = args[0]
self.w_print(res, "white", key_words, "green")
示例4: wiki_search
def wiki_search(option, opt_str, value, parser):
"""
used to search for a certain word.
"""
res = []
key_words = []
length = len(parser.rargs)
if length < 1:
parser.error(colored("option -S needs at least one argument!", "red", attrs=["bold"]))
# there may be more than 2 arguments, for example: wiki -S wiki pedia 10
elif length >= 2:
try:
global num
num = int(parser.rargs[-1])
except ValueError:
parser.error("num should be a decimal number")
exit(1)
res = search(parser.rargs[0 : length - 1], results=num)
key_words = parser.rargs[0 : length - 1]
else:
res = search(parser.rargs[0])
key_words = parser.rargs[0]
w_print(res, "white", key_words, "green")
return parser.rargs[0]
示例5: get_text
def get_text(self):
try:
# do a wikipedia search for the topic
topic_results = wikipedia.search(self.topic)
# pick one of the results and grab the content
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
# DO IT MORE
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + self.uncertain
except wikipedia.exceptions.PageError as e:
self.content += self.topic + self.unknown
# if there are more than one word in the topic try to get some more results with the first and last word
if len(self.topic.split()) > 1:
try:
# get more results with less of the topic for some ambiguity
topic_results = wikipedia.search(self.topic.split()[:1])
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + self.uncertain
except wikipedia.exceptions.PageError as e:
self.content += self.topic + self.unknown
try:
# get even more with the second half of the topic for wierd results maybe
topic_results = wikipedia.search(self.topic.split()[-1:])
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + self.uncertain
except wikipedia.exceptions.PageError as e:
self.content += self.topic + self.unknown
try:
# do a wikipedia search for the topic
topic_results = wikipedia.search(self.topic[:len(self.topic) / 2])
# pick one of the results and grab the self.content
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + self.uncertain
except wikipedia.exceptions.PageError as e:
self.content += self.topic + self.unknown
return self.content.capitalize()
示例6: search_page
def search_page(searchterm):
# Assume the user knows the specific page, it will be first result
try:
result = str(search(searchterm)[0])
except:
# Transform unicode to ascii
searchterm = convert_unicode(searchterm)
result = search(searchterm)[0]
return page(result)
示例7: get_text
def get_text(self):
try:
# do a wikipedia search for the topic
topic_results = wikipedia.search(self.topic)
# pick one of the results and grab the content
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
# DO IT MORE
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + ' can mean many things but to me it is'
except wikipedia.exceptions.PageError as e:
self.content += self.topic + ' is sometimes hard to find'
# if there are more than one word in the topic try to get some more results with the first and last word
if len(self.topic.split()) > 1:
try:
# get more results with less of the topic for some ambiguity
topic_results = wikipedia.search(self.topic.split()[:1])
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + ' can mean many things but to me it is'
except wikipedia.exceptions.PageError as e:
self.content += self.topic + ' is sometimes hard to find'
try:
# get even more with the second half of the topic for wierd results maybe
topic_results = wikipedia.search(self.topic.split()[-1:])
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
if more_content not in self.content:
self.content += more_content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + ' can mean many things but to me it is'
except wikipedia.exceptions.PageError as e:
self.content += self.topic + ' is sometimes hard to find'
try:
# do a wikipedia search for the topic
topic_results = wikipedia.search(self.topic[:len(self.topic) / 2])
# pick one of the results and grab the self.content
self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content
except wikipedia.exceptions.DisambiguationError as e:
self.content += self.topic + ' can mean many things but to me it is'
except wikipedia.exceptions.PageError as e:
self.content += self.topic + ' is sometimes hard to find'
return self.content
示例8: clean_leader
def clean_leader(data):
pope = wikipedia.search('current pope')
data = data.replace('incumbent pope', pope[0])
data = re.sub(r'\[.+?\]\s?', '', data)
data = re.sub(r'\(.+?\)\)s?', '', data).strip()
if '{{' in data and '|' in data:
# print data
try:
data = data.split('|')[1]+' and '+data.split('|')[2].replace('}}', '').strip()
except IndexError:
try:
if '{{' in data and '|' in data:
data = data.split('|')[1].replace('}}', '').strip()
except IndexError:
print data+'cacca22'
pass
if 'http' in data:
data = data.split('http')[0].strip()
if 'holds' in data:
data = data.split('holds')[0].strip()
# if 'current' in data:
if 'current' in data and 'pope' not in data or 'unbulleted' in data:
missing = wikipedia.search(data.replace('{{', '').replace('}}', '').replace('current', '').replace('unbulleted list', 'president of switzerland').replace('\n', '').strip())
try:
missing = wikipedia.page(missing[0])
missing = missing.html()
missing = BeautifulSoup(missing)
for b in missing.find_all('b'):
if 'incumbent' in b.get_text().lower():
missing_president = b.find('a').get_text()
data = data.replace(data, missing_president)
except IndexError:
print data+'cacca3'
pass
except AttributeError:
# print data+'cacca5'
pass
if '|' in data:
data = data.split('|')[0].strip()
if 'party' in data or 'congress' in data or 'front' in data or 'rally' in data:
data = data.split('(')[0].strip()
if ' of ' in data:
data = data.split(' of ')[0].strip()
data = re.sub(' ', ' ', data).replace('{{', '').replace('}}', '').strip().replace('(n)', '').replace(' ', ' ').replace('(independent ', '')
if 'born' not in data:
data = re.sub('([0-9])', '', data).replace(u"\u2013", '').replace('()', '').strip()
return data
开发者ID:StefanoLoi,项目名称:http---gitrepo.jeeves.ask.info-cgi-bin-cgit-sa-ke,代码行数:52,代码来源:world_leaders.py
示例9: getWiki
def getWiki(activity):
pattern = re.compile("\\b(of|the|in|for|at|check|find|how|how\'s|is|tell|me|check|out|about|wiki|wikipedia|summarize)\\W", re.I)
string = re.sub(pattern, "", activity)
if "summarize" in activity or "summary" in activity:
result = wikipedia.summary(string[len('summarize'):], sentences = 2)
elif "wiki" in activity or "wikipedia" in activity:
result = wikipedia.summary(activity[len('wiki'):])
else:
try:
result = wikipedia.search(string,results=10,suggestion=False)
except Exception, e:
result = wikipedia.search(string,results=10,suggestion=True)
示例10: searchwiki
def searchwiki(question):
key = wikipedia.search(question)
app.logger.info(repr("searching wikipedia for" + str(question)))
if key:
try:
answer = wikipedia.summary(key[0],sentences=1)
except Exception:
m = wikipedia.page(wikipedia.search(key[0]))
answer = wikipedia.summary(m.title,sentences=1)
else:
answer = False
return answer
示例11: ne
def ne(self, ne):
"""
Returns Wikipedia article ids related to the given ne,
downloading them if necessary
"""
ne = ne.lower()
d = self.db.nes.find_one({'_id': ne})
if d is not None:
return d # already processed
# Not found -> download it
try:
search = wikipedia.search(ne)
except wikipedia.exceptions.WikipediaException:
search = []
pages = []
for title in search:
found = False
d = self.article(title=title)
if d is None:
continue
pages.append(d['_id'])
d = {'_id': ne, 'articles': pages}
self.db.nes.insert_one(d)
return d
示例12: searchWiki
def searchWiki(page):
wikipedia.set_lang("fr")
link = ''
try:
# p = wikipedia.page(page)
# link = p.url
propos = wikipedia.search(page,results=5,suggestion=False)
for choice in propos:
if choice.encode('utf-8') == page.encode('utf-8'):
p = wikipedia.page(page)
link = p.url
break
elif page in choice:
#TODO
print 'There is a proposition containing the keyWord '
print choice
else:
try:
wikipedia.page(page,redirect=False,auto_suggest=False)
except wikipedia.exceptions.RedirectError:
p = wikipedia.page(page)
link = p.url
break
except:
link =''
except:
link = ""
return link#.encode('utf-8')
示例13: page
def page(title=None, pageid=None, auto_suggest=True, redirect=True, preload=False):
'''
Get a WikipediaPage object for the page with title `title` or the pageid
`pageid` (mutually exclusive).
Keyword arguments:
* title - the title of the page to load
* pageid - the numeric pageid of the page to load
* auto_suggest - let Wikipedia find a valid page title for the query
* redirect - allow redirection without raising RedirectError
* preload - load content, summary, images, references, and links during initialization
'''
if title is not None:
if auto_suggest:
results, suggestion = search(title, results=1, suggestion=True)
try:
title = suggestion or results[0]
except IndexError:
# if there is no suggestion or search results, the page doesn't
# exist
raise PageError(title)
return WikipediaPage(title, redirect=redirect, preload=preload)
elif pageid is not None:
return WikipediaPage(pageid=pageid, preload=preload)
else:
raise ValueError("Either a title or a pageid must be specified")
示例14: wiki_func
def wiki_func(paras, infos):
"""中文维基百科查询"""
wikipedia.set_lang("zh")
candidates = wikipedia.search(paras)
if len(candidates) <= 0:
return {
'text': 'not found',
}
else:
summary = None
for keyword in candidates:
try:
summary = wikipedia.summary(keyword, sentences=1)
break
except Exception: # 可能发生歧义异常,见 wikipedia 文档
continue
if summary:
answer = decode_to_unicode(summary) + \
u'\n候选关键词: %s' % u', '.join(candidates)
return {
'text': answer,
}
else:
return {
'text': 'not found',
}
示例15: index
def index(request):
start = current_milli_time()
print 'START: ', start
auth = tweepy.OAuthHandler(
settings.TWITTER_CONSUMER_KEY,
settings.TWITTER_CONSUMER_SECRET
)
auth.set_access_token(
settings.TWITTER_ACCESS_TOKEN,
settings.TWITTER_ACCESS_SECRET_TOKEN
)
twitter = tweepy.API(auth)
if 'query' in request.GET:
query = request.GET['query']
twitterdata = twitter.search(query, 'en')
print 'AFTER TWITTER CALL: ', current_milli_time() - start
wikidata = wikipedia.search(query)
print 'AFTER WIKIPEDIA CALL: ', current_milli_time() - start
else:
twitterdata = None
wikidata = None
return render(request, 'index.html', {'twitter': twitterdata, 'wikipedia': wikidata
})