-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
180 lines (157 loc) · 4.88 KB
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from re import search
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pronouncing
import random
import nltk
# nltk.download("brown")
# nltk.download("wordnet")
from nltk.corpus import brown
from nltk.corpus import wordnet as wn
def get_results():
url = "https://www.bbc.com/news/topics/c256e4q7z8zt/geese"
page = urlopen(url)
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all("span", {"class": "lx-stream-post__header-text"})
return results
def get_results_cows():
url = "https://www.bbc.com/news/topics/c2d2m3e1p96t/cattle"
page = urlopen(url)
html = page.read().decode("utf-8")
# print(html)
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all("span", {"class": "lx-stream-post__header-text"})
# print(results)
return results
def get_results_chickens():
url = "https://www.bbc.com/news/topics/cqlyx9q32g8t/chickens"
page = urlopen(url)
html = page.read().decode("utf-8")
# print(html)
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all("span", {"class": "lx-stream-post__header-text"})
# print(results)
return results
def get_random_result(results):
return [random.choice(results)]
sents = []
goose_words = ["goose", "geese", "Goose", "Geese", "gosling", "Gosling", "goslings", "Goslings", "goose's", "geese's", "Goose's", "Geese's"]
def contains_goose_word(words):
for w in goose_words:
if w in words:
return True
return False
def find_first_adj(words):
for w in words:
if wn.synsets(w):
synsets = wn.synsets(w)
for s in synsets:
if s.pos() == "a" or s.pos() == "s":
return w
return None
def find_first_noun(words):
for w in words:
if wn.synsets(w):
synsets = wn.synsets(w)
for s in synsets:
if s.pos() == "n":
return w
return None
def goosify(sent):
l = sent.split()
print(l)
for i in range(len(l)):
if l[i] == "cow's":
l[i] = "goose's"
elif l[i] == "Cow's":
l[i] = "Goose's"
elif l[i] == "cow":
l[i] = "goose"
elif l[i] == "Cow":
l[i] = "Goose"
elif l[i] == "Cows":
l[i] = "Geese"
elif l[i] == "cows":
l[i] = "geese"
# chicks
elif l[i] == "chickens" or l[i] == "chicks":
l[i] = "geese"
elif l[i] == "Chickens" or l[i] == "Chicks":
l[i] = "Geese"
elif l[i] == "chicken":
l[i] = "goose"
elif l[i] == "hens":
l[i] = "geese"
elif l[i] == "Chicken":
l[i] = "Goose"
return l
def results_to_words(results):
l = []
for r in results:
r_words = r.text.split(" ")
l.append(r_words)
return l
def cow_results_to_words(results):
l = []
for r in results:
print(r.text)
# txt = r.text.replace('''):
r_words = goosify(r.text)
l.append(r_words)
return l
def find_sents_and_adjs(r_words_list):
sents = []
adjs = []
nouns = []
for r_words in r_words_list:
if contains_goose_word(r_words):
sents.append(" ".join(r_words))
# print(r_words[-1])
rhymes = pronouncing.rhymes(r_words[-1])
freqs = nltk.FreqDist([w.lower() for w in brown.words()])
# sort wordlist by word frequency
wordlist_sorted = sorted(rhymes, key=lambda x: freqs[x.lower()], reverse=True)
# print the sorted list
# print(r_words[-1])
# print(wordlist_sorted)
adj = find_first_adj(wordlist_sorted)
n = find_first_noun(wordlist_sorted)
adjs.append(adj)
nouns.append(n)
# print(sents)
# print(adjs)
# print(nouns)
return sents, adjs, nouns
# def goosify(sent):
# l = sent.split()
# for i in range(len(l)):
# if l[i] == "cow's":
# l[i] = "goose's"
# elif l[i] == "Cow's":
# l[i] = "Goose's"
# elif l[i] == "cow":
# l[i] = "goose"
# elif l[i] == "Cow":
# l[i] = "Goose"
# elif l[i] == "Cows":
# l[i] = "Geese"
# elif l[i] == "cows":
# l[i] = "geese"
# return l
def get_random_stem_set():
r1 = get_results()
r1 = results_to_words(r1)
r2 = get_results_cows()
r2 = cow_results_to_words(r2)
r3 = get_results_chickens()
r3 = cow_results_to_words(r3)
r = r1 + r2 + r3
result = get_random_result(r3)
sents, adjs, nouns = find_sents_and_adjs(result)
print("PRINTING RESULTS IN SCAPER")
print(sents, adjs, nouns)
if not (sents == [] or (adjs == [None] and nouns == [None])):
return sents, adjs, nouns
else:
return get_random_stem_set()