from collections import Counter
import numpy as np

def vector(text):
    return Counter(text.lower().split())

def cosine(v1, v2):
    words = set(v1) | set(v2)
    a = np.array([v1.get(w,0) for w in words])
    b = np.array([v2.get(w,0) for w in words])
    return np.dot(a,b) / (np.linalg.norm(a)*np.linalg.norm(b))

t1 = "this is a sample text"
t2 = "this is another text"

print(cosine(vector(t1), vector(t2)))

