【问题标题】:Module 'pytextrank' has no attribute 'parse_doc'模块“pytextrank”没有属性“parse_doc”
【发布时间】:2019-12-14 05:56:12
【问题描述】:

我正在执行 nlp 任务。我已经编写了以下代码。在执行时,它显示以下错误。任何解决错误的建议都会有所帮助。我在 google colab 中有 python 3 env。

# Pytextrank
import pytextrank
import json

# Sample text
sample_text = 'I Like Flipkart. He likes Amazone. she likes Snapdeal. Flipkart and amazone is on top of google search.'

# Create dictionary to feed into json file

file_dic = {"id" : 0,"text" : sample_text}
file_dic = json.dumps(file_dic)
loaded_file_dic = json.loads(file_dic)

# Create test.json and feed file_dic into it.
with open('test.json', 'w') as outfile:
json.dump(loaded_file_dic, outfile)

path_stage0 = "test.json"
path_stage1 = "o1.json"

# Extract keyword using pytextrank
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._asdict()))

我收到以下错误:

  AttributeError                            Traceback (most recent call last)      
  <ipython-input-33-286ce104df34> in <module>()      
       20 # Extract keyword using pytextrank      
       21 with open(path_stage1, 'w') as f:      
  ---> 22   for graf in 
  pytextrank.parse_doc(pytextrank.json_iter(path_stage0)):     
       23     f.write("%s\n" % pytextrank.pretty_print(graf._asdict()))       
       24     print(pytextrank.pretty_print(graf._asdict()))      

      AttributeError: module 'pytextrank' has no attribute 'parse_doc'   

【问题讨论】:

  • 你做了什么来尝试解决这个问题?你有线索吗?我推荐以下文章:ericlippert.com/2014/03/05/how-to-debug-small-programs.
  • pytextrank 中的 parse_doc 属性似乎不可用。但是我看到有人使用这个属性。可能有任何更新。我试图找出可以解决错误的 parse_doc 的替代方法。
  • 也许您可以分享一些使用它的代码示例?
  • 你到底在用你的 JSON 做什么?您将 dict 转储到字符串,然后立即从该字符串加载相同的 dict,然后将其 再次 转储到文件,然后使用 pytextrank 读取文件?
  • @AlexanderCécile 如上图,我在sample_text上使用。上面显示的代码本身可用于识别和解决错误。

标签: python python-3.x google-colaboratory pytextrank


【解决方案1】:

在 Python 中实现 TextRank 以用于 spaCy 管道

import spacy
import pytextrank
nlp = spacy.load('en_core_web_sm')
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name='textrank', last=True)
# Sample text
sample_text = 'I Like Flipkart. He likes Amazone. she likes Snapdeal. Flipkart and amazone is on top of google search.'
#funct
for p in doc._.phrases:
    print(p.text)

【讨论】:

  • 调用 PyTextRank 所需的代码必须更改以支持 spaCy 3.x 中的管道,因此此答案中上面显示的内容不再正确。 FWIW,新方法更简单,并且基于管道组件工厂。示例代码见derwen.ai/docs/ptr/start
【解决方案2】:

有一个更新的 PyTextRank 版本简化了调用代码,并且不需要这些步骤: https://spacy.io/universe/project/spacy-pytextrank

【讨论】:

  • tr = pytextrank.TextRank() 给出AttributeError: module 'pytextrank' has no attribute 'TextRank'。解决方案(github.com/DerwenAI/pytextrank):使用nlp.add_pipe("textrank") 代替tr = pytextrank.TextRank() ; nlp.add_pipe(tr.PipelineComponent, name='textrank', last=True)
  • 示例代码在 PyTextRank 文档中derwen.ai/docs/ptr/start
猜你喜欢
  • 1970-01-01
  • 2020-06-04
  • 2021-12-24
  • 2015-05-08
  • 2020-10-17
  • 2020-10-03
  • 2018-08-11
  • 2020-01-01
  • 2018-04-14
相关资源
最近更新 更多