Commit 7c40db97 authored by menderes's avatar menderes
Browse files

Upload New File

parent 0d86f836
Loading
Loading
Loading
Loading
+168 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 16:11:25 2021

@author: erkinmenderes
"""
import pandas as pd

#GS_annotations file should be in the same directory, else insert filepath
output = pd.read_csv("knowlege_path_subset_train.csv", sep=';', error_bad_lines=False)
index_list = output.index.tolist()
#output["Essay"] += 1
#print(index_list)
essay_output_list = output["Essay"].tolist()
essay_output_list = essay_output_list[:94]
path_list = output["Path"].tolist()

gold_df = pd.read_csv("input_subset_train.csv", sep=';', error_bad_lines=False)
gold_essay_list = gold_df["Essay"].tolist()
reiss_gold = gold_df["Reiss"].tolist()


def replacer(list_string):
    """
    Function to replace all the unnecessary characters in the paths in order 
    to further process the data and return clean strings

    Parameters
    ----------
    list_string : list
        a list containing strings of words (here: conceptnet paths
    as strings)

    Returns
    -------
    str
        strings cleaned from unwanted and unnecessary characters and tokens

    """
    text = list_string.replace("[", "").replace("]", "").replace('\'', "").replace("\"", "").replace(",", "")
    return text.split()

# create maslow and reiss human needs
maslow_human_needs = ["physiological needs", "stability", "love/belonging", "esteem", "spiritual growth"]
reiss_motives = ["food", "rest", "health", "save_money", "order", "safety",
                 "romance", "belonging", "family", "contact", "competition",
                 "honor", "approval", "status", "power", "curiosity", "serenity",
                 "idealism", "independent"]


# create a cleaned list of paths
cleaned_paths = [replacer(path) for path in path_list]


def assign_reiss(path_list):
    """
    Assigns Reiss motive to a a graph consisting of a list of its paths

    Parameters
    ----------
    path_list : list
        The entire list of subraphs which consist of their paths
        paths are split into their single units as strings

    Returns
    -------
    None.

    """
    temp_list = []
    human_needs = []
    for path in path_list:
        for word in path:
            if word in reiss_motives:
                temp_list.append(word)
        human_needs.append(temp_list[0]) 
        temp_list = []
    return human_needs


#assign reiss human needs for every graph via its top ranked path
reiss_needs = assign_reiss(cleaned_paths)

#maslow needs list to assign maslow need accordingly
physiological_needs = ['food', 'rest']
safety = ['health', 'save_money', 'order', 'safety']
love_belonging = ['love', 'belonging', 'family', 'contact']
esteem = ['competition','honor', 'approval', 'status', 'power']
spiritual_growth = ['curiosity', 'serenity','idealism', 'independent']

def assign_maslow(reiss_list):
    """
    Function that assigns corresponding maslow human need given its 
    reiss human need

    Parameters
    ----------
    reiss_list : list 
        list containing assigned reiss human need for every essay 
        (via the top ranked graphpath)

    Returns
    -------
    maslow_needs : list
        list containing corresponding maslow human needs

    """
    maslow_needs = []
    for r in reiss_needs:
        if r in physiological_needs:
            maslow_needs.append(maslow_human_needs[0])
        elif r in safety:
            maslow_needs.append(maslow_human_needs[1])
        elif r in love_belonging:
            maslow_needs.append(maslow_human_needs[2])
        elif r in esteem:
            maslow_needs.append(maslow_human_needs[3])
        else:
            maslow_needs.append(maslow_human_needs[4])
    return maslow_needs

maslow_needs = assign_maslow(reiss_needs)

# create joint list of reiss and maslow
hn_list_full = list(zip(maslow_needs, reiss_needs))

#post-processing for evaluation in reiss
reiss_needs = [w.replace("independent", "independence") for w in reiss_needs]
reiss_needs = [w.replace("save_money", "savings") for w in reiss_needs]

#post-processing for evaluation in maslow
maslow_needs = [w.replace("love / belonging", "love/belonging") for w in maslow_needs]

# add columns accordingly
output["Maslow_predict"] = maslow_needs
output["Reiss_predict"] = reiss_needs

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import ConfusionMatrixDisplay, classification_report, confusion_matrix

train_acc = accuracy_score(reiss_gold, reiss_needs)
print("Training accuracy: ", train_acc)

train_prec = precision_score(reiss_gold, reiss_needs, average='macro')
print("Training precision: ", train_prec)

train_recall = recall_score(reiss_gold, reiss_needs, average='macro')
print("Training recall: ", train_recall)

train_f1score = f1_score(reiss_gold, reiss_needs, average="macro")
print("Training f1-score: ", train_f1score)

print("-----------------------------------------------------------------------")

print("Classification report:")
class_report = classification_report(reiss_gold, reiss_needs)
print(class_report)

cm_reiss = confusion_matrix(reiss_gold, maslow_needs)
print(cm_reiss)