from collections import Counter


def count_word_frequency(file_path, top_n):

    try:

        words = open(file_path, encoding='utf-8').read().lower().split()

        words = [''.join(c for c in word if c.isalnum()) for word in words]

        print(Counter(words).most_common(top_n))

    except Exception as e:

        print(f"Error: {e}")


# Usage

count_word_frequency("example.txt", 10)


Comments

Popular posts from this blog

Pos using Seq to seq