-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_dataset.py
155 lines (132 loc) · 3.7 KB
/
parse_dataset.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
import argparse
import json
import os
import re
from collections import Counter
from parse_utils import comment_to_tokens
def main():
os.makedirs(OPTS.out_dir, exist_ok=True)
sequences_file = os.path.join(OPTS.out_dir, 'sequences.json')
id2token_file = os.path.join(OPTS.out_dir, 'id2token.json')
lexicon_file = os.path.join(OPTS.out_dir, 'lexicon.txt')
sequences, id2token, tokens_count = parse_dataset(
OPTS.dataset_dir,
lexicon_limit=OPTS.lexicon_limit,
seq_min_len=OPTS.sequence_min_len,
seq_max_len=OPTS.sequence_max_len
)
with open(sequences_file, 'w') as f:
f.write(json.dumps(sequences))
with open(id2token_file, 'wb') as f:
f.write(json.dumps(id2token, ensure_ascii=False).encode('utf-8'))
tcs = list(tokens_count.items())
tcs.sort(key=lambda tc: tc[1])
with open(lexicon_file, 'wb') as f:
f.write('\n'.join([t + ' ' + str(c) for t, c in tcs]).encode('utf-8'))
print('')
print('tokens count:')
for i in range(1, 10):
print('>= %d' % i, len(list(filter(lambda tc: tc[1] >= i, tcs))))
def parse_dataset(ds_dir, lexicon_limit, seq_min_len, seq_max_len):
id2token = [
'<pad>',
'<unk>',
'<eol>',
'<eoc>',
'<n>',
]
token2id = {v: i for i, v in enumerate(id2token)}
pairs = []
files = os.listdir(ds_dir)
files.sort()
for i in range(len(files)):
file = files[i]
if not re.match('\d+\.txt', file):
continue
with open(os.path.join(ds_dir, file), 'rb') as f:
content = f.read().decode('utf-8')
t_pairs = parse_comment_pairs(content, seq_min_len, seq_max_len)
pairs.extend(t_pairs)
print(i, '============================ ' + file + ' ==============================')
for pair in t_pairs:
print(' '.join(pair))
print('')
print('sequences:', len(pairs))
tokens_count = collect_lexicon(pairs)
for tc in tokens_count.most_common(lexicon_limit):
token = tc[0]
if token not in token2id:
id2token.append(token)
token2id[token] = len(token2id)
print('lexicon:', len(id2token))
pairs_ids = tokens_to_ids(pairs, id2token)
return (pairs_ids, id2token, dict(tokens_count))
def parse_comment_pairs(thread_content, min_len, max_len):
tokens = []
pairs = thread_to_comment_pairs(thread_content)
for pair in pairs:
if len(' '.join(pair).split(' ')) > max_len:
continue
c1 = comment_to_tokens(pair[0])
c2 = None
if c1:
c2 = comment_to_tokens(pair[1])
if c1 and c2:
l = len(c1) + len(c2)
if l >= min_len and l <= max_len:
pair_tokens = c1
pair_tokens.extend(c2)
tokens.append(pair_tokens)
return tokens
def thread_to_comment_pairs(thread_content):
pairs = []
comments = thread_content.split('\n\n')
for i in range(0, len(comments) - 1, 2):
pairs.append((comments[i], comments[i + 1]))
return pairs
def collect_lexicon(pairs):
lexicon = Counter()
for pair_tokens in pairs:
lexicon.update(pair_tokens)
return lexicon
def tokens_to_ids(pairs, id2token):
token2id = {token: id for id, token in enumerate(id2token)}
pairs_ids = []
for pair in pairs:
pairs_ids.append(
[token2id[token] if token in token2id else token2id['<unk>'] \
for token in pair]
)
return pairs_ids
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--dataset_dir',
type=str,
default='./dataset/threads/'
)
parser.add_argument(
'--out_dir',
type=str,
default='./dataset/parsed/'
)
parser.add_argument(
'--lexicon_limit',
type=int,
required=True,
help='How many most often words use to train (others will be unknown)'
)
parser.add_argument(
'--sequence_min_len',
type=int,
default=8,
help='Min length of the pair of comments (words)'
)
parser.add_argument(
'--sequence_max_len',
type=int,
default=100,
help='Max length of the pair of comments (words)'
)
OPTS = parser.parse_args()
main()