我正在执行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'
答案 0 :(得分:1)
在spaCy管道中使用Python实现TextRank
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)
答案 1 :(得分:0)
PyTextRank的更新版本简化了调用代码,并使这些步骤变得不必要: https://spacy.io/universe/project/spacy-pytextrank