From f4d852b651d5c01e8776daa24ce1b06ba33fdf7e Mon Sep 17 00:00:00 2001 From: "haiping008@gmail.com" Date: Fri, 17 Aug 2018 17:22:05 -0500 Subject: [PATCH] Optimize NGramTagger. --- BotSharp.NLP/Tag/NGramTagger.cs | 88 ++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/BotSharp.NLP/Tag/NGramTagger.cs b/BotSharp.NLP/Tag/NGramTagger.cs index 42940869..c42ca99f 100644 --- a/BotSharp.NLP/Tag/NGramTagger.cs +++ b/BotSharp.NLP/Tag/NGramTagger.cs @@ -12,55 +12,73 @@ namespace BotSharp.NLP.Tag /// public class NGramTagger : ITagger { - public Dictionary ContextMapping { get; set; } + private List _contextMapping { get; set; } public void Tag(Sentence sentence, TagOptions options) { // need training to generate model - if(ContextMapping == null) + if(_contextMapping == null) { - var cache = new List>(); - var contextTag = new List>(); - - ContextMapping = new Dictionary(); - - options.Corpus.ForEach(sent => - { - // Supplementary place - for (int ngram = 1; ngram < options.NGram; ngram++) - { - sent.Words.Insert(0, new Token { Text = "NIL", Pos = options.Tag, Start = (ngram - 1) * 3 }); - } - - int pos = options.NGram - 1; - for(pos = 1; pos < sent.Words.Count; pos++) - { - Token pre = sent.Words[pos - 1]; - Token cur = sent.Words[pos]; - - cache.Add(new Tuple($"{pre.Pos} {cur.Text}", cur.Pos));// Dictionary.Add($"{pre.Pos} {cur.Text}", cur.Pos); - } - }); - - var results = (from c in cache - group c by c.Item1 into g - select new { g.Key, Count = g.Count() }).ToList(); - - results.ForEach(x => - { - int count = cache.Count(c => c.Item1 == x.Key); - }); + Train(options.Corpus, options); } } public void Train(List sentences, TagOptions options) { - throw new NotImplementedException(); + _contextMapping = new List(); + + for (int idx = 0; idx < options.Corpus.Count; idx++) + { + var sent = options.Corpus[idx]; + + for (int ngram = 1; ngram < options.NGram; ngram++) + { + sent.Words.Insert(0, new Token { Text = "NIL", Pos = options.Tag, Start = (ngram - 1) * 3 }); + } + + int pos = options.NGram - 1; + for (pos = 1; pos < sent.Words.Count; pos++) + { + var freq = new NGramFreq + { + PrecedingTokens = new List { sent.Words[pos - 1] }, + Token = sent.Words[pos], + Count = 0 + }; + + _contextMapping.Add(freq); + } + } + + /*var results = (from c in cache + group c by c.Item1 into g + select new { g.Key, Count = g.Count() }).ToList();*/ } private class NGramFreq { - public string Key { get; set; } + /// + /// Tokens prior current token + /// + public List PrecedingTokens { get; set; } + + /// + /// Current token tag + /// + public Token Token { get; set; } + + /// + /// Occurence frequency + /// + public int Count { get; set; } + + public string Context + { + get + { + return $"{PrecedingTokens.First().Pos} {Token.Text} {Token.Pos}"; + } + } } } }