import math


def counter_cossim(c1, c2):
    """ Given two counters, treat them as feature vectors and return cosine similarity"""
    common_keys = c1.keys() & c2.keys()
    d1 = math.sqrt(sum([value ** 2 for value in c1.values()]))
    d2 = math.sqrt(sum([value ** 2 for value in c2.values()]))
    denom = d1 * d2
    if denom == 0:
        return 0.0, c1 & c2
    
    num = 0
    for key in common_keys:
        num += c1[key] * c2[key]

    return float(num) / denom, c1 & c2