Untitled

 avatar
unknown
plain_text
13 days ago
764 B
1
Indexable
from collections import Counter
import re

# Function to get word frequency matrix from a text file
def get_word_frequency_matrix(file_path):
    with open(file_path, 'r') as file:
        text = file.read()
    
    # Clean the text (remove punctuation and convert to lowercase)
    words = re.findall(r'\b\w+\b', text.lower())
    
    # Count word frequencies
    word_freq = Counter(words)
    
    # Create the word frequency matrix (list of tuples with word and frequency)
    word_freq_matrix = list(word_freq.items())
    
    return word_freq_matrix

# Example usage
file_path = 'your_text_file.txt'
word_freq_matrix = get_word_frequency_matrix(file_path)

# Print the word frequency matrix
for word, freq in word_freq_matrix:
    print(f'{word}: {freq}')
Leave a Comment