当我尝试使用 python networkx 总结文本文档时,我得到了 PowerIterationFailedConvergence:(PowerIterationFailedConvergence(...), 'power iteration failed to converge within 100 iterations') 如下面的代码所示。代码“scores = nx.pagerank(sentence_similarity_graph)”中显示的错误
def read_article(file_name):
file = open(file_name, "r",encoding="utf8")
filedata = file.readlines()
text=""
for s in filedata:
text=text+s.replace("\n","")
text=re.sub(' +', ' ', text) #remove space
text=re.sub('—',' ',text)
article = text.split(". ")
sentences = []
for sentence in article:
# print(sentence)
sentences.append(sentence.replace("[^a-zA-Z]", "").split(" "))
sentences.pop()
new_sent=[]
for lst in sentences:
newlst=[]
for i in range(len(lst)):
if lst[i].lower()!=lst[i-1].lower():
newlst.append(lst[i])
else:
newlst=newlst
new_sent.append(newlst)
return new_sent
def sentence_similarity(sent1, sent2, stopwords=None):
if stopwords is None:
stopwords = []
sent1 = [w.lower() for w in sent1]
sent2 = [w.lower() for w in sent2]
all_words = list(set(sent1 + sent2))
vector1 = [0] * len(all_words)
vector2 = [0] * len(all_words)
# build the vector for the first sentence
for w in sent1:
if w in stopwords:
continue
vector1[all_words.index(w)] += 1
# build the vector for the second sentence
for w in sent2:
if w in stopwords:
continue
vector2[all_words.index(w)] += 1
return 1 - cosine_distance(vector1, vector2)
def build_similarity_matrix(sentences, stop_words):
# Create an empty similarity matrix
similarity_matrix = np.zeros((len(sentences), len(sentences)))
回首忆惘然
拉莫斯之舞
慕勒3428872
相关分类