通过更改数据来分析和绘制网络图而发生的错误

问题描述

背景

我试图理解描述小说人物关系的网络结构,并找到了一个示例项目。

character-network 分析哈利波特小说中的人物网络。

问题

当我更改要分析的文本数据的内容时,显示以下错误并且没有生成网络图。即使内容被更改,我也没有更改“哈利波特 1.txt”中的文件名。

如何解决原来的代码来调节其他新颖像内容

$ python3 characterNetwork-iterative.py
/Users/username/Desktop/character-network/characterNetwork-iterative.py:147: RuntimeWarning: invalid value encountered in double_scalars
  align_rate = np.sum(sentiment_score)/len(np.nonzero(sentiment_score)[0]) * -2
Traceback (most recent call last):
  File "/Users/username/Desktop/character-network/characterNetwork-iterative.py",line 253,in <module>
    name_frequency,name_list = top_names(preliminary_name_list,novel,25)
  File "/Users/username/Desktop/character-network/characterNetwork-iterative.py",line 128,in top_names
    name_frequency = vect.fit_transform([novel.lower()])
  File "/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/text.py",line 1198,in fit_transform
    self._validate_vocabulary()
  File "/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/text.py",line 461,in _validate_vocabulary
    raise ValueError("empty vocabulary passed to fit")
ValueError: empty vocabulary passed to fit

Winnie The Pooh

试玩内容的部分,而不是项目上的原始哈利波特内容

CHAPTER ONE 

in which we are introduced to 
Winnie-the-Pooh and some Bees,and the stories begin 


Here is Edward Bear,coming downstairs Now,bump,on the back of his head,behind 
Christopher Robin. It is,as far as he kNows,the only 
way of coming downstairs,but sometimes he feels that 
there really is another way,if only he Could stop bumping 
for a moment and think of it. And then he feels that 
perhaps there isn’t. Anyhow,here he is at the bottom,and ready to be introduced to you. Winnie-the-Pooh. 

代码

安装所需的库后执行 character-network/characterNetwork-iterative.py

试图分析一个文件character-network/novels/Harry Potter 1.txt,而不是从 1 到 7 的所有系列。

我更改了 characterNetwork-iterative.py底部以适合索引号,但其他部分没有更改。

    # plot network graph by season
    novel_list = [novel_name + ' ' + str(season) for season in range(0,1)]
    for name in novel_list:
        novel = read_novel(name,novel_folder)
        sentence_list = sent_tokenize(novel)
        cooccurrence_matrix,sentiment_matrix = calculate_matrix(name_list,sentence_list,align_rate)
        plot_graph(name_list,name_frequency,cooccurrence_matrix,name + ' co-occurrence graph','co-occurrence')
        plot_graph(name_list,sentiment_matrix,name + ' sentiment graph','sentiment')

我已经尝试过的

即使显示原始数据 character-network/novels/Harry Potter 1.txt 的以下错误消息,也会生成字符网络。

原始数据的错误信息(哈利波特 1)

$ python3 characterNetwork-iterative.py
/Users/username/Desktop/character-network/characterNetwork-iterative.py:193: RuntimeWarning: invalid value encountered in true_divide
  normalized_matrix = matrix / np.max(np.abs(matrix))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)