"""
TextPatternFinder
Generated by Eden via recursive self-improvement
2025-11-01 04:02:46.371377
"""

import re
from collections import Counter

def tokenize_text(text):
    """
    Tokenizes the given text into a list of words.
    
    Parameters:
        - text: A string containing the text to be tokenized.
        
    Returns:
        A list of words.
    """
    # Basic tokenization with regex to split on non-alphanumeric characters
    return re.findall(r'\b\w+\b', text.lower())

def extract_ngrams(text, n=2):
    """
    Extracts n-grams (sequences of n words) from the given text.
    
    Parameters:
        - text: A string containing the text to be tokenized and analyzed.
        - n: The size of the n-gram sequences. Default is 2 for bigrams.
        
    Returns:
        A Counter object with n-gram frequencies.
    """
    words = tokenize_text(text)
    ngrams = zip(*[words[i:] for i in range(n)])
    return Counter([' '.join(n) for n in ngrams])

def get_top_ngrams(text, top_n=5):
    """
    Returns the most frequent n-grams from the given text.
    
    Parameters:
        - text: A string containing the text to be analyzed.
        - top_n: The number of top n-grams to return. Default is 5.
        
    Returns:
        A list of tuples, each containing a n-gram and its frequency.
    """
    ngrams = extract_ngrams(text)
    return ngrams.most_common(top_n)

def text_pattern_finder(text):
    """
    Main function to find recurring patterns in the input text.
    
    Parameters:
        - text: A string containing the text to be analyzed.
        
    Returns:
        A list of tuples, each containing a top n-gram and its frequency.
    """
    return get_top_ngrams(text)

# Example usage
if __name__ == "__main__":
    example_text = "Machine learning is an application of artificial intelligence that allows systems to improve at tasks through experience. Machine learning algorithms are used in various fields such as finance, healthcare, and marketing."
    
    print("Top n-grams found:")
    for ngram, freq in text_pattern_finder(example_text):
        print(f"{ngram}: {freq}")