Esempio n. 1
0
        public void AddAttribute(string intentName, NLUEntity entity, AttributePair attribute)
        {
            if (!this.attributeMap.Keys.Contains(intentName))
            {
                Dictionary <string, List <AttributePair> > wordToAttribute = new Dictionary <string, List <AttributePair> >();
                this.attributeMap.Add(intentName, wordToAttribute);
            }

            string key = entity.GetIdentity();

            if (!this.attributeMap[intentName].Keys.Contains(key))
            {
                this.attributeMap[intentName].Add(key, new List <AttributePair>());
            }

            this.attributeMap[intentName][key].Add(attribute);
        }
Esempio n. 2
0
        public NLUDataFrame Parse(List <NLUIntentRule> iList, List <EntityData> eList, List <EntityAttributeData> eaList)
        {
            NLUDataFrame nluDF = new NLUDataFrame();

            if (iList == null || iList.Count == 0)
            {
                //throw new Exception("No Intent is defined.");
                log.Warning("No Intent is defined.");
            }
            else
            {
                foreach (NLUIntentRule intentRule in iList)
                {
                    nluDF.AddIntentRule(intentRule);
                }
            }

            if (eList == null || eList.Count == 0)
            {
                //throw new Exception("No Entity is defined.");
                log.Warning("No Entity is defined.");
            }
            else
            {
                foreach (EntityData entity in eList)
                {
                    NLUEntity nluEntity = nluDF.AddEntity(entity.intentName, entity.entityValue, entity.entityType);
                    nluDF.AddSimilarWord(entity.intentName, entity.similarWord, nluEntity);
                }
            }

            if (eaList == null || eaList.Count == 0)
            {
                Log.Information("There is not any entity attribute defined.");
            }
            else
            {
                foreach (EntityAttributeData ea in eaList)
                {
                    nluDF.AddAttribute(ea.intentName, new NLUEntity(ea.entityValue, ea.entityType), new AttributePair(ea.attributeName, ea.attributeValue));
                }
            }

            return(nluDF);
        }
Esempio n. 3
0
        public void AddSimilarWord(string intentName, string similarWord, NLUEntity entity)
        {
            if (!this.standValueMap.Keys.Contains(intentName))
            {
                Dictionary <string, List <NLUEntity> > wordToEntity = new Dictionary <string, List <NLUEntity> >();
                this.standValueMap.Add(intentName, wordToEntity);
            }

            if (this.standValueMap[intentName].Keys.Contains(similarWord))
            {
                this.standValueMap[intentName][similarWord].Add(entity);
            }
            else
            {
                this.standValueMap[intentName].Add(similarWord, new List <NLUEntity>()
                {
                    entity
                });
            }
        }
Esempio n. 4
0
        public void Parse()
        {
            if (iList == null || iList.Count == 0)
            {
                throw new Exception("No Intent is defined.");
            }
            else
            {
                foreach (NLUIntentRule intentRule in iList)
                {
                    nluStore.AddIntentRule(intentRule);
                }
            }

            if (eList == null || eList.Count == 0)
            {
                throw new Exception("No Entity is defined.");
            }
            else
            {
                foreach (EntityData entity in eList)
                {
                    NLUEntity nluEntity = nluStore.AddEntity(entity.intentName, entity.entityValue, entity.entityType);
                    nluStore.AddSimilarWord(entity.intentName, entity.similarWord, nluEntity);
                }
            }

            if (eaList == null || eaList.Count == 0)
            {
                Log.Information("There is not any entity attribute defined.");
            }
            else
            {
                foreach (EntityAttributeData ea in eaList)
                {
                    nluStore.AddAttribute(ea.intentName, new NLUEntity(ea.entityValue, ea.entityType), new AttributePair(ea.attributeName, ea.attributeValue));
                }
            }
        }
Esempio n. 5
0
        public NLUEntity AddEntity(string intentName, string entityValue, string entityType)
        {
            Dictionary <string, Dictionary <string, NLUEntity> > typeEntities;

            if (!this.entityMap.Keys.Contains(intentName))
            {
                typeEntities = new Dictionary <string, Dictionary <string, NLUEntity> >();
                this.entityMap.Add(intentName, typeEntities);
            }

            typeEntities = this.entityMap[intentName];


            Dictionary <string, NLUEntity> entities;


            if (!typeEntities.Keys.Contains(entityType))
            {
                entities = new Dictionary <string, NLUEntity>();
                typeEntities.Add(entityType, entities);
            }

            entities = typeEntities[entityType];


            if (!entities.Keys.Contains(entityValue))
            {
                NLUEntity entity = new NLUEntity(entityValue, entityType);

                entities.Add(entityValue, entity);
                return(entity);
            }
            else
            {
                return(entities[entityValue]);
            }
        }
