Ejemplo n.º 1
0
        /// <summary>
        /// parses <emotion/> area to Emotion
        /// </summary>
        /// <param name="emotionNode">XML node of <emotion/></param>
        /// <returns>Emotion object</returns>
        protected Emotion parseEmotion(XmlNode emotionNode)
        {
            Emotion emotion = new Emotion();

            /* ## attributes ## */

            //id
            if (emotionNode.Attributes["id"] != null)
            {
                emotion.Id = emotionNode.Attributes["id"].Value;
            }

            //version
            if (emotionNode.Attributes["version"] != null)
            {
                emotion.Version = emotionNode.Attributes["version"].Value;
            }

            //expressedThrough
            if (emotionNode.Attributes["expressed-through"] != null)
            {
                emotion.ExpressedThrough = emotionNode.Attributes["expressed-through"].Value;
            }

            //add time ralted stuff
            if (emotionNode.Attributes["start"] != null)
            {
                emotion.Start = Convert.ToInt64(emotionNode.Attributes["start"].Value);

            }
            if (emotionNode.Attributes["end"] != null)
            {
                emotion.End = Convert.ToInt64(emotionNode.Attributes["end"].Value);
            }
            if (emotionNode.Attributes["duration"] != null)
            {
                emotion.Duration = Convert.ToInt64(emotionNode.Attributes["duration"].Value);
            }
            if (emotionNode.Attributes["time-ref-uri"] != null)
            {
                emotion.TimeRefUri = new Uri(emotionNode.Attributes["time-ref-uri"].Value);
            }
            if (emotionNode.Attributes["time-ref-anchor-point"] != null)
            {
                emotion.TimeRefAnchorPoint = Convert.ToInt64(emotionNode.Attributes["time-ref-anchor-point"].Value);
            }
            if (emotionNode.Attributes["offset-to-start"] != null)
            {
                emotion.OffsetToStart = Convert.ToInt64(emotionNode.Attributes["offset-to-start"].Value);
            }

            /* ## child tags ## */

            //references
            XmlNodeList referenceNodes = emotionNode.SelectNodes("emo:reference", nsManager);
            foreach (XmlNode refs in referenceNodes)
            {
                emotion.addReference(parseReference(refs));
            }

            //infoblock
            XmlNodeList infoblocks = emotionNode.SelectNodes("emo:info", nsManager);
            if (infoblocks.Count > 1)
            {
                throw new EmotionMLException("Only maximum one instance of <info/> is allowed. " + infoblocks.Count + " given.");
            }
            else if (infoblocks.Count == 1)
            {
                Info infoArea = parseInfo(infoblocks.Item(0));
                emotion.Info = infoArea;
            }

            //handle emotion sets
            Uri categorySet = null;
            Uri dimensionSet = null;
            Uri appraisalSet = null;
            Uri actionTendencySet = null;

            //search emotion sets in attributes of <emotion>, otherwise use this of <emotionml> (can also be null)
            if (emotionNode.Attributes["category-set"] != null)
            {
                categorySet = new Uri(emotionNode.Attributes["category-set"].InnerText);
            }
            else
            {
                categorySet = emotionml.CategorySet;
            }
            if (emotionNode.Attributes["dimension-set"] != null)
            {
                dimensionSet = new Uri(emotionNode.Attributes["dimension-set"].InnerText);
            }
            else
            {
                dimensionSet = emotionml.DimensionSet;
            }
            if (emotionNode.Attributes["appraisal-set"] != null)
            {
                appraisalSet = new Uri(emotionNode.Attributes["appraisal-set"].InnerText);
            }
            else
            {
                appraisalSet = emotionml.AppraisalSet;
            }
            if (emotionNode.Attributes["action-tendency-set"] != null)
            {
                actionTendencySet = new Uri(emotionNode.Attributes["action-tendency-set"].InnerText);
            }
            else
            {
                actionTendencySet = emotionml.ActionTendencySet;
            }

            //add categories
            XmlNodeList categoryTags = emotionNode.SelectNodes("emo:category", nsManager);
            if (categoryTags.Count > 0)
            {
                Set<Category> categories = new Set<Category>(categorySet);

                foreach (XmlNode cat in categoryTags)
                {
                    string categoryName = cat.Attributes["name"].InnerText;
                    double? categoryValue = null;
                    double? categoryConfidence = null;

                    if (cat.Attributes["value"] != null)
                    {
                        categoryValue = Helper.string2double(cat.Attributes["value"].InnerText);
                    }
                    if (cat.Attributes["confidence"] != null)
                    {
                        categoryConfidence = Helper.string2double(cat.Attributes["confidence"].InnerText);
                    }

                    Category newCategory = new Category(categoryName, categoryValue, categoryConfidence);

                    //trace
                    XmlNodeList trace = cat.SelectNodes("emo:trace", nsManager);
                    if (trace.Count == 1)
                    {
                        newCategory.Trace = parseTrace(trace[0]);
                    }
                    else if (trace.Count > 1)
                    {
                        throw new EmotionMLException("Only one trace is allowed in category " + categoryName);
                    }

                    //add category to list
                    categories.Add(newCategory);
                }

                emotion.Categories = categories;
            }

            //add dimensions
            XmlNodeList dimensionTags = emotionNode.SelectNodes("emo:dimension", nsManager);
            if (dimensionTags.Count > 0)
            {
                Set<Dimension> dimensions = new Set<Dimension>(dimensionSet);

                foreach (XmlNode dim in dimensionTags)
                {
                    string dimensionName = dim.Attributes["name"].InnerText;
                    double? dimensionValue = null;
                    double? dimensionConfidence = null;

                    if (dim.Attributes["value"] != null)
                    {
                        dimensionValue = Helper.string2double(dim.Attributes["value"].InnerText);
                    }
                    if (dim.Attributes["confidence"] != null)
                    {
                        dimensionConfidence = Helper.string2double(dim.Attributes["confidence"].InnerText);
                    }

                    Dimension newDimension = new Dimension(dimensionName, dimensionValue, dimensionConfidence);

                    //trace
                    XmlNodeList trace = dim.SelectNodes("emo:trace", nsManager);
                    if (trace.Count == 1)
                    {
                        newDimension.Trace = parseTrace(trace[0]);
                    }
                    else if (trace.Count > 1)
                    {
                        throw new EmotionMLException("Only one trace is allowed in dimension " + dimensionName);
                    }

                    dimensions.Add(newDimension);
                }

                emotion.Dimensions = dimensions;
            }

            //add appraisals
            XmlNodeList appraisalTags = emotionNode.SelectNodes("emo:appraisal", nsManager);
            if (appraisalTags.Count > 0)
            {
                Set<Appraisal> appraisals = new Set<Appraisal>(appraisalSet);

                foreach (XmlNode apr in appraisalTags)
                {
                    string appraisalName = apr.Attributes["name"].InnerText;
                    double? appraisalValue = null;
                    double? appraisalConfidence = null;

                    if (apr.Attributes["value"] != null)
                    {
                        appraisalValue = Helper.string2double(apr.Attributes["value"].InnerText);
                    }
                    if (apr.Attributes["confidence"] != null)
                    {
                        appraisalConfidence = Helper.string2double(apr.Attributes["confidence"].InnerText);
                    }

                    Appraisal newAppraisal = new Appraisal(appraisalName, appraisalValue, appraisalConfidence);

                    //trace
                    XmlNodeList trace = apr.SelectNodes("emo:trace", nsManager);
                    if (trace.Count == 1)
                    {
                        newAppraisal.Trace = parseTrace(trace[0]);
                    }
                    else if (trace.Count > 1)
                    {
                        throw new EmotionMLException("Only one trace is allowed in appraisal " + appraisalName);
                    }

                    appraisals.Add(newAppraisal);
                }

                emotion.Appraisals = appraisals;
            }

            //add action tendencies
            XmlNodeList actionTendencyTags = emotionNode.SelectNodes("emo:action-tendency", nsManager);
            if (actionTendencyTags.Count > 0)
            {
                Set<ActionTendency> actionTendencies = new Set<ActionTendency>(actionTendencySet);

                foreach (XmlNode act in actionTendencyTags)
                {
                    string actionTendencyName = act.Attributes["name"].InnerText;
                    double? actionTendencyValue = null;
                    double? actionTendencyConfidence = null;

                    if (act.Attributes["value"] != null)
                    {
                        actionTendencyValue = Helper.string2double(act.Attributes["value"].InnerText);
                    }
                    if (act.Attributes["confidence"] != null)
                    {
                        actionTendencyConfidence = Helper.string2double(act.Attributes["confidence"].InnerText);
                    }

                    ActionTendency newActionTendency = new ActionTendency(actionTendencyName, actionTendencyValue, actionTendencyConfidence);

                    //trace
                    XmlNodeList trace = act.SelectNodes("emo:trace", nsManager);
                    if (trace.Count == 1)
                    {
                        newActionTendency.Trace = parseTrace(trace[0]);
                    }
                    else if (trace.Count > 1)
                    {
                        throw new EmotionMLException("Only one trace is allowed in action tendency " + actionTendencyName);
                    }

                    actionTendencies.Add(newActionTendency);
                }

                emotion.ActionTendencies = actionTendencies;
            }

            //search for plaintext
            XmlNode textnode = emotionNode.SelectSingleNode("text()");
            if (textnode != null)
            {
                emotion.Plaintext = textnode.InnerText;
            }

            return emotion;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// modify the values of a category, adds it if not exists
 /// </summary>
 /// <param name="newCategory">the values to set the category to</param>
 public void addCategory(Category newCategory)
 {
     int foundOnIndex = category.FindIndex(delegate(Part categoryToMatch)
     {
         return categoryToMatch.Name== newCategory.Name;
     });
     if (foundOnIndex == -1)
     {
         category.Add(newCategory);
     }
     else
     {
         category[foundOnIndex].Value = newCategory.Value;
         category[foundOnIndex].Confidence = newCategory.Confidence;
     }
 }