Exemplo n.º 1
0
        private TempData MatchPositiveRules(string query, int startIndex)
        {
            TempData result = new TempData();

            int index = startIndex;

            while (index < this.intentPositiveDetermineRules.Count())
            {
                NLUIntentRule rule = this.intentPositiveDetermineRules[index];

                bool matched = IsMatched(rule.ruleSecs, query);

                if (matched)
                {
                    result.intentName         = rule.intentName;
                    result.positionInPosRules = index;

                    return(result);
                }
                else
                {
                    index += 1;
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public List <NLUIntentRule> ParseIntentRules()
        {
            List <NLUIntentRule> list = new List <NLUIntentRule>();

            string[] fileNamess = Directory.GetFiles(rootPath, "intentrules*.tsv").Select(Path.GetFileName).ToArray();

            List <string> lines = new List <string>();

            foreach (string fileName in fileNamess)
            {
                string content = File.ReadAllText(rootPath + fileName);
                lines.AddRange(content.Split('\n').ToList <string>());
            }

            int seqNo = 1;

            foreach (string line in lines)
            {
                string theLine = line.Trim();

                string[] tmps = theLine.Split('\t');

                if (tmps.Count() != 3)
                {
                    throw new Exception("invalid line in intentrules.tsv");
                }
                else
                {
                    NLUIntentRule rule = new NLUIntentRule();
                    rule.intentName = tmps[0].Trim();

                    string typeStr = tmps[1].Trim();

                    IntentRuleType type = IntentRuleType.POSITIVE;

                    if (typeStr == "NEGATIVE")
                    {
                        type = IntentRuleType.NEGATIVE;
                    }

                    rule.type = type;

                    List <string> ruleSecs = tmps[2].Trim().Split(';').ToList();

                    rule.ruleSecs = ruleSecs;

                    rule.id = seqNo.ToString();

                    seqNo += 1;

                    list.Add(rule);
                }
            }

            return(list);
        }
Exemplo n.º 3
0
        public void AddIntentRule(NLUIntentRule intentRule)
        {
            if (intentRule.type == IntentRuleType.POSITIVE)
            {
                this.intentPositiveDetermineRules.Add(intentRule);
            }
            else if (intentRule.type == IntentRuleType.NEGATIVE)
            {
                string intentName = intentRule.intentName;

                if (!this.intentNegativeDeterminRules.Keys.Contains(intentName))
                {
                    this.intentNegativeDeterminRules.Add(intentName, new List <NLUIntentRule>());
                }

                this.intentNegativeDeterminRules[intentName].Add(intentRule);
            }
        }
Exemplo n.º 4
0
        private (List <NLUIntentRule>, List <EntityData>, VisulizationConfig) GenerateNLUAndConfig(List <Vertex> vertexes, List <Edge> edges, string scenario, string configPath)
        {
            VisulizationConfig vConfig = new VisulizationConfig();

            vConfig.scenario             = scenario;
            vConfig.labelsOfVertexes     = new List <ColorConfig>();
            vConfig.relationTypesOfEdges = new List <ColorConfig>();

            string hexFilePath = configPath + Path.DirectorySeparatorChar + "HexColorCodeDict.tsv";
            string pdFilePath  = configPath + Path.DirectorySeparatorChar + "PreDefinedVertexColor.tsv";

            Dictionary <string, string> hexDict = new Dictionary <string, string>();

            foreach (var line in System.IO.File.ReadLines(hexFilePath))
            {
                string[] tmps = line.Split('\t');

                if (hexDict.ContainsKey(tmps[0]))
                {
                    hexDict[tmps[0]] = tmps[1];
                }
                else
                {
                    hexDict.Add(tmps[0], tmps[1]);
                }
            }

            Dictionary <string, string> predefinedDict = new Dictionary <string, string>();

            foreach (var line in System.IO.File.ReadLines(pdFilePath))
            {
                string[] tmps = line.Split('\t');
                if (predefinedDict.ContainsKey(tmps[0]))
                {
                    predefinedDict[tmps[0]] = tmps[1];
                }
                else
                {
                    predefinedDict.Add(tmps[0], tmps[1]);
                }
            }
            // ---

            HashSet <string> entityNameSet   = new HashSet <string>();
            HashSet <string> entityTypeSet   = new HashSet <string>();
            HashSet <string> propertyNameSet = new HashSet <string>();

            foreach (Vertex vertex in vertexes)
            {
                entityTypeSet.Add(vertex.label);
                entityNameSet.Add(vertex.name);
                if (vertex.properties != null && vertex.properties.Count > 0)
                {
                    foreach (VertexProperty p in vertex.properties)
                    {
                        propertyNameSet.Add(p.name);
                    }
                }
            }

            HashSet <string> relationTypeSet = new HashSet <string>();

            foreach (Edge edge in edges)
            {
                relationTypeSet.Add(edge.relationType);
            }


            List <EntityData> entityDatas = new List <EntityData>();

            List <NLUIntentRule> intentRuleList = new List <NLUIntentRule>();
            NLUIntentRule        intentRule     = new NLUIntentRule();

            intentRule.id         = Guid.NewGuid().ToString();
            intentRule.intentName = scenario;
            intentRule.type       = IntentRuleType.POSITIVE;
            intentRule.ruleSecs   = new List <string>();

            string entityRule = "";
            int    count      = 0;

            foreach (string entityName in entityNameSet)
            {
                if (count % 20 == 0 && count > 0)
                {
                    entityRule = entityRule.Substring(0, entityRule.Length - 1);
                    intentRule.ruleSecs.Add(entityRule);

                    intentRuleList.Add(intentRule);

                    intentRule            = new NLUIntentRule();
                    intentRule.id         = Guid.NewGuid().ToString();
                    intentRule.intentName = scenario;
                    intentRule.type       = IntentRuleType.POSITIVE;
                    intentRule.ruleSecs   = new List <string>();

                    entityRule = "";
                }

                entityRule += entityName + "|";
                entityDatas.Add(CreateEntityData(scenario, "NodeName", entityName));
                count++;
            }

            entityRule = entityRule.Substring(0, entityRule.Length - 1);
            intentRule.ruleSecs.Add(entityRule);

            intentRuleList.Add(intentRule);

            foreach (string entityType in entityTypeSet)
            {
                vConfig.labelsOfVertexes.Add(GetColor(hexDict, predefinedDict, entityType));
            }

            foreach (string pName in propertyNameSet)
            {
                entityDatas.Add(CreateEntityData(scenario, "PropertyName", pName));
            }

            foreach (string rType in relationTypeSet)
            {
                entityDatas.Add(CreateEntityData(scenario, "RelationType", rType));
                vConfig.relationTypesOfEdges.Add(GetColor(hexDict, predefinedDict, rType));
            }

            return(intentRuleList, entityDatas, vConfig);
        }