Example #1
0
        public JsonResult Analyze(string input)
        {
            ExperimentService         experimentService = new ExperimentService();
            ExperimentResultViewModel pattern           = experimentService.Analyze(input);

            return(Json(pattern, JsonRequestBehavior.AllowGet));
        }
Example #2
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);
        }