-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_politeness.py
173 lines (152 loc) · 6.46 KB
/
parser_politeness.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
import numpy as np
import sys
import os
# import cPickle
import csv
import pickle as pkl
import scipy
from scipy.sparse import csr_matrix
import sklearn
import nltk
import spacy
from data_utils import DebateParser
from IPython import embed
from tqdm import tqdm
import json
nlp = spacy.load('en')
def get_parse(doc):
parse = dict()
parse['parse'] = list()
parse['sentences'] = list()
for sent in nlp(doc).sents:
sent_parse = list()
for token in sent:
dep = token.dep_
current_id = token.i
if(token.head == token):
parent_id = 0
parent = "ROOT"
else:
parent = token.head.text
parent_id = token.head.i
token_parse = dep + '(' + parent + '-' + str(parent_id) + ', ' + token.text + '-' + str(current_id) + ')'
sent_parse.append(token_parse)
parse['parse'].append(sent_parse)
parse['sentences'].append(sent.text)
return parse
def main():
# with open('../data/comments_forPoliteness - comments_forPoliteness.csv') as f, open('../outputs/nic_comments.pkl', 'wb') as f2:
# reader = csv.reader(f)
# new_list = list()
# for row in tqdm(reader):
# parse = get_parse(row[1])
# row.append(parse)
# new_list.append(row)
# pkl.dump(new_list, f2, 2)
# with open('../outputs/minute_toxicity.pkl', 'rb') as f, open('../outputs/minute_parses.pkl', 'wb') as f2:
# data = pkl.load(f)
# new_rows = list()
# for row in tqdm(data):
# parse = get_parse(row[0])
# new_rows.append(parse)
# pkl.dump(new_rows, f2, 2)
with open('../outputs/h_toxicities.csv') as f, open('../outputs/h_toxicity_parse.pkl', 'wb') as f2:
reader = csv.reader(f)
parses = list()
for row in tqdm(reader):
parse = get_parse(row[0])
parses.append(parse)
pkl.dump(parses, f2, 2)
with open('../outputs/m_toxicities.csv') as f, open('../outputs/m_toxicity_parse.pkl', 'wb') as f2:
reader = csv.reader(f)
parses = list()
for row in tqdm(reader):
parse = get_parse(row[0])
parses.append(parse)
pkl.dump(parses, f2, 2)
with open('../outputs/p_toxicities.csv') as f, open('../outputs/p_toxicity_parse.pkl', 'wb') as f2:
reader = csv.reader(f)
parses = list()
for row in tqdm(reader):
parse = get_parse(row[0])
parses.append(parse)
pkl.dump(parses, f2, 2)
# with open('../outputs/h_sentences.csv') as f, open('../outputs/parsed_h.pkl', 'wb') as f2:
# reader = csv.reader(f)
# new_list = list()
# for row in tqdm(reader):
# parse = get_parse(row[0])
# row.append(parse)
# new_list.append(row)
# pkl.dump(new_list, f2, 2)
# with open('../outputs/p_sentences.csv') as f, open('../outputs/parsed_p.pkl', 'wb') as f2:
# reader = csv.reader(f)
# new_list = list()
# for row in tqdm(reader):
# parse = get_parse(row[0])
# row.append(parse)
# new_list.append(row)
# pkl.dump(new_list, f2, 2)
# with open('../outputs/m_sentences.csv') as f, open('../outputs/parsed_m.pkl', 'wb') as f2:
# reader = csv.reader(f)
# new_list = list()
# for row in tqdm(reader):
# parse = get_parse(row[0])
# row.append(parse)
# new_list.append(row)
# pkl.dump(new_list, f2, 2)
# with open('../outputs/combined_tuples_d.csv', 'r') as f, open('../outputs/combined_politeness_d.pkl', 'wb') as f2:
# reader = csv.reader(f)
# new_list = list()
# for row in tqdm(reader):
# parse = get_parse(row[0])
# row.append(parse)
# new_list.append(row)
# pkl.dump(new_list, f2, 2)
# with open('../outputs/h_toxicities.pkl', 'rb') as f, open('h_toxicities_2.pkl', 'wb') as f2:
# toxicities = pkl.load(f)
# for dialogue in tqdm(toxicities):
# dialogue['parse'] = get_parse(dialogue['text'])
# dialogue['sentences'] = list()
# sentences = list(nlp(dialogue['text']).sents)
# if(len(sentences) != len(dialogue['sentence_toxicities'])):
# continue
# for idx, sentence in enumerate(sentences):
# sentence_dict = dict()
# sentence_dict['text'] = sentence.text
# sentence_dict['toxicity'] = dialogue['sentence_toxicities'][idx]
# sentence_dict['parse'] = get_parse(sentence.text)
# dialogue['sentences'].append(sentence_dict)
# pkl.dump(toxicities, f2, 2)
# with open('../outputs/m_toxicities.pkl', 'rb') as f, open('m_toxicities_2.pkl', 'wb') as f2:
# toxicities = pkl.load(f)
# for dialogue in tqdm(toxicities):
# dialogue['parse'] = get_parse(dialogue['text'])
# dialogue['sentences'] = list()
# sentences = list(nlp(dialogue['text']).sents)
# if(len(sentences) != len(dialogue['sentence_toxicities'])):
# continue
# for idx, sentence in enumerate(nlp(dialogue['text']).sents):
# sentence_dict = dict()
# sentence_dict['text'] = sentence.text
# sentence_dict['toxicity'] = dialogue['sentence_toxicities'][idx]
# sentence_dict['parse'] = get_parse(sentence.text)
# dialogue['sentences'].append(sentence_dict)
# pkl.dump(toxicities, f2, 2)
# with open('../outputs/p_toxicities.pkl', 'rb') as f, open('p_toxicities_2.pkl', 'wb') as f2:
# toxicities = pkl.load(f)
# for dialogue in tqdm(toxicities):
# dialogue['parse'] = get_parse(dialogue['text'])
# dialogue['sentences'] = list()
# sentences = list(nlp(dialogue['text']).sents)
# if(len(sentences) != len(dialogue['sentence_toxicities'])):
# continue
# for idx, sentence in enumerate(nlp(dialogue['text']).sents):
# sentence_dict = dict()
# sentence_dict['text'] = sentence.text
# sentence_dict['toxicity'] = dialogue['sentence_toxicities'][idx]
# sentence_dict['parse'] = get_parse(sentence.text)
# dialogue['sentences'].append(sentence_dict)
# pkl.dump(toxicities, f2, 2)
if __name__ == '__main__':
main()