-
Notifications
You must be signed in to change notification settings - Fork 0
/
processXML.py
443 lines (382 loc) · 16.2 KB
/
processXML.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
## processXML.py - Script for processing the XMLs from the talkbank corpus
##
## Copyright (C) 2017 Aaron Keesing
## Version: 2.4.0
import xmltodict
import nltk, nltk.corpus
from pycorenlp import StanfordCoreNLP
import os.path
import os
from concurrent.futures import ProcessPoolExecutor
from itertools import groupby, islice, chain
from operator import itemgetter
from pathlib import Path
import argparse
import pickle
import re
# This class was copied from http://streamhacker.com/2009/02/23/chunk-extraction-with-nltk/
class TagChunker(nltk.ChunkParserI):
def __init__(self, chunk_tagger):
self._chunk_tagger = chunk_tagger
def parse(self, tokens):
# split words and part of speech tags
(words, tags) = zip(*tokens)
# get IOB chunk tags
chunks = self._chunk_tagger.tag(tags)
# join words with chunk tags
wtc = zip(words, chunks)
# w = word, t = part-of-speech tag, c = chunk tag
lines = [' '.join([w, t, c]) for (w, (t, c)) in wtc if c]
# create tree from conll formatted chunk lines
return nltk.conllstr2tree('\n'.join(lines))
class FileProcessor():
"""This is a utility class for processing files.
"""
def __init__(self, processFileFunc, processUtterFunc, debug=False, writeout=True, outputDir='xmlconv/'):
self.processFileFunc = processFileFunc
self.processUtterFunc = processUtterFunc
self.debug = debug
self.writeout = writeout
self.outputDir = outputDir
self.tagger = None
self.chunker = None
def processFile(self, file):
utters, id = self.processFileFunc(file)
sents = self.processUtterFunc(utters, self.tagger, self.debug)
if self.writeout:
FileProcessor.writexml(sents, id, 'conv_' + id + '.xml', self.outputDir)
@staticmethod
def writexml(utters, id, filename, outputDir):
if not filename.endswith('.xml'):
filename = filename + '.xml'
tree = {
'conversation': {
'@id': id,
'u': []
}
}
for utter in utters:
u = {'s': []}
for sent in utter:
s = {'t': []}
for t in sent:
s['t'].append({'@pos': t[1], '#text': t[0], '@stem': t[2], '@ner': t[3]})
u['s'].append(s)
tree['conversation']['u'].append(u)
#print(xmltodict.unparse(tree, pretty=True))
filename = os.path.join(outputDir, filename)
print('Writing to {}'.format(filename))
outfile = open(filename, mode='bw')
xmltodict.unparse(tree, output=outfile, pretty=True)
outfile.close()
def stanfordProcess(utters, tagger=None, debug=False):
# Set up Stanford Stuff
stserver = StanfordCoreNLP('http://localhost:9000/')
processed = []
try:
for utter in utters:
annot = stserver.annotate(utter, properties={
'annotators': 'tokenize,ssplit,pos,ner,lemma',
#'pos.model': 'edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger',
'pos.model': 'gate-EN-twitter.model',
'ner.model': 'edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz',
'outputFormat': 'json'
})
processed.append([[(tok['word'], tok['pos'], tok['lemma'], tok['ner']) for tok in sent['tokens']] for sent in annot['sentences']])
except Exception:
print("Unable to connect to Stanford CoreNLP server. Make sure it is running at http://localhost:9000/")
print("You can start the server with the command: java -mx1000m edu.stanford.nlp.pipeline.StanfordCoreNLPServer 9000")
exit()
return processed
def nltkProcess(utters, tagger, debug=False):
if not isinstance(tagger, nltk.tag.TaggerI):
raise TypeError("Argument 'tagger' is type {} must be of type {}".format(type(tagger), nltk.TaggerI))
processedUtters = []
if debug:
print()
print('Utterances:')
for utter in utters[:20]:
print(utter)
print('...')
print()
for utter in utters:
sents = nltk.sent_tokenize(utter)
processedSents = []
for sent in sents:
tokens = nltk.word_tokenize(sent)
#taggedSent = nltk.pos_tag(tokens)
taggedSent = tagger.tag(tokens)
# Assume each unknown tag is a proper noun
taggedSent = [(w, t) if t else (w, 'NNP') for w, t in taggedSent]
stemmer = nltk.stem.snowball.EnglishStemmer()
stemmedTags = [(w, t, stemmer.stem(w)) for w, t in taggedSent]
if debug:
print('Tagged sentence:')
print(' '.join([nltk.tuple2str(tok) for tok in taggedSent]))
print()
neTags = list(nltk.ne_chunk(taggedSent))
if debug:
print('NE tags:')
for tag in neTags[:20]:
print(tag)
print('...')
print()
print()
k = 0
for i in range(len(neTags)):
tag = neTags[i]
if isinstance(tag, nltk.Tree):
for _ in range(len(tag)):
w, t, s = stemmedTags[k]
stemmedTags[k] = (w, t, s, tag.label())
k += 1
else:
w, t, s = stemmedTags[k]
stemmedTags[k] = (w, t, s, 'O')
k += 1
processedSents.append(stemmedTags)
processedUtters.append(processedSents)
return processedUtters
def joinSentence(tokens):
PUNCTUATION = set(',.?;%!')
outStr = ""
for tok in tokens:
if tok not in PUNCTUATION and "'" not in tok:
outStr += ' '
outStr += tok
return outStr
def isBad(word):
badPatterns = [
re.compile(r'^[OoHh]+$'),
re.compile(r'^[UuHhMm]+$'),
re.compile(r'^[UuGgHh]+$'),
re.compile(r'^[HhAa]+$'),
re.compile(r'^[Xx]+$'),
re.compile(r'^([AaEeIiOoUu])\1{2,}$'),
re.compile(r'^(_)+$')
]
if word.startswith('-') or word.endswith('-'):
return True
elif word == '':
return True
for pat in badPatterns:
if pat.match(word):
return True
def removeBadWords(words):
words = list(chain(*[w.split('_') for w in words]))
words = [re.sub(r'[^A-Za-z0-9)(,.?!$%&;:\'"]', '', w) for w in words]
words = [re.sub(r'([A-Za-z])\1\1+', r'\1\1', w) for w in words]
words = [w for w in words if not isBad(w)]
return words
def processTBFile(filename):
print("Processing file {}".format(filename))
utters = []
with open(filename, encoding='utf8') as f:
xmlDoc = xmltodict.parse(f.read(), encoding='utf-8')
if (len(xmlDoc['CHAT']['@Lang'].split()) > 1
or not isinstance(xmlDoc['CHAT']['Participants']['participant'], list)):
# Ignore files with multiple languages or only one speaker
return
for utter in xmlDoc['CHAT']['u']:
if 'w' in utter:
who = utter['@who']
wordList = []
if isinstance(utter['w'], list): # Multiple words
wordList = utter['w']
else: # Single word
wordList = [utter['w']]
words = []
for w in wordList:
if isinstance(w, dict):
if '@untranscribed' in w or '@type' in w: # Skip non-sensical things
continue
elif '#text' in w:
words.append(w['#text'])
else:
words.append(w)
if 't' in utter:
ttype = utter['t']['@type']
else:
ttype = 'p'
if ttype == 'q':
t = '?'
elif ttype == 'e':
t = '!'
else:
t = '.'
words.append(t)
words = removeBadWords(words)
if len(words) > 0:
utters.append((who, joinSentence(words)))
# Join contiguous responses from identical speakers
groupedUtters = [' '.join([x[1] for x in g]) for k, g in groupby(utters, itemgetter(0))]
return (groupedUtters, xmlDoc['CHAT']['@Id'])
def processTwitterFile(filename):
print("Processing file {}".format(filename))
utters = []
with open(filename, encoding='utf8') as f:
alllines = list(map(lambda x: x.strip(), f.readlines()))
for utter in [line[20:] for line in alllines]: # Ignore '123456789012345678: '
words = utter.split(' ')
words = removeBadWords(words)
if len(words) > 0:
utters.append(' '.join(words)) # Fine because we split on spaces
return (utters, os.path.basename(filename))
def processANCFile(filename):
print("Processing file {}".format(filename))
utters = []
with open(filename, encoding='utf8') as f:
xmlDoc = xmltodict.parse(f.read(), encoding='utf-8')
for utter in xmlDoc['cesDoc']['body']['turn']:
currentUtter = []
sents = []
if isinstance(utter['u'], list):
sents = utter['u']
else:
sents = [utter['u']]
for u in sents:
if 'u' in u:
if isinstance(u['u'], list):
us = u['u']
else:
us = [u['u']]
else:
us = [u]
sent = []
for ut in us:
toks = []
if 'tok' in ut:
if isinstance(ut['tok'], list):
toks = ut['tok']
else:
toks = [ut['tok']]
for tok in toks:
if not isBad(tok['#text']):
sent.append((tok['#text'], tok['@msd'], tok['@base'], 'O'))
if len(sent) > 0:
currentUtter.append(sent)
if len(currentUtter) > 0:
utters.append(currentUtter)
return (utters, os.path.basename(filename))
def processIdent(utters, tagger, debug):
return utters
def main():
argparser = argparse.ArgumentParser()
subparsers = argparser.add_subparsers(dest='method')
parser_train = subparsers.add_parser('train')
parser_train.add_argument('corpus', help="The tagged corpus to train the POS tagger on.", default='../corpora/OpenANC/processed/nltk/spoken')
parser_process = subparsers.add_parser('process')
parser_process.add_argument('infile', help="An input file or directory. If it is a file then the file is processed. " +
"If it is a directory then all .xml files will be processed recursively in " +
"the directory and all subdirectories", type=str)
parser_process.add_argument('-p', '--print', help="Print verbose info", action='store_true')
parser_process.add_argument('-o', '--outputdir', help='The directory to place the processed file(s), default "xmlout/"', nargs='?', type=str, const='xmlout', default='')
parser_process.add_argument('--chunk', action='store_true', help="Chunk sentences into a tree of height 1")
parser_process.add_argument('-s', '--stanford', action='store_true', help="Use Stanford CoreNLP")
group = parser_process.add_mutually_exclusive_group(required=True)
group.add_argument('-t', '--talkbank', action='store_true')
group.add_argument('-w', '--twitter', action='store_true')
group.add_argument('-a', '--anc', action='store_true')
args = argparser.parse_args()
if args.method == 'train':
# Train POS tagger
#train_sents = nltk.corpus.conll2000.tagged_sents()
ANCCorpus = nltk.corpus.TaggedCorpusReader(args.corpus, r'.*\.txt', sep='_')
train_sents = ANCCorpus.tagged_sents()
patterns = [
(r'^[Yy]eah$', 'UH'),
(r'^[Oo]+[Hh]+$', 'UH'),
(r'^[UuHhMm]+$', 'UH'),
(r'^[HhAa]+$', 'UH'),
(r'^okay$', 'JJ'),
(r'^[Oo][Kk]$', 'JJ'),
# The follow were copied from http://streamhacker.com/2008/11/10/part-of-speech-tagging-with-nltk-part-2/
(r'^-?[0-9]+(.[0-9]+)?$', 'CD'),
(r'.*ould$', 'MD'),
(r'.*ing$', 'VBG'),
(r'.*ed$', 'VBD'),
(r'.*ness$', 'NN'),
(r'.*ment$', 'NN'),
(r'.*ful$', 'JJ'),
(r'.*ious$', 'JJ'),
(r'.*ble$', 'JJ'),
(r'.*ic$', 'JJ'),
(r'.*ive$', 'JJ'),
(r'.*ic$', 'JJ'),
(r'.*est$', 'JJ'),
(r'^a$', 'IN'),
]
from nltk.tag import brill, brill_trainer
templates = brill.brill24()
from time import time
startTime = time()
tagger = nltk.TrigramTagger(train_sents,
backoff=nltk.BigramTagger(train_sents,
backoff=nltk.UnigramTagger(train_sents,
backoff=nltk.AffixTagger(train_sents,
backoff=nltk.RegexpTagger(patterns)))))
trainer = brill_trainer.BrillTaggerTrainer(tagger, templates)
tagger = trainer.train(train_sents, max_rules=100, min_score=3)
endTime = time()
print('Trained tagger in {:.2f} secs'.format(endTime - startTime))
with open('tagger.pickle', 'wb') as taggerSerial:
pickle.dump(tagger, taggerSerial)
# Train sentence chunker
train_chunks = nltk.corpus.conll2000.chunked_sents()
tag_sents = [nltk.tree2conlltags(tree) for tree in train_chunks]
tag_chunks = [[(t, c) for (w, t, c) in chunk_tags] for chunk_tags in tag_sents]
startTime = time()
chunktagger = nltk.BigramTagger(tag_chunks,
backoff=nltk.UnigramTagger(tag_chunks))
chunker = TagChunker(chunktagger)
endTime = time()
print('Trained chunker in {:.2f} secs'.format(endTime - startTime))
with open('chunker.pickle', 'wb') as chunkerSerial:
pickle.dump(chunker, chunkerSerial)
print()
else:
DEBUG = bool(args.print)
CHUNK = bool(args.chunk)
STANFORD = bool(args.stanford)
outputDir = args.outputdir
print("Output directory: '{}'".format(outputDir))
if outputDir:
WRITEOUT = True
if not os.path.exists(outputDir):
os.makedirs(name=outputDir)
else:
WRITEOUT = False
if STANFORD:
processUtterFunc = stanfordProcess
else:
processUtterFunc = nltkProcess
with open('tagger.pickle', 'rb') as taggerSerial:
tagger = pickle.load(taggerSerial)
print("Loaded tagger")
with open('chunker.pickle', 'rb') as chunkerSerial:
chunker = pickle.load(chunkerSerial)
print("Loaded chunker")
infile = args.infile
if args.talkbank:
processfilefunc = processTBFile
elif args.twitter:
processfilefunc = processTwitterFile
elif args.anc:
processfilefunc = processANCFile
processUtterFunc = processIdent # No processing necessary for OANC
else:
raise Exception("Type of file(s) not specified")
processor = FileProcessor(processfilefunc, processUtterFunc, DEBUG, WRITEOUT, outputDir)
if not STANFORD:
processor.tagger = tagger
processor.chunker = chunker
if os.path.isdir(infile):
files = filter(lambda x: x.endswith('.xml') or x.endswith('.txt'), (str(p) for p in Path(infile).rglob('*.*')))
# Do tree concurrently
with ProcessPoolExecutor() as executor:
for file in files:
executor.submit(processor.processFile, file)
else:
processor.processFile(infile)
if __name__ == '__main__':
main()