Example #1
0
def prepare_computation(id):
    """Prepare computation parameters according to discussion preferences"""
    from assembl.models import Content
    post = Content.get(id)
    active = any([
        post.discussion.preferences['watson_' + x]
        for x in watson_languages.keys()
    ])
    if active and post.body:
        api_version = config.get("watson_api_version", "2018-03-16")
        features = {}
        post_loc = post.body.first_original().locale.root_locale
        for feature_name, langs in watson_languages.items():
            if not post.discussion.preferences['watson_' + feature_name]:
                continue
            if post_loc not in langs:
                continue
            features[feature_name] = watson_feature_classes[feature_name]()
        if not features:
            return
        features = Features(**features)

        get_or_create_computation_on_post(post, "watson_" + api_version,
                                          features._to_dict())
        return True
Example #2
0
def get_similar_posts(discussion, post_id=None, text=None, cutoff=0.15):
    post_ids = discussion.db.query(Content.id).filter_by(
        discussion_id=discussion.id).all()
    post_ids = [x for (x,) in post_ids]
    (subcorpus, tfidf_model, gensim_model, similarity
     ) = get_similarity_matrix(discussion)
    lang = discussion.discussion_locales[0].split('_')[0]
    bowizer = BOWizer(lang)
    assert post_id or text, "Please give a text or a post_id"
    if post_id:
        words = bowizer.post_to_bow(Content.get(post_id))
    else:
        words = bowizer.text_to_bow(text)
    query_vec = gensim_model[tfidf_model[words]]
    results = [(v, post_ids[n]) for (n, v) in enumerate(similarity[query_vec])]
    results.sort(reverse=True)
    # forget self and duplicates
    results = [x for x in results if x[0] < 0.999]
    cutoff *= results[0][0]
    results = [(post_id, score) for (score, post_id) in results
               if score > cutoff]
    return results
Example #3
0
def prepare_computation(id):
    """Prepare computation parameters according to discussion preferences"""
    from assembl.models import Content
    post = Content.get(id)
    active = any([post.discussion.preferences['watson_' + x]
                  for x in watson_languages.keys()])
    if active and post.body:
        api_version = config.get("watson_api_version", "2018-03-16")
        features = {}
        post_loc = post.body.first_original().locale.root_locale
        for feature_name, langs in watson_languages.items():
            if not post.discussion.preferences['watson_' + feature_name]:
                continue
            if post_loc not in langs:
                continue
            features[feature_name] = watson_feature_classes[feature_name]()
        if not features:
            return
        features = Features(**features)

        get_or_create_computation_on_post(
            post, "watson_" + api_version, features._to_dict())
        return True