Skip to content

Commit e6360a1

Browse files
author
Luz
committed
Add project files for the first time
1 parent f28e5d1 commit e6360a1

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageId>AppLogger</PackageId>
5+
<Version>1.0.0</Version>
6+
<Authors>Felipe Luz</Authors>
7+
<TargetFramework>netcoreapp3.1</TargetFramework>
8+
<LangVersion>10.0</LangVersion>
9+
<AssemblyName>DotnetBadWordDetector</AssemblyName>
10+
<RootNamespace>DotnetBadWordDetector</RootNamespace>
11+
<ImplicitUsings>enable</ImplicitUsings>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.ML" Version="1.7.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<EmbeddedResource Include="Data/bad-words-model-english.zip" />
20+
</ItemGroup>
21+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.ML.Data;
2+
namespace DotnetBadWordDetector.Model;
3+
public class BadWord
4+
{
5+
public string Word { get; set; }
6+
}
7+
public class BadWordPrediction : BadWord
8+
{
9+
[ColumnName("PredictedLabel")]
10+
public bool Prediction { get; set; }
11+
12+
public float Probability { get; set; }
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.ML;
2+
using DotnetBadWordDetector.Model;
3+
4+
namespace DotnetBadWordDetector;
5+
6+
public class ProfanityDetector
7+
{
8+
9+
private const string MODELPATH = "DotnetBadWordDetector.Data.bad-words-model-english.zip";
10+
private PredictionEngine<BadWord, BadWordPrediction> _predictionEngine;
11+
12+
public ProfanityDetector()
13+
{
14+
LoadTrainedModel();
15+
}
16+
17+
private void LoadTrainedModel()
18+
{
19+
DataViewSchema modelSchema;
20+
var mlContext = new MLContext();
21+
var trainedModel = mlContext.Model.Load(GetModelStream(), out modelSchema);
22+
_predictionEngine = mlContext.Model.CreatePredictionEngine<BadWord, BadWordPrediction>(trainedModel);
23+
}
24+
25+
private Stream? GetModelStream()
26+
{
27+
var assembly = typeof(DotnetBadWordDetector.ProfanityDetector).Assembly;
28+
return assembly.GetManifestResourceStream(MODELPATH);
29+
}
30+
31+
/// <summary>
32+
/// Predicts if the phrase is profane
33+
/// </summary>
34+
/// <param name="word"></param>
35+
/// <returns>true if classified as profane</returns>
36+
public bool IsProfane(string word)
37+
{
38+
var obj = new BadWord { Word = word };
39+
return _predictionEngine.Predict(obj).Prediction;
40+
}
41+
42+
/// <summary>
43+
///
44+
/// </summary>
45+
/// <param name="word"></param>
46+
/// <returns> 0 < prediction < 1</returns>
47+
public float GetProbability(string word)
48+
{
49+
var obj = new BadWord { Word = word };
50+
return _predictionEngine.Predict(obj).Probability;
51+
}
52+
}

0 commit comments

Comments
 (0)