Loading human_needs_assigner.ipynb 0 → 100644 +215 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python #!/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 and goldata, 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) """ import pandas as pd #GS_annotations 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 #print(index_list) essay_list = output_df["Essay"].tolist() path_list = output_df["Path"].tolist() gold_df = pd.read_csv("gold_final.csv", sep=';', error_bad_lines=False) 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 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] # 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 ``` %% Cell type:code id: tags: ``` python gold_df ``` %% Output Essay Maslow Reiss 0 50 spiritual growth curiosity 1 51 spiritual growth curiosity 2 52 love/belonging social contact 3 53 stability health 4 54 spiritual growth curiosity .. ... ... ... 99 371 physiological needs food 100 372 esteem power 101 373 stability order 102 374 stability safety 103 375 love/belonging social contact [104 rows x 3 columns] %% Cell type:code id: tags: ``` python output_df ``` %% Output Essay Path \ 0 50 ['sports PartOf competition', 'competition Rel... 1 51 ['cultural RelatedTo appaduraian RelatedTo soc... 2 52 ['behavior RelatedTo herding_instinct RelatedT... 3 53 ['manner RelatedTo social', 'social RelatedTo ... 4 54 ['learning Causes meeting_interesting_people C... .. ... ... 99 371 ['control RelatedTo power', 'power RelatedTo i... 100 372 ['function RelatedTo order', 'order RelatedTo ... 101 373 ['extinct RelatedTo titanothere RelatedTo fami... 102 374 ['preventative RelatedTo prophylactical Synony... 103 375 ['restriction RelatedTo absolute RelatedTo ind... Maslow_predict Reiss_predict 0 esteem competition 1 spiritual growth independence 2 love/belonging family 3 spiritual growth curiosity 4 spiritual growth curiosity .. ... ... 99 esteem power 100 stability order 101 love/belonging family 102 stability health 103 spiritual growth independence [104 rows x 4 columns] %% Cell type:code id: tags: ``` python ``` Loading
human_needs_assigner.ipynb 0 → 100644 +215 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python #!/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 and goldata, 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) """ import pandas as pd #GS_annotations 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 #print(index_list) essay_list = output_df["Essay"].tolist() path_list = output_df["Path"].tolist() gold_df = pd.read_csv("gold_final.csv", sep=';', error_bad_lines=False) 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 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] # 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 ``` %% Cell type:code id: tags: ``` python gold_df ``` %% Output Essay Maslow Reiss 0 50 spiritual growth curiosity 1 51 spiritual growth curiosity 2 52 love/belonging social contact 3 53 stability health 4 54 spiritual growth curiosity .. ... ... ... 99 371 physiological needs food 100 372 esteem power 101 373 stability order 102 374 stability safety 103 375 love/belonging social contact [104 rows x 3 columns] %% Cell type:code id: tags: ``` python output_df ``` %% Output Essay Path \ 0 50 ['sports PartOf competition', 'competition Rel... 1 51 ['cultural RelatedTo appaduraian RelatedTo soc... 2 52 ['behavior RelatedTo herding_instinct RelatedT... 3 53 ['manner RelatedTo social', 'social RelatedTo ... 4 54 ['learning Causes meeting_interesting_people C... .. ... ... 99 371 ['control RelatedTo power', 'power RelatedTo i... 100 372 ['function RelatedTo order', 'order RelatedTo ... 101 373 ['extinct RelatedTo titanothere RelatedTo fami... 102 374 ['preventative RelatedTo prophylactical Synony... 103 375 ['restriction RelatedTo absolute RelatedTo ind... Maslow_predict Reiss_predict 0 esteem competition 1 spiritual growth independence 2 love/belonging family 3 spiritual growth curiosity 4 spiritual growth curiosity .. ... ... 99 esteem power 100 stability order 101 love/belonging family 102 stability health 103 spiritual growth independence [104 rows x 4 columns] %% Cell type:code id: tags: ``` python ```