def compute_similarities(text, models, count=None):
    # Get the simhash of the submitted message
    _hash = simhash(util.unicodeToAscii(text))
    candidates, scores = {}, []

    # TODO: Investiage ways of speeding this - complexity is O(n)
    for model in models:
        target = simhash(hash=long(model.simhash))
        if long(target) == long(_hash):
            continue
        similarity = _hash.similarity(target)
        if similarity >= similarity_threshold:
            scores.append((model.id, similarity))
            candidates[model.id] = model
    if len(scores) == 0:
        return []

    scores.sort(key=lambda x: x[1], reverse=True)
    result_size = max_similar_messages if count is None else count

    retval = []
    for x in range(result_size):
        message_dict = candidates[scores[x][0]].as_dict()
        del message_dict['simhash']
        message_dict['score'] = scores[x][1]
        retval.append(message_dict)
    return retval
Finds items that are similar to the specified text
dataset/ETHPy150Open dssg/ushine-learning/dssg/webapp/rest_api.py/compute_similarities