This commit is contained in:
Ben Harris committed 2018-10-09 02:50:10 -04:00
1 parent a8a5de36f2
commit 58753c7234
38 files changed
+4549 -2523

No files matched your search

+69 -40
View File
@@ -1,50 +1,79 @@
import urllib
from bs4 import BeautifulSoup
import random
def get_philosophy(word, max_steps=20):
step_words = [word]
steps = 0
url = 'https://en.wikipedia.org/wiki/%s' % word
while steps < max_steps:
soup = BeautifulSoup(urllib.urlopen(url).read(), 'html.parser')
title = soup.find('h1', id='firstHeading')
content = soup.find('div', id='mw-content-text')
if not content:
break
item = [item for item in content.find_all('a') if not item.get('class') and not item.get('target') and item.get('title') and not 'Wikipedia:' in item.get('title') and not 'Category:' in item.get('title') and not 'Help:' in item.get('title') and not 'Portal:' in item.get('title') and not 'Special:' in item.get('title') and not 'Talk:' in item.get('title') and not 'Template:' in item.get('title') and not 'File:' in item.get('title') and 'Edit section:' not in item.get('title') and 'Commons:' not in item.get('title') and not item.get('title') in step_words][0]
step_words.append(item.get('title'))
#print item.get('title') + "\n"
url = 'https://en.wikipedia.org%s' % item.get('href')
steps += 1
return step_words
def get_philosophy(word, max_steps=20):
step_words = [word]
steps = 0
url = "https://en.wikipedia.org/wiki/%s" % word
while steps < max_steps:
soup = BeautifulSoup(urllib.urlopen(url).read(), "html.parser")
title = soup.find("h1", id="firstHeading")
content = soup.find("div", id="mw-content-text")
if not content:
break
item = [
item
for item in content.find_all("a")
if not item.get("class")
and not item.get("target")
and item.get("title")
and not "Wikipedia:" in item.get("title")
and not "Category:" in item.get("title")
and not "Help:" in item.get("title")
and not "Portal:" in item.get("title")
and not "Special:" in item.get("title")
and not "Talk:" in item.get("title")
and not "Template:" in item.get("title")
and not "File:" in item.get("title")
and "Edit section:" not in item.get("title")
and "Commons:" not in item.get("title")
and not item.get("title") in step_words
][0]
step_words.append(item.get("title"))
# print item.get('title') + "\n"
url = "https://en.wikipedia.org%s" % item.get("href")
steps += 1
return step_words
def containsAny(str, set):
return 1 in [c in str for c in set]
def get_philosophy_lower(word, max_steps=20):
step_words = [word]
steps = 0
url = 'https://en.wikipedia.org/wiki/%s' % word
while steps < max_steps:
soup = BeautifulSoup(urllib.urlopen(url).read(), 'html.parser')
title = soup.find('h1', id='firstHeading')
content = soup.find('div', id='mw-content-text')
if not content:
break
links = [item for item in content.find_all('a') if not item.get('class') and item.text and item.text[0].islower() and not containsAny(item.text, ':()') and item.get('title') and not containsAny(item.get('title'), ':()') and not item.get('title') in step_words]
if not links:
step_words.append("(dead end)")
break
item = links[0] #grab the first good link item
#print "Checking %s %s" % (item.get('title'), item.text)
step_words.append(item.get('title'))
if item.get('title') == 'Philosophy':
break
#print item.get('title') + "\n"
url = 'https://en.wikipedia.org%s' % item.get('href')
steps += 1
return step_words
step_words = [word]
steps = 0
url = "https://en.wikipedia.org/wiki/%s" % word
while steps < max_steps:
soup = BeautifulSoup(urllib.urlopen(url).read(), "html.parser")
title = soup.find("h1", id="firstHeading")
content = soup.find("div", id="mw-content-text")
if not content:
break
links = [
item
for item in content.find_all("a")
if not item.get("class")
and item.text
and item.text[0].islower()
and not containsAny(item.text, ":()")
and item.get("title")
and not containsAny(item.get("title"), ":()")
and not item.get("title") in step_words
]
if not links:
step_words.append("(dead end)")
break
item = links[0] # grab the first good link item
# print "Checking %s %s" % (item.get('title'), item.text)
step_words.append(item.get("title"))
if item.get("title") == "Philosophy":
break
# print item.get('title') + "\n"
url = "https://en.wikipedia.org%s" % item.get("href")
steps += 1
return step_words