simplifier.py 18.2 KB
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
import copy
import sys
import requests

class Simp(object):
  def __init__(self):
    self.TYPE=""
    self.TYPEx=0
    self.TYPEy=0
    self.TEXT=""
    self.COMP=[]
  def agregarTYPE(self,Type):
    self.TYPE=Type
  def agregarTEXT(self,text):
    self.TEXT=text
  def agregarCOMP(self,comp):
    self.COMP.append(comp)

class Frase(object):
  def __init__(self):
    self.TYPE=""
    self.TEXT=""
    self.POS=""
    self.TREE=""
    self.SIMP=[]
  def agregarTYPE(self,Type):
    self.TYPE=Type
  def agregarTEXT(self,text):
    self.TEXT=text
  def agregarPOS(self,Pos):
    self.POS=Pos
  def agregarTREE(self,Tree):
    self.TREE=Tree
  def agregarSIMP(self):
    self.SIMP.append(Simp())

class Sentence(object):
  def __init__(self):
    self.FLAG=True
    self.TEXT=""
    self.TREE=""
    self.SIMP=[]
  def agregarTEXT(self,text):
    self.TEXT=text
  def agregarTREE(self,Tree):
    self.TREE=Tree
  def agregarSIMP(self):
    self.SIMP.append(Simp())


MEMORIAB=[]
MEMORIAA=[]


#----lectura de datos desde archivo
arch=(sys.argv[1])
f = open(arch)
dato = f.read().splitlines()
f.close
frase=Frase()
for i in range(len(dato)):
  if 'TYPE: ' in dato[i][0:6]:
    frase.agregarTYPE(dato[i][6:])
  elif 'TEXT: ' in dato[i][0:6]:
    frase.agregarTEXT(dato[i][6:])
  elif 'POS : ' in dato[i][0:6]:
    frase.agregarPOS(dato[i][6:])
  elif 'TREE: ' in dato[i][0:6]:
    frase.agregarTREE(dato[i][6:])
  elif 'SIMP:' in dato[i]:
    frase.agregarSIMP()
  elif '  TYPE: ' in dato[i][0:8]:
    frase.SIMP[-1].agregarTYPE(dato[i][8:])
  elif '  TEXT: ' in dato[i][0:8]:
    frase.SIMP[-1].agregarTEXT(dato[i][8:])
  elif '  COMP: ' in dato[i]:
    frase.SIMP[-1].agregarCOMP(dato[i][8:])
#------------


#-------Programa principal
#Algoritmo v4


if ((frase.TYPE.find('sentence')) !=- 1) and (frase.SIMP!=[]) and (frase.SIMP[0].TYPE != ''):
  y=1
  w=1
  SIMPworkspace=[]
  # copia TREE y cada SIMP a SENTENCE.1
  Sentence1=Sentence()
  Sentence1.TREE=copy.deepcopy(frase.TREE)
  Sentence1.TEXT=copy.deepcopy(frase.TEXT)
  for i in range(len(frase.SIMP)):
    #Sentence1.SIMP.append(Simp())
    #Sentence1.SIMP[i]=copy.deepcopy(frase.SIMP[i])
    SIMPworkspace.append(Simp())
    SIMPworkspace[i]=copy.deepcopy(frase.SIMP[i])
  
