本文整理汇总了Python中wikipedia.random函数的典型用法代码示例。如果您正苦于以下问题:Python random函数的具体用法?Python random怎么用?Python random使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: search
def search(self):
""" Takes value from text fields and uses the wiki algorithm"""
self.text.delete(0.0, END)
# Assigns start to random if empty
x = self.start.get()
y = self.end.get()
if x == '':
while True:
try:
x = wikipedia.random(1)
self.text.insert(0.0, 'Random start is %s.\n' % x)
break
except UnicodeEncodeError:
pass
# Assigns end to random if empty
if y == '':
while True:
try:
y = wikipedia.random(1)
self.text.insert(0.0, 'Random end is %s.\n' % y)
break
except UnicodeEncodeError:
pass
self.text.insert(0.0, 'Start is %s.\nTarget is %s.\n' % (x,y))
开发者ID:anthonylu123,项目名称:WikiGame,代码行数:25,代码来源:WikiGame.py
示例2: get_random_articles_v2
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: ')
开发者ID:Mikerah,项目名称:WikiSummary,代码行数:12,代码来源:wiki_summary.py
示例3: getPage
def getPage():
rpage = wikipedia.page(wikipedia.random(1))
while len(rpage.content) <= minlength:
try:
#TODO: Exception for lists
rpage = wikipedia.page(wikipedia.random(1))
except wikipedia.exceptions.DisambiguationError as e:
print 'ERROR'
rpage = wikipedia.page(e.options[0])
else:
print 'MISS'
rpage = wikipedia.page(wikipedia.random(1))
return rpage
开发者ID:axani,项目名称:PythonLab,代码行数:13,代码来源:WikiSampleText.py
示例4: get_random_page
def get_random_page():
# https://wikipedia.readthedocs.org/en/latest/quickstart.html
random_title = wikipedia.random(pages=1)
random_page = None
while not random_page:
try:
random_page = wikipedia.page(title=random_title)
except wikipedia.PageError:
random_title = wikipedia.random(pages=1)
random_page = None
except wikipedia.DisambiguationError as e:
random_title = random.choice(e.options)
random_page = None
return random_page
开发者ID:andrewtatham,项目名称:twitterpibot,代码行数:14,代码来源:wikipediahelper.py
示例5: wikipedia_random
def wikipedia_random(self, n):
print "Downloading pages from Wikipedia. This may take a moment..."
f = open(self.inf, "w")
pages = []
# get a list of random wikipedia pages.
# wikipedia.random can only get 10 at a time, so call it so many times
for x in range(n/10):
pages += (wikipedia.random(10))
if n%10 > 0:
pages += (wikipedia.random(n%10))
log.info("Found {} random wikipedia pages".format(len(pages)))
# get the summary info from all the pages.
# this is a lot of pages, so can take some time
for page in pages:
info = None
try:
log.debug("Getting page "+page)
info = wikipedia.page(page)
# if the request threw a disambiguation error, try to get the first suggestion
# if that files, just give up on that request. we didn't want it that much anyway
except wikipedia.exceptions.DisambiguationError as e:
try:
log.debug("Getting page "+page)
info = wikipedia.page(e.options[0])
except wikipedia.exceptions.DisambiguationError as e:
continue
except (wikipedia.exceptions.HTTPTimeoutError, wikipedia.exceptions.PageError):
continue
except (wikipedia.exceptions.HTTPTimeoutError, wikipedia.exceptions.PageError):
continue
# save the summary. if it exceeds 127 characters, try to truncate by the first sentence end.
# if that fails, write it anyway, but it will give warning when it generates the config
s = info.summary.encode("ascii", "ignore").replace("\n", " ")
if len(s) < 127:
log.info("Wrote line with no edits")
f.write(s + "\n")
else:
if s[:127].rfind(". ") != -1:
f.write(s[:s[:127].rfind(". ")+1] + "\n")
log.info("Summary too long, but was truncated")
else:
f.write(s + "\n")
log.info("Summary too long and couldn't truncate")
f.close()
开发者ID:GuitaringEgg,项目名称:Dotannoy,代码行数:48,代码来源:Annoy.py
示例6: __init__
def __init__(self):
try:
self.page = wikipedia.page((wikipedia.random(pages=1)))
self.name = self.page.title
self.summary = self.page.summary
except wikipedia.DisambiguationError:
Wiki_page()
开发者ID:DennisMelamed,项目名称:WikiArticleAskerGui,代码行数:7,代码来源:Wiki+Article+Asker.py
示例7: randomWiki
def randomWiki():
'''
This function gives you a list of n number of random articles
Choose any article.
'''
number=input("No: of Random Pages : ")
lst=wk.random(number)
for i in enumerate(lst):
print(i)
try:
key=input("Enter the number : ")
assert key>=0 and key<number
except AssertionError:
key=input("Please enter corresponding article number : ")
page=wk.page(lst[key])
url=page.url
#originalTitle=page.original_title
pageId=page.pageid
#references=page.references
title=page.title
#soup=BeautifulSoup(page.content,'lxml')
pageLength=input('''Wiki Page Type : 1.Full 2.Summary : ''')
if pageLength==1:
soup=fullPage(page)
print(soup)
else:
print(title)
print("Page Id = ",pageId)
print(page.summary)
print("Page Link = ",url)
#print "References : ",references
pass
开发者ID:geekcomputers,项目名称:Python,代码行数:34,代码来源:WikipediaModule.py
示例8: get_random_wiki
def get_random_wiki():
random_wiki = wikipedia.random()
random_wiki_page = wikipedia.page(random_wiki)
summary = wikipedia.summary(random_wiki, sentences=1)
return random_wiki_page.title, random_wiki_page.url, summary
开发者ID:moreisee,项目名称:wiki-of-the-day-twitter,代码行数:7,代码来源:wotd.py
示例9: getArt
def getArt():
randArt = wikipedia.random(pages = 1)
userInput = input("Would you like to read about %s? " %randArt)
if userInput.lower() == "yes" or userInput.lower() == "y":
launchArt(randArt)
if userInput.lower() == "no" or userInput.lower() == "n":
getArt()
开发者ID:SonOfLava,项目名称:random-wiki,代码行数:7,代码来源:ranwik.py
示例10: getRandArticle
def getRandArticle():
# getting directory for easy use
myPath = os.path.expanduser("~/documents/contexter/")
# how many articles to get
for x in xrange(0, 1):
# Get an article, if DisambiguationError, then try again
try:
article = wikipedia.random()
except wikipedia.exceptions.DisambiguationError:
print "Disambiguation, retrying..."
article = wikipedia.random()
except wikipedia.exceptions.PageError:
print "PageError, retrying..."
article = wikipedia.random()
pageObj = wikipedia.page(article)
text = pageObj.content
pageUrl = pageObj.url
# Writing the text and to the created file, encoded utf8
f = open(article + ".txt", "w")
f.write(text.encode('utf-8'))
f.close()
# Getting all lines
f = open(article + ".txt", "r")
tempText = f.readlines()
f.close()
# Decoding all lines for cleanup of file
for i, v in enumerate(tempText):
tempText[i] = tempText[i].decode('utf-8')
# Clean up the file, the file has '==' characters denoting sections
f = open(article + ".txt", "w")
for line in tempText:
if ('==' not in line):
f.write(line.encode('utf-8'))
f.close()
# move file into raw text folder
shutil.move(myPath + "scripts/" + article + ".txt", myPath + "/raw text/" + article + ".txt")
articleLoc = myPath + "/raw text/" + article + ".txt"
return [articleLoc, pageUrl]
开发者ID:jpierz,项目名称:contexter,代码行数:47,代码来源:wikiscanner.py
示例11: fact
def fact():
pageTitle = wikipedia.random();
page = wikipedia.summary(pageTitle, sentences=1);
if ("is" in page or "was" in page) and page[0] != '<':
page = re.sub("[\(\[].*?[\)\]]", "", page)
return page
else:
return fact()
开发者ID:benjaminmisell,项目名称:unicorn,代码行数:8,代码来源:wsgi.py
示例12: makePage
def makePage():
# get new random page
global page
text.delete(1.0, END)
try:
page = wikipedia.page((wikipedia.random(pages=1)))
except wikipedia.DisambiguationError:
makePage()
开发者ID:DennisMelamed,项目名称:WikiArticleAskerGui,代码行数:8,代码来源:Wiki+Article+Asker+with+GUI.py
示例13: get_title
def get_title( progress_count ):
"""Obtain title of a random wikipedia article, checking it's not a disambiguation."""
# Print progress
sys.stdout.write( "%d " %progress_count )
sys.stdout.flush()
# Get title, try to load page. Pick another if there's a disambiguation error.
while True:
try:
title = wikipedia.random()
wikipedia.page( title )
break
except ( wikipedia.exceptions.DisambiguationError, wikipedia.exceptions.PageError ):
title = wikipedia.random()
return title
开发者ID:jbaker92,项目名称:LDA-wiki,代码行数:17,代码来源:titles.py
示例14: read_random_from_topic
def read_random_from_topic(topic = None):
"""Reads from a topic (or random topic if not specified), and returns
an internal content type that includes a random sentence"""
if not topic:
topic = wikipedia.random()
content = wikipedia.page(topic)
return to_system_format(content)
开发者ID:adamsar,项目名称:flask-mail-generator,代码行数:8,代码来源:wiki_api.py
示例15: get_random_articles_v1
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))
开发者ID:Mikerah,项目名称:WikiSummary,代码行数:18,代码来源:wiki_summary.py
示例16: test_random_pages
def test_random_pages(self):
print("------ random pages test ------------")
pages = wikipedia.random(2)
print(pages)
article1 = Article(pages[0], repo=DataDict())
article2 = Article(pages[1], op=op_backlinks, repo=DataDict())
run_test(article1, article2)
print('=========================================')
开发者ID:billtsay,项目名称:cursornet,代码行数:9,代码来源:test.py
示例17: random_page
def random_page():
# Fetches a random page from wikipedia
try:
page = wikipedia.page(wikipedia.random())
print(page.links)
show_links(page)
# Deals with wiki disambiguation error
except wikipedia.exceptions.DisambiguationError:
random_page()
开发者ID:pla7a,项目名称:GettingToPhilosophy,代码行数:10,代码来源:g2p.py
示例18: random
def random():
try:
query = wikipedia.random(pages=1)
input = wikipedia.WikipediaPage(title=query).summary
title = wikipedia.WikipediaPage(title=query).title
image = wikipedia.WikipediaPage(title=query).images[0]
client = Algorithmia.client('Simple simR+{}'.format(api_key))
algo = client.algo('nlp/Summarizer/0.1.2')
contents ={
'image': image,
'title': title,
'summary': algo.pipe(input),
'link': 'https://en.wikipedia.org/wiki/{}'.format(wikipedia.random(pages=1))
}
except:
return json.dumps({
'msg': "Sorry, we couldn't find a Wikipedia article matching your search."
})
return json.dumps(contents)
开发者ID:cherylafitz,项目名称:react_flask,代码行数:19,代码来源:app.py
示例19: do_til
def do_til(self, arg):
if check_core('DISCIPLINE') is not True:
req = urllib2.Request("http://hipsteripsum.me/?paras=1&type=hipster-centric", headers={'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36', "Accept" : "text/html"})
contents = urllib2.urlopen(req).read()
soup = BeautifulSoup(contents, "html5lib")
for div in soup.find_all(id='content'):
text = div.contents[1].string
elif check_core("CURIOSITY") is True and check_core('EMPATHY') is True:
wikipedia.set_lang("en")
print "english"
text = wikipedia.summary(wikipedia.random(pages=1))
elif check_core("CURIOSITY") is not True:
wikipedia.set_lang("simple")
print "simple"
text = wikipedia.summary(wikipedia.random(pages=1))
elif check_core("CURIOSITY") is True and check_core('EMPATHY') is not True:
req = urllib2.Request("http://www.elsewhere.org/pomo/", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(req).read()
soup = BeautifulSoup(contents, "html5lib")
h3counter = 0
text = ''
for div in soup.find_all('div', class_='storycontent'):
for element in div.contents:
if element.name == 'h1':
text = text + element.string + '\n'
elif element.name == 'h3' and h3counter < 1:
text = text + element.string + '\n'
h3counter = h3counter + 1
elif element.name == 'h3' and h3counter == 1:
break
elif element.name == 'p' and element.string is not None:
text = text + element.string + '\n'
question = "Is that interesting " + dev_names[random.randint(0, len(dev_names)-1)] + "?"
say(text, agnes_core.voice)
if check_core("CURIOSITY") is not True:
say(question, agnes_core.voice)
开发者ID:piscosour,项目名称:agnes,代码行数:43,代码来源:agnes.py
示例20: get_random_page
def get_random_page(nsent):
"""
Get sentences from random page (up to nsent sentences)
"""
page = wiki.page(wiki.random())
sentences = split_content(page.content)
sentences = filter_noise(sentences)
ct = len(sentences)
takenct = min(nsent, ct)
takensent = sentences[:takenct]
return (takensent, takenct)
开发者ID:josepablocam,项目名称:snlp_project,代码行数:11,代码来源:collect_noemotion.py
注:本文中的wikipedia.random函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论