I have this code which migrates HTML elements to the word chunks by bracketing each element. Here, chunks is the list of word chunks to be processed and dom is to access the given HTML source. This returns a list of processed word chunks.
Could you suggest any improvements to this :
def _migrate_html(self, chunks, dom):
elements = self._get_elements_list(dom)
for element in elements:
result = []
index = 0
concat_chunks = []
for chunk in chunks:
if (index + len(chunk.word) <= element.index or
element.index + len(element.text) <= index):
result.append(chunk)
elif (index <= element.index and
element.index + len(element.text) <= index + len(chunk.word)):
result.append(Chunk(
chunk.word.replace(element.text, element.source),
HTML_POS, HTML_POS, True))
elif (index < element.index + len(element.text) and
element.index + len(element.text) <= index + len(chunk.word)):
concat_chunks.append(chunk)
new_word = u''.join([c_chunk.word for c_chunk in concat_chunks])
new_word = new_word.replace(element.text, element.source)
result.append(Chunk(new_word, HTML_POS, HTML_POS, True))
concat_chunks = []
else:
concat_chunks.append(chunk)
index += len(chunk.word)
chunks = result
return chunks