## ORDENAMIENTO DE SIMPs
  for i in range(len(SIMPworkspace)):
    #print SIMPworkspace[i].TEXT
    #print SIMPworkspace[i].TYPE
    SIMPworkspace[i].TYPEx = int(SIMPworkspace[i].TYPE[SIMPworkspace[i].TYPE.find('[')+1:SIMPworkspace[i].TYPE.find('..')])
    SIMPworkspace[i].TYPEy = int(SIMPworkspace[i].TYPE[SIMPworkspace[i].TYPE.find('..')+2:SIMPworkspace[i].TYPE.find(']')])
    if 'parenthesis' in SIMPworkspace[i].TYPE:
      SIMPworkspace[i].TYPEy = SIMPworkspace[i].TYPEy + 2
    #print SIMPworkspace[i].TYPEx
    #print SIMPworkspace[i].TYPEy


  SIMPworkspace.sort(key=lambda x: x.TYPEy, reverse=True)
  SIMPworkspace.sort(key=lambda x: x.TYPEx)

 
 # for i in range(len(SIMPworkspace)):
 #   print "\nSIMP " + str(i) + " :"
 #   print SIMPworkspace[i].TYPE 
 #   print SIMPworkspace[i].TYPEx
 #   print SIMPworkspace[i].TYPEy
 # print "\n"  

  for i in range(len(SIMPworkspace)):
    Sentence1.SIMP.append(Simp())
    Sentence1.SIMP[i]=copy.deepcopy(SIMPworkspace[i])  
   

  # Agrega la oracion original Sentence1 a la memoria como primer objeto en ser analizado
  MEMORIAB.append(Sentence())
  MEMORIAB[0]=copy.deepcopy(Sentence1)



  # 1 entrada al bucle A por cada SIMP diferente en Sentence1
  numSimp=len(Sentence1.SIMP)
  s = 0
  #bucle A
  while s < numSimp :
    #print "\nEntro por vez " + str(s) + " al bucle A"
    #print "Analizando todos los SIMP de tipo: " + MEMORIAB[0].SIMP[s].TYPE
    #Entra al bucle B el numero de veces igual al numerode elementos en MEMORIAB
    numMEM = len(MEMORIAB)
    t = 0
    #bucle B
    while t < numMEM :
      #print "Entro por vez " + str(t) + " al bucle B"
      #Entra si la oracion no ha sido analizada antes (FLAG==True) y si el texto del simp esta presente en la oracion.
      #print "CONDICIONES:"
      #print "SIMP " + MEMORIAB[0].SIMP[s].TEXT
      #print "SIMP " + MEMORIAB[0].SIMP[s].TYPE
      #print "MEMB " + str(MEMORIAB[t].FLAG)
      #print "MEMB " + MEMORIAB[t].TEXT
      if ( MEMORIAB[0].SIMP[s].TEXT in MEMORIAB[t].TEXT ) and ( MEMORIAB[t].FLAG == True ):
        MEMORIAB[t].FLAG = False
	#print "False to: " + MEMORIAB[t].TEXT
        #print "Entro a condicional"
        #Reglas de simplificacion
        if ( 'coordination' in MEMORIAB[t].SIMP[s].TYPE ) and ( not ('sentence coordination' in MEMORIAB[t].SIMP[s].TYPE ) ) :
          #print "Aplico regla coord"
          TEMPORALES = []
          c = len(MEMORIAB[t].SIMP[s].COMP)
          #print "Hay " + str(c) + " COMP en este SIMP"
          tt = 0
          while c > 0 :
            c = c - 1
            if ( 'conjunct' in MEMORIAB[0].SIMP[s].COMP[c] ) and ( not ( 'conjunction' in MEMORIAB[0].SIMP[s].COMP[c] ) ) :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t])
              replaced = MEMORIAB[0].SIMP[s].TEXT
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              replacer = MEMORIAB[0].TEXT[indice1:indice2]
              TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
              tt = tt + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'parenthesis' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla par"
          TEMPORALES = []
          c = len(MEMORIAB[t].SIMP[s].COMP)
          #print "Hay " + str(c) + " COMP en este SIMP"
          tt = 0
          while c > 0 :
            #print "entro al while de par"
            c = c - 1
            TEMPORALES.append(Sentence())
            TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t])
            replaced = MEMORIAB[0].SIMP[s].TEXT + ' )'
            indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
            indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
            replacer = MEMORIAB[0].TEXT[indice1:indice2]
            #print "replaced: " + replaced
            #print "replacer: " + replacer
            TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
            tt = tt + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'apposition' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla Apposition"
          TEMPORALES = []
          c = len(MEMORIAB[t].SIMP[s].COMP)
          #print "Hay " + str(c) + " COMP en este SIMP"
          tt = 0
          while c > 0 :
            #print "entro al while de par"
            c = c - 1
            TEMPORALES.append(Sentence())
            TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t])
            replaced = MEMORIAB[0].SIMP[s].TEXT
            indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
            indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
            replacer = MEMORIAB[0].TEXT[indice1:indice2]
            #print "replaced: " + replaced
            #print "replacer: " + replacer
            TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
            tt = tt + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print "Copio a memoria: " + MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'member-collection' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla member-collection"
          TEMPORALES = []
          c = len(MEMORIAB[t].SIMP[s].COMP)
          #print "Hay " + str(c) + " COMP en este SIMP"
          tt = 0
          while c > 0 :
            #print "entro al while de mem"
            c = c - 1
            TEMPORALES.append(Sentence())
            TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t])
            replaced = MEMORIAB[0].SIMP[s].TEXT
            indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
            indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
            replacer = MEMORIAB[0].TEXT[indice1:indice2]
            #print "replaced: " + replaced
            #print "replacer: " + replacer
            TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
            tt = tt + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print "Copio a memoria: " + MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'sentence coordination' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla Verb"
          TEMPORALES = []
          c = len(MEMORIAB[t].SIMP[s].COMP)
          #print "Hay " + str(c) + " COMP en este SIMP"
          tt = 0
          while c > 0 :
            c = c - 1
            if ( 'conjunct' in MEMORIAB[0].SIMP[s].COMP[c] ) and ( not ( 'conjunction' in MEMORIAB[0].SIMP[s].COMP[c] ) ) :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t])
              #sustituye todo el contenido de TEMPORAL.r/TREE, por el contenido la oracion coordinada
              #replaced = MEMORIAB[0].SIMP[s].TEXT
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              replacer = MEMORIAB[0].TEXT[indice1:indice2]
              #print replacer
              TEMPORALES[tt].TEXT = replacer 
              ## si la oracion no termina en punto o ! 
              tt = tt + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'full relative clause' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla RelCl"
          TEMPORALES = []
          c = 0
          tt = 0
          while c < 2 :
            if 'referred noun phrase' in MEMORIAB[0].SIMP[s].COMP[c] :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t]) #ok
              if MEMORIAB[0].TEXT[MEMORIAB[0].TEXT.index(TEMPORALES[tt].SIMP[s].TEXT)+len(TEMPORALES[tt].SIMP[s].TEXT)-1] == ',':
                replaced = MEMORIAB[0].SIMP[s].TEXT + ',' #posible error, si es asi probar con ' ,'
              else:
                replaced = MEMORIAB[0].SIMP[s].TEXT
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              replacer = MEMORIAB[0].TEXT[indice1:indice2]
              TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
              indice3 = indice1
              indice4 = indice2
            if 'clause' in MEMORIAB[0].SIMP[s].COMP[c] :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t]) #ok
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              TEMPORALES[tt].TEXT = copy.deepcopy(MEMORIAB[0].TEXT[indice3:indice4]+' '+MEMORIAB[0].TEXT[indice1:indice2] ) ##
              cad3 = MEMORIAB[0].TEXT[indice1:indice2]
              cad4 = cad3.split()
              if (cad4[0]+'_WDT') in frase.POS:
                TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(' '+cad4[0],'')
            tt = tt + 1
            c = c + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'reduced relative clause' in MEMORIAB[t].SIMP[s].TYPE:
          #print "Aplico regla RelCl"
          TEMPORALES = []
          c = 0
          tt = 0
          while c < 2 :
            if 'referred noun phrase' in MEMORIAB[0].SIMP[s].COMP[c] :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t]) #ok
              replaced = MEMORIAB[0].SIMP[s].TEXT
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              replacer = MEMORIAB[0].TEXT[indice1:indice2]
              #subj = MEMORIAB[0].TEXT[indice1:(indice2+1)]
              subj = MEMORIAB[0].TEXT[indice1:(indice2)]
              TEMPORALES[tt].TEXT = TEMPORALES[tt].TEXT.replace(replaced,replacer)
            if 'clause' in MEMORIAB[0].SIMP[s].COMP[c] :
              TEMPORALES.append(Sentence())
              TEMPORALES[tt] = copy.deepcopy(MEMORIAB[t]) #el referente debera estar antes que la clausula para tener orden correcto
              indice1 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('[')+1:TEMPORALES[tt].SIMP[s].COMP[c].find('..')])
              indice2 = (int)(TEMPORALES[tt].SIMP[s].COMP[c][TEMPORALES[tt].SIMP[s].COMP[c].find('..')+2:TEMPORALES[tt].SIMP[s].COMP[c].find(']')])
              replacer = MEMORIAB[0].TEXT[indice1:indice2]
              TEMPORALES[tt].TEXT = subj + " _ " + replacer #en este punto para ingresar copula necesitas info de numero y tiempo
            tt = tt + 1
            c = c + 1
          #copiar simplificaciones de memoria temporal a MEMORIAB
          indtempamem = 0
          while indtempamem < len(TEMPORALES) :
            MEMORIAB.append(Sentence())
            MEMORIAB[-1]=copy.deepcopy(TEMPORALES[indtempamem])
            MEMORIAB[-1].FLAG = True
            #print MEMORIAB[-1].TEXT
            indtempamem = indtempamem + 1
        elif 'hypernymy' in MEMORIAB[t].SIMP[s].TYPE:
	  print "**hypernymy detected**"
	  #print "True to: " + MEMORIAB[t].TEXT
	  MEMORIAB[t].FLAG = True
        else:
	  print "Error: Unknown simplification construct detected."
	  #print "True to: " + MEMORIAB[t].TEXT
	  MEMORIAB[t].FLAG = True
      t = t + 1
    s = s + 1

  #CONDICIONES PARA IMPRESION DE SIMPLIFICACIONES EN ARCHIVO DE TEXTO
  #print "Sentence simplificated. New sentences generated:"
  for i in range(len(MEMORIAB)):
    #se reutiliza flag para marcar las oraciones finales
    MEMORIAB[i].FLAG = True
    for j in range(len(MEMORIAB[0].SIMP)):   
      #NOTA: si se agrega un constructo simplificable, anadirlo tambien a esta lista:
      if ( ('member-collection' in MEMORIAB[0].SIMP[j].TYPE) or ('apposition' in MEMORIAB[0].SIMP[j].TYPE) or ('coordination' in MEMORIAB[0].SIMP[j].TYPE) or ('parenthesis' in MEMORIAB[0].SIMP[j].TYPE) or ('sentence coordination' in MEMORIAB[0].SIMP[j].TYPE) or ('full relative clause' in MEMORIAB[0].SIMP[j].TYPE) or ('reduced relative clause' in MEMORIAB[0].SIMP[j].TYPE) ) and (MEMORIAB[0].SIMP[j].TEXT in MEMORIAB[i].TEXT) :
        MEMORIAB[i].FLAG = False

  ##areglar numeracion archivos salida ej 011
  arcsalnum = 0
  for i in range(len(MEMORIAB)):
    if MEMORIAB[i].FLAG == True:
      arcsalnum = arcsalnum + 1
  length = len(str(arcsalnum))
  #print('{:03d}'.format(arcsalnum))  # python >= 2.7 + python3
