Commit c6613fba authored by Studer Brendan's avatar Studer Brendan
Browse files

added scores to main program

parent cb8cec35
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -38,5 +38,6 @@ It's possible to add arguments
- -i or --ifile" to specified the input json file
- -o or --ofile to specify the name of the output folder
- -t or --tree to only print the tree of scenarios
- -s or --scores to compute the min and max scores for all scenes

If input file and/or output folder are not specified, the cript will take them from you .env file
 No newline at end of file
+10 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import shutil
import sys

from config.app_config import AppConfig
from features.min_max_stats_finder import min_max_scores

logging.basicConfig(
    format='[%(name)s::%(levelname)s]\t\t%(message)s', level=logging.DEBUG)
@@ -41,7 +42,7 @@ def main(argv):
    tree = False

    try:
        opts, _ = getopt.getopt(argv, "hti:o:", ["ifile=", "ofile=", "tree"])
        opts, _ = getopt.getopt(argv, "hsti:o:", ["ifile=", "ofile=", "tree","scores"])
    except getopt.GetoptError:
        print('EODLJsonParser.py -i <inputfile> -o <outputFolder>')
        sys.exit(2)
@@ -55,6 +56,8 @@ def main(argv):
            output_folder = arg
        elif opt in ("-t", "--tree"):
            tree = True
        elif opt in ("-s", "--scores"):
            scores = True

    with open(inputfile, 'r', encoding="utf-8") as j:
        contents = json.loads(j.read())
@@ -64,6 +67,12 @@ def main(argv):
            show_chapters(contents["Chapters"])
            sys.exit(0)
            
        #if --scores argument exists
        if scores:
            scores = min_max_scores(contents)
            _logger.info(scores)
            sys.exit(0)

        # if output folder already exist, we delete it
        if os.path.exists(output_folder):
            shutil.rmtree(output_folder)
+51 −46
Original line number Diff line number Diff line
import json
import logging
import networkx as nx
import matplotlib.pyplot as plt

def find_min_max_scores(content):
    scenes_array = []
logging.basicConfig(
    format='[%(name)s::%(levelname)s]\t\t%(message)s', level=logging.DEBUG)

_logger = logging.getLogger(__name__)

def min_max_scores(content):
    _logger.info("Computing scores for all scenes. It may take a bit of time...")
    scores = dict()
    #Ierate over chapters
    for idx_chapter, chapter in enumerate(content["Chapters"]):
        #Iterate over scenes
        for idx_scene, scene in enumerate(chapter["Scenes"]):
            scene_name = scene["Title"]
            min_score, max_score = find_scene_scores(scene)
            scores[scene_name] = {
                "max": max_score,
                "min": min_score
            }
    return scores
            
            scenes_array.append({
                "Authenticity": 0,
                  "Respect": 0,
                  "Compassion": 0,
                  "Hope": 0,
                  "Empathy": 0
            })
def find_scene_scores(scene):
    graph, end_nodes = create_graph(scene)
    
            #Iterate over interactions
            for interaction in scene["Interactions"]:
    scene_min_score = 0
    scene_max_score = 0
    
                    #Iterate over choices
                    for choice in interaction['Responses']:
                        pass
    #scene_min_path = []
    #scene_max_path = []
    
    # Iterate over each end node
    for end_nodes in end_nodes:
        paths =nx.all_simple_paths(graph,source=1,target=end_nodes)
        
        #Iterate over all paths
        for path in paths:
            path_weight = nx.path_weight(graph,path,weight='weight')
            
            # if the score of the current path is lower than the scene_min_score, we replace scene_min_score by the weight of the current path
            if path_weight < scene_min_score:
                scene_min_score = path_weight
                #scene_min_path = path
                
            # if the score of the current path is bigger than the scene_max_score, we replace scene_max_score by the weight of the current path
            if path_weight > scene_max_score:
                scene_max_score = path_weight
                #scene_max_path = path
                
    return scene_min_score, scene_max_score

def create_graph(scene):
    graph = nx.DiGraph()
@@ -55,40 +82,18 @@ def show_graph(graph):
    plt.show()

if __name__ == "__main__":
    with open("Chapters.json", 'r', encoding="utf-8") as j:
        contents = json.loads(j.read())
        
        graph, end_nodes = create_graph(contents["Chapters"][0]["Scenes"][0])
        #show_graph() 
        
        scene_min_score = 0
        scene_max_score = 0
        
        scene_min_path = []
        scene_max_path = []
        
        # Iterate over each end node
        for end_nodes in end_nodes:
            paths =nx.all_simple_paths(graph,source=1,target=end_nodes)
    
            #Iterate over all paths
            for path in paths:
                path_weight = nx.path_weight(graph,path,weight='weight')
                # if the score of the current path is lower than the scene_min_score, we replace scene_min_score by the weight of the current path
                if path_weight < scene_min_score:
                    scene_min_score = path_weight
                    scene_min_path = path
    _logger.info("Testing find_scene_scores for first scene...")
    
                # if the score of the current path is bigger than the scene_max_score, we replace scene_max_score by the weight of the current path
                if path_weight > scene_max_score:
                    scene_max_score = path_weight
                    scene_max_path = path
    with open("Chapters.json", 'r', encoding="utf-8") as j:
        contents = json.loads(j.read())
        
        print(f"Min: {scene_min_score}")
        print(f"Min path: {scene_min_path}")
        scene = contents["Chapters"][0]["Scenes"][0]
        min_score, max_score = find_scene_scores(scene)
        
        print(f"Max: {scene_max_score}")
        print(f"Max path: {scene_max_path}")
        print("Scores for "+scene["Title"])          
        print(f"Min: {min_score}")  
        print(f"Max: {max_score}")