File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44# published by PACKT Publishing
55#
66# It is made available under the MIT License
7+ from __future__ import print_function
78
8- import nltk .corpus
9- import milk
9+ try :
10+ import nltk .corpus
11+ except ImportError :
12+ print ("nltk not found" )
13+ print ("please install it" )
14+ raise
15+ from scipy .spatial import distance
1016import numpy as np
1117import string
1218from gensim import corpora , models , similarities
@@ -26,8 +32,17 @@ def get_texts(self):
2632
2733 def __len__ (self ):
2834 return len (self .input )
29- dataset = sklearn .datasets .load_mlcomp ("20news-18828" , "train" ,
30- mlcomp_root = '../data' )
35+ try :
36+ dataset = sklearn .datasets .load_mlcomp ("20news-18828" , "train" ,
37+ mlcomp_root = './data' )
38+ except :
39+ print ("Newsgroup data not found." )
40+ print ("Please download from http://mlcomp.org/datasets/379" )
41+ print ("And expand the zip into the subdirectory data/" )
42+ print ()
43+ print ()
44+ raise
45+
3146otexts = dataset .data
3247texts = dataset .data
3348
@@ -62,13 +77,14 @@ def __len__(self):
6277for i , c in enumerate (corpus ):
6378 for ti , v in model [c ]:
6479 thetas [i , ti ] += v
65- distances = milk .unsupervised .pdist (thetas )
80+
81+ distances = distance .squareform (distance .pdist (thetas ))
6682large = distances .max () + 1
6783for i in xrange (len (distances )):
6884 distances [i , i ] = large
6985
70- print otexts [1 ]
71- print
72- print
73- print
74- print otexts [distances [1 ].argmin ()]
86+ print ( otexts [1 ])
87+ print ()
88+ print ()
89+ print ()
90+ print ( otexts [distances [1 ].argmin ()])
You can’t perform that action at this time.
0 commit comments