Commit 026dce80 authored by kuehner's avatar kuehner
Browse files

Upload New File

parent 13bb276d
Loading
Loading
Loading
Loading
+167 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 23 02:03:59 2021

@author: SWP-Group
This file assigns the Human Needs via the generated output paths.
Since the output paths are ranked according to the "strength" (the strongest 
and best paths appear at the top), these can be taken as indications for 
human needs annotations

The input data is the output of the system and the golddata, both as in csv file format.
These files are then processed via pandas module and necessary changes are made
to the both the files (such as adjusting strings accordingly)

This file creates the necessary output to run the "human_needs_evaluation.py"
file and evaluate the results of the system
"""
import pandas as pd

#output_final.csv file should be in the same directory, else insert filepath
output_df = pd.read_csv("output_final.csv", sep=';', error_bad_lines=False)
index_list = output_df.index.tolist()
#output["Essay"] += 1 essay numbers must correspond
#print(index_list)
essay_list = output_df["Essay"].tolist()
path_list = output_df["Path"].tolist()

#gold_final.csv should be in same directory, else insert filepath
gold_df = pd.read_csv("gold_final.csv", sep=';', error_bad_lines=False)
#gold_df["Essay] += 1
maslow_gold = gold_df["Maslow"].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
    
    This allows the proper format for the paths 
    of the subgraphs in order to assign human needs

    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
    Since paths are ordered according to their rankings, first found
    reiss motive is assigned due to conjecture that it must be the most fitting
    human need

    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 motive

    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

#Assign maslow category 
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]

# post-processing of gold data
maslow_gold = [w.replace("love / belonging", "love/belonging") for w in maslow_gold]

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

"""
The necessary data (all of type list) for evaluation are:
    maslow_needs: contains maslow human needs assigned by system (i.e. system output)
    reiss_needs: contains reiss motives assigned by system
    maslow_gold: contains gold data for maslow human needs
    reiss_gold: contains gold data for reiss motives
These variables need to be imported in order to run evaluation
"""