Hello. I have a dataset I want to filter entries out of with a words list using this…
- User
Hello. I have a dataset I want to filter entries out of with a words list using this script: import argparse import json import re
from tqdm import tqdm
in_file = "openhermesv2.json" out_file = "openhermes_filtered_oai.json"
def contains_unwanted_words(text): unwanted_words = [ "text-based AI language model", "domestic violence", "please refrain", "derogatory", "inappropriate", "offensive" ] for word in unwanted_words: if word.lower() in text.lower(): return True return False
if name == "main": content = json.load(open(in_file, "r", encoding='utf-8')) num_conv = len(content)
new_content = [] removed_content = [] for conv in tqdm(content, desc="Processing entries"): if not contains_unwanted_words(conv["output"]): new_content.append(conv) else: removed_content.append(conv) print(f"return {len(new_content)} out of {len(content)}, start dump ...") with open(out_file, "w") as f: json.dump(new_content, f, indent=2) with open("oai_flagged.json", "w") as f: json.dump(removed_content, f, indent=2)But, the script is made for a different dataset format than I have. My dataset format looks like this: [ { "conversations": [ { "from": "system", "value": "" }, { "from": "human", "value": "Every day, a tree drops 7 leaves. How many leaves would it drop in a month of February in a non-leap year? Include your logic." }, { "from": "gpt", "value": "Here's the logic behind this:\n\n1. We know that February has 28 days in a non-leap year.\n2. If the tree drops 7 leaves every day, then over the course of February, it would drop:\n Leaves dropped in February = Leaves per day * Days in February\n = 7 leaves * 28 days\n = 196 leaves\n\nSo, the tree would drop 196 leaves in February in a non-leap year." } ], "source": "airoboros2.2", "category": "orca", "skip_prompt_formatting": false },
I need to remove any object like the above where any value in the dicts inside of the conversations list. I need the removed entries in one dataset and the rest in a new dataset - like the original script does
- Assistant
To adjust the script to your dataset format, you will need to make some modifications in how the script processes the data. Specifically, the following changes need to be made:
- Update the
contains_unwanted_wordsfunction to iterate through the "conversations" list inside each data entry. - Adjust the filtering logic to process the "conversations" list instead of the "output" key.
Here's the modified version of the script based on your dataset format:
import json from tqdm import tqdm in_file = "openhermesv2.json" out_file = "openhermes_filtered_oai.json" def contains_unwanted_words(conversations): unwanted_words = [ "text-based AI language model", "domestic violence", "please refrain", "derogatory", "inappropriate", "offensive" ] for conv in conversations: for word in unwanted_words: if word.lower() in conv["value"].lower(): return True return False if __name__ == "__main__": with open(in_file, "r", encoding='utf-8') as f: content = json.load(f) new_content = [] removed_content = [] for entry in tqdm(content, desc="Processing entries"): if not contains_unwanted_words(entry["conversations"]): new_content.append(entry) else: removed_content.append(entry) print(f"return {len(new_content)} out of {len(content)}, start dump ...") with open(out_file, "w", encoding='utf-8') as f: json.dump(new_content, f, indent=2) with open("oai_flagged.json", "w", encoding='utf-8') as f: json.dump(removed_content, f, indent=2) - Update the
- User
Now, I want to make it so this script adds removals of any entry with any human or gpt string that contains a url, whether http, https, www or .com in the conversations lists. I also want it to filter out any ANSI escape's, and I want it to deduplicate exact matches of conversation lists, all entries in the conversation must be equivalent
- Assistant
- User
Explain the regular expression you wrote for urls
- Assistant
Of course, let's break down the regular expression used for detecting URLs:
https?://|www\.|\.com-
https?://: This part of the regular expression matches HTTP or HTTPS URLs.http: This matches the literal string "http".s?: Thesis followed by a?, which means the "s" is optional. This allows the pattern to match both "http" and "https".://: This matches the "://" that follows the protocol in URLs.
-
|: This is the OR operator in regex, meaning either the pattern before or after this operator will match.
-
- User
Now explain the hashes system to deduplicate
- Assistant