Skip to content

Instantly share code, notes, and snippets.

@ayende
Created November 28, 2020 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayende/acda40bf82a357de21aadbbad8bb0418 to your computer and use it in GitHub Desktop.
Save ayende/acda40bf82a357de21aadbbad8bb0418 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Raven.Server.Documents.Indexes.Static;
public class SentimentData
{
[LoadColumn(0)]
public string SentimentText;
[LoadColumn(1), ColumnName("Label")]
public string Sentiment;
}
public class SentimentPrediction
{
public float[] Score;
[ColumnName("PredictedLabel")]
public string Prediction { get; set; }
}
public static class SentimentAnalysis
{
private const string ModelZipPath = @"/path/to/Model.zip";
[ThreadStatic]
private static PredictionEngine<SentimentData, SentimentPrediction> _engine;
[ThreadStatic] private static string[] _names;
private static void Init()
{
if (_names != null) return;
MLContext mlContext = new MLContext();
var model = mlContext.Model.Load(ModelZipPath, out var _);
var predEngine = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
VBuffer<ReadOnlyMemory<char>> slotNames = default;
predEngine.OutputSchema["Score"].GetSlotNames(ref slotNames);
_names = slotNames.DenseValues().Select(x => x.ToString()).ToArray();
_engine = predEngine;
}
public static IEnumerable Analyze(string text)
{
Init();
var sentiment = _engine.Predict(new SentimentData { SentimentText = text });
dynamic result = new ExpandoObject();
result.Sentiment = sentiment.Prediction;
result.Tags = new DynamicArray(new List<object>(
sentiment.Score.Select((f, i) => new {Name = _names[i], Score = f})
));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment