-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdata_preprocess_twitter.py
158 lines (128 loc) · 5.07 KB
/
data_preprocess_twitter.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
'''
Biaffine Dependency parser from AllenNLP
'''
import argparse
import json
import os
import re
import sys
from allennlp.predictors.predictor import Predictor
from lxml import etree
from nltk.tokenize import TreebankWordTokenizer
from tqdm import tqdm
MODELS_DIR = '/data1/yangyy/models'
model_path = os.path.join(
MODELS_DIR, "biaffine-dependency-parser-ptb-2018.08.23.tar.gz")
def parse_args():
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument('--model_path', type=str, default=model_path,
help='Path to biaffine dependency parser.')
parser.add_argument('--data_path', type=str, default='/data1/SHENWZH/ABSA_online/data/twitter',
help='Directory of where semeval14 or twiiter data held.')
return parser.parse_args()
sentiment_map = {0: 'neutral', 1: 'positive', -1: 'negative'}
def read_file(file_name):
'''
Read twitter data and extract text and store.
return sentences of [sentence, aspect_sentiment, from_to]
'''
with open(file_name, 'r') as f:
data = f.readlines()
data = [d.strip('\n') for d in data]
# list of dict {text, aspect, sentiment}
sentences = []
idx = 0
while idx < len(data):
text = data[idx]
idx += 1
aspect = data[idx]
idx += 1
sentiment = data[idx]
idx += 1
sentence = get_sentence(text, aspect, sentiment)
sentences.append(sentence)
print(file_name, len(sentences))
with open(file_name.replace('.raw', '.txt'), 'w') as f:
for sentence in sentences:
f.write(sentence['sentence'] + '\n')
return sentences
def get_sentence(text, aspect, sentiment):
sentence = dict()
sentence['sentence'] = text.replace('$T$', aspect)
sentence['aspect_sentiment'] = [[aspect, sentiment_map[int(sentiment)]]]
frm = text.split().index('$T$')
to = frm + len(aspect.split())
sentence['from_to'] = [[frm, to]]
return sentence
def text2docs(file_path, predictor):
'''
Annotate the sentences from extracted txt file using AllenNLP's predictor.
'''
with open(file_path, 'r') as f:
sentences = f.readlines()
docs = []
print('Predicting dependency information...')
for i in tqdm(range(len(sentences))):
docs.append(predictor.predict(sentence=sentences[i]))
return docs
def dependencies2format(doc): # doc.sentences[i]
'''
Format annotation: sentence of keys
- tokens
- tags
- predicted_dependencies
- predicted_heads
- dependencies
'''
sentence = {}
sentence['tokens'] = doc['words']
sentence['tags'] = doc['pos']
# sentence['energy'] = doc['energy']
predicted_dependencies = doc['predicted_dependencies']
predicted_heads = doc['predicted_heads']
sentence['predicted_dependencies'] = doc['predicted_dependencies']
sentence['predicted_heads'] = doc['predicted_heads']
sentence['dependencies'] = []
for idx, item in enumerate(predicted_dependencies):
dep_tag = item
frm = predicted_heads[idx]
to = idx + 1
sentence['dependencies'].append([dep_tag, frm, to])
return sentence
def get_dependencies(file_path, predictor):
docs = text2docs(file_path, predictor)
sentences = [dependencies2format(doc) for doc in docs]
return sentences
def syntaxInfo2json(sentences, sentences_with_dep, file_name):
json_data = []
tk = TreebankWordTokenizer()
# mismatch_counter = 0
for idx, sentence in enumerate(sentences):
sentence['tokens'] = sentences_with_dep[idx]['tokens']
sentence['tags'] = sentences_with_dep[idx]['tags']
sentence['predicted_dependencies'] = sentences_with_dep[idx]['predicted_dependencies']
sentence['dependencies'] = sentences_with_dep[idx]['dependencies']
sentence['predicted_heads'] = sentences_with_dep[idx]['predicted_heads']
# sentence['energy'] = sentences_with_dep[idx]['energy']
json_data.append(sentence)
with open(file_name.replace('.txt', '_biaffine.json'), 'w') as f:
json.dump(json_data, f)
print('done', len(json_data))
def main():
args = parse_args()
predictor = Predictor.from_path(args.model_path)
train_file = os.path.join(args.data_path, 'train.raw')
test_file = os.path.join(args.data_path, 'test.raw')
# raw -> txt
train_sentences = read_file(train_file)
test_sentences = read_file(test_file)
# Get dependency annotation
train_sentences_with_dep = get_dependencies(os.path.join(args.data_path, 'train.txt'), predictor)
test_sentences_with_dep = get_dependencies(os.path.join(args.data_path, 'test.txt'), predictor)
print(len(train_sentences), len(test_sentences))
# to json
syntaxInfo2json(train_sentences, train_sentences_with_dep, os.path.join(args.data_path, 'train.txt'))
syntaxInfo2json(test_sentences, test_sentences_with_dep, os.path.join(args.data_path, 'test.txt'))
if __name__ == "__main__":
main()