我正在尝试使用Gensim摘要生成器生成一个大型文本文件的摘要。我收到内存错误。从一段时间以来一直面临这个问题,任何帮助都是非常感谢的。请随时询问更多细节。
from gensim.summarization.summarizer import summarize
file_read =open("xxxxx.txt",'r')
Content= file_read.read()
def Summary_gen(content):
print(len(Content))
summary_r=summarize(Content,ratio=0.02)
print(summary_r)
Summary_gen(Content)文档的长度为:
365042错误消息:
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-6-a91bd71076d1> in <module>()
10
11
---> 12 Summary_gen(Content)
<ipython-input-6-a91bd71076d1> in Summary_gen(content)
6 def Summary_gen(content):
7 print(len(Content))
----> 8 summary_r=summarize(Content,ratio=0.02)
9 print(summary_r)
10
c:\python3.6\lib\site-packages\gensim\summarization\summarizer.py in summarize(text, ratio, word_count, split)
428 corpus = _build_corpus(sentences)
429
--> 430 most_important_docs = summarize_corpus(corpus, ratio=ratio if word_count is None else 1)
431
432 # If couldn't get important docs, the algorithm ends.
c:\python3.6\lib\site-packages\gensim\summarization\summarizer.py in summarize_corpus(corpus, ratio)
367 return []
368
--> 369 pagerank_scores = _pagerank(graph)
370
371 hashable_corpus.sort(key=lambda doc: pagerank_scores.get(doc, 0), reverse=True)
c:\python3.6\lib\site-packages\gensim\summarization\pagerank_weighted.py in pagerank_weighted(graph, damping)
57
58 """
---> 59 adjacency_matrix = build_adjacency_matrix(graph)
60 probability_matrix = build_probability_matrix(graph)
61
c:\python3.6\lib\site-packages\gensim\summarization\pagerank_weighted.py in build_adjacency_matrix(graph)
92 neighbors_sum = sum(graph.edge_weight((current_node, neighbor)) for neighbor in graph.neighbors(current_node))
93 for j in xrange(length):
---> 94 edge_weight = float(graph.edge_weight((current_node, nodes[j])))
95 if i != j and edge_weight != 0.0:
96 row.append(i)
c:\python3.6\lib\site-packages\gensim\summarization\graph.py in edge_weight(self, edge)
255
256 """
--> 257 return self.get_edge_properties(edge).setdefault(self.WEIGHT_ATTRIBUTE_NAME, self.DEFAULT_WEIGHT)
258
259 def neighbors(self, node):
c:\python3.6\lib\site-packages\gensim\summarization\graph.py in get_edge_properties(self, edge)
404
405 """
--> 406 return self.edge_properties.setdefault(edge, {})
407
408 def add_edge_attributes(self, edge, attrs):
MemoryError: 我试着在互联网上查找这个错误,但是,找不到一个可行的解决方案。
发布于 2018-05-29 16:22:59
从日志中看,代码似乎构建了一个邻接矩阵
---> 59 adjacency_matrix = build_adjacency_matrix(graph)
这可能试图创建一个巨大的邻接矩阵与您的365042文档,这是无法在您的内存(即,内存)。
您可以尝试:
发布于 2018-05-29 16:48:08
您是否尝试使用word_count参数而不是ratio
如果上面的方法仍然不能解决问题,那是因为gensim的实现限制。如果你仍然有OOM错误,使用gensim的唯一方法就是拆分文档。这也会加速你的解决方案(如果文档真的很大,无论如何也不应该是问题)。
summarize有什么问题
gensim的summarizer默认使用TextRank,这是一种使用PageRank的算法。在gensim中,不幸的是它实现了using a Python list of PageRank graph nodes,所以如果你的图太大,它可能会失败。
顺便说一下,文档的长度是以字还是以字符来衡量的?
https://stackoverflow.com/questions/50567108
复制相似问题