Untitled
unknown
plain_text
a year ago
764 B
11
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}')Editor is loading...
Leave a Comment