import numpy as np

def compute_co_occurrence_matrix(corpus, window_size=4): words = corpus.split() vocab = sorted(set(words)) vocab_size = len(vocab) co_occurrence_matrix = np.zeros((vocab_size, vocab_size))

for i, word in enumerate(words):
    for j in range(max(0, i-window_size), min(len(words), i+window_size+1)):
        if i!=j:
            co_occurrence_matrix[vocab.index(word), vocab.index(words[j])] += 1

return co_occurrence_matrix, vocab
Implement compute_co_occurrence_matrix codePython Write a method that constructs a co-occurrence matrix for a certain window-size 𝑛 with a default of 4 considering words 𝑛 before and 𝑛 after

原文地址: https://www.cveoy.top/t/topic/eDj1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录