19 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
# |
# |
21 |
|
|
22 |
#Fichero: contexto.py |
#Fichero: contexto.py |
23 |
|
|
24 |
"""Clase Contexto.""" |
"""Clase Contexto.""" |
25 |
|
|
26 |
|
|
27 |
from xml.dom import minidom |
from xml.dom import minidom |
28 |
import random |
import random |
29 |
import sys |
import sys |
30 |
import string |
import string |
31 |
import os |
import os |
32 |
|
|
33 |
from utils import * |
from utils import * |
34 |
from tema import * |
from tema import * |
35 |
from bloquedialogo import * |
from bloquedialogo import * |
36 |
from tema import * |
|
37 |
|
class Contexto(BloqueDialogo): |
38 |
class Contexto(BloqueDialogo): |
"""Una coleccion de temas relacionados. |
39 |
"""Una coleccion de temas relacionados. |
|
40 |
|
Contiene los siguientes atributos: |
41 |
Contiene los siguientes atributos: |
- id (string): identifica univocamente al contexto. No puede contener espacios. |
42 |
- id (string): identifica univocamente al contexto. No puede contener espacios. |
- _conversacion (string): nombre de la conversacion a la que pertenece el contexto. |
43 |
- _conversacion (string): nombre de la conversacion a la que pertenece el contexto. |
- _temas: lista de temas contenidos en el contexto. |
44 |
- _temas: lista de temas contenidos en el contexto. |
- _temas_especiales: diccionario con las lista de temas especiales ("temaAgotado", "temaNoEncontrado" y "temaOfftopic") |
45 |
- _temas_especiales: diccionario con las lista de temas especiales ("temaAgotado", "temaNoEncontrado" y "temaOfftopic") |
|
46 |
|
|
47 |
|
Invariantes: |
48 |
Invariantes: |
id != None |
49 |
id != None |
_conversacion != None |
50 |
_conversacion != None |
""" |
51 |
""" |
|
52 |
|
def __init__(self, contexto_xml, conversacion): |
53 |
def __init__(self, contexto_xml, conversacion): |
"""Crea un contexto nuevo a partir de un arbol XML de descripción de contexto. |
54 |
"""Crea un contexto nuevo a partir de un arbol XML de descripción de contexto. |
|
55 |
|
Requiere: |
56 |
Requiere: |
contexto_xml != None |
57 |
contexto_xml != None |
contexto_xml corresponde con un nodo DOM de contexto |
58 |
contexto_xml corresponde con un nodo DOM de contexto |
conversacion != None |
59 |
conversacion != None |
""" |
60 |
""" |
|
61 |
|
self.id = contexto_xml.getAttribute("id").encode('ISO-8859-1') |
62 |
self.id = contexto_xml.getAttribute("id").encode('ISO-8859-1') |
# print "Contexto: " + self.id |
63 |
# print "Contexto: " + self.id |
self._conversacion = conversacion |
64 |
self._conversacion = conversacion |
self._temas = self._parsear_temas(contexto_xml) |
|
self._temas = self._parsear_temas(contexto_xml) |
|
65 |
self._temas_especiales = self._parsear_temas_especiales(contexto_xml) |
self._temas_especiales = self._parsear_temas_especiales(contexto_xml) |
66 |
nombres = contexto_xml.getElementsByTagName("nombre") |
nombres = contexto_xml.getElementsByTagName("nombre") |
67 |
if nombres: |
if nombres: |
68 |
self._nombre = nombres[0].firstChild.data.encode('ISO-8859-1') |
self._nombre = nombres[0].firstChild.data.encode('ISO-8859-1') |
69 |
else: |
else: |
70 |
self._nombre = self.id.split('-')[1] |
self._nombre = self.id.split('-')[1] |
71 |
|
|
72 |
def conversacion(self): |
def conversacion(self): |
73 |
return self._conversacion |
return self._conversacion |
74 |
|
|
75 |
def nombre(self): |
def nombre(self): |
76 |
return self._nombre |
return self._nombre |
77 |
|
|
78 |
def tema(self, nombretema): |
def tema(self, nombretema): |
79 |
"""Devuelve el tema con ese nombre, si está en este contexto.""" |
"""Devuelve el tema con ese nombre, si está en este contexto.""" |
80 |
try: |
try: |
81 |
i = map(lambda t: t.nombre(), self._temas).index(nombretema) |
i = map(lambda t: t.nombre(), self._temas).index(nombretema) |
82 |
return self._temas[i] |
return self._temas[i] |
83 |
except: |
except: |
84 |
return None |
return None |
85 |
|
|
86 |
def temas(self): |
def temas(self): |
87 |
return self._temas |
return self._temas |
88 |
|
|
89 |
def temas_especiales(self): |
def temas_especiales(self): |
90 |
return self._temas_especiales |
return self._temas_especiales |
91 |
|
|
92 |
def __str__(self): |
def __str__(self): |
93 |
texto = ' <Contexto - "' + self.id |
texto = ' <Contexto - "' + self.id |
94 |
texto = texto + '"\n\tTemas:\n' |
texto = texto + '"\n\tTemas:\n' |
95 |
for tema in self._temas: |
for tema in self._temas: |
96 |
texto = texto + "\t\t" + tema.nombre() + "\n" |
texto = texto + "\t\t" + tema.nombre() + "\n" |
97 |
texto = texto + " />\n" |
texto = texto + " />\n" |
98 |
return texto |
return texto |
99 |
|
|
100 |
# Código para pruebas unitarias del módulo. |
# Código para pruebas unitarias del módulo. |
101 |
|
|
102 |
if (__name__ == '__main__'): |
if (__name__ == '__main__'): |
103 |
archivo = os.path.join(BloqueDialogo.DIR_DESC_DIALOGOS, "ejemplo", "ejemplo-hablar" + BloqueDialogo.EXT_DESC_DIALOGOS) |
archivo = os.path.join(BloqueDialogo.DIR_DESC_DIALOGOS, "ejemplo", "ejemplo-hablar" + BloqueDialogo.EXT_DESC_DIALOGOS) |
104 |
print archivo |
print archivo |
105 |
stream = open(archivo) |
stream = open(archivo) |
106 |
doc_xml = minidom.parse(stream).documentElement |
doc_xml = minidom.parse(stream).documentElement |
107 |
|
|
108 |
contexto = Contexto(doc_xml, None) |
contexto = Contexto(doc_xml, None) |
109 |
stream.close() |
stream.close() |
110 |
|
|
111 |
print "Contexto cargado correctamente.\n" + str(contexto) # + str(tema._sinonimos) |
print "Contexto cargado correctamente.\n" + str(contexto) # + str(tema._sinonimos) |
112 |
|
|