-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.py
168 lines (141 loc) · 5.39 KB
/
format.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
from __future__ import print_function
from collections import Counter
import csv
import datetime
import json
import os
import re
import string
import sys
class DebateDoc:
def __init__(self, speaker, id, text, **kargs):
self.speaker = speaker
self.id = id
self.text = text
for k, v in kargs.iteritems():
setattr(self, "_%s" % (k), v)
def name(self):
return str(self.id)
def start_time(self):
try:
hms = [int(n) for n in self._start_time.split(':')]
return datetime.time(*hms)
except AttributeError:
return None
def end_time(self):
try:
hms = [int(n) for n in self._stop_time.split(':')]
return datetime.time(*hms)
except AttributeError:
return None
def qtopic(self):
return self._qtopic[0]
def topic(self):
return Counter(self._topic).most_common()[0][0]
def frame(self):
return Counter(self._frame).most_common()[0][0]
def tone(self):
return Counter(self._tone).most_common()[0][0]
def __str__(self):
return "%d,%s,%s,%s,%s,%s,%s,%s" % (self.id, self.speaker,
self.start_time(), self.end_time(),
self.qtopic(), self.topic(),
self.frame(), self.tone())
class DebateDocSet:
def __init__(self):
self._train = []
self._test = []
self._next_id = 0
def add_docs(self, corpus_info):
corpus_fname = corpus_info['fname']
speaker_field = corpus_info['speaker_field']
text_field = corpus_info['text_field']
with open(corpus_fname) as f:
csvreader = csv.reader(f, delimiter=',', quotechar='"')
for n, row in enumerate(csvreader):
speaker = row[speaker_field]
text = row[text_field].strip()
# Corner cases
if n == 0:
continue
if n == 1:
last_speaker = speaker
turn = self._new_turn(text)
turn_data = self._new_turn_data(corpus_info, row)
continue
# Normal cases
if speaker != last_speaker:
self._add_doc(last_speaker, turn, turn_data,
corpus_info['unlabeled_corpus'])
last_speaker = speaker
turn = self._new_turn(text)
turn_data = self._new_turn_data(corpus_info, row)
else:
text = text.translate(string.maketrans("", ""), '"')
turn.append("%s" % (text))
self._add_to_turn_data(corpus_info, row, turn_data)
self._add_doc(last_speaker, turn, turn_data,
corpus_info['unlabeled_corpus'])
def _add_doc(self, speaker, turn, misc_data, test_corpus):
turn_text = ' '.join(turn).lower()
doc = DebateDoc(speaker, self._next_id, turn_text, **misc_data)
if test_corpus:
self._test.append(doc)
else:
self._train.append(doc)
self._next_id += 1
def _new_turn(self, text):
# Turns in the presidential debate corpus start with NAME:
# This removes those names.
if re.match(r'^\w+:.*', text):
return [text.split(':')[1].strip()]
return [text]
def _new_turn_data(self, corpus_info, line):
turn_data = {}
if 'time' in corpus_info:
turn_data['start_time'] = line[corpus_info['time']['start']]
turn_data['stop_time'] = line[corpus_info['time']['stop']]
for field_type, field_index in corpus_info['misc'].iteritems():
turn_data[str(field_type)] = [line[field_index]]
return turn_data
def _add_to_turn_data(self, corpus_info, line, turn_data):
if 'time' in corpus_info:
turn_data['stop_time'] = line[corpus_info['time']['stop']]
for field_type, field_index in corpus_info['misc'].iteritems():
turn_data[str(field_type)].append(line[field_index])
def dump_malletdir(self, dirname):
try:
os.mkdir(dirname)
except OSError:
decision = raw_input('%s/ exists, continue anyway? (y,n): '
% (dirname))
if decision != 'y':
sys.exit(1)
for doc in self._train + self._test:
with open(os.path.join(dirname, doc.name()), 'w') as f:
f.write(doc.text)
def dump_doc_data(self, fname):
with open("%s.labeled" % (fname), 'w') as f:
for doc in self._train:
print(doc, file=f)
with open("%s.unlabeled" % (fname), 'w') as f:
for doc in self._test:
print(doc, file=f)
def complete_dump(self, name):
self.dump_malletdir(name)
self.dump_doc_data(name)
def main(argv):
if len(argv) < 2:
print("USAGE: python format.py output_name corpus_info_file")
ds = DebateDocSet()
outdir = argv[0]
argv = argv[1:]
input_file = argv[0]
argv = argv[1:]
with open(input_file) as f:
corpora = json.loads(f.read())
for corpus_info in corpora:
ds.add_docs(corpus_info)
ds.complete_dump(outdir)
if __name__ == "__main__":
main(sys.argv[1:])