Skip to content
Permalink
Browse files

v3

  • Loading branch information...
DXSdata committed Nov 28, 2017
1 parent 9829cae commit 8f21d736b55a677e4cbb439fca0a764477209600
Showing with 89 additions and 51 deletions.
  1. +34 −29 README.md
  2. +9 −5 nBayes.Sample/Program.cs
  3. +7 −2 nBayes.Sample/nBayes.Sample.csproj
  4. +35 −13 nBayes/Analyzer.cs
  5. +4 −2 nBayes/nBayes.csproj
@@ -5,38 +5,43 @@ A Nuget package is also available.


# Sample
The same sample is used by the original project.
With v3, I did a few analyzer bugfixes and changed methods and properties for simpler, consistent usage, different to the original project.



```CSharp
Index spam = Index.CreateMemoryIndex();
Index notspam = Index.CreateMemoryIndex();
// train the indexes
spam.Add(Entry.FromString("want some viagra?"));
spam.Add(Entry.FromString("cialis can make you larger"));
notspam.Add(Entry.FromString("Hello, how are you?"));
notspam.Add(Entry.FromString("Did you go to the park today?"));
Analyzer analyzer = new Analyzer();
CategorizationResult result = analyzer.Categorize(
Entry.FromString("cialis viagra"),
spam,
notspam);
switch (result)
{
case CategorizationResult.First:
Console.WriteLine("Spam");
break;
case CategorizationResult.Undetermined:
Console.WriteLine("Undecided");
break;
case CategorizationResult.Second:
Console.WriteLine("Not Spam");
break;
}
Index spam = Index.CreateMemoryIndex();
Index notspam = Index.CreateMemoryIndex();
// train the indexes
spam.Add(Entry.FromString("want some viagra?"));
spam.Add(Entry.FromString("cialis can make you larger"));
notspam.Add(Entry.FromString("Hello, how are you?"));
notspam.Add(Entry.FromString("Did you go to the park today?"));
Analyzer analyzer = new Analyzer(
Entry.FromString("cialis viagra"),
spam,
notspam);
float prediction = analyzer.Prediction;
Console.WriteLine("Prediction (good < 0.5 > spam): " + prediction);
CategorizationResult result = analyzer.Category;
switch (result)
{
case CategorizationResult.First:
Console.WriteLine("Spam");
break;
case CategorizationResult.Undetermined:
Console.WriteLine("Undecided");
break;
case CategorizationResult.Second:
Console.WriteLine("Not Spam");
break;
}
```

# Links
- Website http://www.dxsdata.com
- Website http://www.dxsdata.com/de/2017/07/bayes-classifier-library-for-net-core/
@@ -15,11 +15,15 @@ static void Main(string[] args)
notspam.Add(Entry.FromString("Hello, how are you?"));
notspam.Add(Entry.FromString("Did you go to the park today?"));

Analyzer analyzer = new Analyzer();
CategorizationResult result = analyzer.Categorize(
Entry.FromString("cialis viagra"),
spam,
notspam);
Analyzer analyzer = new Analyzer(
Entry.FromString("cialis viagra"),
spam,
notspam);

float prediction = analyzer.Prediction;
Console.WriteLine("Prediction (good < 0.5 > spam): " + prediction);

CategorizationResult result = analyzer.Category;

switch (result)
{
@@ -2,8 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.0.0</Version>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>Portable;win10-x64</RuntimeIdentifiers>

<Version>3.0.0</Version>



</PropertyGroup>

<ItemGroup>
@@ -7,29 +7,51 @@ public class Analyzer
private float I = 0;
private float invI = 0;

public Analyzer()
{
this.Tolerance = .05f;
}
private float _prediction = float.NaN;

public float Tolerance { get; set; }
private Entry item;
private Index first;
private Index second;
private float tolerance;

public CategorizationResult Categorize(Entry item, Index first, Index second)
public Analyzer(Entry item, Index first, Index second, float tolerance = .05f)
{
float prediction = GetPrediction(item, first, second);
this.item = item;
this.first = first;
this.second = second;
this.tolerance = tolerance;
}

if (prediction <= .5f - this.Tolerance)
return CategorizationResult.Second;
public float Prediction
{
get
{
if (float.IsNaN(_prediction))
_prediction = GetPrediction();

if (prediction >= .5 + this.Tolerance)
return CategorizationResult.First;
return _prediction;
}
}

public CategorizationResult Category
{
get
{
if (Prediction <= .5f - tolerance)
return CategorizationResult.Second;

return CategorizationResult.Undetermined;
if (Prediction >= .5 + tolerance)
return CategorizationResult.First;

return CategorizationResult.Undetermined;
}
}

public float GetPrediction(Entry item, Index first, Index second)
private float GetPrediction()
{
I = 0;
invI = 0;

foreach (string token in item)
{
int firstCount = first.GetTokenCount(token);
@@ -12,12 +12,14 @@
<Authors>DXSdata</Authors>
<Description>Fork of joelmartinez/nBayes, ported for usage with .NET Core / .NET Standard</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageReleaseNotes>Slightly changed the methods and properties for easier, consistent usage.
Bugfix regarding GetPrediction() when called multiple times.
Changed framework for the sample project back from netstandard to netcoreapp, otherwise VS would not produce an .exe file.</PackageReleaseNotes>
<Copyright>DXSdata / MIT</Copyright>
<PackageTags>Bayes nBayes classifier .NET Core Standard DXSdata</PackageTags>
<PackageProjectUrl>http://www.dxsdata.com/2017/07/bayes-classifier-library-for-net-core/</PackageProjectUrl>
<Title>nBayes for .NET Core</Title>
<Version>2.0.0</Version>
<Version>3.0.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIconUrl></PackageIconUrl>
<RepositoryUrl>https://github.com/DXSdata/nBayes</RepositoryUrl>

0 comments on commit 8f21d73

Please sign in to comment.
You can’t perform that action at this time.