Example #1
0
    def it_should_find_related_test(self):
        vector_space = VectorSpace(self.documents)

        eq_(vector_space.related(0), [1.0000000000000002, 0.9999999999999998, 0.0])
Example #2
0
def anotate(inpt, skipsize):
	k = 2
	queueSize = skipsize * 2 + 1
	queueMid = skipsize + 1

	queueIsReady = lambda x : len(x) == queueSize
	def push(element, queue):
		queue.append(element)
		if len(queue) > queueSize:
			queue.pop(0)
	
	vocabulary = get_document_vocabulary(inpt)
	vocSize = len(vocabulary) + 1

        print "Starting on determining word co-occurences."

	cocs = defaultdict(list)
	queue = []
	for word in inpt:
		push(word, queue)
		if queueIsReady(queue):
			mid = queue[queueMid]
			if mid in vocabulary:
				coc = []
				for i in xrange(skipsize):
					if queue[i] in vocabulary:
						word1 = queue[i]
					else:
						word1 = "_UNKNOWN_"
					if queue[i+1+skipsize] in vocabulary:
						word2 = queue[i+1+skipsize]
					else:
						word2 = "_UNKNOWN_"

					coc.append(word1)
					coc.append(word2)
				#print "final co-occurences"
				#print coc
				cocs[mid].append(coc)
				#print "Coc[mid]"
				#print cocs[mid]

	print "Determining LSA relatedness scores between documents..."
        
	clustered = dict()
	for key in cocs.keys():
                
            #print "KEY:" + key
            #print "\ncocs[" + key + "]:"
            #print "len cocs key"
            #print len(cocs[key])
            #print cocs[key][0]
            #print cocs[key][1]
            #print " ".join(cocs[key][0])
            a = [" ".join(cocs[key][i]) for i in range(len(cocs[key]))]
            #a = a[:6]
            #print "len a"
            #print len(a)
            """
            print a[0]
            print a[1]
            print a[2]
            print a[3]
            print a[4]
            print a[5]
            """
            vector_space = VectorSpace(a)
            scores = vector_space.related(0)
            LSIscores = []
            for i in range(len(a)):
                    ss = {"docText" : a[i], "similarity" : scores[i]}
                    LSIscores.append(ss)
            LSIscores = sorted(LSIscores, key=lambda k: k['similarity'], reverse=True)
            """print "scores"
            print LSIscores"""
            LSIscores = LSIscores[:len(LSIscores)/2]

            """
            text = ""
            for item in LSIscores:
                    text += item["docText"] + " "
            print "text is: " +  text

            d = defaultdict(int)
            for word in text.split():
                    d[word] += 1
            """
            itemsToCluster = []
            for item in LSIscores:
                    text = item["docText"]
                    d = defaultdict(int)
                    for word in text.split():
                            d[word] += 1
                    d = normalize_coc(d)
                    itemsToCluster.append(d)
            """
            print "printing d follows"
            print d
            #normalize
            d = normalize_coc(d)
            print "after normalization"
            print d
            d = list(d)
            print d
            """
            
            clustered[key] = kmeans_process(itemsToCluster)
            #print "half scores"
            #print LSIscores
            
            #print cocs[key]
	    #clustered[key] = kmeans_process(LSIScores)
	
        
	print "Starting anotating corpus."
	anotated = []
	queue = []
	for word in inpt:
		push(word, queue)
		if queueIsReady(queue):
			word = queue[queueMid]
			if word in clustered and len(clustered[word]) > 1:
				coc = defaultdict(int)
				for i in xrange(skipsize):
					if queue[i] in vocabulary:
						word1 = queue[i]
					else:
						word1 = "_UNKNOWN_"
					if queue[i+1+skipsize] in vocabulary:
						word2 = queue[i+1+skipsize]
					else:
						word2 = "_UNKNOWN_"

					coc[word1] += 1
					coc[word2] += 1

				coc = normalize_coc(coc)

				# Now get the best cluster
				bestValue = 1
				bestIndex = -1
				for i in xrange(k):
					distance = clustered[word][i].distance(coc)
					if distance < bestValue:
						bestValue = distance
						bestIndex = i
				word = word + "_" + str(bestIndex) + " "
			anotated.append(word)

	return (clustered, anotated)
Example #3
0
    def it_should_find_return_similarity_rating_test(self):
        vectorSpace = VectorSpace(self.documents)

        eq_(vectorSpace.related(0), [1.0, 0.9922455760198575, 0.08122814162371816, 0.0762173599906487])
Example #4
0
offset = 0
rows = c.execute('select * from articles limit {0} offset {1}'.format(limit, offset))

for row in rows:
  #print row
  texts.append(row[1] + " " + row[11])
  ids.append(row[0])

#vector_space = VectorSpace(["The cat in the hat disabled", "A cat is a fine pet ponies.", "Dogs and cats make good pets.","I haven't got a hat."])
vector_space = VectorSpace(texts)

#Search for cat
#print vector_space.search(["cat"])
#Show score for relatedness against document 0

group = dict()
for id in range(len(ids)):
  prob = vector_space.related(id)
  for i in range(len(ids)):
    if prob[i] > 0.2 and prob[i] < 0.9 and id != i:
      if group.has_key(offset + id + 1) == False:
        group[offset + id + 1] = []
      group[offset + id + 1].append(offset + i + 1)
      print(offset + id + 1, offset + i + 1, prob[i])
print(group)

#id = 139
#prob = vector_space.related(id)
#for i in range(len(ids)):
#  if prob[i] > 0.2 and id != i:
#    print(offset + id + 1, offset + i + 1, prob[i])