modify this unity c# script so the ai is smarter and also it functions like a real ai and also make the ai really understand what the player is saying. using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;

public class AdvancedAIScript : MonoBehaviour
{
    public TMP_InputField userInputField;
    public TMP_Text aiResponseText;
    public Button submitButton;

    private List<WordData> vocabulary = new List<WordData>
    {
        new WordData("hello", new WordMeaning("greeting", "neutral", new string[] { "hi", "hey" })),
        new WordData("happy", new WordMeaning("emotion", "positive", new string[] { "joyful", "content", "pleased" })),
        new WordData("sad", new WordMeaning("emotion", "negative", new string[] { "unhappy", "downcast", "gloomy" })),
        new WordData("run", new WordMeaning("action", "movement", new string[] { "sprint", "jog", "dash" })),
        new WordData("talk", new WordMeaning("action", "communication", new string[] { "speak", "chat", "discuss" })),
        new WordData("idea", new WordMeaning("concept", "neutral", new string[] { "thought", "notion", "plan" })),
        new WordData("fun", new WordMeaning("emotion", "positive", new string[] { "enjoyable", "exciting", "pleasure" })),
        new WordData("tired", new WordMeaning("emotion", "negative", new string[] { "exhausted", "weary", "fatigued" })),
        // Add more words to build a rich vocabulary
    };

    private Queue<string> memory = new Queue<string>(); // Memory for context
    private const int MemoryLimit = 10; // Store up to 10 previous exchanges

    private void Start()
    {
        submitButton.onClick.AddListener(OnSubmit);
    }

    private void OnSubmit()
    {
        string userInput = userInputField.text.ToLower();
        AddToMemory(userInput);

        var detectedWords = DetectWords(userInput);
        string response = GenerateResponse(detectedWords);

        aiResponseText.text = response;
        AddToMemory(response);
        userInputField.text = string.Empty;
    }

    private void AddToMemory(string text)
    {
        if (memory.Count >= MemoryLimit)
            memory.Dequeue(); // Remove oldest memory

        memory.Enqueue(text);
    }

    private List<WordData> DetectWords(string input)
    {
        return vocabulary.Where(word => input.Contains(word.Word)).ToList();
    }

    private string GenerateResponse(List<WordData> detectedWords)
    {
        if (detectedWords.Count == 0)
        {
            return GenerateFallbackResponse();
        }

        List<string> responseWords = new List<string>();

        // Prioritize detected words and expand with related terms
        foreach (var word in detectedWords)
        {
            responseWords.Add(word.Word);
            string synonym = word.Meaning.Synonyms[Random.Range(0, word.Meaning.Synonyms.Length)];
            responseWords.Add(synonym);
        }

        // Fill in with additional words for longer sentences
        while (responseWords.Count < Random.Range(8, 20))
        {
            var randomWord = vocabulary[Random.Range(0, vocabulary.Count)];
            responseWords.Add(randomWord.Word);
        }

        // Structure the response dynamically
        return ConstructDynamicSentence(responseWords);
    }

    private string GenerateFallbackResponse()
    {
        string randomWord = vocabulary[Random.Range(0, vocabulary.Count)].Word;
        return $"I'm not sure what you mean, but let's talk about \"{randomWord}\" instead.";
    }

    private string ConstructDynamicSentence(List<string> words)
    {
        // Shuffle words to create natural variation
        var shuffledWords = words.OrderBy(x => Random.value).ToList();

        // Capitalize the first word and construct the sentence
        return char.ToUpper(shuffledWords[0][0]) + shuffledWords[0].Substring(1) +
               " " + string.Join(" ", shuffledWords.Skip(1)) + ".";
    }
}

[System.Serializable]
public class WordData
{
    public string Word { get; private set; }
    public WordMeaning Meaning { get; private set; }

    public WordData(string word, WordMeaning meaning)
    {
        Word = word;
        Meaning = meaning;
    }
}

[System.Serializable]
public class WordMeaning
{
    public string Type { get; private set; }
    public string Emotion { get; private set; }
    public string[] Synonyms { get; private set; }

    public WordMeaning(string type, string emotion, string[] synonyms)
    {
        Type = type;
        Emotion = emotion;
        Synonyms = synonyms;
    }
}