wordle_solve_automated.py
def solve_row(row_results, word_guess):
    """
    Receives a dictionary with the results of the previous word entered.
    It then performs two actions:
    1. Eliminates words from the possible word list.
    2. Checks this list against the WordAPI (RapidAPI) for popularity
    using the frequency metric.

    The most popular word is then returned.This word with
    the highest frequency is considered more likely as it would be less
    obscure.

    Args:
        row_result (dictionary): Dictionary of each character of the previous
        word with the results like "present", "absent" or "correct"

    Returns:
        [str]: Best solution word based on previous results
    """
    multi_char = {}

    # Checks if a word guess has repeating characters. If there is a charater
    # repearing, it then updates the multi_char dictionary with the row
    # result determination of each of those repeating charaters.
    # This is done using regex. The span for each finditer result
    # returns a tuple with the location where the substring started and ended.
    # We use the starting position of the substring to gather the row result
    # for that character from the row_result list and add all the findings
    # is to a temp list which is then added to the multi_char dictionary
    # and used further down in the word elimination logic.

    if len(set(word_guess)) < 5:
        for char, occurance in Counter(word_guess).most_common():
            if occurance > 1:
                temp_list = []
                matches = re.finditer(char, word_guess)
                for result in matches:
                    temp_list.append(row_results[result.span()[0]])
                multi_char[char] = temp_list

    word_list = current_word_list.copy()
    for idx, guess in enumerate(zip(word_guess, row_results)):
        match guess[1]:
            case "present":
                for word in word_list:
                    if not (guess[0] in word) or (guess[0] == word[idx]):
                        current_word_list.remove(word)
                word_list = current_word_list.copy()
            case "absent":
                for word in word_list:
                    if guess[0] in word and Counter(word_guess)[guess[0]] == 1:
                        current_word_list.remove(word)
                    elif (
                        guess[0] in word
                        and Counter(word_guess)[guess[0]] > 1
                        and set(multi_char[guess[0]]) == {"absent"}
                    ):
                        current_word_list.remove(word)
                    elif guess[0] == word[idx]:
                        current_word_list.remove(word)
                word_list = current_word_list.copy()
            case "correct":
                for word in word_list:
                    if (
                        not (guess[0] == word[idx])
                        # and Counter(word_guess)[guess[0]] == 1
                    ):
                        current_word_list.remove(word)
                word_list = current_word_list.copy()

            case "other":
                print(f"Character - {guess[0]} at index {idx} has state 'other'")
                sys.exit("Something went wrong. Character status incorrect !")
    # Check if a word guess has repeating characters. If there is a charater
    # repearing, it then checks to see if both the characters need to exist
    # in the word based of word results.
    # i.e : If one is present and other is correct or the other way around.
    # If that is the case then it will remove all words from the word list
    # which do not have these characters.
    # Assumption: There can only be two or three repeating characters
    if len(set(word_guess)) < 5:
        for char, occurance in Counter(word_guess).most_common():
            if occurance > 1:
                match multi_char[char]:
                    case ["present", "correct"] | ["correct", "present"]:
                        for word in word_list:
                            if Counter(word)[char] != occurance:
                                current_word_list.remove(word)
                    case ["present", "correct", "correct"] | [
                        "present",
                        "correct",
                        "present",
                    ] | ["correct", "present", "present"] | [
                        "correct",
                        "present",
                        "correct",
                    ]:
                        for word in word_list:
                            if Counter(word)[char] != occurance:
                                current_word_list.remove(word)
        word_list = current_word_list.copy()

    # rprint(f"Multi Character Dictionary: {multi_char}")
    rprint(f"Current Word List length - {str(len(word_list))}")

    # This is the avoid a API Call to get word frequencies
    # as we know the second word is going to be 'stomp'
    # if "".join(row_result.keys()) == "uraei":
    #     return {7.0: "stomp"}

    word_dict = {}
    max_frequency = 0

    with open("words_json.txt", "r") as fh:
        frequency_dict = json.load(fh)

    for word in word_list:
        word_dict[word] = frequency_dict[word]
        if max_frequency < word_dict[word]:
            recommended_word = word
            max_frequency = word_dict[word]

    # reverse_sorted_keys = sorted(word_dict, reverse=True)
    current_word_list.remove(recommended_word)
    # rprint(f"Word-Dict: {word_dict}")
    # rprint(f"API Cache Hit Rate : {(cache_count*100)/len(word_list):.2f} %")
    rprint(
        f"Recommended Word : {recommended_word}, Frequency : {word_dict[recommended_word]}"
    )
    return recommended_word

    Receives a dictionary with the results of the previous word entered.
    It then performs two actions:
    1. Eliminates words from the possible word list.
    2. Checks this list against the WordAPI (RapidAPI) for popularity
    using the frequency metric.

    The most popular word is then returned.This word with
    the highest frequency is considered more likely as it would be less
    obscure.

    Args:
        row_result (dictionary): Dictionary of each character of the previous
        word with the results like "present", "absent" or "correct"

    Returns:
        [str]: Best solution word based on previous results
    
