# -*- coding: utf-8 -*-
import codecs,lxml,re,os
from lxml import etree
def proc(inputFile,outPath):
	try:
		document=etree.parse(inputFile)
	except etree.XMLSyntaxError:
		print 'Cannot parse '+inputFile
		return False
		
	root=document.getroot()
	chunkList=root[0]
	chunks=chunkList.findall("chunk[@type='p']")

	i=0;	
	for ch in chunks:
		outNum=i.__str__()
		if not os.path.exists(outPath):
			os.makedirs(outPath)
		out = codecs.open(outPath+"/"+outNum+".xml", "w",encoding="utf=8")
		out.write(unicode('<?xml version="1.0" encoding="UTF-8" ?>\n','utf=8'))
		out.write(unicode('<cesAna xmlns:xlink="http://www.w3.org/1999/xlink" type="pre_morph" version="STUB-1.0">\n','utf=8'))
		out.write(unicode('<chunkList xml:base="text.xml">\n','utf=8'))
		line=etree.tostring(ch,encoding="UTF-8")
		line=re.sub(r' xmlns:xlink.*/xlink"','',line)
		out.write(unicode(line,'utf=8'))
		out.write(unicode('</chunkList>\n','utf=8'))
		out.write(unicode('</cesAna>\n','utf=8'))
		out.close()
		i=i+1
