modify this code to track unmatched entries not matched entries:

import json
import os
import re

def check_phrases_in_explanation(input_path, phrases=["unrelated"], verbose=True, case_sensitive=False, save_missing_ids=False, output_path="missing_ids.txt"):
    """
    Checks each entry in the JSON file to see if the 'explanation' field contains all specified phrases.
    Prints the 'id' of entries where any of the phrases are not found.

    Args:
        input_path (str): Path to the input JSON file (e.g., 'synonymtemp1.json').
        phrases (list of str): The phrases to search for in the 'explanation' field.
        verbose (bool): If True, prints detailed warnings and information.
        case_sensitive (bool): If True, the search is case-sensitive.
        save_missing_ids (bool): If True, saves the missing ids to a text file.
        output_path (str): Path to save the missing ids if `save_missing_ids` is True.
    """
    # Check if the input file exists
    if not os.path.exists(input_path):
        print(f"Error: The file '{input_path}' does not exist.")
        return

    # Load the JSON data
    try:
        with open(input_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON from '{input_path}': {e}")
        return

    # Ensure the JSON data is a list
    if not isinstance(data, list):
        print(f"Error: Expected the JSON data to be a list of entries in '{input_path}'.")
        return

    # List to store ids missing any of the phrases
    missing_ids = []

    # Compile the search patterns
    flags = 0
    if not case_sensitive:
        flags = re.IGNORECASE
    patterns = [re.compile(re.escape(phrase), flags) for phrase in phrases]

    # Iterate through each entry
    for entry in data:
        entry_id = entry.get('id', 'Unknown ID')
        explanation = entry.get('explanation', '')

        if not explanation:
            if verbose:
                print(f"Warning: Entry with id '{entry_id}' has an empty 'explanation' field.")
            missing_ids.append(entry_id)
            continue

        # Check all phrases
        missing_phrases = [phrase for phrase, pattern in zip(phrases, patterns) if not pattern.search(explanation)]
        if missing_phrases:
            if verbose:
                print(f"Entry with id '{entry_id}' is missing the phrase(s): {', '.join(missing_phrases)}.")
            missing_ids.append(entry_id)

    # Print the results
    if missing_ids:
        print("\nEntries missing one or more specified phrases:")
        for eid in missing_ids:
            print(eid)
    else:
        print(f"All entries contain all specified phrases in their explanations.")

    # Optionally, save the missing ids to a file
    if save_missing_ids:
        try:
            with open(output_path, 'w', encoding='utf-8') as out_file:
                for eid in missing_ids:
                    out_file.write(f"{eid}\n")
            print(f"\nMissing ids have been saved to '{output_path}'.")
        except IOError as e:
            print(f"Error writing to '{output_path}': {e}")

def find_phrase_in_explanations(input_path, phrase="unrelated", verbose=True, case_sensitive=False, save_matching_ids=False, output_path_ids="matching_ids.txt", save_matching_entries=False, output_json_path="matching_entries.json"):
    """
    Finds and lists the IDs and/or full entries of entries in the JSON file where the 'explanation' field contains a specific phrase.
    Optionally saves the matching IDs and/or the full entries to separate files.

    Args:
        input_path (str): Path to the input JSON file (e.g., 'synonymtemp1.json').
        phrase (str): The phrase to search for in the 'explanation' field.
        verbose (bool): If True, prints detailed information.
        case_sensitive (bool): If True, the search is case-sensitive.
        save_matching_ids (bool): If True, saves the matching ids to a text file.
        output_path_ids (str): Path to save the matching ids if `save_matching_ids` is True.
        save_matching_entries (bool): If True, saves the matching full entries to a JSON file.
        output_json_path (str): Path to save the matching entries if `save_matching_entries` is True.
    """
    # Check if the input file exists
    if not os.path.exists(input_path):
        print(f"Error: The file '{input_path}' does not exist.")
        return

    # Load the JSON data
    try:
        with open(input_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON from '{input_path}': {e}")
        return

    # Ensure the JSON data is a list
    if not isinstance(data, list):
        print(f"Error: Expected the JSON data to be a list of entries in '{input_path}'.")
        return

    # List to store ids and entries that contain the phrase
    matching_ids = []
    matching_entries = []

    # Compile the search pattern
    flags = 0
    if not case_sensitive:
        flags = re.IGNORECASE
    pattern = re.compile(re.escape(phrase), flags)

    # Iterate through each entry
    for entry in data:
        entry_id = entry.get('id', 'Unknown ID')
        explanation = entry.get('explanation', '')

        if not explanation:
            if verbose:
                print(f"Info: Entry with id '{entry_id}' has an empty 'explanation' field.")
            continue

        # Search for the phrase in the explanation
        if pattern.search(explanation):
            if verbose:
                print(f"Entry with id '{entry_id}' contains the phrase '{phrase}'.")
            matching_ids.append(entry_id)
            matching_entries.append(entry)
        else:
            if verbose:
                print(f"Entry with id '{entry_id}' does not contain the phrase '{phrase}'.")

    # Print the results
    if matching_ids:
        print(f"\nEntries containing the phrase '{phrase}':")
        for eid in matching_ids:
            print(eid)
    else:
        print(f"No entries contain the phrase '{phrase}' in their explanations.")

    # Optionally, save the matching ids to a file
    if save_matching_ids:
        try:
            with open(output_path_ids, 'w', encoding='utf-8') as out_file:
                for eid in matching_ids:
                    out_file.write(f"{eid}\n")
            print(f"\nMatching ids have been saved to '{output_path_ids}'.")
        except IOError as e:
            print(f"Error writing to '{output_path_ids}': {e}")

    # Optionally, save the matching full entries to a new JSON file
    if save_matching_entries:
        try:
            with open(output_json_path, 'w', encoding='utf-8') as out_json_file:
                json.dump(matching_entries, out_json_file, ensure_ascii=False, indent=4)
            print(f"Matching entries have been saved to '{output_json_path}'.")
        except IOError as e:
            print(f"Error writing to '{output_json_path}': {e}")

# Example usage
if __name__ == "__main__":
    # Define the path to your JSON file
    input_file = r'D:\DBImportExport\AllJsonFiles\synonymnew1.json'

    # **Option 1:** Check for missing phrases (original functionality)
    check_phrases_in_explanation(
        input_path=input_file,
        phrases=["unrelated"],  # List of phrases to check
        verbose=True,
        case_sensitive=False,          # Set to True if the search should be case-sensitive
        save_missing_ids=True,        # Set to True to save the missing ids to a file
        output_path="missing_ids.txt" # File to save missing ids
    )

    print("\n" + "="*50 + "\n")  # Separator for clarity in output

    # **Option 2:** Find and list IDs that contain the specified phrase, and save matching entries
    find_phrase_in_explanations(
        input_path=input_file,
        phrase="unrelated",          # Phrase to search for
        verbose=True,
        case_sensitive=False,                # Set to True if the search should be case-sensitive
        save_matching_ids=True,              # Set to True to save the matching ids to a file
        output_path_ids="matching_ids.txt",  # File to save matching ids
        save_matching_entries=True,          # Set to True to save the matching full entries to a JSON file
        output_json_path="matching_entries.json"  # File to save matching entries
    )