-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatin.py
52 lines (46 loc) · 1.61 KB
/
Latin.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
import requests
import sys
from bs4 import BeautifulSoup
def getTrans(word):
URL = "https://www.online-latin-dictionary.com/latin-english-dictionary.php?parola=" + word
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
#results = soup.find(id='wrapper')
#print(soup.div)
search = soup.find_all('span', class_='english')
res = ''
if (search == []):
search = soup.find_all('a')[20]
page = requests.get("https://www.online-latin-dictionary.com/" + search.get('href'))
soup = BeautifulSoup(page.content, 'html.parser')
search = soup.find_all('span', class_='english')
if search != []:
i = 0
search = search[0]
while search.text[i] != ',':
i +=1
if i == len(search.text):
break
res = search.text[0:i]
return res
text = sys.argv[1]
s = open (text, 'r+')
t = open (text + '(trans)', 'a')
for line in s:
wstart = 0
newline = ''
test = False
for i in range(len(line)):
if (line[i] == ' ' or line[i] == ',' or line[i] == '.' or line[i] == ':' or line[i] == ';' or line[i] == '?' or line[i] == '!' or line[i] == '\n') and not test:
newline += line[wstart : i]
word = line[wstart : i]
newline += ' (' + getTrans(word) + ')' + line[i]
wstart = i+2 if i < len(line)-1 and line[i+1] == ' ' else i+1
i += 1
if i < len(line) and line[i] == '\n':
newline += line[i]
test = True
else:
test = False
continue
t.write(newline)