Skip to content

Instantly share code, notes, and snippets.

@simoninithomas
Created September 16, 2021 08:55
Show Gist options
  • Save simoninithomas/929d7a5e797bb8a0109742cb3b8ee939 to your computer and use it in GitHub Desktop.
Save simoninithomas/929d7a5e797bb8a0109742cb3b8ee939 to your computer and use it in GitHub Desktop.
HuggingFaceAPI Part 3
/// <summary>
/// We receive a string like "[0.7777, 0.19, 0.01]", we need to process this data to transform it to an array of floats
/// [0.77, 0.19, 0.01] to be able to perform operations on it.
/// </summary>
/// <param name="result">json return from API call</param>
private IEnumerator ProcessResult(string result)
{
// The data is a score for each possible sentence candidate
// But, it looks something like this "[0.7777, 0.19, 0.01]"
// First, we need to remove [ and ]
string cleanedResult = result.Replace("[", "");
cleanedResult = cleanedResult.Replace("]", "");
// Then, we need to split each element of the array and convert to float
string[] splitArray = cleanedResult.Split(char.Parse(","));
float[] myFloat = splitArray.Select(float.Parse).ToArray();
// Now that we have a array of floats, we can find the max score and the index of it.
// We don't need to return these 2 variables, we'll access them directly since they are public.
maxScore = myFloat.Max();
maxScoreIndex = myFloat.ToList().IndexOf(maxScore);
yield return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment