public static IList <ICoreMap> ReplaceMergedUsingTokenOffsets <_T0, _T1>(IList <_T0> list, IList <_T1> matchedExprs)
            where _T0 : ICoreMap
            where _T1 : MatchedExpression
        {
            if (matchedExprs == null)
            {
                return(list);
            }
            IDictionary <int, int> tokenBeginToListIndexMap = new Dictionary <int, int>();
            //Generics.newHashMap();
            IDictionary <int, int> tokenEndToListIndexMap = new Dictionary <int, int>();

            //Generics.newHashMap();
            for (int i = 0; i < list.Count; i++)
            {
                ICoreMap cm = list[i];
                if (cm.ContainsKey(typeof(CoreAnnotations.TokenBeginAnnotation)) && cm.ContainsKey(typeof(CoreAnnotations.TokenEndAnnotation)))
                {
                    tokenBeginToListIndexMap[cm.Get(typeof(CoreAnnotations.TokenBeginAnnotation))] = i;
                    tokenEndToListIndexMap[cm.Get(typeof(CoreAnnotations.TokenEndAnnotation))]     = i + 1;
                }
                else
                {
                    tokenBeginToListIndexMap[i]   = i;
                    tokenEndToListIndexMap[i + 1] = i + 1;
                }
            }
            matchedExprs.Sort(ExprTokenOffsetComparator);
            IList <ICoreMap> merged = new List <ICoreMap>(list.Count);
            // Approximate size
            int last = 0;

            foreach (MatchedExpression expr in matchedExprs)
            {
                int start  = expr.tokenOffsets.First();
                int end    = expr.tokenOffsets.Second();
                int istart = tokenBeginToListIndexMap[start];
                int iend   = tokenEndToListIndexMap[end];
                if (istart != null && iend != null)
                {
                    if (istart >= last)
                    {
                        Sharpen.Collections.AddAll(merged, list.SubList(last, istart));
                        ICoreMap m = expr.GetAnnotation();
                        merged.Add(m);
                        last = iend;
                    }
                }
            }
            // Add rest of elements
            if (last < list.Count)
            {
                Sharpen.Collections.AddAll(merged, list.SubList(last, list.Count));
            }
            return(merged);
        }
        //      throw e;
        private Optional <string> GetPubDate(ICoreMap document)
        {
            //--Get Date
            //(error checks)
            if (!document.ContainsKey(typeof(CoreAnnotations.CalendarAnnotation)) && !document.ContainsKey(typeof(CoreAnnotations.DocDateAnnotation)))
            {
                throw new ArgumentException("CoreMap must have either a Calendar or DocDate annotation");
            }
            //not strictly necessary, technically...
            //(variables)
            Calendar dateCalendar = document.Get(typeof(CoreAnnotations.CalendarAnnotation));

            if (dateCalendar != null)
            {
                //(case: calendar annotation)
                return(Optional.Of(string.Format("%TF", dateCalendar)));
            }
            else
            {
                //(case: docdateannotation)
                string s = document.Get(typeof(CoreAnnotations.DocDateAnnotation));
                if (s != null)
                {
                    return(Optional.Of(s));
                }
            }
            return(Optional.Empty());
        }
 public virtual IList <MatchedExpression> Extract(ICoreMap annotation)
 {
     if (!annotation.ContainsKey(typeof(CoreAnnotations.NumerizedTokensAnnotation)))
     {
         IList <ICoreMap> mergedNumbers = NumberNormalizer.FindAndMergeNumbers(annotation);
         annotation.Set(typeof(CoreAnnotations.NumerizedTokensAnnotation), mergedNumbers);
     }
     return(extractor.ExtractExpressions(annotation));
 }
        /// <summary>Two CoreMaps are equal iff all keys and values are .equal.</summary>
        public override bool Equals(object obj)
        {
            if (!(obj is ICoreMap))
            {
                return(false);
            }
            if (obj is HashableCoreMap)
            {
                // overridden behavior for HashableCoreMap
                return(obj.Equals(this));
            }
            if (obj is Edu.Stanford.Nlp.Util.ArrayCoreMap)
            {
                // specialized equals for ArrayCoreMap
                return(Equals((Edu.Stanford.Nlp.Util.ArrayCoreMap)obj));
            }
            // TODO: make the general equality work in the situation of loops in the object graph
            // general equality
            ICoreMap other = (ICoreMap)obj;

            if (!this.KeySet().Equals(other.KeySet()))
            {
                return(false);
            }
            foreach (Type key in this.KeySet())
            {
                if (!other.ContainsKey(key))
                {
                    return(false);
                }
                object thisV  = this.Get(key);
                object otherV = other.Get(key);
                if (thisV == otherV)
                {
                    continue;
                }
                // the two values must be unequal, so if either is null, the other isn't
                if (thisV == null || otherV == null)
                {
                    return(false);
                }
                if (!thisV.Equals(otherV))
                {
                    return(false);
                }
            }
            return(true);
        }
        private bool ExtractAnnotation(ICoreMap sourceAnnotation, CoreMapAggregator aggregator)
        {
            Type tokensAnnotationKey = extractFunc.tokensAnnotationField;

            if (chunkOffsets != null)
            {
                annotation = aggregator.Merge((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), chunkOffsets.GetBegin(), chunkOffsets.GetEnd());
                if (sourceAnnotation.ContainsKey(typeof(CoreAnnotations.TextAnnotation)))
                {
                    ChunkAnnotationUtils.AnnotateChunkText(annotation, sourceAnnotation);
                }
                if (tokenOffsets != null)
                {
                    if (annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)) == null)
                    {
                        annotation.Set(typeof(CoreAnnotations.TokenBeginAnnotation), tokenOffsets.GetBegin());
                    }
                    if (annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)) == null)
                    {
                        annotation.Set(typeof(CoreAnnotations.TokenEndAnnotation), tokenOffsets.GetEnd());
                    }
                }
                charOffsets  = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation)));
                tokenOffsets = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)), Interval.IntervalOpenEnd);
            }
            else
            {
                int baseCharOffset = sourceAnnotation.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation));
                if (baseCharOffset == null)
                {
                    baseCharOffset = 0;
                }
                chunkOffsets = ChunkAnnotationUtils.GetChunkOffsetsUsingCharOffsets((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), charOffsets.GetBegin() + baseCharOffset, charOffsets.GetEnd() + baseCharOffset);
                ICoreMap annotation2 = aggregator.Merge((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), chunkOffsets.GetBegin(), chunkOffsets.GetEnd());
                annotation   = ChunkAnnotationUtils.GetAnnotatedChunkUsingCharOffsets(sourceAnnotation, charOffsets.GetBegin(), charOffsets.GetEnd());
                tokenOffsets = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)), Interval.IntervalOpenEnd);
                annotation.Set(tokensAnnotationKey, annotation2.Get(tokensAnnotationKey));
            }
            text = annotation.Get(typeof(CoreAnnotations.TextAnnotation));
            extractFunc.Annotate(this, (IList <ICoreMap>)annotation.Get(tokensAnnotationKey));
            return(true);
        }
示例#6
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void Annotate(ICoreMap document)
        {
            //--Create Input File
            //(create file)
            File inputFile = File.CreateTempFile("heideltime", ".input");
            //(write to file)
            PrintWriter inputWriter = new PrintWriter(inputFile);

            inputWriter.Println(document.Get(typeof(CoreAnnotations.TextAnnotation)));
            inputWriter.Close();
            //--Get Date
            //(error checks)
            if (!document.ContainsKey(typeof(CoreAnnotations.CalendarAnnotation)) && !document.ContainsKey(typeof(CoreAnnotations.DocDateAnnotation)))
            {
                throw new ArgumentException("CoreMap must have either a Calendar or DocDate annotation");
            }
            //not strictly necessary, technically...
            //(variables)
            Calendar dateCalendar = document.Get(typeof(CoreAnnotations.CalendarAnnotation));
            string   pubDate      = null;

            if (dateCalendar != null)
            {
                //(case: calendar annotation)
                pubDate = string.Format("%TF", dateCalendar);
            }
            else
            {
                //(case: docdateannotation)
                string s = document.Get(typeof(CoreAnnotations.DocDateAnnotation));
                if (s != null)
                {
                    pubDate = s;
                }
            }
            //--Build Command
            List <string> args = new List <string>();

            args.Add("java");
            args.Add("-jar");
            args.Add(this.heideltimePath.GetPath() + "/heideltime.jar");
            args.Add("-c");
            args.Add(this.heideltimePath.GetPath() + "/config.props");
            args.Add("-l");
            args.Add(this.language);
            args.Add("-t");
            args.Add("NEWS");
            if (pubDate != null)
            {
                args.Add("-dct");
                args.Add(pubDate);
            }
            args.Add(inputFile.GetPath());
            // run HeidelTime on the input file
            ProcessBuilder process      = new ProcessBuilder(args);
            StringWriter   outputWriter = new StringWriter();

            SystemUtils.Run(process, outputWriter, null);
            string  output   = outputWriter.GetBuffer().ToString();
            Pattern docClose = Pattern.Compile("</DOC>.*", Pattern.Dotall);

            output = docClose.Matcher(output).ReplaceAll("</DOC>").ReplaceAll("<!DOCTYPE TimeML SYSTEM \"TimeML.dtd\">", string.Empty);
            //TODO TimeML.dtd? FileNotFoundException if we leave it in
            Pattern badNestedTimex = Pattern.Compile(Pattern.Quote("<T</TIMEX3>IMEX3"));

            output = badNestedTimex.Matcher(output).ReplaceAll("</TIMEX3><TIMEX3");
            Pattern badNestedTimex2 = Pattern.Compile(Pattern.Quote("<TI</TIMEX3>MEX3"));

            output = badNestedTimex2.Matcher(output).ReplaceAll("</TIMEX3><TIMEX3");
            //output = output.replaceAll("\\n\\n<TimeML>\\n\\n","<TimeML>");
            output = output.ReplaceAll("<TimeML>", string.Empty);
            // parse the HeidelTime output
            IElement outputXML;

            try
            {
                outputXML = XMLUtils.ParseElement(output);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("error:\n%s\ninput:\n%s\noutput:\n%s", ex, IOUtils.SlurpFile(inputFile), output), ex);
            }
            inputFile.Delete();
            // get Timex annotations
            IList <ICoreMap> timexAnns = ToTimexCoreMaps(outputXML, document);

            document.Set(typeof(TimeAnnotations.TimexAnnotations), timexAnns);
            if (outputResults)
            {
                System.Console.Out.WriteLine(timexAnns);
            }
            // align Timex annotations to sentences
            int timexIndex = 0;

            foreach (ICoreMap sentence in document.Get(typeof(CoreAnnotations.SentencesAnnotation)))
            {
                int sentBegin = BeginOffset(sentence);
                int sentEnd   = EndOffset(sentence);
                // skip times before the sentence
                while (timexIndex < timexAnns.Count && BeginOffset(timexAnns[timexIndex]) < sentBegin)
                {
                    ++timexIndex;
                }
                // determine times within the sentence
                int sublistBegin = timexIndex;
                int sublistEnd   = timexIndex;
                while (timexIndex < timexAnns.Count && sentBegin <= BeginOffset(timexAnns[timexIndex]) && EndOffset(timexAnns[timexIndex]) <= sentEnd)
                {
                    ++sublistEnd;
                    ++timexIndex;
                }
                // set the sentence timexes
                sentence.Set(typeof(TimeAnnotations.TimexAnnotations), timexAnns.SubList(sublistBegin, sublistEnd));
            }
        }
        /// <exception cref="System.IO.IOException"/>
        public virtual void Annotate(ICoreMap document)
        {
            // write input file in GUTime format
            IElement inputXML  = ToInputXML(document);
            File     inputFile = File.CreateTempFile("gutime", ".input");
            //Document doc = new Document(inputXML);
            PrintWriter inputWriter = new PrintWriter(inputFile);

            inputWriter.Println(XMLUtils.NodeToString(inputXML, false));
            // new XMLOutputter().output(inputXML, inputWriter);
            inputWriter.Close();
            bool          useFirstDate = (!document.ContainsKey(typeof(CoreAnnotations.CalendarAnnotation)) && !document.ContainsKey(typeof(CoreAnnotations.DocDateAnnotation)));
            List <string> args         = new List <string>();

            args.Add("perl");
            args.Add("-I" + this.gutimePath.GetPath());
            args.Add(new File(this.gutimePath, "TimeTag.pl").GetPath());
            if (useFirstDate)
            {
                args.Add("-FDNW");
            }
            args.Add(inputFile.GetPath());
            // run GUTime on the input file
            ProcessBuilder process      = new ProcessBuilder(args);
            StringWriter   outputWriter = new StringWriter();

            SystemUtils.Run(process, outputWriter, null);
            string  output   = outputWriter.GetBuffer().ToString();
            Pattern docClose = Pattern.Compile("</DOC>.*", Pattern.Dotall);

            output = docClose.Matcher(output).ReplaceAll("</DOC>");
            //The TimeTag.pl result file contains next tags which must be removed
            output = output.ReplaceAll("<lex.*?>", string.Empty);
            output = output.Replace("</lex>", string.Empty);
            output = output.Replace("<NG>", string.Empty);
            output = output.Replace("</NG>", string.Empty);
            output = output.Replace("<VG>", string.Empty);
            output = output.Replace("</VG>", string.Empty);
            output = output.Replace("<s>", string.Empty);
            output = output.Replace("</s>", string.Empty);
            // parse the GUTime output
            IElement outputXML;

            try
            {
                outputXML = XMLUtils.ParseElement(output);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("error:\n%s\ninput:\n%s\noutput:\n%s", ex, IOUtils.SlurpFile(inputFile), output), ex);
            }

            /*
             * try {
             * outputXML = new SAXBuilder().build(new StringReader(output)).getRootElement();
             * } catch (JDOMException e) {
             * throw new RuntimeException(String.format("error:\n%s\ninput:\n%s\noutput:\n%s",
             * e, IOUtils.slurpFile(inputFile), output));
             * } */
            inputFile.Delete();
            // get Timex annotations
            IList <ICoreMap> timexAnns = ToTimexCoreMaps(outputXML, document);

            document.Set(typeof(TimeAnnotations.TimexAnnotations), timexAnns);
            if (outputResults)
            {
                System.Console.Out.WriteLine(timexAnns);
            }
            // align Timex annotations to sentences
            int timexIndex = 0;

            foreach (ICoreMap sentence in document.Get(typeof(CoreAnnotations.SentencesAnnotation)))
            {
                int sentBegin = BeginOffset(sentence);
                int sentEnd   = EndOffset(sentence);
                // skip times before the sentence
                while (timexIndex < timexAnns.Count && BeginOffset(timexAnns[timexIndex]) < sentBegin)
                {
                    ++timexIndex;
                }
                // determine times within the sentence
                int sublistBegin = timexIndex;
                int sublistEnd   = timexIndex;
                while (timexIndex < timexAnns.Count && sentBegin <= BeginOffset(timexAnns[timexIndex]) && EndOffset(timexAnns[timexIndex]) <= sentEnd)
                {
                    ++sublistEnd;
                    ++timexIndex;
                }
                // set the sentence timexes
                sentence.Set(typeof(TimeAnnotations.TimexAnnotations), timexAnns.SubList(sublistBegin, sublistEnd));
            }
        }
        public virtual IList <TimeExpression> ExtractTimeExpressions(ICoreMap annotation, SUTime.Time refDate, SUTime.TimeIndex timeIndex)
        {
            if (!annotation.ContainsKey(typeof(CoreAnnotations.NumerizedTokensAnnotation)))
            {
                try
                {
                    IList <ICoreMap> mergedNumbers = NumberNormalizer.FindAndMergeNumbers(annotation);
                    annotation.Set(typeof(CoreAnnotations.NumerizedTokensAnnotation), mergedNumbers);
                }
                catch (NumberFormatException e)
                {
                    logger.Warn("Caught bad number: " + e.Message);
                    annotation.Set(typeof(CoreAnnotations.NumerizedTokensAnnotation), new List <ICoreMap>());
                }
            }
            IList <MatchedExpression> matchedExpressions = expressionExtractor.ExtractExpressions(annotation);
            IList <TimeExpression>    timeExpressions    = new List <TimeExpression>(matchedExpressions.Count);

            foreach (MatchedExpression expr in matchedExpressions)
            {
                // Make sure we have the correct type (instead of just MatchedExpression)
                //timeExpressions.add(TimeExpression.TimeExpressionConverter.apply(expr));
                // TODO: Fix the extraction pipeline so it creates TimeExpression instead of MatchedExpressions
                // For now, grab the time expression from the annotation (this is good, so we don't have duplicate copies)
                TimeExpression annoTe = expr.GetAnnotation().Get(typeof(TimeExpression.Annotation));
                if (annoTe != null)
                {
                    timeExpressions.Add(annoTe);
                }
            }
            // We cache the document date in the timeIndex
            if (timeIndex.docDate == null)
            {
                if (refDate != null)
                {
                    timeIndex.docDate = refDate;
                }
                else
                {
                    if (options.searchForDocDate)
                    {
                        // there was no document date but option was set to look for document date
                        timeIndex.docDate = FindReferenceDate(timeExpressions);
                    }
                }
            }
            // Didn't have a reference date - try using cached doc date
            if (refDate == null)
            {
                refDate = timeIndex.docDate;
            }
            // Some resolving is done even if refDate null...
            ResolveTimeExpressions(annotation, timeExpressions, refDate);
            if (options.restrictToTimex3)
            {
                // Keep only TIMEX3 compatible timeExpressions
                IList <TimeExpression> kept = new List <TimeExpression>(timeExpressions.Count);
                foreach (TimeExpression te in timeExpressions)
                {
                    if (te.GetTemporal() != null && te.GetTemporal().GetTimexValue() != null)
                    {
                        kept.Add(te);
                    }
                    else
                    {
                        IList <ICoreMap> children = te.GetAnnotation().Get(typeof(TimeExpression.ChildrenAnnotation));
                        if (children != null)
                        {
                            foreach (ICoreMap child in children)
                            {
                                TimeExpression childTe = child.Get(typeof(TimeExpression.Annotation));
                                if (childTe != null)
                                {
                                    ResolveTimeExpression(annotation, childTe, refDate);
                                    if (childTe.GetTemporal() != null && childTe.GetTemporal().GetTimexValue() != null)
                                    {
                                        kept.Add(childTe);
                                    }
                                }
                            }
                        }
                    }
                }
                timeExpressions = kept;
            }
            // Add back nested time expressions for ranges....
            // For now only one level of nesting...
            if (options.includeNested)
            {
                IList <TimeExpression> nestedTimeExpressions = new List <TimeExpression>();
                foreach (TimeExpression te in timeExpressions)
                {
                    if (te.IsIncludeNested())
                    {
                        IList <ICoreMap> children = te.GetAnnotation().Get(typeof(TimeExpression.ChildrenAnnotation));
                        if (children != null)
                        {
                            foreach (ICoreMap child in children)
                            {
                                TimeExpression childTe = child.Get(typeof(TimeExpression.Annotation));
                                if (childTe != null)
                                {
                                    nestedTimeExpressions.Add(childTe);
                                }
                            }
                        }
                    }
                }
                ResolveTimeExpressions(annotation, nestedTimeExpressions, refDate);
                Sharpen.Collections.AddAll(timeExpressions, nestedTimeExpressions);
            }
            timeExpressions.Sort(MatchedExpression.ExprTokenOffsetsNestedFirstComparator);
            // Some resolving is done even if refDate null...
            ResolveTimeExpressions(annotation, timeExpressions, refDate);
            return(timeExpressions);
        }
        /// <summary>Annotate every token for its polarity, based on the operators found.</summary>
        /// <remarks>
        /// Annotate every token for its polarity, based on the operators found. This function will set the
        /// <see cref="PolarityAnnotation"/>
        /// for every token.
        /// </remarks>
        /// <param name="sentence">
        /// As in
        /// <see cref="DoOneSentence(Edu.Stanford.Nlp.Pipeline.Annotation, Edu.Stanford.Nlp.Util.ICoreMap)"/>
        /// </param>
        private static void AnnotatePolarity(ICoreMap sentence)
        {
            // Collect all the operators in this sentence
            IList <OperatorSpec> operators = new List <OperatorSpec>();
            IList <CoreLabel>    tokens    = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));

            foreach (CoreLabel token in tokens)
            {
                OperatorSpec specOrNull = token.Get(typeof(NaturalLogicAnnotations.OperatorAnnotation));
                if (specOrNull != null)
                {
                    operators.Add(specOrNull);
                }
            }
            // Make sure every node of the dependency tree has a polarity.
            // This is separate from the code below in case the tokens in the dependency
            // tree don't correspond to the tokens in the sentence. This happens at least
            // when the constituency parser craps out on a long sentence, and the
            // dependency tree is put together haphazardly.
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            // Set polarity for each token
            for (int i = 0; i < tokens.Count; ++i)
            {
                CoreLabel token_1 = tokens[i];
                // Get operators in scope
                IList <Triple <int, Monotonicity, MonotonicityType> > inScope = new List <Triple <int, Monotonicity, MonotonicityType> >(4);
                foreach (OperatorSpec @operator in operators)
                {
                    if (i >= @operator.subjectBegin && i < @operator.subjectEnd)
                    {
                        inScope.Add(Triple.MakeTriple(@operator.subjectEnd - @operator.subjectBegin, @operator.instance.subjMono, @operator.instance.subjType));
                    }
                    else
                    {
                        if (i >= @operator.objectBegin && i < @operator.objectEnd)
                        {
                            inScope.Add(Triple.MakeTriple(@operator.objectEnd - @operator.objectBegin, @operator.instance.objMono, @operator.instance.objType));
                        }
                    }
                }
                // Sort the operators by their scope (approximated by the size of their argument span)
                inScope.Sort(null);
                // Create polarity
                IList <Pair <Monotonicity, MonotonicityType> > info = new List <Pair <Monotonicity, MonotonicityType> >(inScope.Count);
                foreach (Triple <int, Monotonicity, MonotonicityType> term in inScope)
                {
                    info.Add(Pair.MakePair(term.second, term.third));
                }
                Polarity polarity = new Polarity(info);
                // Set polarity
                token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), polarity);
            }
            // Set the PolarityDirectionAnnotation
            foreach (CoreLabel token_2 in tokens)
            {
                Polarity polarity = token_2.Get(typeof(NaturalLogicAnnotations.PolarityAnnotation));
                if (polarity != null)
                {
                    if (polarity.IsUpwards())
                    {
                        token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "up");
                    }
                    else
                    {
                        if (polarity.IsDownwards())
                        {
                            token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "down");
                        }
                        else
                        {
                            token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "flat");
                        }
                    }
                }
            }
        }
 private static void AddWordInfo(Element wordInfo, ICoreMap token, int id, string curNS)
 {
     // store the position of this word in the sentence
     wordInfo.AddAttribute(new Attribute("id", int.ToString(id)));
     SetSingleElement(wordInfo, "word", curNS, token.Get(typeof(CoreAnnotations.TextAnnotation)));
     SetSingleElement(wordInfo, "lemma", curNS, token.Get(typeof(CoreAnnotations.LemmaAnnotation)));
     if (token.ContainsKey(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation)) && token.ContainsKey(typeof(CoreAnnotations.CharacterOffsetEndAnnotation)))
     {
         SetSingleElement(wordInfo, "CharacterOffsetBegin", curNS, int.ToString(token.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation))));
         SetSingleElement(wordInfo, "CharacterOffsetEnd", curNS, int.ToString(token.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation))));
     }
     if (token.ContainsKey(typeof(CoreAnnotations.PartOfSpeechAnnotation)))
     {
         SetSingleElement(wordInfo, "POS", curNS, token.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)));
     }
     if (token.ContainsKey(typeof(CoreAnnotations.NamedEntityTagAnnotation)))
     {
         SetSingleElement(wordInfo, "NER", curNS, token.Get(typeof(CoreAnnotations.NamedEntityTagAnnotation)));
     }
     if (token.ContainsKey(typeof(CoreAnnotations.NormalizedNamedEntityTagAnnotation)))
     {
         SetSingleElement(wordInfo, "NormalizedNER", curNS, token.Get(typeof(CoreAnnotations.NormalizedNamedEntityTagAnnotation)));
     }
     if (token.ContainsKey(typeof(CoreAnnotations.SpeakerAnnotation)))
     {
         SetSingleElement(wordInfo, "Speaker", curNS, token.Get(typeof(CoreAnnotations.SpeakerAnnotation)));
     }
     if (token.ContainsKey(typeof(TimeAnnotations.TimexAnnotation)))
     {
         Timex   timex     = token.Get(typeof(TimeAnnotations.TimexAnnotation));
         Element timexElem = new Element("Timex", curNS);
         timexElem.AddAttribute(new Attribute("tid", timex.Tid()));
         timexElem.AddAttribute(new Attribute("type", timex.TimexType()));
         timexElem.AppendChild(timex.Value());
         wordInfo.AppendChild(timexElem);
     }
     if (token.ContainsKey(typeof(CoreAnnotations.TrueCaseAnnotation)))
     {
         Element cur = new Element("TrueCase", curNS);
         cur.AppendChild(token.Get(typeof(CoreAnnotations.TrueCaseAnnotation)));
         wordInfo.AppendChild(cur);
     }
     if (token.ContainsKey(typeof(CoreAnnotations.TrueCaseTextAnnotation)))
     {
         Element cur = new Element("TrueCaseText", curNS);
         cur.AppendChild(token.Get(typeof(CoreAnnotations.TrueCaseTextAnnotation)));
         wordInfo.AppendChild(cur);
     }
     if (token.ContainsKey(typeof(SentimentCoreAnnotations.SentimentClass)))
     {
         Element cur = new Element("sentiment", curNS);
         cur.AppendChild(token.Get(typeof(SentimentCoreAnnotations.SentimentClass)));
         wordInfo.AppendChild(cur);
     }
     if (token.ContainsKey(typeof(CoreAnnotations.WikipediaEntityAnnotation)))
     {
         Element cur = new Element("entitylink", curNS);
         cur.AppendChild(token.Get(typeof(CoreAnnotations.WikipediaEntityAnnotation)));
         wordInfo.AppendChild(cur);
     }
 }