|
"""Script to remove some of the most common issues in the .tsv transcription file""" |
|
|
|
import csv |
|
import re |
|
import os |
|
import sys |
|
import glob |
|
|
|
def clean_tsv(input_file : str, output_file: str, revision_file: str) -> None: |
|
"""Generic function |
|
|
|
Args: |
|
input_file (str): The path to the tsv file to clean |
|
output_file (str): The path to the file to save the cleaned rows in. (Typically the same file) |
|
revision_file (str): The path to the file to save still inconsistent rows in |
|
|
|
Returns: |
|
_type_: _description_ |
|
""" |
|
cleaned_rows = [] |
|
revision_rows = [] |
|
|
|
|
|
def clean_line(line): |
|
|
|
line = re.sub(r'[<>"]', '', line) |
|
|
|
|
|
line = re.sub(r'\t+', '\t', line) |
|
|
|
return line |
|
|
|
|
|
with open(input_file, 'r', encoding='utf-8') as infile: |
|
reader = csv.reader(infile, delimiter='\t') |
|
|
|
for row in reader: |
|
|
|
row = [clean_line(item) for item in row] |
|
|
|
|
|
if len(row) == 4: |
|
try: |
|
|
|
start_time = int(row[0]) |
|
end_time = int(row[1]) |
|
|
|
|
|
cleaned_rows.append(row) |
|
except ValueError: |
|
|
|
revision_rows.append(row) |
|
print("One problematic row has been added to revision") |
|
|
|
elif len(row) > 4: |
|
row_str = "\t".join(row) |
|
if ',' in row_str: |
|
row_str = re.sub(r',\t', ', ', row_str) |
|
|
|
row_fixed = row_str.split('\t') |
|
if len(row_fixed) == 4: |
|
cleaned_rows.append(row_fixed) |
|
else: |
|
revision_rows.append(row_fixed) |
|
print("One problematic row has been added to revision") |
|
else: |
|
|
|
revision_rows.append(row) |
|
print("One problematic row has been added to revision") |
|
|
|
|
|
with open(output_file, 'w', encoding='utf-8', newline='') as outfile: |
|
writer = csv.writer(outfile, delimiter='\t') |
|
writer.writerows(cleaned_rows) |
|
print(f"**** New cleaned tsv file saved at {output_file} ****") |
|
|
|
if revision_rows: |
|
|
|
with open(revision_file, 'w', encoding='utf-8', newline='') as revfile: |
|
writer = csv.writer(revfile, delimiter='\t') |
|
writer.writerows(revision_rows) |
|
print(f"**** New revision file saved at {revision_file} ****") |
|
|
|
if __name__ == "__main__": |
|
transcription_dir = sys.argv[1] |
|
|
|
|
|
rev_dir = f'{transcription_dir}/revisions' |
|
os.makedirs(rev_dir, exist_ok=True) |
|
|
|
|
|
tsv_files = glob.glob(transcription_dir + "/*.tsv") |
|
|
|
for tsv_file in tsv_files: |
|
in_file = tsv_file |
|
out_file = in_file |
|
rev_file = rev_dir + "/" + tsv_file.split("/")[-1][:-4] + "-rev.tsv" |
|
clean_tsv(input_file=in_file, output_file=out_file, revision_file=rev_file) |
|
|