属性错误:“ spacy.tokens.doc.Doc”对象没有属性“已发送”

问题描述

我正在使用spaCy编写代码,并且在运行时遇到属性错误。 这是我编写的完整代码。 我打算创建一个文本摘要程序工具桌面GUI

import spacy
nlp = spacy.load('en')
from spacy.lang.en.stop_words import STOP_WORDS
from string import punctuation
from heapq import nlargest


def text_summarizer(raw_doc):
    raw_text = raw_doc
    doc = nlp(raw_text)
    stopwords = list(STOP_WORDS)

    # word frequency
    word_freq = {}
    for word in doc:
        if word.text not in word_freq.keys():
            word_freq[word.text] = 1
        else:
            word_freq[word.text] += 1

    max_freq = max(word_freq.values())

    for word in word_freq.keys():
        word_freq[word] = (word_freq[word] / max_freq)

    # sentence tokens

    sentence_list = [sentence for sentence in doc.sent]

    # sentence scores
    sentence_scores = {}
    for sent in sentence_list:
        for word in sent:
            if word.text.lower() in word_freq.key():
                if len(sent.text.split(' ')) < 30:
                    if sent not in sentence_scores.keys():
                        sentence_scores[sent] = word_freq[word.text.lower()]
                    else:
                        sentence_scores[sent] += word_freq[word.text.lower()]

    summarized_sentence = nlargest(7,sentence_scores,key=sentence_scores.get)
    final_sentence = [w.text for w in summarized_sentence]
    summary = ' '.join(final_sentence)

    return summary

这是源代码。 运行它时,出现以下错误

C:\Users\aayus\AppData\Local\Programs\Python\python37\python.exe "C:/Users/aayus/Desktop/MYP 5/ICT/ict_project/main.py"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\aayus\AppData\Local\Programs\Python\python37\lib\tkinter\__init__.py",line 1705,in __call__
    return self.func(*args)
  File "C:/Users/aayus/Desktop/MYP 5/ICT/ict_project/main.py",line 39,in get_summary
    final_text = text_summarizer(raw)
  File "C:\Users\aayus\Desktop\MYP 5\ICT\ict_project\sumarization.py",line 28,in text_summarizer
    sentence_list = [sentence for sentence in doc.sent]
AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'sent'

Process finished with exit code 0

我在Tkinter中添加一个GUI。 预先感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)