List Index out of range in Recurrent Nural Network

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saad Bin Ahmed
    New Member
    • Jul 2011
    • 25

    List Index out of range in Recurrent Nural Network

    I have not too much experience in python. Here goes my problem. I am writing the code for recurrent neural network in order to recognize words given as .xml file and search in the dictionary which word .xml file conatins.
    For this task I have initially defined labels of characters, then read the lines in .xml file and then try to figure out which word it contains, but here it shows List Index out of range error.

    labels contain the characters
    Code:
    from optparse import OptionParser
    import sys
    import os
    from xml.dom.minidom import parse
    
    labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g','ga', 'h', 'i', 'j', 'k', 'km', 'l', 'm', 'n', 'o', 'p', 'pt','q','r','s','sc','sp','t','u','v','w','x','y','z']
    I have some words defined in dictionary and some .xml files which I have to train by using dictionary words. But the problem I am suffering now is how can I search a word (by taking .xml file and explore it which word it contains, and search against it in a dictionary).

    Code:
    inputFilename = args [0]
    ncFilename = args[1]
    print options
    print "input filename", inputFilename
    print "data filename", ncFilename
    seqDims = []
    seqLengths = []
    targetStrings = []
    wordTargetStrings = []
    seqTags = []
    inputs = []
    print "reading data files"
    for l in file(inputFilename).readlines():
    	xmlfile = l.strip()
    	if len(xmlfile):
    		print xmlfile
    		seqTags.append(xmlfile)
    		if os.path.exists(xmlfile):
    			word = parse(xmlfile).getElementsByTagName('hwData')[0].getElementsByTagName('label')[0].getElementsByTagName('alternate')[0].getAttribute('value').strip().replace(' ','*')
    			print 'WORD',word
    			wts = word.encode('unicode_escape')
    			print "WTS",wts
    			wordTargetStrings.append(wts)	
    			ts = ""
    			for c in word:
    				label = c.encode('unicode_escape')
    				ts += label + ' '
    			ts = ts.strip()
    			print ts
    			targetStrings.append(ts)
    		else:
    			wordTargetStrings.append(' ')
    			targetStrings.append(' ')			
    		oldlen = len(inputs)
    		for trace in parse(xmlfile).getElementsByTagName('trace'):		
    			for coords in trace.firstChild.nodeValue.split(','):
    				pt = coords.split()
    				inputs.append([float(pt[0]), float(pt[1]), 0.0])
    			inputs[-1][-1] = 1
    		seqLengths.append(len(inputs) - oldlen)
    		seqDims.append([seqLengths[-1]])

    When I execute this code it shows me the following error

    word = parse(xmlfile). getElementsByTa gName('hwData')[0].getElementsByT agName('label')[0].getElementsByT agName('alterna te')[0].getAttribute(' value').strip() .replace(' ','*')
    IndexError: list index out of range

    Any Suggestions.... .
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The problem is one of the calls to getElementsByTa gName is returning an empty list, as in:
    Code:
    >>> s = []
    >>> s[0]
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    IndexError: list index out of range
    >>>

    Comment

    Working...