-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(SentimentAnalyzer): Improve performance #3
Merged
arafattehsin
merged 2 commits into
arafattehsin:master
from
kamranayub:feat/perf-sentiment-analysis
Apr 6, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace SentimentAnalyzer.Tests | ||
{ | ||
public class Benchmarks | ||
{ | ||
private readonly ITestOutputHelper output; | ||
|
||
public Benchmarks(ITestOutputHelper output) | ||
{ | ||
this.output = output; | ||
} | ||
|
||
/// <summary> | ||
/// Use IMDB dataset of 50k reviews to benchmark analysis. | ||
/// See: https://github.com/nas5w/imdb-data | ||
/// </summary> | ||
/// <returns></returns> | ||
[Fact] | ||
public async Task Loop_Of_50000_IMDB_Reviews() | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var stream = assembly.GetManifestResourceStream("SentimentAnalyzer.Tests.reviews.json"); | ||
|
||
Assert.NotNull(stream); | ||
|
||
var reviews = await JsonSerializer.DeserializeAsync<Review[]>(stream); | ||
Assert.Equal(50000, reviews.Length); | ||
|
||
var stopWatch = new Stopwatch(); | ||
stopWatch.Start(); | ||
|
||
for(var i = 0; i < reviews.Length; i++) | ||
{ | ||
Sentiments.Predict(reviews[i].t); | ||
} | ||
|
||
stopWatch.Stop(); | ||
|
||
Assert.True(stopWatch.ElapsedMilliseconds > 0); | ||
output.WriteLine($"Loop duration: {stopWatch.ElapsedMilliseconds}ms"); | ||
} | ||
} | ||
|
||
public class Review | ||
{ | ||
public string t { get; set; } | ||
public int s { get; set; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Cognitive-Library/SentimentAnalyzer.Tests/SentimentAnalyzer.Tests.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="reviews.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="reviews.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="System.Text.Json" Version="5.0.1" /> | ||
<PackageReference Include="xunit" Version="2.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SentimentAnalyzer\SentimentAnalyzer.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
# SentimentAnalyzer - On device (offline) Sentiment Analysis for .NET Standard apps | ||
# SentimentAnalyzer - On-device (offline) Sentiment Analysis for .NET Standard apps | ||
|
||
This is the library which can be used to consume the model which I created using ML.NET. For further inforamtion, you can always have a look at my [blog](https://www.arafattehsin.com/blog/sentimentanalyzer-ondevice-machine-learning/) | ||
This is a library which can be used to consume a model which I created using ML.NET. | ||
|
||
For further information, you can always have a look at my [blog](https://www.arafattehsin.com/blog/sentimentanalyzer-ondevice-machine-learning/) | ||
|
||
## Quick Start | ||
|
||
```c# | ||
using SentimentAnalyzer; | ||
|
||
var sentiment = Sentiments.Predict("some string"); | ||
``` | ||
|
||
`Predict` returns a `SentimentPrediction` which contains: | ||
|
||
- `Prediction` (`bool`) - `true` is Positive sentiment, `false` is Negative sentiment. | ||
- `Score` (`float`) - A score representing the model's accuracy |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be
[ThreadStatic]
and initialized once per threadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, addressed in #5