Untitled
unknown
plain_text
9 months ago
1.0 kB
6
Indexable
def word_follow_matrix(sentences):
words_list = [sentence.split() for sentence in sentences] # Tokenize sentences
unique_words = sorted(set(word for words in words_list for word in words)) # Unique words sorted
word_index = {word: i for i, word in enumerate(unique_words)} # Map words to indices
n = len(unique_words)
# Initialize matrix with 0s
matrix = [[0] * n for _ in range(n)]
# Populate matrix based on word-follow relationships within sentences
for words in words_list:
for i in range(len(words) - 1):
row, col = word_index[words[i]], word_index[words[i + 1]]
matrix[row][col] = 1
# Print header row
print("\n ", " ".join(unique_words))
# Print matrix with row labels
for word in unique_words:
print(word.ljust(5), " ".join(map(str, matrix[word_index[word]])))
# Get multiple sentences from user
sentences = input("Enter multiple sentences (separated by '. '): ").split('. ')
word_follow_matrix(sentences)Editor is loading...
Leave a Comment