-
Notifications
You must be signed in to change notification settings - Fork 1
/
clancomments2.py
72 lines (59 loc) · 2.15 KB
/
clancomments2.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
import pyclan as pc
import os
import argparse
import csv
from shutil import copy
processed_dir = "processed2"
def parse_dirs(dirs):
comms = []
for dir in dirs:
for root, dir_dirs, files in os.walk(dir):
for file in files:
if file.endswith(".cha"):
print file
cf = pc.ClanFile(os.path.join(root, file))
comments = cf.get_user_comments()
# print "{} -- {} comments".format(file, len(comments))
os.rename(os.path.join(root, file),
os.path.join(processed_dir, file))
# if not any("silence" in x.content for x in comments):
# print "\tno silences here"
output_csv([(file, comments)])
# comms.append((file, comments))
return comms
def parse_files(files):
comms = []
for file in files:
if file.endswith(".cha"):
cf = pc.ClanFile(file)
comments = cf.get_user_comments()
print "{} -- {} comments".format(file, len(comments))
comms.append((file, comments))
return comms
def output_csv(comments, fname="clancomments.csv"):
if os.path.exists("clancomments.csv"):
exists = True
else:
exists = False
with open(fname, "a") as out:
writer = csv.writer(out)
if not exists:
writer.writerow(["file", 'onset', 'comment'])
for group in comments:
for comm in group[1]:
writer.writerow([group[0], comm.onset, comm.content])
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Pull out all the comments in CLAN files")
parser.add_argument("--dirs", nargs="+", type=str,
help="Directories with CLAN files")
parser.add_argument("--files", nargs="+", type=str, help="CLAN files")
args = parser.parse_args()
if args.dirs:
output_csv([])
comments = parse_dirs(args.dirs)
if args.files:
# output_csv([])
comments = parse_files(args.files)
output_csv(comments)
# output_csv(comments)