Last active
March 8, 2020 14:18
-
-
Save adeen-s/68dcf6e4df9ff35f35fdb0a0edbda653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dotenv import load_dotenv | |
load_dotenv() | |
from nltk.corpus import stopwords | |
from nltk.cluster.util import cosine_distance | |
import numpy as np | |
import networkx as nx | |
from flask import Flask, abort | |
import requests | |
import os | |
import json | |
app = Flask(__name__) | |
def read_article(text): | |
article = text.split(".") | |
sentences = [] | |
for sentence in article: | |
sentences.append(sentence.replace("[^a-zA-Z]", " ").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) | |
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 | |
def generate_summary(text, top_n=2): | |
stop_words = stopwords.words('english') | |
summarize_text = [] | |
# Step 1 - Read text and split it | |
sentences = read_article(text) | |
# 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 - Output the summarized text | |
result = (". ".join(summarize_text)) | |
return json.dumps({"synopsis": result}) | |
def synopsis(request): | |
if not request.method == 'POST': | |
return abort(403) | |
data = request.form.to_dict() | |
if not ("text" in data): | |
return abort(422) | |
headers = { | |
'Access-Control-Allow-Origin': '*' | |
} | |
return (generate_summary(data["text"]), 200, headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment