<commit_before>﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;

namespace UURAGE
{
    class Program
    {        
        static readonly string scenarioReasonerFileName = "ScenarioReasoner.cgi";
        static readonly string scenarioParserFileName = "ScenarioParser.cgi";
        static readonly string binDirectory = "bins";

        // Relative to the .exe in bin
        static readonly string cgiDirectory = ConfigurationManager.AppSettings["cgi_directory"];
        // Relative to the cgi directory
        static readonly string xmlDirectory = ConfigurationManager.AppSettings["xml_directory"];

        static JToken PerformReasonerRequest(string method, JArray parameters)
        {
            JObject param = new JObject(
                new JProperty("method", method),
                new JProperty("params", parameters)
            );
            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = Path.Combine(cgiDirectory, scenarioReasonerFileName),
                WorkingDirectory = cgiDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false
            };
            string input = Uri.EscapeDataString(param.ToString());
            startInfo.EnvironmentVariables.Add("REQUEST_METHOD", "GET");
            startInfo.EnvironmentVariables.Add("QUERY_STRING", "input=" + input);
            using (Process srProcess = Process.Start(startInfo))
            {
                srProcess.WaitForExit();
                string output = new StreamReader(srProcess.StandardOutput.BaseStream).ReadToEnd();
                JObject response = JObject.Parse(output.Split(new string[] { "\r\n\r\n" }, 2, StringSplitOptions.None)[1]);
                if (response["error"].Type != JTokenType.Null)
                {
                    throw new InvalidOperationException(((JValue)response["error"]).Value.ToString());
                }
                return response["result"];
            }
        }

        static string PerformParserRequest(string scenarioName)
        {
            // This directory is relative to the cgi
            string scenarioBinPath = Path.Combine(binDirectory, scenarioName + ".bin");
            string scenarioXMLPath = Path.Combine(xmlDirectory, scenarioName + ".xml");

            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = Path.Combine(cgiDirectory, scenarioParserFileName),
                WorkingDirectory = cgiDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false                
            };

            startInfo.EnvironmentVariables.Add("REQUEST_METHOD", "GET");

            string query = "script_path=" + Uri.EscapeDataString(scenarioXMLPath) + "&" + "bin_path=" + Uri.EscapeDataString(scenarioBinPath);
            startInfo.EnvironmentVariables.Add("QUERY_STRING", query);

            using (Process spProcess = Process.Start(startInfo))
            {
                spProcess.WaitForExit();
                string output = new StreamReader(spProcess.StandardOutput.BaseStream).ReadToEnd();

                return output;
            }
        }

        static JArray DoStep(string scenarioID, JArray nextSteps)
        {
            string firstNextDetails = ((JValue)((JArray)((JArray)nextSteps[0])[3])[2]).Value.ToString();
            string firstNextType = ((JValue)(((JObject)JObject.Parse(firstNextDetails)["statement"])["type"])).Value.ToString();
            if (firstNextType == "player")
            {
                Console.WriteLine("Choose the step that you want to take by giving the appropriate number");

                int optionCounter = 1;
                foreach (JToken nextStep in nextSteps)
                {
                    JArray nextState = (JArray)((JArray)nextStep)[3];<commit_msg>Simplified and made explicit Parser/Reasoner communication encoding<commit_after>﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;

namespace UURAGE
{
    class Program
    {        
        static readonly string scenarioReasonerFileName = "ScenarioReasoner.cgi";
        static readonly string scenarioParserFileName = "ScenarioParser.cgi";
        static readonly string binDirectory = "bins";

        // Relative to the .exe in bin
        static readonly string cgiDirectory = ConfigurationManager.AppSettings["cgi_directory"];
        // Relative to the cgi directory
        static readonly string xmlDirectory = ConfigurationManager.AppSettings["xml_directory"];

        static JToken PerformReasonerRequest(string method, JArray parameters)
        {
            JObject param = new JObject(
                new JProperty("method", method),
                new JProperty("params", parameters)
            );
            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = Path.Combine(cgiDirectory, scenarioReasonerFileName),
                WorkingDirectory = cgiDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                StandardOutputEncoding = Encoding.UTF8,
                StandardErrorEncoding = Encoding.UTF8,
                UseShellExecute = false
            };
            string input = Uri.EscapeDataString(param.ToString());
            startInfo.EnvironmentVariables.Add("REQUEST_METHOD", "GET");
            startInfo.EnvironmentVariables.Add("QUERY_STRING", "input=" + input);
            using (Process srProcess = Process.Start(startInfo))
            {
                srProcess.WaitForExit();
                string output = srProcess.StandardOutput.ReadToEnd();
                JObject response = JObject.Parse(output.Split(new string[] { "\r\n\r\n" }, 2, StringSplitOptions.None)[1]);
                if (response["error"].Type != JTokenType.Null)
                {
                    throw new InvalidOperationException(((JValue)response["error"]).Value.ToString());
                }
                return response["result"];
            }
        }

        static string PerformParserRequest(string scenarioName)
        {
            // This directory is relative to the cgi
            string scenarioBinPath = Path.Combine(binDirectory, scenarioName + ".bin");
            string scenarioXMLPath = Path.Combine(xmlDirectory, scenarioName + ".xml");

            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = Path.Combine(cgiDirectory, scenarioParserFileName),
                WorkingDirectory = cgiDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                StandardOutputEncoding = Encoding.UTF8,
                StandardErrorEncoding = Encoding.UTF8,
                UseShellExecute = false                
            };

            startInfo.EnvironmentVariables.Add("REQUEST_METHOD", "GET");

            string query = "script_path=" + Uri.EscapeDataString(scenarioXMLPath) + "&" + "bin_path=" + Uri.EscapeDataString(scenarioBinPath);
            startInfo.EnvironmentVariables.Add("QUERY_STRING", query);

            using (Process spProcess = Process.Start(startInfo))
            {
                spProcess.WaitForExit();
                string output = spProcess.StandardOutput.ReadToEnd();

                return output;
            }
        }

        static JArray DoStep(string scenarioID, JArray nextSteps)
        {
            string firstNextDetails = ((JValue)((JArray)((JArray)nextSteps[0])[3])[2]).Value.ToString();
            string firstNextType = ((JValue)(((JObject)JObject.Parse(firstNextDetails)["statement"])["type"])).Value.ToString();
            if (firstNextType == "player")
            {
                Console.WriteLine("Choose the step that you want to take by giving the appropriate number");

                int optionCounter = 1;
                foreach (JToken nextStep in nextSteps)
                {
                    JArray nextState = (JArray)((JArray)nextStep)[3];
{'fraction_non_alphanumeric': 0.09378001921229587, 'fraction_numerical': 0.002161383285302594, 'mean_word_length': 2.1406485671191553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '907051', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1916, 'n_words': 437}