Today we describe a method to do text summarization:
#codingexercise
def summarize(self, text):
from gensim.models import word2vec
# load the test sample
sentences = text.split('\n')
model =
word2vec.Word2Vec(sentences, size=200)
model.save_word2vec_format('/tmp/vectors.bin',
binary=True)
model =
word2vec.Word2Vec.load_word2vec_format('/tmp/vectors.bin', binary=True)
#get selection from text based on centroids
selected from k-means classification avoiding graph
from nltk.cluster.kmeans import
KMeansClusterer
NUM_CLUSTERS = 3 # user can specify this or we take a default
value
kclusterer =
KMeansClusterer(NUM_CLUSTERS, distance=model.similarity, repeats=25)
assigned_clusters =
kclusterer.cluster(model, assign_clusters=True)
pr =
translate_to_words(assigned_clusters)
# render the summary
import operator
sorted_pr = sorted(pr.items(),
key=operator.itemgetter(1), reverse=True)
important = [int(i[0]) for i in
sorted_x][:10]
scored_sentences = {}
for sentence in sentences:
matches =
set(sentence.split()).intersection(important)
score = 0
for match in matches:
score+= pr[match]
scored_sentences[sentence]=score
reordered_sentences = [ i[0] for i
in sorted(scored_sentences.items(), key=operator.itemgetter(1),
reverse=True)[:10] ]
ordered_sentences = [ x for x in
sentences if x in reordered_sentences ]
summary =
'\n'.join(ordered_sentences)
print(summary)
return ordered_sentences
Given a sequence of motions in terms of unit displacements as - G - for moving forward, L for moving to the left and R for moving to the right, determine if a robot executing the sequence can stay within a circle
Solution: We keep track of the current position and the orientation in terms of N, E, W, and S. For each movement in the sequence, we update the current co-ordinate by displacing x or y but not both and by updating the orientation. If the ending position converges towards the origin, we know the robot stays within a circle.
There is one observation for this coding exercise. There is no way to be sure that a given sequence converges in a predetermined number of steps.
Solution: We keep track of the current position and the orientation in terms of N, E, W, and S. For each movement in the sequence, we update the current co-ordinate by displacing x or y but not both and by updating the orientation. If the ending position converges towards the origin, we know the robot stays within a circle.
There is one observation for this coding exercise. There is no way to be sure that a given sequence converges in a predetermined number of steps.
No comments:
Post a Comment