Created
October 16, 2012 17:25
-
-
Save abhinavgupta/3900708 to your computer and use it in GitHub Desktop.
PageRank for text summarization
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
import networkx as nx | |
import numpy as np | |
from nltk.tokenize.punkt import PunktSentenceTokenizer | |
from sklearn.feature_extraction.text import TfidfTransformer, CountVectorizer | |
def textrank(document): | |
sentence_tokenizer = PunktSentenceTokenizer() | |
sentences = sentence_tokenizer.tokenize(document) | |
bow_matrix = CountVectorizer().fit_transform(sentences) | |
normalized = TfidfTransformer().fit_transform(bow_matrix) | |
similarity_graph = normalized * normalized.T | |
nx_graph = nx.from_scipy_sparse_matrix(similarity_graph) | |
scores = nx.pagerank(nx_graph) | |
return sorted(((scores[i],s) for i,s in enumerate(sentences)), reverse=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment