コード例 #1
0
        public void PushToRedis()
        {
            IntentService  intentService        = new IntentService();
            PatternService patternService       = new PatternService();
            List <IntentRedisViewModel> intents = intentService.GetAllForRedis();
            List <string> patterns = patternService.GetFullPatterns();

            var db = RedisConnectionHelper.Connection.GetDatabase();

            db.StringSet(@"BotIntents", JsonConvert.SerializeObject(intents));
            db.StringSet(@"BotPatterns", JsonConvert.SerializeObject(patterns));
        }
コード例 #2
0
        public void Delete(int intentId)
        {
            var intent = this.FirstOrDefault(q => q.Id == intentId);

            if (intent != null)
            {
                intent.Active = false;
                PatternService patternService = new PatternService();
                var            patterns       = patternService.Get(q => q.Active == true && q.IntentId == intentId).ToList();
                foreach (var pattern in patterns)
                {
                    pattern.IntentId = null;
                    pattern.Group    = -1;
                }

                patternService.SaveChanges();
                this.SaveChanges();
            }
        }
コード例 #3
0
        public void Update(int id, string name, int[] patterns, int[] groups)
        {
            try
            {
                Intent intent = this.FirstOrDefault(q => q.Id == id);
                if (intent != null)
                {
                    intent.Name = name;
                }
                this.DbSet.SaveChanges();

                PatternService patternService = new PatternService();
                List <Pattern> ps             = patternService.Get(q => q.IntentId == id).ToList();
                foreach (var p in ps)
                {
                    p.Active   = false;
                    p.IntentId = null;
                }

                if (patterns != null)
                {
                    for (var i = 0; i < patterns.Length; ++i)
                    {
                        Pattern pattern = patternService.FirstOrDefault(q => q.Id == patterns[i]);
                        pattern.IntentId = id;
                        pattern.Group    = groups[i];
                        pattern.Active   = true;
                    }
                }


                patternService.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #4
0
        public Intent Create(string name, int[] patterns, int[] groups)
        {
            Intent intent = this.Add(new Intent()
            {
                Active = true,
                Name   = name,
            });

            PatternService patternService = new PatternService();

            int i = 0;

            foreach (var patternId in patterns)
            {
                var pattern = patternService.FirstOrDefault(q => q.Id == patternId && q.Active == true);
                pattern.Group    = groups[i];
                pattern.IntentId = intent.Id;
                ++i;
            }
            patternService.SaveChanges();

            this.SaveChanges();
            return(intent);
        }
コード例 #5
0
        public ExperimentResultViewModel Analyze(string input)
        {
            string         inputTmp       = input.Trim();
            PatternService patternService = new PatternService();
            List <Pattern> patterns       = patternService.Get(q => q.Active == true).ToList();

            int     maxElements  = -1;
            Pattern matchPattern = null;
            Dictionary <string, string> matches = null;
            string specialValues = ""; //when the entity simply just want some words

            foreach (var pattern in patterns)
            {
                specialValues = "";
                bool hasUnknwonPhrase = false;
                Dictionary <string, string> matchesTmp = new Dictionary <string, string>();
                inputTmp = input.Trim();
                List <PatternEntityMapping> pems = pattern.PatternEntityMappings.Where(q => q.Active == true).OrderBy(q => q.Position).ToList();
                for (int i = 0; i < pems.Count; i++)
                {
                    Regex regex = null;
                    if (pems[i].Entity.Words == @".*?")
                    {
                        specialValues    = inputTmp;
                        hasUnknwonPhrase = true;
                    }
                    else
                    {
                        if (pattern.MatchBegin && pattern.MatchEnd && pems.Count == 1)
                        {
                            regex = new Regex(@"(?:^|\W)^(" + pems[i].Entity.Words + @")$(?:$|\W)", RegexOptions.IgnoreCase);
                        }
                        else if (i == pems.Count - 1 && pattern.MatchEnd)
                        {
                            regex = new Regex(@"(?:^|\W)(" + pems[i].Entity.Words + @")$(?:$|\W)", RegexOptions.IgnoreCase);
                        }
                        else if (specialValues == "")
                        {
                            if (i == 0)
                            {
                                if (pattern.MatchBegin)
                                {
                                    regex = new Regex(@"(?:^|\W)^(" + pems[i].Entity.Words + @")(?:$|\W)", RegexOptions.IgnoreCase);
                                }
                                else
                                {
                                    regex = new Regex(@"(?:^|\W)(" + pems[i].Entity.Words + @")(?:$|\W)", RegexOptions.IgnoreCase);
                                }
                            }
                            else
                            {
                                regex = new Regex(@"(?:^|\W)^(" + pems[i].Entity.Words + @")(?:$|\W)", RegexOptions.IgnoreCase);
                            }
                        }
                        else
                        {
                            regex = new Regex(@"(?:^|\W)" + pems[i].Entity.Words + @"(?:$|\W)", RegexOptions.IgnoreCase);
                        }
                        Match result = regex.Match(inputTmp);
                        if (result.Success)
                        {
                            if (matchesTmp.ContainsKey(pems[i].Entity.Name))
                            {
                                string value = matchesTmp[pems[i].Entity.Name];
                                matchesTmp[pems[i].Entity.Name] = value + ";" + result.Value;
                            }
                            else
                            {
                                matchesTmp.Add(pems[i].Entity.Name, result.Value);
                            }
                            if (specialValues != "")
                            {
                                matchesTmp.Add("Cụm từ", specialValues.Substring(0, specialValues.Length - result.Length).Trim());
                                specialValues = "";
                            }
                            if (i == pems.Count - 1)
                            {
                                if ((!hasUnknwonPhrase && matchesTmp.Count > maxElements) || (hasUnknwonPhrase && matchesTmp.Count - 1 > maxElements))
                                {
                                    matchPattern = pattern;
                                    matches      = matchesTmp;
                                    maxElements  = hasUnknwonPhrase ? matchesTmp.Count - 1 : matchesTmp.Count;
                                    if (result.Index + result.Value.Trim().Length + 1 < inputTmp.Length)
                                    {
                                        specialValues = inputTmp.Substring(result.Index + result.Value.Trim().Length + 1);
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                if (result.Index + result.Value.Trim().Length + 1 < inputTmp.Length)
                                {
                                    inputTmp = inputTmp.Substring(result.Index + result.Value.Trim().Length + 1).Trim();
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (specialValues != "")
                {
                    if (matches == null && ((!hasUnknwonPhrase && matchesTmp.Count > maxElements) || (hasUnknwonPhrase && matchesTmp.Count - 1 > maxElements)))
                    {
                        matches      = matchesTmp;
                        matchPattern = pattern;
                        matches.Add("Cụm từ", specialValues.Trim());
                        maxElements = hasUnknwonPhrase ? matchesTmp.Count - 1 : matchesTmp.Count;
                    }
                    else if (matches != null && !matches.ContainsKey("Cụm từ") && ((!hasUnknwonPhrase && matchesTmp.Count > maxElements) || (hasUnknwonPhrase && matchesTmp.Count - 1 > maxElements)))
                    {
                        matches.Add("Cụm từ", specialValues.Trim());
                        matchPattern = pattern;
                        maxElements  = hasUnknwonPhrase ? matchesTmp.Count - 1 : matchesTmp.Count;
                    }
                    specialValues = "";
                }
            }



            ExperimentResultViewModel model = new ExperimentResultViewModel();

            if (matchPattern != null)
            {
                model.IntentId = matchPattern.IntentId ?? 0;
                model.Group    = matchPattern.Group ?? 0;
                model.Matches  = matches;
            }

            return(model);
        }