在python中使用Text-Summarizer进行文本摘要

问题描述 投票:0回答:2

我正在尝试使用网站上提供的以下库

https://pypi.org/project/text-summarizer/

从 text_summarizer 导入摘要器

正如网站所告知的,我已经使用下面的语法安装了 text_summarizer 包,当在 python 中加载它时,我收到错误 导入错误:无法导入名称“summarizer”

如果这个软件包有效,任何人都可以帮助我吗

python text summarization
2个回答
0
投票

检查是否有循环依赖项导入。这可能与这里的问题的答案相同:ImportError: Cannot import name X


0
投票

试试这个

import bs4 as bs
import urllib.request
import re
import nltk

impfile = ""

number_of_sentences_in_summary = 7
max_words_in_sentence = 20


import sys, getopt

## From https://stackabuse.com/text-summarization-with-nltk-in-python/
## NLTK install instructions are at https://www.nltk.org/

number_of_sentences_in_summary = int(sys.argv[1])
max_words_in_sentence = int(sys.argv[2])
scraped_data = urllib.request.urlopen(sys.argv[3])
scraped_data = urllib.request.urlopen('https://en.wikipedia.org/wiki/Artificial_intelligence')
article = scraped_data.read()

parsed_article = bs.BeautifulSoup(article,'lxml')

paragraphs = parsed_article.find_all('p')

article_text = ""

for p in paragraphs:
    article_text += p.text


# Removing Square Brackets and Extra Spaces
article_text = re.sub(r'\[[0-9]*\]', ' ', article_text)
article_text = re.sub(r'\s+', ' ', article_text)


# Removing special characters and digits
formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text )
formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)

sentence_list = nltk.sent_tokenize(article_text)


stopwords = nltk.corpus.stopwords.words('english')
#stopwords = nltk.corpus.stopwords.words('hungarian')

word_frequencies = {}
for word in nltk.word_tokenize(formatted_article_text):
    if word not in stopwords:
        if word not in word_frequencies.keys():
            word_frequencies[word] = 1
        else:
            word_frequencies[word] += 1

maximum_frequncy = max(word_frequencies.values())

for word in word_frequencies.keys():
    word_frequencies[word] = (word_frequencies[word]/maximum_frequncy)


sentence_scores = {}
for sent in sentence_list:
    for word in nltk.word_tokenize(sent.lower()):
        if word in word_frequencies.keys():
            if len(sent.split(' ')) < max_words_in_sentence:
                if sent not in sentence_scores.keys():
                    sentence_scores[sent] = word_frequencies[word]
                else:
                    sentence_scores[sent] += word_frequencies[word]



import heapq
summary_sentences = heapq.nlargest(number_of_sentences_in_summary, sentence_scores, key=sentence_scores.get)

summary = '\n'.join(summary_sentences)
print(summary)
© www.soinside.com 2019 - 2024. All rights reserved.