Skip to content

Commit

Permalink
created echo agent and quantize character functions
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 3, 2024
1 parent 72cc67b commit 627b35f
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Assets/CommandTerminal/CommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ private void ProcessLogLine(LogItem item, int maxChars, ref List<string> lines,
}
}

public string GetLastLog()
{
if (_Logs.Count == 0)
{
return string.Empty;
}
return _Logs[_Logs.Count - 1].Message;
}

public void Reset()
{
_Logs.Clear();
Expand Down
69 changes: 69 additions & 0 deletions Assets/Scripts/Agents/EchoAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using System.Collections.Generic;
using CommandTerminal;

namespace DialogosEngine
{
public class EchoAgent : Agent
{
char _GuessedChar;

public override void OnEpisodeBegin()
{
ClearConsole();
}

public void FixedUpdate()
{
char expectedChar = GetExpectedChar();
float reward = CalculateReward(expectedChar, _GuessedChar);
SetReward(reward);
}

public override void CollectObservations(VectorSensor sensor)
{
string buffer = GetConsoleBuffer();
float[] vectorizedBuffer = Lexer.VectorizeUTF8(buffer);
foreach (var obs in vectorizedBuffer)
{
sensor.AddObservation(obs);
}
}

public override void OnActionReceived(ActionBuffers actions)
{
float[] actionArray = new float[1] { actions.ContinuousActions[0] };
_GuessedChar = Lexer.QuantizeUTF8(actionArray)[0];
HandleGuessedCharacter(_GuessedChar);
}

private void ClearConsole()
{
Terminal.Instance.Buffer.Reset();
}

private float CalculateReward(char expectedChar, char guessedChar)
{
// Implementation to calculate the reward based on the guessed character
return 0;
}

private string GetConsoleBuffer()
{
return Terminal.Instance.Buffer.GetLastLog();
}

private void HandleGuessedCharacter(char guessedChar)
{
// Implementation to handle the guessed character
}

private char GetExpectedChar()
{
// Implementation to get the expected character for the current step
return new char();
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Agents/EchoAgent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion Assets/Scripts/Agents/Lexer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
Expand Down Expand Up @@ -82,6 +81,34 @@ public static float[] VectorizeUTF8(string line)
return vector;
}

public static string QuantizeUTF8(float[] vector)
{
if (vector == null)
{
throw new ArgumentNullException(nameof(vector), "Input vector cannot be null.");
}

byte[] utf8Bytes = new byte[vector.Length];
int index = 0;
while (index < vector.Length)
{
utf8Bytes[index] = (byte)(vector[index] / MultiplierUTF8);
index++;
}

Decoder utf8Decoder = Encoding.UTF8.GetDecoder();
int charCount = utf8Decoder.GetCharCount(utf8Bytes, 0, utf8Bytes.Length);
if (charCount > k_MaxChars)
{
throw new LexerException($"Output exceeds the maximum length of {k_MaxChars} characters.");
}

char[] chars = new char[charCount];
utf8Decoder.GetChars(utf8Bytes, 0, utf8Bytes.Length, chars, 0);

return new string(chars);
}

public static float CalculateWhitespace(string[] text)
{
int _totalWhitespace = text.Sum(line => line.Count(char.IsWhiteSpace));
Expand Down
105 changes: 101 additions & 4 deletions Tests/LexerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics;

namespace DialogosEngine.Tests
namespace DialogosEngine.Tests
{
[TestFixture]
public static class LexerTests
Expand Down Expand Up @@ -94,7 +92,6 @@ public static void Vectorize_GivenStringWithSpecialChars_ConvertsToAsciiFloatArr
TestContext.WriteLine($"Test passed: Input string '{input}' converts to expected packed float array.");
}


[Test]
public static void VectorizeUTF8_GivenString_ConvertsToUtf8FloatArray()
{
Expand Down Expand Up @@ -306,5 +303,105 @@ public static void VectorizeUTF8_ChineseHeading_ConvertsToUtf8FloatArray()
TestContext.WriteLine($"Test passed: Input string '{input}' converts to expected UTF-8 float array.");
}

[Test]
public static void QuantizeUTF8_GivenFloatArray_ConvertsToUtf8String()
{
// Arrange
string expected = "Test"; // The expected UTF-8 string output
float[] input = Lexer.VectorizeUTF8(expected); // Use VectorizeUTF8 to get the correct float array

TestContext.WriteLine($"Testing with input float array: '{Utility.FormatFloatArray(input)}'.");

// Act
string result = Lexer.QuantizeUTF8(input);
TestContext.WriteLine($"Resulting UTF-8 string: '{result}'");

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expected), "The resulting string should match the expected UTF-8 string.");
TestContext.WriteLine($"Test passed: Input float array converts to expected UTF-8 string '{expected}'.");
}

[Test]
public static void QuantizeUTF8_ComplexStringWithEmojis_ConvertsToUtf8String()
{
// Arrange
string expected = "The quick brown fox jumps over the lazy dog 🚀🦊🐶";
float[] input = Lexer.VectorizeUTF8(expected); // Use VectorizeUTF8 to get the correct float array

TestContext.WriteLine($"Testing with input float array: '{Utility.FormatFloatArray(input)}'.");

// Act
string result = Lexer.QuantizeUTF8(input);
TestContext.WriteLine($"Resulting UTF-8 string: '{result}'");

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expected), "The resulting string should match the expected complex UTF-8 string with emojis.");
TestContext.WriteLine($"Test passed: Input float array converts to expected UTF-8 string with emojis '{expected}'.");
}

