-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm_n1.py
287 lines (248 loc) · 9.43 KB
/
algorithm_n1.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
###########################################################################
# proposal-n1.py
#
# Alternative proposal for current CorporaCreator implementation
#
# It uses validated.tsv to re-create train, dev, test splits
# using lowercased sentence recording frequencies.
#
# A sentence only lives in one split.
#
# This version does not take voices into account, a voice can live
# in any split.
#
# It practically uses the whole dataset.
#
# The script works on multiple CV versions and locales.
#
# The data is grouped as:
# experiments - Common Voice versions - locales - splits
#
# Use:
# python proposal-n1.py
#
# This script is part of Common Voice ToolBox Package
#
# github: https://github.com/HarikalarKutusu/common-voice-diversity-check
# Copyright: (c) Bülent Özden, License: AGPL v3.0
###########################################################################
import os
import sys
import shutil
import glob
import csv
# from datetime import datetime
import pandas as pd
HERE: str = os.path.dirname(os.path.realpath(__file__))
if not HERE in sys.path:
sys.path.append(HERE)
# Constants - TODO These should be arguments
SOURCE_EXPERIMENT_DIR: str = "s1"
DESTINATION_EXPERIMENT_DIR: str = "n1"
TRAIN_PERCENTAGE: float = 80.0
DEV_PERCENTAGE: float = 10.0
TEST_PERCENTAGE: float = 10.0
# Program parameters
VERBOSE: bool = True
FAIL_ON_NOT_FOUND: bool = True
#
# DataFrame file read-write
#
def df_read(fpath: str) -> pd.DataFrame:
"""Read a tsv file into a dataframe"""
if not os.path.isfile(fpath):
print(f"FATAL: File {fpath} cannot be located!")
if FAIL_ON_NOT_FOUND:
sys.exit(1)
df: pd.DataFrame = pd.read_csv(
fpath,
sep="\t",
parse_dates=False,
engine="python",
encoding="utf-8",
on_bad_lines="skip",
quotechar='"',
quoting=csv.QUOTE_NONE,
)
return df
def df_write(df: pd.DataFrame, fpath: str) -> None:
"""Write dataframe to a tsv file"""
df.to_csv(
fpath,
header=True,
index=False,
encoding="utf-8",
sep="\t",
escapechar="\\",
quoting=csv.QUOTE_NONE,
)
#
# Handle one split creation, this is where calculations happen
#
def corpora_creator_v2(pth: str):
"""Processes validated.tsv and create new train, dev, test splits"""
def _check_intersection(
df1: pd.DataFrame, df2: pd.DataFrame, df1_name: str, df2_name: str
) -> None:
_intersect_df: pd.DataFrame = pd.merge(df1, df2, how="inner")
if _intersect_df.shape[0] > 0:
print(
f"!!! ERROR IN ALGORITHM, SPLIT INTERSECTION FOUND - {df1_name} vs {df2_name}"
)
print(_intersect_df.head(999))
sys.exit(1)
elif VERBOSE:
print(f"--- No intersection found - {df1_name} vs {df2_name} splits")
validated_path: str = os.path.join(pth, "validated.tsv")
validated_df: pd.DataFrame = df_read(validated_path)
# add lowercase sentence column
validated_df["sentence_lower"] = validated_df["sentence"].str.lower()
# get unique lowercase sentences
sentences_df: pd.DataFrame = (
validated_df.groupby("sentence_lower").agg({"path": "count"}).reset_index()
) # get kst with count agg
sentences_df.rename(
columns={"path": "recorded_count"}, inplace=True
) # rename agg column
sentences_df.sort_values(
by="recorded_count", ascending=True, inplace=True
) # sort in ascending recorded count
sentences_df["cumulative"] = sentences_df[
"recorded_count"
].cumsum() # add a cumulative sum for easy access
sentences_df.reset_index()
# CALCULATE split sizes as record counts
total_validated: int = validated_df.shape[0]
total_sentences: int = sentences_df.shape[0]
test_target: int = int(TEST_PERCENTAGE / 100 * total_validated)
dev_target: int = int(DEV_PERCENTAGE / 100 * total_validated)
train_target: int = total_validated - dev_target - test_target
if VERBOSE:
print(
f">>> Processing - {total_validated} validated records with {total_sentences} lower-case unique sentences."
)
print(
f">>> Targeting - Train: {TRAIN_PERCENTAGE}%=>{train_target} recs, Dev: {DEV_PERCENTAGE}%=>{dev_target} recs, Test: {TEST_PERCENTAGE}%=>{test_target} recs "
)
# print()
# Test
_slice: pd.DataFrame = sentences_df[
sentences_df["cumulative"].astype(int) <= test_target
] # use cumulative column to get list of sentences to match the amount
_sentences: list[str] = _slice["sentence_lower"].to_list() # convert to list
test_df: pd.DataFrame = validated_df[
validated_df["sentence_lower"].isin(_sentences)
] # select all validated records for that list
test_df: pd.DataFrame = test_df.drop(
columns=["sentence_lower"], errors="ignore"
) # drop temp columns
test_voices: "list[str]" = test_df["client_id"].to_list()
df_write(test_df, os.path.join(pth, "test.tsv")) # output the result
# Dev
_slice: pd.DataFrame = sentences_df[
(sentences_df["cumulative"].astype(int) > test_target)
& (sentences_df["cumulative"].astype(int) <= test_target + dev_target)
]
_sentences: list[str] = _slice["sentence_lower"].to_list()
dev_df: pd.DataFrame = validated_df[validated_df["sentence_lower"].isin(_sentences)]
dev_df: pd.DataFrame = dev_df.drop(columns=["sentence_lower"], errors="ignore")
dev_voices: "list[str]" = dev_df["client_id"].to_list()
df_write(dev_df, os.path.join(pth, "dev.tsv"))
# check any possible intersection(s)
_check_intersection(
df1=dev_df, df2=test_df, df1_name="DEV Split", df2_name="TEST Split"
)
# Train
_slice: pd.DataFrame = sentences_df[
sentences_df["cumulative"].astype(int) > test_target + dev_target
]
_sentences: list[str] = _slice["sentence_lower"].to_list()
train_df: pd.DataFrame = validated_df[
validated_df["sentence_lower"].isin(_sentences)
]
train_df: pd.DataFrame = train_df.drop(columns=["sentence_lower"], errors="ignore")
train_voices: "list[str]" = train_df["client_id"].to_list()
df_write(train_df, os.path.join(pth, "train.tsv"))
# check any possible intersection(s)
_check_intersection(
df1=train_df, df2=test_df, df1_name="TRAIN Split", df2_name="TEST Split"
)
_check_intersection(
df1=train_df, df2=dev_df, df1_name="TRAIN Split", df2_name="DEV Split"
)
if VERBOSE:
print(
f">>> Result splits - Train: {train_df.shape[0]} recs, Dev: {dev_df.shape[0]} recs, Test: {test_df.shape[0]} recs "
)
print("--- Voice overlaps:")
print(
f"--- TRAIN has voices already in DEV: {len(set(train_voices).intersection(dev_voices))}"
)
print(
f"--- TRAIN has voices already in TEST: {len(set(train_voices).intersection(test_voices))}"
)
print(
f"--- DEV has voices already in TEST: {len(set(dev_voices).intersection(test_voices))}"
)
# done
#
# Main loop for versions-locales
#
def main() -> None:
"""A New Corpora Creator Algorithm for Common Voice Datasets"""
print("=== A New Corpora Creator Algorithm for Common Voice Datasets ===")
# Copy source experiment tree to destination experiment
experiments_path: str = os.path.join(HERE, "experiments")
src_exppath: str = os.path.join(experiments_path, SOURCE_EXPERIMENT_DIR)
dst_exppath: str = os.path.join(experiments_path, DESTINATION_EXPERIMENT_DIR)
shutil.copytree(src=src_exppath, dst=dst_exppath, dirs_exist_ok=True)
# !!! from now on we will work on destination !!!
# Remove old files
to_delete: "list[str]" = []
to_delete.extend(
glob.glob(os.path.join(dst_exppath, "**", "train.tsv"), recursive=True)
)
to_delete.extend(
glob.glob(os.path.join(dst_exppath, "**", "dev.tsv"), recursive=True)
)
to_delete.extend(
glob.glob(os.path.join(dst_exppath, "**", "test.tsv"), recursive=True)
)
for fpath in to_delete:
os.remove(fpath)
exp_corpora_paths: "list[str]" = glob.glob(
os.path.join(dst_exppath, "*"), recursive=False
)
# Get total for progress display
all_validated: "list[str]" = glob.glob(
os.path.join(dst_exppath, "**", "validated.tsv"), recursive=True
)
print(f"Re-splitting for {len(all_validated)} corpora...")
print() # extra line is for progress line
# For each corpus
cnt: int = 0 # counter of corpora done
for corpus_path in exp_corpora_paths:
exp_corpus_name: str = os.path.split(corpus_path)[-1]
if VERBOSE:
print(f"== Processing Corpus: {exp_corpus_name}")
# Now get the list of locales
exp_corpus_locale_paths: "list[str]" = glob.glob(
os.path.join(corpus_path, "*"), recursive=False
)
# For each locale
for locale_path in exp_corpus_locale_paths:
locale_name: str = os.path.split(locale_path)[-1]
cnt += 1
if VERBOSE:
print(f"=== Processing Locale: {locale_name}")
else:
print("\033[F" + " " * 80)
print(
f"\033[FProcessing {cnt}/{all_validated} => {exp_corpus_name} - {locale_name}"
)
# apply algorithm (splits are created there)
corpora_creator_v2(pth=locale_path)
# done locales in version
# done version in versions
main()