Sentence Similarity

SkillfulAPI.SentenceSimilarity

Overview

The SentenceSimilarity function is a simple example of how to interact with the SkillfulAI API to compare an input sentence against multiple context sentences. This functionality can be used to enhance dialogue systems, improve search relevance, and more.

Function Example

using SkillfulAI.API;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SentenceSimilarity : MonoBehaviour
{
    /// <summary>
    /// Unity's Start method, called when the script is initialized. Initiates the sentence similarity check.
    /// </summary>
    private void Start()
    {
        CheckSimilarity();
    }

    /// <summary>
    /// Initiates sentence similarity check based on a specified input sentence and context sentences.
    /// </summary>
    public void CheckSimilarity()
    {
        // Context sentences used for comparison
        string[] context = new string[] {
            "Ham and cheese burger",
            "Tomato Salad Burger",
            "Steak and Cheese Burger"
        };

        // Input sentence to compare against the context
        string inputSentence = "I really want a yummy steak burger please!";

        // Perform sentence similarity check
        SkillfulAPI.SentenceSimilarity(inputSentence, context,
        (scores, maxScoreIndex, contexts) =>
        {
            // Get the best match based on score
            string bestMatch = contexts[maxScoreIndex];

            // Debug the best match
            Debug.Log(bestMatch);
        },
        error =>
        {
            Debug.LogError($"Error: {error}");
        });
    }
}

Detailed Explanation

Start Method

Description: Unity's Start method is called once when the script is initialized. This method initiates the sentence similarity check.

Code:

private void Start()
{
    CheckSimilarity();
}

Functionality:

  • Calls the CheckSimilarity method to begin the process.

CheckSimilarity Method

Description: The CheckSimilarity method handles the sentence similarity logic, using the SkillfulAI API to compare an input sentence against a set of context sentences.

Code:

public void CheckSimilarity()
{
    // Context sentences used for comparison
    string[] context = new string[] {
        "Ham and cheese burger",
        "Tomato Salad Burger",
        "Steak and Cheese Burger"
    };

    // Input sentence to compare against the context
    string inputSentence = "I really want a yummy steak burger please!";

    // Perform sentence similarity check
    SkillfulAPI.SentenceSimilarity(inputSentence, context,
    (scores, maxScoreIndex, contexts) =>
    {
        // Get the best match based on score
        string bestMatch = contexts[maxScoreIndex];

        // Debug the best match
        Debug.Log(bestMatch);
    },
    error =>
    {
        Debug.LogError($"Error: {error}");
    });
}

Functionality:

  • Defines a context array containing sentences for comparison.

  • Defines an inputSentence string that will be compared against the context sentences.

  • Calls SkillfulAPI.SentenceSimilarity, passing the inputSentence, context, and callback functions to handle the response and errors.

  • The success callback processes the similarity scores, identifies the best match, and logs it to the Unity console.

  • The error callback logs any errors to the Unity console.

Usage Example

  1. Add the Script to a GameObject:

    • Attach the SentenceSimilarity script to any GameObject in your Unity scene.

  2. Run the Scene:

    • When you start the scene, the Start method is called, which in turn calls the CheckSimilarity method.

    • The CheckSimilarity method sends the input sentence and context sentences to the SkillfulAI API and logs the best match to the console.

Conclusion

The SentenceSimilarity function provides a straightforward example of how to leverage the SkillfulAI Gaming SDK to integrate AI-powered sentence similarity checks into your Unity project. This can be expanded and customized to suit more complex scenarios and interactions in your game.

Last updated