[Test]
public static void QuantizeUTF8_JapaneseText_ConvertsToUtf8String()
{
// Arrange
string expected = "これは日本語の段落テストです。このテストは、UTF-8関数が日本語の文字を含むテキストを正しく処理できることを確認するためのものです。"; // A sample Japanese paragraph
// Ensure the string is not longer than 1000 characters
expected = expected.Substring(0, Math.Min(1000, expected.Length));
float[] input = Lexer.VectorizeUTF8(expected); // Use VectorizeUTF8 to get the correct float array

TestContext.WriteLine($"Testing with input float array: '{Utility.FormatFloatArray(input)}'.");

// Act
string result = Lexer.QuantizeUTF8(input);
TestContext.WriteLine($"Resulting UTF-8 string: '{result}'");

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expected), "The resulting string should match the expected Japanese UTF-8 string.");
TestContext.WriteLine($"Test passed: Input float array converts to expected UTF-8 Japanese text '{expected}'.");
}

[Test]
public static void QuantizeUTF8_ChineseText_ConvertsToUtf8String()
{
// Arrange
string expected = "这是一个中文段落测试。这个测试将验证UTF-8函数是否能够正确处理包含中文字符的文本。"; // A sample Chinese paragraph
// Ensure the string is not longer than 1000 characters
expected = expected.Substring(0, Math.Min(1000, expected.Length));
float[] input = Lexer.VectorizeUTF8(expected); // Use VectorizeUTF8 to get the correct float array

TestContext.WriteLine($"Testing with input float array: '{Utility.FormatFloatArray(input)}'.");

// Act
string result = Lexer.QuantizeUTF8(input);
TestContext.WriteLine($"Resulting UTF-8 string: '{result}'");

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expected), "The resulting string should match the expected Chinese UTF-8 string.");
TestContext.WriteLine($"Test passed: Input float array converts to expected UTF-8 Chinese text '{expected}'.");
}

[Test]
public static void QuantizeUTF8_EmojiString_ConvertsToUtf8String()
{
// Arrange
string expected = "😀😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞😔😟😕🙁☹️😣😖😫😩🥺😢😭😤😠😡🤬😱😨😰😥😓🤗🤔🤭🤫🤥😶😐😑😬🙄😯😦😧😮😲🥱😴🤤😪😵🤐🥴🤢🤮🤧😷🤒🤕😈👿👹👺💀☠️👻👽👾🤖💩😺😸😹😻😼😽🙀😿😾";
// Ensure the string is not longer than 1000 characters
expected = expected.Substring(0, Math.Min(1000, expected.Length));
float[] input = Lexer.VectorizeUTF8(expected); // Use VectorizeUTF8 to get the correct float array

TestContext.WriteLine($"Testing with input float array: '{Utility.FormatFloatArray(input)}'.");

// Act
string result = Lexer.QuantizeUTF8(input);
TestContext.WriteLine($"Resulting UTF-8 string: '{result}'");

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expected), "The resulting string should match the expected emoji-only UTF-8 string.");
TestContext.WriteLine($"Test passed: Input float array converts to expected UTF-8 emoji-only string '{expected}'.");
}
}
}

0 comments on commit 627b35f

Please sign in to comment.