我正在执行一个nlp任务。我编写了以下代码。执行时,它显示以下错误。任何解决错误的建议都会有所帮助。我在谷歌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("%sn" % 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("%sn" % pytextrank.pretty_print(graf._asdict()))
24 print(pytextrank.pretty_print(graf._asdict()))
AttributeError: module 'pytextrank' has no attribute 'parse_doc'
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)
AttributeError: module 'pytextrank' has no attribute 'TextRank'
重现错误:
跑:
def summarize_text_returns_expected_summary(nlp, text):
doc = process_text(nlp, text)
if 'textrank' not in nlp.pipe_names:
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
doc = nlp(text)
return [str(sent) for sent in doc._.textrank.summary(limit_phrases=15, limit_sentences=5)]
错误:
AttributeError: module 'pytextrank' has no attribute 'TextRank'
修复:
step_1
检查pytextrank
安装
pip list | grep pytextrank
step_2
取代:
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
跟:
nlp.add_pipe("textrank")
更新的代码:
def summarize_text_returns_expected_summary(nlp, text):
doc = process_text(nlp, text)
if 'textrank' not in nlp.pipe_names:
nlp.add_pipe("textrank")
doc = nlp(text)
return [str(sent) for sent in doc._.textrank.summary(limit_phrases=15, limit_sentences=5)]
省略
if
语句,则在访问textrank
时可能会遇到错误:脚本不会检查管道中是否存在textrank
。
为什么?
空间管道:处理步骤的顺序(标记化,POS标记,NER(。
不正确的代码手动使用pytextrank.TextRank()
,然后尝试将其添加到管道中。
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
正确的代码:
nlp.add_pipe("textrank")
自动正确添加textrank
组件,确保正确的注册和可访问性。
将
TextRank
添加到 Spacy 管道会注册其方法、属性,并允许通过文档(例如doc._.textrank.summary()
(上的._
进行访问。
关于module 'pytextrank' has no attribute 'parse_doc
的说明
解析器通常是 NLP 管道中的必要组件。
它可以与PyTextRank一起添加到管道中。
因为:
错误消息表示在pytextrank
模块中找不到parse_doc
函数。 可能是由于 PyTextrank 库中的更改:某些函数可能已被删除; 或者干脆,不存在。
改为:
加载一个spacy parser
,并将其沿pytextrank
添加到管道中。
即空间小英语模型en_core_web_sm
在解析文本之前对其进行标记。
例:
import spacy
import pytextrank
import json
def get_top_ranked_phrases(text):
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("textrank")
doc = nlp(text)
top_phrases = []
for phrase in doc._.phrases:
top_phrases.append({
"text": phrase.text,
"rank": phrase.rank,
"count": phrase.count,
"chunks": phrase.chunks
})
return top_phrases
sample_text = 'I Like Flipkart. He likes Amazone. she likes Snapdeal. Flipkart and amazone is on top of google search.'
top_phrases = get_top_ranked_phrases(sample_text)
for phrase in top_phrases:
print(phrase["text"], phrase["rank"], phrase["count"], phrase["chunks"])
输出:
output_of_sample.py
代码说明:
✔︎加载空间小英文模型
✔︎将PytextRank添加到管道中
✔︎存储排名靠前的短语
✔︎检查文档中排名靠前的短语
✔︎打印排名靠前的短语
引用:
-德文艾
-(https://spacy.io/universe/project/spacy-pytextrank(
-textrank:将顺序带入文本
- 关键字和句子提取与文本排名(pytextrank(
-模块'pytextrank'没有属性'parse_doc'
-散文/问题/92
-属性错误:模块 'pytextrank' 没有属性 'TextRank' #2
有一个较新版本的 PyTextRank,它简化了调用代码,并且不需要执行这些步骤: https://spacy.io/universe/project/spacy-pytextrank