Beispiel #1
0
    def post(self, request, num_task):
        """
            Post function used to calculate similarity between models of a word
            :param request: request Object
            :param num_task: ID of task used
        """
        word_form = self.word_class()
        word = request.POST['word']
        topn = int(request.POST['topn'])
        task = Task.objects.get(pk=num_task)
        comparable_models = calculate_comparable_models(task)
        modelChoice = request.POST['modelChoice'].split("-")
        selected_models = [
            Word2Vec.load(task.model_set.get(name=modelChoice[0]).model),
            Word2Vec.load(task.model_set.get(name=modelChoice[1]).model)
        ]
        models = [Word2Vec.load(model.model) for model in task.model_set.all()]
        results = [
            (word,
             round(lncs2(word, selected_models[0], selected_models[1], topn),
                   2), round(c_measure(word, selected_models)[0], 2))
        ]

        return render(
            request, 'similarity.html', {
                'results': results,
                'word_form': word_form,
                'task': task,
                'comparable_models': comparable_models,
                'word': word,
                'topn': topn,
                'modelChoice': request.POST['modelChoice'],
            })
Beispiel #2
0
def fitness(threshold: float, topn: int, t: float):

    similarity = 3
    # Task 1 - Binary Classification
    predictions = []
    for word in binary_truth[:, 0]:
        if similarity == 0:  # cosine similarity
            prediction = (0 if
                          1 - cosine(model1[word], model2[word]) >= threshold
                          else 1)
        elif similarity == 1:  # lncs2 similarity
            prediction = (0 if lncs2(word, model1, model2, topn) >= threshold
                          else 1)
        elif similarity == 2:  # intersection of nearest neighbours
            prediction = (1 if intersection_nn(word, model1, model2, topn) >=
                          threshold else 0)
        elif similarity == 3:
            prediction = (0 if moving_lncs2(word, model1, model2, topn, t) >=
                          threshold else 1)
        predictions.append(prediction)

    result_accu = accuracy_score(
        binary_truth[:, 1].astype(int),
        np.array(predictions),
    )
    result_f1 = f1_score(
        binary_truth[:, 1].astype(int),
        np.array(predictions),
    )
    logger.info("F1: " + str(result_f1))
    logger.info("PRE: " + str(
        precision_score(
            binary_truth[:, 1].astype(int),
            np.array(predictions),
        )))
    logger.info("REC: " + str(
        recall_score(
            binary_truth[:, 1].astype(int),
            np.array(predictions),
        )))
    logger.info("ACC: " + str(result_accu))
    return result_accu
Beispiel #3
0
 # Load binary truths
 binary_truth = numpy.loadtxt(
     "./data/"
     + lang
     + "/semeval2020_ulscd_"
     + lang[:3]
     + "/truth/binary.txt",
     dtype=str,
     delimiter="\t",
 )
 # Task 1 - Binary Classification
 predictions = []
 for word in binary_truth[:, 0]:
     prediction = (
         0
         if lncs2(word, model1, model2, 10) >= config["THRESHOLD"]
         else 1
     )
     predictions.append(prediction)
 logger.info("CLassification score for " + lang)
 logger.info(
     "\n"
     + classification_report(
         binary_truth[:, 1].astype(float),
         numpy.array(predictions),
         target_names=["class 0 (stable)", "class 1 (change)"],
     )
 )
 # Load scores truths
 score_truth = numpy.loadtxt(
     "./data/"
Beispiel #4
0
     CURRENT_EXP_DIR.split("_")[0] + "_0" + "/model/" + lang +
     "/corpus1.model")
 model2 = Word2Vec.load(
     CURRENT_EXP_DIR.split("_")[0] + "_0" + "/model/" + lang +
     "/corpus2.model")
 # Load binary truths
 binary_truth = numpy.loadtxt(
     "./data/" + lang + "/semeval2020_ulscd_" + lang[:3] +
     "/targets.txt",
     dtype=str,
     delimiter="\t",
 )
 # Task 1 - Binary Classification
 predictions = []
 for word in binary_truth:
     prediction = (0 if lncs2(word, model1, model2, 10) >=
                   config["THRESHOLD"] else 1)
     predictions.append([word, str(prediction)])
 numpy.savetxt(
     CURRENT_EXP_DIR + "/res/answer/task1/" + lang + ".txt",
     numpy.array(predictions),
     fmt="%s",
     delimiter="\t",
 )
 # Load scores truths
 score_truth = numpy.loadtxt(
     "./data/" + lang + "/semeval2020_ulscd_" + lang[:3] +
     "/targets.txt",
     dtype=str,
     delimiter="\t",
 )