|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# Imports |
| 5 | +from nltk.corpus import stopwords |
| 6 | +from nltk.cluster.util import cosine_distance |
| 7 | +import numpy as np |
| 8 | +import networkx as nx |
| 9 | + |
| 10 | +# Enter the File path |
| 11 | +file_name = input("Enter the Source File: ") |
| 12 | +print("This script requires 'stopwords' from NLTK, see README" |
| 13 | + "Quick Download Command: ```python -m nltk.downloader stopwords```") |
| 14 | + |
| 15 | +def read_article(file_name): |
| 16 | + """ |
| 17 | + Reads the Text file, and coverts them into sentences. |
| 18 | + :param file_name: Path of text file (line 12) |
| 19 | + :return: sentences |
| 20 | + """ |
| 21 | + file = open(file_name, 'r', encoding="utf-8") |
| 22 | + filedata = file.readlines() |
| 23 | + article = filedata[0].split(". ") |
| 24 | + sentences = [] |
| 25 | + |
| 26 | + for sentence in article: |
| 27 | + # Uncomment if you want to print the whole file on screen. |
| 28 | + # print(sentence) |
| 29 | + sentences.append(sentence.replace("[^a-zA-Z]", " ").split(" ")) |
| 30 | + sentences.pop() |
| 31 | + |
| 32 | + return sentences |
| 33 | + |
| 34 | + |
| 35 | +def sentence_similarity(sent1, sent2, stopwords=None): |
| 36 | + """ |
| 37 | + To determine the Cosine Similarity between sentences |
| 38 | + :param sent1: Vector of sentence 1 |
| 39 | + :param sent2: Vector of sentence 2 |
| 40 | + :param stopwords: Words to be ignored in Vectors (Read README.md) |
| 41 | + :return: Cosine Similarity score |
| 42 | + """ |
| 43 | + if stopwords is None: |
| 44 | + stopwords = [] |
| 45 | + |
| 46 | + sent1 = [w.lower() for w in sent1] |
| 47 | + sent2 = [w.lower() for w in sent2] |
| 48 | + |
| 49 | + all_words = list(set(sent1 + sent2)) |
| 50 | + |
| 51 | + vector1 = [0] * len(all_words) |
| 52 | + vector2 = [0] * len(all_words) |
| 53 | + |
| 54 | + # build the vector for the first sentence |
| 55 | + for w in sent1: |
| 56 | + if w in stopwords: |
| 57 | + continue |
| 58 | + vector1[all_words.index(w)] += 1 |
| 59 | + |
| 60 | + # build the vector for the second sentence |
| 61 | + for w in sent2: |
| 62 | + if w in stopwords: |
| 63 | + continue |
| 64 | + vector2[all_words.index(w)] += 1 |
| 65 | + |
| 66 | + return 1 - cosine_distance(vector1, vector2) |
| 67 | + |
| 68 | + |
| 69 | +def build_similarity_matrix(sentences, stop_words): |
| 70 | + """ |
| 71 | + Build the similarity index of words in sentences |
| 72 | + :param sentences: Clean sentences |
| 73 | + :param stop_words: Words to be ignored in Vectors (Read README.md) |
| 74 | + :return: Similarity index (Tokenized words) |
| 75 | + """ |
| 76 | + # Create an empty similarity matrix |
| 77 | + similarity_matrix = np.zeros((len(sentences), len(sentences))) |
| 78 | + |
| 79 | + for idx1 in range(len(sentences)): |
| 80 | + for idx2 in range(len(sentences)): |
| 81 | + if idx1 == idx2: # ignore if both are same sentences |
| 82 | + continue |
| 83 | + similarity_matrix[idx1][idx2] = sentence_similarity(sentences[idx1], sentences[idx2], stop_words) |
| 84 | + |
| 85 | + return similarity_matrix |
| 86 | + |
| 87 | + |
| 88 | +def generate_summary(file_name, top_n=5): |
| 89 | + """ |
| 90 | + Generate Summary of the text file |
| 91 | + :param file_name: Path of text file (line 12) |
| 92 | + :param top_n: Number of Sentence to be vectorized (tokenized) |
| 93 | + :return: Summary of text |
| 94 | + """ |
| 95 | + stop_words = stopwords.words('english') |
| 96 | + summarize_text = [] |
| 97 | + |
| 98 | + # Step 1 - Read text anc split it |
| 99 | + sentences = read_article(file_name) |
| 100 | + |
| 101 | + # Step 2 - Generate Similarity Matrix across sentences |
| 102 | + sentence_similarity_martix = build_similarity_matrix(sentences, stop_words) |
| 103 | + |
| 104 | + # Step 3 - Rank sentences in similarity matrix |
| 105 | + sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix) |
| 106 | + scores = nx.pagerank(sentence_similarity_graph) |
| 107 | + |
| 108 | + # Step 4 - Sort the rank and pick top sentences |
| 109 | + ranked_sentence = sorted(((scores[i], s) for i, s in enumerate(sentences)), reverse=True) |
| 110 | + |
| 111 | + # Print the index of the statements |
| 112 | + # print("Indexes of top ranked_sentence order are ", ranked_sentence) |
| 113 | + |
| 114 | + for i in range(top_n): |
| 115 | + summarize_text.append(" ".join(ranked_sentence[i][1])) |
| 116 | + |
| 117 | + # Step 5 - Output of the text file |
| 118 | + filepath_index = file_name.find('.txt') |
| 119 | + outputpath = file_name[:filepath_index]+'_textRank.txt' |
| 120 | + |
| 121 | + with open(outputpath, 'w') as w: |
| 122 | + for sentence in summarize_text: |
| 123 | + w.write(str(sentence)+'\n') |
| 124 | + |
| 125 | + |
| 126 | +generate_summary(file_name, 5) |
0 commit comments