import json import re def clean_gender_from_tags(tags): """ Clean gender/sex related terms from tag strings. Args: tags (str): String containing tags Returns: str: Cleaned tags with gender terms removed """ if not tags: return tags # Define patterns for gender/sex related terms to remove gender_patterns = [ r"\bmale vocals?\b", r"\bfemale vocals?\b", r"\bmale voice\b", r"\bfemale voice\b", r"\bmale singer\b", r"\bfemale singer\b", r"\bwoman\b", r"\bwomen\b", r"\bman\b", r"\bmen\b", r"\bgirl\b", r"\bgirls\b", r"\bboy\b", r"\bboys\b", r"\bsouthern male\b", ] # Replace each gender pattern with an empty string cleaned_tags = tags for pattern in gender_patterns: cleaned_tags = re.sub(pattern, "", cleaned_tags, flags=re.IGNORECASE) # Clean up by removing extra commas and spaces cleaned_tags = re.sub(r",\s*,", ",", cleaned_tags) # Remove double commas cleaned_tags = re.sub(r",\s*$", "", cleaned_tags) # Remove trailing comma cleaned_tags = re.sub(r"^\s*,", "", cleaned_tags) # Remove leading comma cleaned_tags = re.sub( r"\s+", " ", cleaned_tags ) # Replace multiple spaces with a single space cleaned_tags = cleaned_tags.strip() # Trim white space return cleaned_tags def process_json_file(input_filename, output_filename): """ Process a JSON file to remove gender information from tags. Args: input_filename (str): Path to input JSON file output_filename (str): Path to save the cleaned JSON file """ # Read the input JSON file with open(input_filename, "r", encoding="utf-8") as file: json_data = json.load(file) # Create a clean copy of the data cleaned_data = {} # Process each genre and its songs for genre, songs in json_data.items(): cleaned_data[genre] = [] for song in songs: cleaned_song = song.copy() cleaned_song["tags"] = clean_gender_from_tags(song.get("tags", "")) cleaned_data[genre].append(cleaned_song) # Write the cleaned data to the output file with open(output_filename, "w", encoding="utf-8") as file: json.dump(cleaned_data, file, indent=4, ensure_ascii=False) print(f"Processed JSON saved to {output_filename}") if __name__ == "__main__": # Usage example input_file = ( "/home/sara/glockenspiel/suno_utils/task_eval/labelbox/labelbox_sources.json" ) output_file = "labelbox_sources_cleaned.json" process_json_file(input_file, output_file)