Esempio n. 6
0
        public QueryResult SearchVertexes(ContextManager contextMgmt, List <NLUEntity> entities)
        {
            QueryResult responseContent = new QueryResult(false, "Failed", ResponseItemType.Other);

            if (entities == null && entities.Count == 0)
            {
                responseContent = this.msgGenerator.GenerateErrorMessage("无法查找到对应节点,请确定输入的限定条件正确");
                contextMgmt.ExitDialog();
            }
            else
            {
                NLUEntity firstEntity = entities[0];

                List <Vertex> results = new List <Vertex>();

                if (firstEntity.GetEntityType() == "NodeName")
                {
                    Vertex vertex = dsFrame.SearchGraph(firstEntity.GetEntityValue(), contextMgmt.GetSecnarioName(), contextMgmt.GetSavedAttributes());

                    results.Add(vertex);
                }
                else if (firstEntity.GetEntityType() == "Label")
                {
                    results = dsFrame.SearchGraphByLabel(firstEntity.GetEntityValue(), contextMgmt.GetSecnarioName(), contextMgmt.GetSavedAttributes());
                }


                if (results == null || results.Count == 0)
                {
                    responseContent = this.msgGenerator.GenerateErrorMessage("无法查找到对应节点,请确定输入的限定条件正确");
                    contextMgmt.ExitDialog();
                }
                else
                {
                    string headMessage = "";

                    if (entities.Count > 1)
                    {
                        headMessage = firstEntity.GetEntityValue();

                        for (int i = 1; i < entities.Count; i++)
                        {
                            NLUEntity entity = entities[i];

                            if (entity.GetEntityType() == "RelationType")
                            {
                                HashSet <string> relationTypeSet = new HashSet <string>();
                                relationTypeSet.Add(entity.GetEntityValue());

                                List <Vertex> children = new List <Vertex>();

                                foreach (Vertex vertex in results)
                                {
                                    Dictionary <string, List <Vertex> > childrenDict = dsFrame.GetChildren(vertex, relationTypeSet, contextMgmt.GetSavedAttributes(), contextMgmt.GetSecnarioName());
                                    if (childrenDict != null)
                                    {
                                        List <Vertex> currentChildren = childrenDict[entity.GetEntityValue()];
                                        children.AddRange(currentChildren);
                                    }
                                }

                                if (children != null && children.Count > 0)
                                {
                                    headMessage += "的" + entity.GetEntityValue();

                                    results = children;
                                }
                            }
                            else if (entity.GetEntityType() == "Label")
                            {
                                List <Vertex> newResults = new List <Vertex>();
                                foreach (Vertex vertex in results)
                                {
                                    if (vertex.label == entity.GetEntityValue())
                                    {
                                        newResults.Add(vertex);
                                    }
                                }

                                if (newResults.Count > 0)
                                {
                                    headMessage += "的" + entity.GetEntityValue();
                                    results      = newResults;
                                }
                            }
                        }
                    }

                    if (results.Count == 1)
                    {
                        Vertex vertex = results[0];

                        if (vertex.isLeaf())
                        {
                            responseContent = this.msgGenerator.GenerateEndVertexMessage(vertex);
                            contextMgmt.ExitDialog();
                        }
                        else
                        {
                            List <DialogSlot> validSlots = GetValidSlots(contextMgmt);

                            if (validSlots == null || validSlots.Count() == 0)
                            {
                                contextMgmt.StartDialog();
                                responseContent = GetChildren(contextMgmt, vertex, null);
                            }
                            else
                            {
                                contextMgmt.SetSlots(validSlots);
                                contextMgmt.EnterSlotFilling();

                                responseContent = this.msgGenerator.GenerateSlotMessage(validSlots[0]);
                            }
                        }
                    }
                    else
                    {
                        contextMgmt.StartDialog();
                        responseContent = GetMessageForVertexes(contextMgmt, results);
                    }

                    if (!string.IsNullOrWhiteSpace(headMessage) && responseContent.success)
                    {
                        responseContent.responseMessage = headMessage + ":\n" + responseContent.responseMessage;
                    }
                }
            }

            return(responseContent);
        }
Esempio n. 7
0
        public NLUEntity Clone()
        {
            NLUEntity newOne = new NLUEntity(this.value, this.type);

            return(newOne);
        }