Esempio n. 1
0
def binary(lang):
    g.lang = lang
    s = set()
    s.add(lang)
    other_lang = list(set(language_dicts.keys()) -
                      s)[0]  # works only for two languages
    g.strings = language_dicts[lang]

    if request.method == "GET":
        return render_template("binary.html",
                               other_lang=other_lang,
                               languages=languages,
                               model1=list(our_models.keys())[-2],
                               model2=list(our_models.keys())[-1],
                               models=our_models,
                               url=url)
    else:
        if request.form.getlist("word"):  # First time click
            word = request.form.getlist("word")[0]
            word = process_query(word)
            models = request.form.getlist("models")
            if len(models) != 2:
                error_value = 'Select 2 years!'
                return render_template('binary.html',
                                       error=error_value,
                                       models=our_models,
                                       url=url,
                                       other_lang=other_lang,
                                       model1=list(our_models.keys())[-3],
                                       model2=list(our_models.keys())[-2],
                                       languages=languages)

            model1, model2 = models
            if model1 == model2:
                error_value = "Identical models"
                return render_template('binary.html',
                                       error=error_value,
                                       model1=model1,
                                       model2=model2,
                                       other_lang=other_lang,
                                       languages=languages,
                                       models=our_models,
                                       url=url)

            if word == "Incorrect tag!":
                error_value = "Incorrect tag!"
                return render_template('binary.html',
                                       error=error_value,
                                       model1=model1,
                                       model2=model2,
                                       other_lang=other_lang,
                                       languages=languages,
                                       models=our_models,
                                       url=url)
            message = {
                'operation': '7',
                'word': word,
                'model1': model1,
                "model2": model2,
                'with_examples': True
            }

            result = json.loads(serverquery(message).decode('utf-8'))
            if word + " is unknown to the model" in result:
                error_value = "Unknown word"
                return render_template("binary.html",
                                       error=error_value,
                                       word=word,
                                       models=our_models,
                                       tags=tags,
                                       url=url,
                                       usermodels=[defaultmodel],
                                       tags2show=exposed_tags)
        else:  # User presses the 'Confirm' button to wait longer for the results
            word, model1, model2 = request.form.get("confirm").split()
            message = {
                'operation': '7',
                'word': word,
                'model1': model1,
                "model2": model2,
                'with_examples': "slow"
            }
            result = json.loads(serverquery(message).decode('utf-8'))
        label = result["label"]
        proba = float(result["proba"])
        examples = result["examples"]
        frequencies = result["frequencies"]
        if type(examples) is dict:
            examples_type = 1
        else:
            examples_type = 0

        if label == "1":
            ok_models = [model1, model2]

            m = hashlib.md5()
            hashword = ":".join(
                [",".join([str(i) for i in j]) for j in ok_models] + [word])
            hashword = hashword.encode('ascii', 'backslashreplace')
            m.update(hashword)

            if not os.path.isdir("data/images/heatmaps"):
                os.mkdir("data/images/heatmaps")

            fname = m.hexdigest()

            trajectory_message = {
                'operation': '6',
                'query': word,
                'pos': "ALL",
                'model': ok_models
            }
            trajectory_result = json.loads(
                serverquery(trajectory_message).decode('utf-8'))

            if not os.path.exists(root + 'data/images/tsne_shift'):
                os.makedirs(root + 'data/images/tsne_shift')
            if trajectory_result['word_list']:
                tsne_semantic_shifts(trajectory_result, fname)
            return render_template("binary.html",
                                   model1=model1,
                                   model2=model2,
                                   frequencies=frequencies,
                                   other_lang=other_lang,
                                   languages=languages,
                                   models=our_models,
                                   url=url,
                                   label=label,
                                   proba="{:.2f}".format(proba),
                                   word=word,
                                   examples=examples,
                                   examples_type=examples_type,
                                   fname=fname)

        return render_template("binary.html",
                               model1=model1,
                               model2=model2,
                               frequencies=frequencies,
                               other_lang=other_lang,
                               languages=languages,
                               models=our_models,
                               url=url,
                               label=label,
                               proba="{:.2f}".format(proba),
                               word=word,
                               examples=examples,
                               examples_type=examples_type)
