Add ratio filtering script for ST
Browse files
scripts/filter_tsv_based_on_ratio.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025 FBK
|
| 2 |
+
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License
|
| 14 |
+
|
| 15 |
+
import argparse
|
| 16 |
+
import csv
|
| 17 |
+
import logging
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def filter_data(tsv_in, threshold_min, threshold_max, tsv_out):
|
| 22 |
+
# Setup logger
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
| 25 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 26 |
+
level=os.environ.get("LOGLEVEL", "INFO").upper(),
|
| 27 |
+
)
|
| 28 |
+
logger = logging.getLogger("filter_on_char_ratio")
|
| 29 |
+
filtered = 0
|
| 30 |
+
with open(tsv_in, 'r') as in_f, open(tsv_out, 'w') as out_f:
|
| 31 |
+
# Copy header
|
| 32 |
+
reader = csv.DictReader(
|
| 33 |
+
in_f,
|
| 34 |
+
delimiter="\t",
|
| 35 |
+
quotechar=None,
|
| 36 |
+
doublequote=False,
|
| 37 |
+
lineterminator="\n",
|
| 38 |
+
quoting=csv.QUOTE_NONE,
|
| 39 |
+
)
|
| 40 |
+
writer = csv.DictWriter(
|
| 41 |
+
out_f,
|
| 42 |
+
fieldnames=reader.fieldnames,
|
| 43 |
+
delimiter="\t",
|
| 44 |
+
quotechar=None,
|
| 45 |
+
doublequote=False,
|
| 46 |
+
lineterminator="\n",
|
| 47 |
+
quoting=csv.QUOTE_NONE,
|
| 48 |
+
)
|
| 49 |
+
writer.writeheader()
|
| 50 |
+
for sample in reader:
|
| 51 |
+
filtered += 1
|
| 52 |
+
if len(sample['src_text']) > 0:
|
| 53 |
+
ratio = float(len(sample['tgt_text'])) / len(sample['src_text'])
|
| 54 |
+
if threshold_min < ratio < threshold_max:
|
| 55 |
+
writer.writerow(sample)
|
| 56 |
+
filtered -= 1
|
| 57 |
+
logger.info(f"Filtered rows: {filtered}")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def process_multiple_files(tsv_files, threshold_min, threshold_max, tsv_out_dir):
|
| 61 |
+
for tsv_in in tsv_files:
|
| 62 |
+
# Create output file path
|
| 63 |
+
filename = os.path.basename(tsv_in)
|
| 64 |
+
tsv_out = os.path.join(tsv_out_dir, filename)
|
| 65 |
+
filter_data(tsv_in, threshold_min, threshold_max, tsv_out)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
parser = argparse.ArgumentParser(description="Filter based on character ratio")
|
| 70 |
+
parser.add_argument("--tsv-datasets", required=True, nargs='+', help="List of TSV files to process")
|
| 71 |
+
parser.add_argument("--tsv-out-dir", required=True, help="Directory to save the filtered TSV files")
|
| 72 |
+
parser.add_argument("--threshold-min", required=True, type=float,
|
| 73 |
+
help="The minimum ratio admitted. Everything with a lower ratio is filtered.")
|
| 74 |
+
parser.add_argument("--threshold-max", required=True, type=float,
|
| 75 |
+
help="The maximum ratio admitted. Everything with a higher ratio is filtered.")
|
| 76 |
+
|
| 77 |
+
args = parser.parse_args()
|
| 78 |
+
|
| 79 |
+
# Ensure the output directory exists
|
| 80 |
+
os.makedirs(args.tsv_out_dir, exist_ok=True)
|
| 81 |
+
|
| 82 |
+
# Process multiple TSV files
|
| 83 |
+
process_multiple_files(args.tsv_datasets, args.threshold_min, args.threshold_max, args.tsv_out_dir)
|