#  >>> n = '4'
#>>> print n.zfill(3)
  arcsalnum = 0
  for i in range(len(MEMORIAB)):
    if MEMORIAB[i].FLAG == True:
      arcsalnum = arcsalnum + 1
      print MEMORIAB[i].TEXT#Salida
      archSalNombre = sys.argv[2]
      archSalNombre=archSalNombre[:-4] + "-" + (str(arcsalnum)).zfill(length) + '.alg'
      archivoSalida=open(archSalNombre,"w")
      archivoSalida.write(MEMORIAB[i].TEXT+"\n")##
      archivoSalida.close()
      #WRITE OUTPUT FILE PATH TO INDEX (Arg 3)
      index_name = sys.argv[3]
      index = open(index_name, "a+")
      archSalNombreforIndex=archSalNombre + "\n"
      index.write(archSalNombreforIndex)
      index.close()
else:
  print frase.TEXT #----Salida si no habia constructos simplificables
  archSalNombre = sys.argv[2]
  archSalNombre = archSalNombre[:-4] + ".alg"
  archivoSalida = open(archSalNombre,"a+")
  archivoSalida.write(frase.TEXT+"\n")##
  archivoSalida.close()
  #WRITE OUTPUT FILE PATH TO INDEX (Arg 3)
  index_name = sys.argv[3]
  index = open(index_name, "a+")
  archSalNombreforIndex=archSalNombre + "\n"
  index.write(archSalNombreforIndex)
  index.close()


#FIN