-
-
Save ayende/acda40bf82a357de21aadbbad8bb0418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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