-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
148 lines (116 loc) · 3.31 KB
/
functions.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
#Author: Goldie Segol
import bs4 as bs
import requests
import urllib3
import xlwt
import lxml
import re
from dynamicArray import*
def isVowel(char):
return char.lower() in 'aeiouāā̆ēẹ̄ē̆īōō̆ūǣ'
def removeVowels(str):
result = ''
for i in range(len(str)):
if((isVowel(str[i]))==False): result = result + str[i]
return result
def printArr(arr):
for i in range(0, len(arr)):
print(arr[i])
def trimArr(tempArr, headWord):
formArr = DynamicArray()
for i in range(len(tempArr)):
foo = tempArr[i]
comp = foo[0]
if(comp == headWord[0]):
formArr.append(foo)
elif((headWord[0] == 'c') | (headWord[0] == 'k')):
if((comp == 'c') | (comp == 'k')):
formArr.append(foo)
elif(foo[:2] == 'i)'):
formArr.append(foo[2:])
return formArr
def stringToArr(str):
result = DynamicArray()
result2 = DynamicArray()
temp = ''
for i in range(len(str)):
if((str[i] != ' ') & (str[i] != ',') & (str[i] != '.') & (str[i] != ';') & (str[i] != '(')):
temp = temp + str[i]
elif(str[i] == ' '):
result.append(temp)
temp = ''
for i in range(len(result)):
if(len(result[i])>0):
result2.append(result[i])
return result2
def consonantsRatio(str, headWord):
str1 = removeVowels(str)
str2 = removeVowels(headWord)
set1 = set()
set2 = set()
for i in range(len(str1)):
set1.add(str1[i])
for i in range(len(str2)):
set2.add(str2[i])
intLength = len(set2.intersection(set1))
result = intLength/len(str2)
return result
def main(url):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
http = urllib3.PoolManager()
response = http.request('GET', url)
soup = bs.BeautifulSoup(response.data, "lxml")
forms = soup.find("p").findNext("p").get_text()
headWord = soup.find("strong").get_text()
print()
print("HEADWORD: " + headWord)
print("FORMS: " + forms)
formArr = trimArr(stringToArr(forms), headWord)
citations = ''
for paragraph in soup.find_all('blockquote'):
citations = citations + ' ' + paragraph.get_text()
citationsArr = DynamicArray()
start = ':'
end = '.'
add = False
foo = ''
#filter out citations
for i in range(len(citations)-1):
if(citations[i] == start):
add = True
if((citations[i] == end) & (citations[i+1] == end)):
foo = foo + citations[i]
elif((citations[i] == end) & (citations[i-1] !=end)):
add = False
citationsArr.append(foo)
foo = ''
elif(add):
foo = foo + citations[i]
wordArr = DynamicArray()
#filter each sentence down to target words
for i in range(len(citationsArr)):
#full sentence
sent = citationsArr[i]
sentArr = stringToArr(sent)
for j in range(len(sentArr)):
word = sentArr[j]
if(word[0] == headWord[0]):
wordArr.append(word)
elif((headWord[0] == 'c') | (headWord[0] == 'k')):
if((word[0] == 'c') | (word[0] == 'k')):
wordArr.append(word)
elif((headWord[0] == 'f') | (headWord[0] == 'v')):
if((word[0] == 'f') | (word[0] == 'v')):
wordArr.append(word)
finalArr = DynamicArray()
for i in range(len(wordArr)):
if((consonantsRatio((wordArr[i]),headWord))>.4):
finalArr.append(wordArr[i])
return finalArr
def returnHeadWord(url):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
http = urllib3.PoolManager()
response = http.request('GET', url)
soup = bs.BeautifulSoup(response.data, "lxml")
headWord = soup.find("strong").get_text()
return headWord