Esempio n. 2
0
def associates_page(lang):
    global our_models
    g.lang = lang
    s = set()
    s.add(lang)
    other_lang = list(set(language_dicts.keys()) -
                      s)[0]  # works only for two languages
    g.strings = language_dicts[lang]
    if request.method == 'POST':
        list_data = request.form['list_query']

        # Nearest associates queries
        if list_data != 'dummy' and list_data.replace('_', '').replace('-', '').\
                replace('::', '').replace(' ', '').isalnum():
            list_data = list_data.strip()
            query = process_query(list_data)

            model_value = request.form.getlist('model')

            if len(model_value) < 1:
                model_value = [defaultmodel]

            if query == "Incorrect tag!":
                error_value = "Incorrect tag!"
                return render_template('associates.html',
                                       error=error_value,
                                       word=list_data,
                                       models=our_models,
                                       tags2show=exposed_tags,
                                       other_lang=other_lang,
                                       languages=languages,
                                       url=url,
                                       usermodels=model_value)
            userpos = []
            if tags:
                pos_value = request.form.getlist('pos')

                if len(pos_value) < 1:
                    pos = query.split('_')[-1]
                else:
                    pos = pos_value[0]
                if pos != 'ALL':
                    userpos.append(pos)
                if pos == 'Q':
                    pos = query.split('_')[-1]
            else:
                pos = 'ALL'

            images = {query.split('_')[0]: None}
            models_row = {}
            inferred = set()
            frequencies = {}
            labels, probas = list(), list()
            results = map(
                get_model_changes,
                [(model1, model2, query)
                 for (model1, model2) in zip(model_value, model_value[1:])])
            results = sorted(results)
            for model1, model2, result in results:
                if query + " is unknown to the model" in result:
                    label = "0"
                    proba = 0.0
                else:
                    label = result["label"]
                    proba = float(result["proba"])
                labels.append(label)
                probas.append(proba)

            for model in model_value:
                if not model.strip() in our_models:
                    return render_template('associates.html',
                                           other_lang=other_lang,
                                           url=url,
                                           languages=languages,
                                           usermodels=model_value)

            results = map(get_model_neighbors,
                          [(x, query, pos) for x in model_value])
            results = sorted(results)
            ok_models = list()
            for model, result, model_query in results:
                frequencies[model] = result['frequencies']
                if model_query != query:
                    frequencies[model][query] = frequencies[model][model_query]
                if model_query + " is unknown to the model" in result:
                    models_row[model] = "Unknown!"
                    continue
                elif 'No results' in result:
                    models_row[model] = 'No results!'
                    continue
                else:
                    for word in result['neighbors']:
                        images[word[0].split('_')[0]] = None
                    models_row[model] = result['neighbors']
                    ok_models.append(model)
                    if dbpedia:
                        try:
                            images = get_images(images)
                        except TimeoutError:
                            pass
                    if 'inferred' in result:
                        inferred.add(model)

            m = hashlib.md5()
            hashword = ":".join(
                [",".join([str(i) for i in j])
                 for j in model_value] + [query, pos])
            hashword = hashword.encode('ascii', 'backslashreplace')
            m.update(hashword)

            fname = m.hexdigest()

            if len(ok_models) == 0:
                error_value = "Unknown word"
                return render_template("associates.html",
                                       error=error_value,
                                       models=our_models,
                                       tags=tags,
                                       url=url,
                                       usermodels=model_value,
                                       tags2show=exposed_tags)

            trajectory_message = {
                'operation': '6',
                'query': query,
                'pos': pos,
                'model': ok_models
            }
            trajectory_result = json.loads(
                serverquery(trajectory_message).decode('utf-8'))

            if query + " is unknown to the model" in trajectory_result:
                error_value = "Unknown word"
                return render_template("associates.html",
                                       error=error_value,
                                       models=our_models,
                                       tags=tags,
                                       url=url,
                                       usermodels=[defaultmodel],
                                       tags2show=exposed_tags)

            if not os.path.exists(root + 'data/images/tsne_shift'):
                os.makedirs(root + 'data/images/tsne_shift')
            if trajectory_result['word_list']:
                tsne_semantic_shifts(trajectory_result, fname)

            return render_template('associates.html',
                                   list_value=models_row,
                                   word=query,
                                   pos=pos,
                                   number=len(model_value),
                                   wordimages=images,
                                   models=our_models,
                                   tags=tags,
                                   other_lang=other_lang,
                                   languages=languages,
                                   tags2show=exposed_tags,
                                   url=url,
                                   usermodels=model_value,
                                   userpos=userpos,
                                   inferred=inferred,
                                   frequencies=frequencies,
                                   visible_neighbors=10,
                                   fname=fname,
                                   labels=labels,
                                   probas=probas)
        else:
            error_value = "Incorrect query!"
            return render_template("associates.html",
                                   error=error_value,
                                   models=our_models,
                                   tags=tags,
                                   url=url,
                                   usermodels=[defaultmodel],
                                   tags2show=exposed_tags)

    return render_template('associates.html',
                           models=our_models,
                           tags=tags,
                           other_lang=other_lang,
                           languages=languages,
                           url=url,
                           usermodels=['2013', '2014', '2015'],
                           tags2show=exposed_tags)