-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm.py
205 lines (147 loc) · 4.59 KB
/
tm.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
import pandas as pd
import re
from sklearn.model_selection import train_test_split
# %%
# uploading data needed
df = pd.read_parquet(r'/Users/apple/BDML/data/theta_transposed_сс_rus.parquet.gzip')
#df.drop(columns=['text'], inplace=True)
# %%
# text: raw before concatination
initial_text = pd.read_csv(r'/Users/apple/BDML/data/group_posts_divided.csv')
#
# %%
lst = df.columns[:len(df.columns)-2]
print(lst)
# %%
import os
def create_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
print("Created Directory : ", dir)
else:
print("Directory already existed : ", dir)
return dir
def fix_length(df, length = 100):
df['length'] = df.text.str.len()
df = df.loc[df['length'] > length]
df.drop(columns=['length'], inplace=True)
len(df)
return df
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def build_dataset(df, dest_path):
f = open(dest_path, 'w')
data = ''
summaries = df['text'].tolist()
for summary in summaries:
summary = str(summary).strip()
summary = re.sub(r"\s", " ", summary)
bos_token = '<BOS>'
eos_token = '<EOS>'
data += bos_token + ' ' + summary + ' ' + eos_token + '\n'
f.write(data)
# %%
# choosing topic
data_topic = 'игра команда место'
a = pd.DataFrame()
a['theme'] = df.drop(columns=['owner_id']) \
.idxmax(axis=1)
# a['text'] = df['text']
a['owner_id'] = df['owner_id']
a['coef'] = df[data_topic]
a = a.loc[a['theme'] == data_topic] \
.drop(columns=['theme']) \
.sort_values('coef', ascending=False) \
.reset_index(drop=True)
# taking top 25% of the topic data
# a = a.loc[a['coef'] < 0.95].head(round(len(a)*0.25))
a = a.loc[a['coef'] < 0.95] # .head(40000)
a = a.loc[a['coef'] > 0.65]
# %%
# a = a.merge(initial_text,how='left', on=['owner_id'])
# a = fix_length(a, 100)
# a.drop_duplicates(inplace = True)
# a['text'] = a['text'].apply(lambda x: cleanhtml(x))
# a = fix_length(a, 100)
# a.drop_duplicates(inplace = True)
# %%
df_sample = a.sample(frac=1).reset_index(drop=True)
test_data = df_sample.text[:round(len(df_sample) / 4)]
train_data = df_sample.text[round(len(df_sample) / 4):]
# saving the samples
file_test = open("validation.txt", "w")
file_train = open('train.txt', "w")
for i in train_data:
file_train.write(i + '. \n')
file_train.close()
for i in test_data:
file_test.write(i + '. \n')
file_test.close()
# %%
train_valid_ratio = 7 / 9
df_train, df_valid = train_test_split(a, train_size=train_valid_ratio, random_state=1)
build_dataset(df_train, 'train.txt')
build_dataset(df_valid, 'validation.txt')
# %%
# saving into the tree of the datasets
train_valid_ratio = 7 / 9
dataframes = dict()
temp = pd.DataFrame()
temp['theme'] = df.drop(columns=['text', 'owner_id']) \
.idxmax(axis=1)
temp['text'] = df['text']
for n in lst:
print(n)
create_dir('pre_gpt/'+n)
dataframes[n] = temp
dataframes[n]['coef'] = df[n]
dataframes[n] = dataframes[n].loc[dataframes[n]['coef'] < 0.95] \
.loc[dataframes[n]['coef'] > 0.65] \
.drop_duplicates(subset='text') \
.sort_values('coef', ascending=False) \
.reset_index(drop=True)
df_train, df_valid = train_test_split(dataframes[n],
train_size=train_valid_ratio,
random_state=1)
build_dataset(df_train, 'pre_gpt/'+n+'/train.txt')
build_dataset(df_valid, 'pre_gpt/'+n+'/validation.txt')
# %%
# saving as the single file
file_multitrain = open('multitrain.txt', "w")
for d in dataframes.values():
for i in d['text']:
file_multitrain.write(i + '. \n')
file_multitrain.close()
# %% OR
dataframe = pd.DataFrame()
for i in dataframes.values():
dataframe = pd.concat([dataframe, i])
dataframe.drop(['coef', 'theme'],
axis=1,
inplace=True)
print((len(dataframe)))
dataframe.to_parquet(r'train_high_score.parquet.gzip', compression='gzip')
# %%
temp = pd.DataFrame()
temp['theme'] = df.drop(columns=['text', 'owner_id']) \
.idxmax(axis=1)
temp['text'] = df['text']
file_multitrain = open('multitrain.txt', "w")
for n in df.columns:
print(n)
d = temp
d['coef'] = df[n]
d = d.drop_duplicates(subset='text') \
.sort_values('coef', ascending=False) \
.reset_index(drop=True) \
.loc[d['coef'] < 0.95] \
.loc[d['coef'] > 0.65]
for i in d['text']:
file_multitrain.write(i + '. \n')
file_multitrain.close()
# %%