text: import pandas as pd
  import numpy
  import nltk
  import sklearn
  import re
  from pandas import DataFrame
  from nltk.stem.snowball import SnowballStemmer
  from sklearn.feature_extraction.text import TfidfVectorizer
  from sklearn.metrics.pairwise import cosine_similarity
  from sklearn.cluster import KMeans
  import matplotlib.pyplot as plt
  import matplotlib as mpl
  from sklearn.manifold import MDS
  import seaborn as sns
  
  #df = pd.read_csv(r"C:\Users\98285\Documents\Book4.csv")
  
  df = pd.read_csv("corpus.csv")
  
  summaries = df.summary.tolist()
          
  stemmer = SnowballStemmer("english")
  stopwords = nltk.corpus.stopwords.words('english')
  def tokenize_and_stem(text):
      # first tokenize by sentence, then by word to ensure that punctuation is caught as it's own token
      tokens = [word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
      filtered_tokens = []
      # filter out any tokens not containing letters (e.g., numeric tokens, raw punctuation)
      for token in tokens:
          if re.search('[a-zA-Z]', token):
              filtered_tokens.append(token)
      stems = [stemmer.stem(t) for t in filtered_tokens]
      return stems
         
  def tokenize_only(text):
      tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
      filtered_tokens = []
      # filter out any tokens not containing letters (e.g., numeric tokens, raw punctuation)
      for token in tokens:
          if re.search('[a-zA-Z]', token):
              filtered_tokens.append(token)
      return filtered_tokens
      
  totalvocab_stemmed = []
  totalvocab_tokenized = []
  for i in summaries:
      allwords_stemmed = tokenize_and_stem(i) #for each item in 'synopses', tokenize/stem
      totalvocab_stemmed.extend(allwords_stemmed) #extend the 'totalvocab_stemmed' list
      allwords_tokenized = tokenize_only(i)
      totalvocab_tokenized.extend(allwords_tokenized)
  
  vocab_frame = pd.DataFrame({'words': totalvocab_tokenized}, index = totalvocab_stemmed)    
  tfidf_vectorizer = TfidfVectorizer(min_df=0.1, stop_words='english',
                                    use_idf=True, tokenizer=tokenize_and_stem, ngram_range=(1,3))
  tfidf_matrix = tfidf_vectorizer.fit_transform(summaries)
  terms = tfidf_vectorizer.get_feature_names()
  dist = 1 - cosine_similarity(tfidf_matrix)
  
  num_clusters = 4
  km = KMeans(n_clusters=num_clusters)
  km.fit(tfidf_matrix)
  clusters = km.labels_.tolist()
  
  titles = df.titles.tolist()
  
  papers = { 'title': titles, 'summary': summaries, 'cluster': clusters }
  frame = pd.DataFrame(papers, index = [clusters] , columns = ['title', 'cluster'])
  frame['cluster'].value_counts()
  
  order_centroids = km.cluster_centers_.argsort()[:, ::-1]
  for i in range(num_clusters):
      print("Cluster %d words:" % i)
      for ind in order_centroids[i, :5]:
          print(' %s' % vocab_frame.ix[terms[ind].split(' ')].values.tolist()[0][0].encode('utf-8', 'ignore'))
  
      #for title in frame.ix[i]['title'].values.tolist():
         #print(' %s,' % title) 
  
  MDS()
  mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
  pos = mds.fit_transform(dist)
  xs, ys = pos[:, 0], pos[:, 1]
  
  df1 = pd.DataFrame(dict(x=xs, y=ys, label=clusters, title=titles))
  sns.set(style="white")
  sns.pairplot(df1, hue="label", palette="hls")
  