Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2011-12-05 07:29:22 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2011-12-05 07:29:22 (GMT)
commite7fc1f03b3057ce32483b3e16e172519a037130c (patch)
treeaadb796c55cc27cfd65efb96b52c3abca5a91522
parent82d622a9edfc72f0b383d83382209b40c55dd77a (diff)
make_selection.py create the selection of pages to include in the wikipedia slice
-rw-r--r--blacklist.txt20
-rw-r--r--favorites.txt431
-rwxr-xr-xmake_selection.py316
3 files changed, 767 insertions, 0 deletions
diff --git a/blacklist.txt b/blacklist.txt
new file mode 100644
index 0000000..9139b2b
--- /dev/null
+++ b/blacklist.txt
@@ -0,0 +1,20 @@
+Penetración_anal
+Felación
+Orgía
+Juguete_sexual
+Sexo_anal
+Sexo_oral
+Perversión_sexual
+Parafilia
+Prostitución
+Prostituta
+Desnudo
+Fotografía_glamour
+Erotismo
+Erótica
+Sadomasoquismo
+Pene
+Incesto
+Masturbacion
+Relaciones_sexuales
+Eyaculación
diff --git a/favorites.txt b/favorites.txt
new file mode 100644
index 0000000..751036f
--- /dev/null
+++ b/favorites.txt
@@ -0,0 +1,431 @@
+Acero
+Acrópolis
+Adicción
+Adjetivo
+Adolescencia
+Adverbio
+Africa
+Agricultura
+Agua
+Ajedrez
+Alcohol
+Alcoholismo
+Alemania
+Alfabeto
+Algoritmo
+Alimentación
+Alimento
+Aluminio
+América
+América_del_Norte
+América_del_Sur
+América_Latina
+Aminoácido
+Amistad
+Amor
+Anarquismo
+Anatomía
+Anemia
+Ángulo
+Animalia
+Anorexia
+Antártida
+Antigua_Grecia
+Antiguo_Egipto
+Antropología
+Aparato_circulatorio
+Aparato_digestivo
+Aparato_reproductor_femenino
+Aparato_reproductor_masculino
+Aparato_respiratorio
+Aprendizaje
+Área
+Argentina
+Aritmética
+Arqueología
+Arquitectura
+Arte
+Arte_Visual
+Asia
+Astronomía
+Atletismo
+Átomo
+Australia
+Autismo
+Autoestima
+Automóvil
+Aves
+Avión
+Bacteria
+Baloncesto
+Balonmano
+Barco
+Barroco
+Base_de_datos
+Béisbol
+Biblia
+Biodiversidad
+Biología
+Bioma
+Biotecnología
+Bolivia
+Brasil
+Budismo
+Bulimia
+Calentamiento_global
+Calor
+Cambio_climático
+Canadá
+Cáncer
+Cáncer_de_mama
+Capitalismo
+Carbono
+Célula
+Cerebro
+Chile
+Chocolate
+Ciencia
+Ciencias_Sociales
+Cine
+Circunferencia
+Ciudad
+Clima
+Cobre
+Cocaína
+Cólera
+Colombia
+Color
+Combustible
+Comercio
+Computadora
+Comunicación
+Comunidad
+Comunismo
+Concentración
+Constitución
+Contaminación
+Continente
+Corazón
+Costa_Rica
+Cristianismo
+Cristóbal_Colón
+Cromosoma
+Cuba
+Cuento
+Cultura
+Cultura_maya
+Danza
+Debate
+Declaración_Universal_de_los_Derechos_Humanos
+Democracia
+Dengue
+Densidad
+Deporte
+Derecho
+Derechos_del_niño
+Derechos_Humanos
+Desarrollo_sostenible
+Desastre natural
+Descubrimiento_de_América
+Desierto
+Día_Mundial_de_la_Lucha_contra_el_Sida
+Diccionario
+Dinero
+Discriminación
+Dislexia
+División_(matemática)
+Eclipse
+Ecología
+Economía
+Ecosistema
+Ecuación
+Ecuador
+Edad_Media
+Educación
+Educación_física
+Efecto_invernadero
+Egipto
+Electricidad
+Electrónica
+Elipse
+Embarazo
+Emoción
+Energía
+Energía_cinética
+Energía_eléctrica
+Energía_eólica
+Energía_nuclear
+Energía_potencial
+Energía_renovable
+Energía_solar
+Escultura
+España
+Estadística
+Estado
+Estados_Unidos
+Estrella
+Ética
+Europa
+Evolución_biológica
+Evolución_humana
+Factorización
+Familia
+Feudalismo
+Filosofía
+Física
+Flor
+Fotosíntesis
+Fracción
+Fuego
+Fuerza
+Función_matemática
+Fútbol
+Género_literario
+Genética
+Geografía
+Geología
+Geometría
+Globalización
+Gobierno
+Gravedad
+Grecia
+Gripe
+Grupo_sanguíneo
+Guatemala
+Guitarra
+Haití
+Hardware
+Hidrógeno
+Hierro
+Hígado
+Historia
+Historia_universal
+Holocausto
+Homo_sapiens
+Homosexualidad
+Honduras
+Hormona
+Huella_ecológica
+Humanidades
+Humanismo
+Humor
+Identidades_trigonométricas
+Idioma_español
+Idioma_inglés
+Ilustración
+Imperio_romano
+Indumentaria
+Infecciones_de_transmisión_sexual
+Inflación
+Informática
+Ingeniería
+Internet
+Introducción
+Islam
+Judaísmo
+Juego
+Juegos_Olímpicos
+Justicia
+Las_Nuevas_Siete_Maravillas_del_Mundo_Moderno
+Lenguaje
+Lenguas_de_México
+Ley_de_Ohm
+Leyenda
+Leyes_de_Mendel
+Leyes_de_Newton
+Liberalismo
+Libertad
+Liderazgo
+Literatura
+Lluvia_ácida
+Logaritmo
+Lógica
+Luna
+Luz
+Machu_Picchu
+Magnetismo
+Mapa
+Mapa_conceptual
+Marea
+Marxismo
+Masa
+Matemática
+Materia
+Matrimonio
+Medicina
+Medición
+Medio_ambiente
+Medio_de_comunicación
+Menstruación
+Mesopotamia
+Metabolismo
+Metáfora
+Metal
+Meteorología
+Método_anticonceptivo
+Método_científico
+México
+Mezcla
+Microprocesador
+Mineral
+Mito
+Mitología
+Mitología_griega
+Mitosis
+Monopolio
+Moral
+Movimiento
+Muerte
+Multiplicación
+Música
+Natación
+Naturaleza
+Neoclasicismo
+Neoliberalismo
+Neumonía
+Novela
+Número
+Número_áureo
+Número_primo
+Nutrición
+Obesidad
+Oceanía
+Océano
+Organización_de_las_Naciones_Unidas
+Origen_de_la_vida
+Oro
+País
+Panamá
+Páncreas
+Parábola_(matemática)
+Paradigma
+Paraguay
+Perú
+Petróleo
+PH
+Placa_tectónica
+Planeta
+Plantae
+Plástico
+Platón
+Población
+Población_mundial
+Poesía
+Política
+Potencia
+Premio_Nobel
+Premio_Nobel_de_Literatura
+Preservativo
+Presión
+Presión_arterial
+Primera_Guerra_Mundial
+Producto_interior_bruto
+Prólogo
+Proteína
+Proyecto
+Psicoanálisis
+Psicología
+Publicidad
+Puerto_Rico
+Química
+Radio_(medio_de_comunicación)
+Reacción_química
+Reciclaje
+Recurso_natural
+Recursos_humanos
+Religión
+Renacimiento
+Reproducción
+República_Dominicana
+Resistencia_eléctrica
+Respeto
+Responsabilidad
+Resta
+Revolución_francesa
+Revolución_Industrial
+Revolución_mexicana
+Río_Amazonas
+Romanticismo
+Salud
+Sangre
+Segunda_Guerra_Mundial
+Selva
+Ser_vivo
+Sexo
+Sexualidad
+Sida
+Siete_maravillas_del_mundo
+Signo_de_puntuación
+Simón_Bolívar
+Síndrome_de_Asperger
+Síndrome_de_Down
+Sistema_binario
+Sistema_endocrino
+Sistema_Internacional_de_Unidades
+Sistema_nervioso
+Sistema_nervioso_central
+Sistema_operativo
+Sistema_Solar
+Socialismo
+Sociedad
+Sociología
+Software
+Software_libre
+Sol
+Solsticio
+Sonido
+Suelo
+Sueño
+Suma
+Surrealismo
+Sustantivo
+Tabaquismo
+Tabla_periódica_de_los_elementos
+Taekwondo
+Teatro
+Tecnología
+Telecomunicación
+Teléfono
+Televisión
+Temperatura
+Tenis
+Teorema_de_Pitágoras
+Teoría_del_Big_Bang
+Teotihuacan
+Termodinámica
+Terremoto
+Testigos_de_Jehová
+Tiempo
+Tierra
+Tipos_de_texto
+Trabajo_(física)
+Tradición
+Tren
+Triángulo
+Trigonometría
+Tuberculosis
+Turismo
+Unión_Europea
+Universo
+Uruguay
+Varicela
+Vector_(física)
+Velocidad
+Venezuela
+Verbo
+Vida
+VIH
+Villancico
+Vino
+Violencia
+Virus
+Virus_informático
+Vitamina
+Volcán
+Voleibol
+Volumen
+Wiki
+Wikipedia
+Wikipedia:Artículos_destacados
+World_Wide_Web
+Xenofobia
diff --git a/make_selection.py b/make_selection.py
new file mode 100755
index 0000000..b550365
--- /dev/null
+++ b/make_selection.py
@@ -0,0 +1,316 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# take a list of pages
+# select a level default = 1
+# prepare a list of links in the pages from the original list
+# create a file with the titles of all the selected pages
+# create a file with the content of all the selected pages
+
+import codecs
+import re
+from xml.sax import make_parser, handler
+import os
+from operator import itemgetter
+
+
+input_xml_file_name = './eswiki-20111112-pages-articles.xml'
+favorites_file_name = 'favorites.txt'
+blacklist_file_name = './blacklist.txt'
+
+REDIRECT_TAGS = [u'#REDIRECT', u'#REDIRECCIÓN']
+
+BLACKLISTED_NAMESPACES = ['Wikipedia:', 'MediaWiki:']
+
+TEMPLATE_NAMESPACES = ['Plantilla:']
+
+LINKS_NAMESPACES = [u'Categoría']
+
+
+class FileListReader():
+
+ def __init__(self, file_name):
+ _file = codecs.open(file_name,
+ encoding='utf-8', mode='r')
+ self.list = []
+ line = _file.readline()
+ while line:
+ self.list.append(line.strip())
+ line = _file.readline()
+
+
+class RedirectParser:
+
+ def __init__(self, file_name):
+ self.link_re = re.compile('\[\[.*?\]\]')
+ # Load redirects
+ input_redirects = codecs.open('%s.redirects' % file_name,
+ encoding='utf-8', mode='r')
+
+ line = input_redirects.readline()
+ self.redirects = {}
+ count = 0
+ while line:
+ links = links = self.link_re.findall(unicode(line))
+ if len(links) == 2:
+ self.redirects[links[0]] = links[1]
+ line = input_redirects.readline()
+ count += 1
+ print "Processing %d\r" % count,
+ input_redirects.close()
+
+
+class LinksFilter():
+
+ def __init__(self, file_name, redirects, favorites):
+ self.links = []
+ input_links = codecs.open('%s.links' % file_name,
+ encoding='utf-8', mode='r')
+ line = input_links.readline()
+ while line:
+ words = line.split()
+ if len(words) > 0:
+ page = words[0]
+ #print "Processing page %s \r" % page,
+ if page in favorites:
+ print "Adding page %s" % page
+ for n in range(1, len(words) - 1):
+ link = words[n]
+ # check if is a redirect
+ try:
+ link = redirects[link]
+ except:
+ pass
+
+ if not link in self.links and \
+ not link in favorites:
+ self.links.append(link)
+ line = input_links.readline()
+ input_links.close()
+
+
+class PagesProcessor(handler.ContentHandler):
+
+ def __init__(self, file_name, selected_pages_list, pages_blacklist,
+ redirects):
+ handler.ContentHandler.__init__(self)
+ self._page_counter = 0
+ self._page = None
+ self._output = codecs.open('%s.processed' % file_name,
+ encoding='utf-8', mode='w')
+ self._selected_pages_list = selected_pages_list
+ self._pages_blacklist = pages_blacklist
+ self._redirects = redirects
+
+ def startElement(self, name, attrs):
+ if name == "page":
+ self._page = {}
+ self._page_counter += 1
+ self._text = ""
+
+ def characters(self, content):
+ self._text = self._text + content
+
+ def _register_page(self, register):
+ register.write('\01\n')
+ register.write('%s\n' % self._title)
+ register.write('%d\n' % len(self._page))
+ register.write('\02\n')
+ register.write('%s\n' % self._page)
+ register.write('\03\n')
+
+ def endElement(self, name):
+ if name == "title":
+ self._title = self._text
+ elif name == "text":
+ self._page = self._text
+ elif name == "page":
+
+ for namespace in BLACKLISTED_NAMESPACES:
+ if unicode(self._title).startswith(namespace):
+ return
+
+ for namespace in TEMPLATE_NAMESPACES:
+ if unicode(self._title).startswith(namespace):
+ return
+
+ for tag in REDIRECT_TAGS:
+ if unicode(self._page).startswith(tag):
+ return
+
+ if self._title not in self._pages_blacklist and \
+ self._title in self._selected_pages_list:
+ print "%d Page '%s', length %d \r" % \
+ (self._page_counter, self._title, len(self._page)),
+ # processed
+ self._register_page(self._output)
+
+ elif name == "mediawiki":
+ self._output.close()
+ print "Processed %d pages." % self._page_counter
+
+
+class TemplatesCounter:
+
+ def __init__(self, file_name, pages_selected, redirects):
+ self.templates_to_counter = {}
+ input_links = codecs.open('%s.page_templates' % file_name,
+ encoding='utf-8', mode='r')
+ line = input_links.readline()
+ while line:
+ words = line.split()
+ page = words[0]
+ if page in pages_selected:
+ print "Processing page %s \r" % page,
+ for n in range(1, len(words) - 1):
+ template = words[n]
+ # check if is a redirect
+ try:
+ template = redirects[template]
+ except:
+ pass
+
+ try:
+ self.templates_to_counter[template] = \
+ self.templates_to_counter[template] + 1
+ except:
+ self.templates_to_counter[template] = 1
+ line = input_links.readline()
+ input_links.close()
+
+
+class CountedTemplatesReader():
+
+ def __init__(self, file_name):
+ _file = codecs.open('%s.templates_counted' % file_name,
+ encoding='utf-8', mode='r')
+ self.templates = {}
+ line = _file.readline()
+ while line:
+ words = line.split()
+ template_name = words[0]
+ cant_used = int(words[1])
+ self.templates[template_name] = {'cant': cant_used}
+ line = _file.readline()
+
+
+class TemplatesLoader():
+
+ def __init__(self, file_name, templates_used):
+ _file = codecs.open('%s.templates' % file_name,
+ encoding='utf-8', mode='r')
+ self._output = codecs.open('%s.processed' % file_name,
+ encoding='utf-8', mode='a')
+ line = _file.readline()
+ while line:
+ if len(line) == 2:
+ if ord(line[0]) == 1:
+ title = _file.readline()
+ size = _file.readline()
+ separator = _file.readline()
+ finish = False
+ template_content = ''
+ while not finish:
+ line = _file.readline()
+ #print line
+ if len(line) == 2:
+ if ord(line[0]) == 3:
+ finish = True
+ break
+ template_content += line
+ template_name = title[title.find(':') + 1:].capitalize()
+ template_name = template_name.strip().replace(' ', '_')
+ #print "checking", template_name,
+
+ if template_name in templates_used.keys():
+ #print "Adding", template_name,
+ self._register_page(title.strip(), template_content.strip())
+
+ line = _file.readline()
+
+ def _register_page(self, title, content):
+ self._output.write('\01\n')
+ self._output.write('%s\n' % title)
+ self._output.write('%d\n' % len(content))
+ self._output.write('\02\n')
+ self._output.write('%s\n' % content)
+ self._output.write('\03\n')
+
+
+if __name__ == '__main__':
+ MAX_LEVELS = 1
+
+ fav_reader = FileListReader(favorites_file_name)
+ print "Loaded %d favorite pages" % len(fav_reader.list)
+
+ if not os.path.exists(blacklist_file_name):
+ pages_blacklisted_reader = FileListReader(blacklist_file_name)
+ pages_blacklist = pages_blacklisted_reader
+ print "Loaded %d blacklisted pages" % len(pages_blacklist)
+ else:
+ pages_blacklist = []
+
+ print "Loading redirects"
+ redirect_parser = RedirectParser(input_xml_file_name)
+ print "Processed %d redirects" % len(redirect_parser.redirects)
+
+ level = 1
+
+ selected_pages_file_name = '%s.pages_selected-level-%d' % \
+ (input_xml_file_name, MAX_LEVELS)
+ if not os.path.exists(selected_pages_file_name):
+ while level <= MAX_LEVELS:
+ print "Processing links level %d" % level
+ links_filter = LinksFilter(input_xml_file_name,
+ redirect_parser.redirects, fav_reader.list)
+ fav_reader.list.extend(links_filter.links)
+ level += 1
+
+ print "Writing pages_selected-level-%d file" % MAX_LEVELS
+ output_file = codecs.open(selected_pages_file_name,
+ encoding='utf-8', mode='w')
+ for page in fav_reader.list:
+ output_file.write('%s\n' % page)
+ output_file.close()
+ selected_pages_list = fav_reader.list
+ else:
+ print "Loading selected pages"
+ pages_selected_reader = FileListReader(selected_pages_file_name)
+ selected_pages_list = pages_selected_reader.list
+
+ if not os.path.exists('%s.processed' % input_xml_file_name):
+ print "Writing .processed file"
+ parser = make_parser()
+ parser.setContentHandler(PagesProcessor(input_xml_file_name,
+ selected_pages_list, pages_blacklist,
+ redirect_parser.redirects))
+ parser.parse(input_xml_file_name)
+
+ # if there are a .templates_counted file should be removed
+ # because we need recalculate it
+ if os.path.exists('%s.templates_counted' % input_xml_file_name):
+ os.remove('%s.templates_counted' % input_xml_file_name)
+
+ if not os.path.exists('%s.templates_counted' % input_xml_file_name):
+ print "Processing templates"
+ templates_counter = TemplatesCounter(input_xml_file_name,
+ selected_pages_list, redirect_parser.redirects)
+
+ print "Sorting counted templates"
+ items = templates_counter.templates_to_counter.items()
+ items.sort(key=itemgetter(1), reverse=True)
+
+ print "Writing templates_counted file"
+ output_file = codecs.open('%s.templates_counted' % input_xml_file_name,
+ encoding='utf-8', mode='w')
+ for n in range(len(items)):
+ output_file.write('%s %d\n' % (items[n][0], items[n][1]))
+ output_file.close()
+
+ print "Loading templates used"
+ templates_used_reader = CountedTemplatesReader(input_xml_file_name)
+ print "Readed %d templates used" % len(templates_used_reader.templates)
+
+ print "Adding used templates to .processed file"
+ templates_loader = TemplatesLoader(input_xml_file_name,
+ templates_used_reader.templates)