Skip to content
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

Made _train and _untrain public so that tokens can be bulk trained an… #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions BayesSharp/BayesClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void Train(TTagType tagId, string input)
{
var tokens = _tokenizer.Tokenize(input);
var tag = GetAndAddIfNotFound(_tags.Items, tagId);
_train(tag, tokens);
Train(tag, tokens);
_tags.SystemTag.TrainCount += 1;
tag.TrainCount += 1;
_mustRecache = true;
Expand All @@ -189,7 +189,7 @@ public void Untrain(TTagType tagId, string input)
{
return;
}
_untrain(tag, tokens);
Untrain(tag, tokens);
_tags.SystemTag.TrainCount += 1;
tag.TrainCount += 1;
_mustRecache = true;
Expand All @@ -201,25 +201,30 @@ public void Untrain(TTagType tagId, string input)
/// <param name="input">Input to be classified</param>
public Dictionary<TTagType, double> Classify(string input)
{
var tokens = _tokenizer.Tokenize(input).ToList();
var tags = CreateCacheAnsGetTags();
var tokens = _tokenizer.Tokenize(input).ToList();
return Classify(tokens);
}

var stats = new Dictionary<TTagType, double>();
private Dictionary<TTagType, double> Classify(List<TTokenType> tokens)
{
var tags = CreateCacheAnsGetTags();

foreach (var tag in tags.Items)
{
var probs = GetProbabilities(tag.Value, tokens).ToList();
if (probs.Count() != 0)
{
stats[tag.Key] = _combiner.Combine(probs);
}
}
return stats.OrderByDescending(s => s.Value).ToDictionary(s => s.Key, pair => pair.Value);
}
var stats = new Dictionary<TTagType, double>();

foreach (var tag in tags.Items)
{
var probs = GetProbabilities(tag.Value, tokens).ToList();
if (probs.Count() != 0)
{
stats[tag.Key] = _combiner.Combine(probs);
}
}
return stats.OrderByDescending(s => s.Value).ToDictionary(s => s.Key, pair => pair.Value);
}

#region Private Methods
#region Private Methods

private void _train(TagData<TTokenType> tag, IEnumerable<TTokenType> tokens)
public void Train(TagData<TTokenType> tag, IEnumerable<TTokenType> tokens)
{
var tokenCount = 0;
foreach (var token in tokens)
Expand All @@ -234,7 +239,7 @@ private void _train(TagData<TTokenType> tag, IEnumerable<TTokenType> tokens)
_tags.SystemTag.TokenCount += tokenCount;
}

private void _untrain(TagData<TTokenType> tag, IEnumerable<TTokenType> tokens)
public void Untrain(TagData<TTokenType> tag, IEnumerable<TTokenType> tokens)
{
foreach (var token in tokens)
{
Expand Down