-
Notifications
You must be signed in to change notification settings - Fork 10
/
jeita2txt.py
executable file
·51 lines (42 loc) · 1.22 KB
/
jeita2txt.py
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xml.sax
import xml.sax.handler
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
class Handler(xml.sax.handler.ContentHandler):
def startDocument(self):
self.text = []
self.m = False
self.nest = 0
def startElement(self, name, attrs):
self.m = (name == 'm')
if name == 'text':
self.nest = 0
def endElement(self, name):
self.m = False
if name == 'text':
self.outputSentence()
def characters(self, content):
if self.m:
self.text.append(content)
if self.nest == 0 and content in u'。..!!??':
self.outputSentence()
elif content in u'「『【[[〈《(({{':
self.nest += 1
elif content in u'」』】]]〉》))}}':
self.nest -= 1
return
def outputSentence(self):
if len(self.text) == 0:
return
print ' '.join(self.text)
self.text = []
def main():
parser = xml.sax.make_parser()
parser.setContentHandler(Handler())
parser.parse(sys.stdin)
return
if __name__=="__main__":
main()