-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
144 lines (117 loc) · 3.54 KB
/
tools.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
from bs4 import BeautifulSoup
from xml.etree.ElementTree import Element, SubElement, Comment, tostring, ElementTree
from unidecode import unidecode
def clean_html(url):
# Leer archivo html
html = open(url, encoding="latin1")
soup = BeautifulSoup(html, "html.parser")
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
# borrar acentos
# text = unidecode(text)
"""
# separar simbolos de palabras
text = text.replace("(", " ( ")
text = text.replace(")", " ) ")
text = text.replace(":", " : ")
text = text.replace('"', ' " ')
text = text.replace(".", " . ")
text = text.replace(",", " , ")
text = text.replace("-", " - ")
text = text.replace("", " ")
text = text.replace("<i>", " ")
text = text.replace("</i>", " ")
text = text.replace("¿", "")
text = text.replace("?", "")
"""
# traducir codigos
text = text.replace('ñ', 'ñ')
text = text.replace('á', 'á')
text = text.replace('é', 'é')
text = text.replace('í', 'í')
text = text.replace('ó', 'ó')
text = text.replace('ú', 'ú')
text = text.replace("(", "")
text = text.replace(")", "")
text = text.replace(":", " ")
text = text.replace(";", "")
text = text.replace('"', '')
text = text.replace(".", "")
text = text.replace(",", "")
text = text.replace("-", "")
text = text.replace("", "")
text = text.replace("<i>", "")
text = text.replace("</i>", "")
text = text.replace("¿", "")
text = text.replace("?", "")
text = text.replace("/", "")
text = text.replace("<b>", "")
text = text.replace("<br>", "")
text = text.replace("!", "")
text = text.replace("¡", "")
text = text.replace("'", "")
text = text.replace(""", "")
text = text.replace(">>", "")
text = text.replace("<", "")
text = text.replace(">", "")
text = text.replace("%", "")
text = text.replace("$", "")
text = text.replace("=", "")
text = text.replace("+", "")
text = text.replace("* ", "")
return text
def crear_bases():
root = Element("base")
tree = ElementTree(root)
tree.write("bases/1998.xml")
tree.write("bases/1999.xml")
tree.write("bases/2000.xml")
tree.write("bases/2001.xml")
def add_xml(url, doc_id, year, month, day, text):
# lee xml existente, si no exite, se rompe el programa
file_name = "bases/{}.xml".format(year)
file = open(file_name, 'r')
tree = ElementTree()
tree.parse(file)
file.close()
root = tree.getroot()
doc = SubElement(root, 'document')
doc.set('url', url)
doc.set('id', str(doc_id))
year_xml = SubElement(doc, 'year')
year_xml.text = year
month_xml = SubElement(doc, 'month')
month_xml.text = month
day_xml = SubElement(doc, 'day')
day_xml.text = day
content_xml = SubElement(doc, 'content')
content_xml.text = text
tree.write(file_name, encoding="utf-8")
"""
# creacion documento xml
root = Element('base')
doc = SubElement(root,'document')
year = SubElement(doc, 'year')
#TODO modificar fecha dependiente del documento
year.text = "2000"
month = SubElement(doc, 'month')
month.text = "02"
day = SubElement(doc, 'date')
day.text = "25"
content = SubElement(doc, 'content')
content.text = text
file = open("base.xml", "w")
file.write(prettify(root))
file.close()
print(prettify(root))
# print(text)
"""