共现矩阵实现代码python
时间: 2023-09-02 15:09:38 浏览: 50
以下是一个简单的 Python 代码示例,用于创建共现矩阵:
```python
import numpy as np
# 语料库
corpus = ["I like apples and oranges",
"I eat apples every day",
"Oranges are juicy and delicious"]
# 构建单词列表
words = []
for sentence in corpus:
words.extend(sentence.split())
# 去除重复的单词,构建单词表
words = list(set(words))
word_index = {w: i for i, w in enumerate(words)}
# 初始化共现矩阵
num_words = len(words)
co_matrix = np.zeros((num_words, num_words))
# 构建共现矩阵
for sentence in corpus:
sentence_words = sentence.split()
for i in range(len(sentence_words)):
for j in range(i+1, len(sentence_words)):
index_i = word_index[sentence_words[i]]
index_j = word_index[sentence_words[j]]
co_matrix[index_i][index_j] += 1
co_matrix[index_j][index_i] += 1
# 打印共现矩阵
print(co_matrix)
```
在这个示例中,我们首先定义了一个语料库作为输入。然后,我们从所有句子中提取所有单词,并构建一个单词表。接下来,我们初始化共现矩阵,并在每个句子中计算单词之间的共现关系,并更新共现矩阵。最后,我们打印共现矩阵。