Receives a dictionary with the results of the previous word entered.
It then performs two actions:
1. Eliminates words from the possible word list.
2. Checks this list against the WordAPI (RapidAPI) for popularity
using the frequency metric.

The most popular word is then returned.This word with
the highest frequency is considered more likely as it would be less
obscure.



[str]: Best solution word based on previous results
Receives a dictionary with the results of the previous word entered . It then performs two actions : 1 . Eliminates words from the possible word list . 2 . Checks this list against the WordAPI ( RapidAPI ) for popularity using the frequency metric . The most popular word is then returned . This word with the highest frequency is considered more likely as it would be less obscure . [ str ] : Best solution word based on previous results
def solve_row(row_results, word_guess):
    multi_char = {}
    if len(set(word_guess)) < 5:
        for char, occurance in Counter(word_guess).most_common():
            if occurance > 1:
                temp_list = []
                matches = re.finditer(char, word_guess)
                for result in matches:
                    temp_list.append(row_results[result.span()[0]])
                multi_char[char] = temp_list
    word_list = current_word_list.copy()
    for idx, guess in enumerate(zip(word_guess, row_results)):
        match guess[1]:
            case "present":
                for word in word_list:
                    if not (guess[0] in word) or (guess[0] == word[idx]):
                        current_word_list.remove(word)
                word_list = current_word_list.copy()
            case "absent":
                for word in word_list:
                    if guess[0] in word and Counter(word_guess)[guess[0]] == 1:
                        current_word_list.remove(word)
                    elif (
                        guess[0] in word
                        and Counter(word_guess)[guess[0]] > 1
                        and set(multi_char[guess[0]]) == {"absent"}
                    ):
                        current_word_list.remove(word)
                    elif guess[0] == word[idx]:
                        current_word_list.remove(word)
                word_list = current_word_list.copy()
            case "correct":
                for word in word_list:
                    if (
                        not (guess[0] == word[idx])
                    ):
                        current_word_list.remove(word)
                word_list = current_word_list.copy()
            case "other":
                print(f"Character - {guess[0]} at index {idx} has state 'other'")
                sys.exit("Something went wrong. Character status incorrect !")
    if len(set(word_guess)) < 5:
        for char, occurance in Counter(word_guess).most_common():
            if occurance > 1:
                match multi_char[char]:
                    case ["present", "correct"] | ["correct", "present"]:
                        for word in word_list:
                            if Counter(word)[char] != occurance:
                                current_word_list.remove(word)
                    case ["present", "correct", "correct"] | [
                        "present",
                        "correct",
                        "present",
                    ] | ["correct", "present", "present"] | [
                        "correct",
                        "present",
                        "correct",
                    ]:
                        for word in word_list:
                            if Counter(word)[char] != occurance:
                                current_word_list.remove(word)
        word_list = current_word_list.copy()
    rprint(f"Current Word List length - {str(len(word_list))}")
    word_dict = {}
    max_frequency = 0
    with open("words_json.txt", "r") as fh:
        frequency_dict = json.load(fh)
    for word in word_list:
        word_dict[word] = frequency_dict[word]
        if max_frequency < word_dict[word]:
            recommended_word = word
            max_frequency = word_dict[word]
    current_word_list.remove(recommended_word)
    rprint(
        f"Recommended Word : {recommended_word}, Frequency : {word_dict[recommended_word]}"
    )
    return recommended_word
