Spacy使用pytextrank时出现值错误(Python中textrank的实现)

2024-04-26 00:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我用pytextrank来提取关键字。我使用以下命令安装了pytextrank和spacy。在

pip install pytextrank
pip install -U spacy
python -m spacy download en

这是我的密码

^{pr2}$

当我尝试执行此操作时,出现以下错误

ValueError                                Traceback (most recent call last)
<ipython-input-12-07819fc6acea> in <module>()
  6 
  7 with open(path_stage1, 'w') as f:
  ----> 8     for graf in 
  pytextrank.parse_doc(pytextrank.json_iter(path_stage0)):
  9         f.write("%s\n" % pytextrank.pretty_print(graf._asdict()))
 10         # to view output in this notebook

 /home/sameera/anaconda2/lib/python2.7/site-
 packages/pytextrank/pytextrank.pyc in parse_doc(json_iter)
259                 print("graf_text:", graf_text)
260 
--> 261             grafs, new_base_idx = parse_graf(meta["id"], graf_text, base_idx)
262             base_idx = new_base_idx
263 

/home/sameera/anaconda2/lib/python2.7/site-packages/pytextrank/pytextrank.pyc in parse_graf(doc_id, graf_text, base_idx, spacy_nlp)
193     doc = spacy_nlp(graf_text, parse=True)
194 
--> 195     for span in doc.sents:
196         graf = []
197         digest = hashlib.sha1()

/home/sameera/anaconda2/lib/python2.7/site-packages/spacy/tokens/doc.pyx in __get__ (spacy/tokens/doc.cpp:9664)()
432 
433             if not self.is_parsed:
--> 434                 raise ValueError(
435                     "sentence boundary detection requires the dependency parse, which "
436                     "requires data to be installed. If you haven't done so, run: "

ValueError: sentence boundary detection requires the dependency parse, which 
requires data to be installed. If you haven't done so, run: 
python -m spacy download en
to install the data

我使用的是python2.7,anaconda4.3,jupyter笔记本和ubuntu14.04


Tags: installtotextinhomebasedocspacy
2条回答

这可能只是在将代码复制到StackOverflow时出错,但如果不是:

一定要缩进“with”语句下面的所有内容,包括for循环。在

基本上:

with open(path_stage1, 'w') as f:
    for graf in pytextrank.parse_doc(pytextrank.json_iter(path_stage0)):
        f.write("%s\n" % pytextrank.pretty_print(graf._asdict()))
        print(pytextrank.pretty_print(graf))

最好使用pytextrank包中的requirements.txt,而不是{},因为spaCy正在快速发展,-U将安装最新版本。这些更新并不总是向后兼容的。在

另外,可以在GitHub repo上发布pytextrank:https://github.com/ceteri/pytextrank/issues的问题

用法:)

相关问题 更多 >