我设法安装了spacy,但当我尝试使用nlp时,出于某种奇怪的原因,我得到了一个MemoryError。
我写的代码如下:
import spacy
import re
from nltk.corpus import gutenberg
def clean_text(astring):
#replace newlines with space
newstring=re.sub("\n"," ",astring)
#remove title and chapter headings
newstring=re.sub("\[[^\]]*\]"," ",newstring)
newstring=re.sub("VOLUME \S+"," ",newstring)
newstring=re.sub("CHAPTER \S+"," ",newstring)
newstring=re.sub("\s\s+"," ",newstring)
return newstring.lstrip().rstrip()
nlp=spacy.load('en')
alice=clean_text(gutenberg.raw('carroll-alice.txt'))
nlp_alice=list(nlp(alice).sents)我得到的错误如下
虽然当我的代码是这样的时候,它就可以工作了:
import spacy
nlp=spacy.load('en')
alice=nlp("hello Hello")如果有人能指出我做错了什么,我将不胜感激。
发布于 2019-01-04 05:56:18
我猜你真的快没内存了。我找不到一个确切的数字,但我敢肯定Carrol的“爱丽丝漫游仙境”肯定有成千上万的句子。这相当于来自Spacy的数万个Span元素。无需修改,nlp()就可以确定从POS值到传递给它的字符串的依赖项的所有内容。此外,sents属性返回一个迭代器,应该加以利用,而不是立即在列表中展开。
基本上,您正在尝试的计算很可能会遇到内存限制。你的机器支持多少内存?在Joe建议观察机器内存使用的评论中,我赞同这一点。我的建议是:检查是否真的内存不足,或者限制nlp()的功能,或者考虑使用迭代器功能:
for sentence in nlp(alice).sents:
passhttps://stackoverflow.com/questions/54014422
复制相似问题