This package provides a C# wrapper for the library: https://github.com/lytics/multibayes - providing multiclass naive Bayesian document classification in Unity
A basic example looks like this
using UnityEngine;
using SkybirdGames.TextClassifier;
public class TextClassiferTest : MonoBehaviour
{
[SerializeField] private NaiveBayesTextClassifierData trainingData;
void Start()
{
var textClassifier = new NaiveBayesTextClassifier(trainingData.ToJson());
print(textClassifier.Predict(text: "Hello there my friend", threshold: 0.4));
// result = "greetings"
}
}To install the package in your Unity project:
- Open the package manager
- Press the "+" button
- Select "add package from git URL"
- Enter: https://github.com/claytonpeterson/unity-text-classifier.git
- Press "add"
The underlying library requires a json training string in the format
[
{
"Text": "Example statement",
"Classes": [ "class" ]
},
{
"Text": "Example statement",
"Classes": [ "class" ]
}...
]- Text contains a statement that provides an example of the class
"Text": "Hello my friend" - Classes contains the collection of classes that the statement belongs to
"Classes": [ "greetings", "friendship" ]
You can write your json training data manually, or to make things easier you can create a NaiveBayesTextClassifierData scriptable object in Unity
- To create the training object select "Assets/Create/SkybirdGames/Naive Bayes Text Classifier/New Dataset"
- Next, select the object in the inspector, open it's TrainingData array and press the "+" button to add a new class
- Finally, open the class's Texts array, and press the "+" button to add example statements to the class
The NaiveBayesTextClassifier class is initialized with a string containing the json training data
var textClassifier = new NaiveBayesTextClassifier(jsonTrainingData);The Predict method returns the predicted class
The Threshold is used to filter out results that have a lower probability than the threshold
var text = "Hide behind that rock!"
var threshold = 0.4;
var result = textClassifier.Predict(text, threshold);
// result = "cover"The folder "Text Classifier/Samples/TrainingData" includes:
- A raw json data file ("combat_training_data") that is trained to recognize "combat dialogue"
- A NaiveBayesTextClassifierData object that is trained to recognize greetings and farewells