def solve_row ( row_results , word_guess ) : multi_char = { } if len ( set ( word_guess ) ) < 5 : for char , occurance in Counter ( word_guess ) . most_common ( ) : if occurance > 1 : temp_list = [ ] matches = re . finditer ( char , word_guess ) for result in matches : temp_list . append ( row_results [ result . span ( ) [ 0 ] ] ) multi_char [ char ] = temp_list word_list = current_word_list . copy ( ) for idx , guess in enumerate ( zip ( word_guess , row_results ) ) : match guess [ 1 ] : case "present" : for word in word_list : if not ( guess [ 0 ] in word ) or ( guess [ 0 ] == word [ idx ] ) : current_word_list . remove ( word ) word_list = current_word_list . copy ( ) case "absent" : for word in word_list : if guess [ 0 ] in word and Counter ( word_guess ) [ guess [ 0 ] ] == 1 : current_word_list . remove ( word ) elif ( guess [ 0 ] in word and Counter ( word_guess ) [ guess [ 0 ] ] > 1 and set ( multi_char [ guess [ 0 ] ] ) == { "absent" } ) : current_word_list . remove ( word ) elif guess [ 0 ] == word [ idx ] : current_word_list . remove ( word ) word_list = current_word_list . copy ( ) case "correct" : for word in word_list : if ( not ( guess [ 0 ] == word [ idx ] ) ) : current_word_list . remove ( word ) word_list = current_word_list . copy ( ) case "other" : print ( f"Character - {guess[0]} at index {idx} has state 'other'" ) sys . exit ( "Something went wrong. Character status incorrect !" ) if len ( set ( word_guess ) ) < 5 : for char , occurance in Counter ( word_guess ) . most_common ( ) : if occurance > 1 : match multi_char [ char ] : case [ "present" , "correct" ] | [ "correct" , "present" ] : for word in word_list : if Counter ( word ) [ char ] != occurance : current_word_list . remove ( word ) case [ "present" , "correct" , "correct" ] | [ "present" , "correct" , "present" , ] | [ "correct" , "present" , "present" ] | [ "correct" , "present" , "correct" , ] : for word in word_list : if Counter ( word ) [ char ] != occurance : current_word_list . remove ( word ) word_list = current_word_list . copy ( ) rprint ( f"Current Word List length - {str(len(word_list))}" ) word_dict = { } max_frequency = 0 with open ( "words_json.txt" , "r" ) as fh : frequency_dict = json . load ( fh ) for word in word_list : word_dict [ word ] = frequency_dict [ word ] if max_frequency < word_dict [ word ] : recommended_word = word max_frequency = word_dict [ word ] current_word_list . remove ( recommended_word ) rprint ( f"Recommended Word : {recommended_word}, Frequency : {word_dict[recommended_word]}" ) return recommended_word
Receives a dictionary with the results of the previous word entered.
Receives a dictionary with the results of the previous word entered .
"""
    Receives a dictionary with the results of the previous word entered.
    It then performs two actions:
    1. Eliminates words from the possible word list.
    2. Checks this list against the WordAPI (RapidAPI) for popularity
    using the frequency metric.

    The most popular word is then returned.This word with
    the highest frequency is considered more likely as it would be less
    obscure.

    Args:
        row_result (dictionary): Dictionary of each character of the previous
        word with the results like "present", "absent" or "correct"

    Returns:
        [str]: Best solution word based on previous results
    """ # Checks if a word guess has repeating characters. If there is a charater # repearing, it then updates the multi_char dictionary with the row # result determination of each of those repeating charaters. # This is done using regex. The span for each finditer result # returns a tuple with the location where the substring started and ended. # We use the starting position of the substring to gather the row result # for that character from the row_result list and add all the findings # is to a temp list which is then added to the multi_char dictionary # and used further down in the word elimination logic. # and Counter(word_guess)[guess[0]] == 1 # Check if a word guess has repeating characters. If there is a charater # repearing, it then checks to see if both the characters need to exist # in the word based of word results. # i.e : If one is present and other is correct or the other way around. # If that is the case then it will remove all words from the word list # which do not have these characters. # Assumption: There can only be two or three repeating characters # rprint(f"Multi Character Dictionary: {multi_char}") # This is the avoid a API Call to get word frequencies # as we know the second word is going to be 'stomp' # if "".join(row_result.keys()) == "uraei": #     return {7.0: "stomp"} # reverse_sorted_keys = sorted(word_dict, reverse=True) # rprint(f"Word-Dict: {word_dict}") # rprint(f"API Cache Hit Rate : {(cache_count*100)/len(word_list):.2f} %")
row_results
word_guess