Como experimento y para probar la nueva versión de Pywc, nació (más bien se escribió) un script que usa Wikitionary (el diccionario de la Wikipedia) para extraer listas de palabras.

La sintaxis es:

=============================================================================== ./wikiwords.py [-help] [-min ] [-max ] [-lang ] [- numpags ] [-incial ] [-nullwd ] -help: muestra esto -min: las palabras de salida serán como mínimo de letras -max: las palabras de salida serán como máximo de letras -lang: se usara el wiki en -numpags: se leeran páginas -inicial: la palabra inicial será -nullwd: la lista de palabras "inútiles" (artículos) está en o no existe (off) ===============================================================================

La lista de palabras "inútiles" de ejemplo es:

=============================================================================== cual cuales de el la los las o y que se en un una ===============================================================================

El script se puede descargar aquí [wikiwords.zip] (con la lista de palabras) y con Pywc [wikiwords_03.zip]. Y el código coloreado con pygments...

===============================================================================

!/usr/bin/env python

-- coding: utf-8 --

""" Escrito por kenkeiras [CodigoParaLlevar.blogspot.com]

This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.

This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the     GNU General Public License for more details.

You should have received a copy of the GNU General Public License     along with this program. If not, see http://www.gnu.org/licenses/. """

import pywc, string, sys

argc = len(sys.argv)

def show_help():

print "./wikiwords.py [-help] [-min ] [-max ] [-lang ] [-numpags ] [-incial ] [-nullwd ]"

print "-help: muestra esto"     print "-min: las palabras de salida serán como mínimo de letras"     print "-max: las palabras de salida serán como máximo de letras"

print "-lang: se usara el wiki en "     print "-numpags: se leeran páginas"     print "-inicial: la palabra inicial será "

print "-nullwd: la lista de palabras \"inútiles\" (artículos) está en o no existe (off)"

Caracteres que componen las palabras

Characters that compose words

global textchars textchars = u"abcdefghijklmnñopqrstuvwxyzABCDEEFGHIJKLMNÑOPQRSTUVWXYZááíóúÁÉÍÓÚ"

Si es una simple palabra

If is a plain word

def isClean(s):

global textchars     try:         for c in unicode(s):

if (not c in textchars):                 return False

except Exception as ex:         return False     return True

Valores por defecto

By default values

minchars = 0 # Caracteres mínimos maxchars = 100 # Caracteres máximos

numpags = 100 # Páginas que se recorrerán inicial="meta" # Palabra inicial lang = "es" # Lenguaje

nullwords = "nullwd" # Useless word list

Reading command line options

Leyendo las opciónes de linea de comandos

i = 1

while ( i < argc ):     if ( sys.argv[i] == "-min" ): # Opción: -min

i += 1         minchars = int (sys.argv[i])

elif ( sys.argv[i] == "-max" ):# Opción: -max

i += 1         maxchars = int (sys.argv[i])

elif ( sys.argv[i] == "-lang" ):# Opción: -lang

i += 1         lang = sys.argv[i]

elif ( sys.argv[i] == "-numpags" ):# Opción: -numpags

i += 1         numpags = int (sys.argv[i])

elif ( sys.argv[i] == "-inicial" ):# Opción: -inicial

i += 1         inicial = sys.argv[i]

elif ( sys.argv[i] == "-nullwd" ):# Opción: -nullwd

i += 1         nullwords = sys.argv[i]

elif ( sys.argv[i] == "-help" ):# Opción: -help

show_help()         sys.exit(0)

i += 1

Caracteres que separan palabras

Characters that split words

splitchars = ",.-/+|:!?\"'\&\n"

new = ["http://"+lang+".wiktionary.org/wiki/"+inicial] # próximas Url's

wordlist = ['wikitionary'] # Lista de palabras que ya salieron

Add null characters to the list

Se añaden los caracteres nulos a la lista

if (nullwords.lower() != "off"):

try:         f = open(nullwords,"r")         line = f.readline()

while ( len (line) > 0 ):             line = line.replace("\n","").replace("\r","").strip()

if (len (line) > 0) and (not line in wordlist):

wordlist.append(line)             line = f.readline()

f.close()     except Exception as ex:

print >> sys.stderr, "Error al leer archivo de palabras nulas:",ex

crawler = pywc.crawler() # Se inicia el crawler

Comienza la acción...

while ( len(new) > 0 ) and (numpags > 0):

site = crawler.crawlsite( new.pop(0) ) # Se lee la página

inPost = False     numpags -= 1

for el in site.element_list: # Para todos los elementos de una página

if (el.tag_type == "div"):             try:

if ("id" in el.property) and \                     ( el.property['id'] == "bodyContent" ): # Si es el inicio

inPost = True                            # del artículo...

elif ( el.property['class'] == "printfooter" ): # Si es el

break                                     # final...             except:                 pass

if (inPost) and (not el.tag): # Si es parte del artículo

el.text = el.text.lower()             tmp = el.text.strip()

for schar in splitchars: # Se separan las palabras                 tmp = tmp.split(schar)

tmp = string.join(tmp," ")

for word in tmp.split(" "): # Se comprueba cada palabra

# por separado

# Se comprueba que sean palabras normales, que no excedan                 # los límites y que no están repetidas                 if (isClean(word)):                     lonx = len(word)

if ( lonx > 0 ) and ( lonx >= minchars ) and \                      ( lonx <= maxchars ):

if (not word in wordlist):                             wordlist.append(word)

print word                             new.append("http://"+lang+".wikipedia.org/wiki/"+\                                 word) ===============================================================================