我們都與使用文本摘要的應(yīng)用程序進行交互。 這些應(yīng)用程序中的許多應(yīng)用程序都是用于發(fā)布有關(guān)每日新聞,娛樂和體育的文章的平臺。 由于我們的日程安排很忙,因此我們決定在閱讀全文之前先閱讀這些文章的摘要。 閱讀摘要有助于我們確定感興趣的領(lǐng)域,并提供故事的簡要背景信息。

摘要可以定義為在保持關(guān)鍵信息和整體含義的同時,提供簡潔明了的摘要的任務(wù)。
影響力
匯總系統(tǒng)通常具有其他證據(jù),可用于指定最重要的文檔主題。 例如,在總結(jié)博客時,博客文章后會有一些討論或評論,這些信息或信息是確定博客的哪些部分至關(guān)重要且有趣的信息。
在科學(xué)論文摘要中,存在大量信息,例如被引用的論文和會議信息,可以用來識別原始論文中的重要句子。
文本摘要的類型
通常,摘要有兩種類型,Abstractive 和 Extractive summarization.
Abstractive Summarization: 抽象方法是基于語義理解來選擇單詞的,即使這些單詞沒有出現(xiàn)在源文檔中。 它旨在以新的方式生產(chǎn)重要的材料。 他們使用先進的自然語言技術(shù)解釋和檢查文本,以便生成新的較短文本,從而傳達原始文本中最關(guān)鍵的信息。
它可以與人類閱讀文本文章或博客文章,然后以自己的單詞進行摘要的方式相關(guān)。
輸入文檔→了解上下文→語義→創(chuàng)建自己的摘要
Extractive Summarization:摘錄方法嘗試通過選擇保留最重要要點的單詞子集來對文章進行摘要。
這種方法對句子的重要部分進行加權(quán),并使用它們來構(gòu)成摘要。 使用不同的算法和技術(shù)來定義句子的權(quán)重,并根據(jù)彼此之間的重要性和相似性對它們進行排名。
輸入文檔→句子相似度→加權(quán)句子→選擇排名更高的句子
有限的研究可用于抽象總結(jié),因為與提取方法相比,它需要對文本有更深刻的理解。
與Extractive Summarization摘要相比,Abstractive Summarization摘要通常可以提供更好的結(jié)果。 這是因為,Abstractive Summarization方法可以應(yīng)對語義表示,推理和自然語言生成,這比數(shù)據(jù)驅(qū)動的方法(例如句子提?。┮y得多。
有許多技術(shù)可用于生成提取摘要。 為簡單起見,我將使用一種無監(jiān)督的學(xué)習(xí)方法來查找句子相似度并對其進行排名。 這樣的好處之一是,您無需在開始將其用于項目時就進行訓(xùn)練和構(gòu)建模型。
最好理解余弦相似度,以充分利用您將要看到的代碼。 余弦相似度是度量內(nèi)部乘積空間的兩個非零向量之間相似度的度量,該向量測量兩個向量之間的夾角余弦。 由于我們將句子表示為向量束,因此我們可以使用它來找到句子之間的相似性。 它測量向量之間夾角的余弦值。 如果句子相似,角度將為0。
接下來,下面是我們用于生成摘要文本的流程:
輸入文章→分解為句子→刪除停用詞→建立相似度矩陣→基于矩陣生成等級→選擇最前面的N個句子進行匯總
#Import all necessary libraries
from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
import numpy as np
import networkx as nx
import re
#Generate clean sentences
def read_article(file_name):
file = open(file_name, "r")
filedata = file.readlines()
article = filedata[0].split(". ")
sentences = []
for sentence in article:
print(sentence)
sentences.append(re.sub("[^a-zA-Z]", " ", sentence).split(" "))
sentences.pop()
return sentences
def sentence_similarity(sent1, sent2, stopwords=None):
if stopwords is None:
stopwords = []
sent1 = [w.lower() for w in sent1]
sent2 = [w.lower() for w in sent2]
all_words = list(set(sent1 + sent2))
vector1 = [0] * len(all_words)
vector2 = [0] * len(all_words)
# build the vector for the first sentence
for w in sent1:
if w in stopwords:
continue
vector1[all_words.index(w)] += 1
# build the vector for the second sentence
for w in sent2:
if w in stopwords:
continue
vector2[all_words.index(w)] += 1
return 1 - cosine_distance(vector1, vector2)
#Similarity matrix
#This is where we will be using cosine similarity to find similarity between sentences.
def build_similarity_matrix(sentences, stop_words):
# Create an empty similarity matrix
similarity_matrix = np.zeros((len(sentences), len(sentences)))
for idx1 in range(len(sentences)):
for idx2 in range(len(sentences)):
if idx1 == idx2: #ignore if both are same sentences
continue
similarity_matrix[idx1][idx2] = sentence_similarity(sentences[idx1], sentences[idx2], stop_words)
return similarity_matrix
#Generate Summary Method
def generate_summary(file_name, top_n=5):
stop_words = stopwords.words('english')
summarize_text = []
# Step 1 - Read text and tokenize
sentences = read_article(file_name)
# Step 2 - Generate Similary Martix across sentences
sentence_similarity_martix = build_similarity_matrix(sentences, stop_words)
# Step 3 - Rank sentences in similarity martix
sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix)
scores = nx.pagerank(sentence_similarity_graph)
# Step 4 - Sort the rank and pick top sentences
ranked_sentence = sorted(((scores[i],s) for i,s in enumerate(sentences)), reverse=True)
print("Indexes of top ranked_sentence order are ", ranked_sentence)
for i in range(top_n):
summarize_text.append(" ".join(ranked_sentence[i][1]))
# Step 5 - Offcourse, output the summarize texr
print("Summarize Text: \n", ". ".join(summarize_text))