使用python读取文本文档创建人物关系图
时间: 2023-09-12 20:07:55 浏览: 126
可以使用Python中的nltk(自然语言处理工具包)和networkx库来读取文本文档并创建人物关系图。具体步骤如下:
1. 安装nltk和networkx库:
```python
!pip install nltk
!pip install networkx
```
2. 导入库:
```python
import nltk
import networkx as nx
import matplotlib.pyplot as plt
from nltk.tokenize import sent_tokenize, word_tokenize
```
3. 读取文本文件并进行分句和分词:
```python
with open('text.txt', 'r') as f:
text = f.read()
sentences = sent_tokenize(text)
words = [word_tokenize(sent) for sent in sentences]
```
4. 根据文本的上下文创建人物关系:
```python
relationships = []
for i in range(len(sentences)):
for j in range(i+1, len(sentences)):
for word1 in words[i]:
for word2 in words[j]:
if word1 == word2:
relationships.append((sentences[i], sentences[j]))
break
```
5. 创建有向图:
```python
G = nx.DiGraph()
```
6. 添加节点:
```python
nodes = list(set([word for sentence in sentences for word in word_tokenize(sentence)]))
G.add_nodes_from(nodes)
```
7. 添加边:
```python
for rel in relationships:
G.add_edge(rel[0], rel[1])
```
8. 绘制网络图:
```python
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
plt.show()
```
这样就可以根据文本文件创建人物关系图了。注意,这只是一个简单的示例,实际应用中可能需要更复杂的数据处理和图形绘制方法。
阅读全文