private static void AddCorefMention(AnnotationOutputter.Options options, Element chainElem, string curNS, IList <ICoreMap> sentences, CorefChain.CorefMention mention, bool representative)
        {
            Element mentionElem = new Element("mention", curNS);

            if (representative)
            {
                mentionElem.AddAttribute(new Attribute("representative", "true"));
            }
            SetSingleElement(mentionElem, "sentence", curNS, int.ToString(mention.sentNum));
            SetSingleElement(mentionElem, "start", curNS, int.ToString(mention.startIndex));
            SetSingleElement(mentionElem, "end", curNS, int.ToString(mention.endIndex));
            SetSingleElement(mentionElem, "head", curNS, int.ToString(mention.headIndex));
            string text = mention.mentionSpan;

            SetSingleElement(mentionElem, "text", curNS, text);
            // Do you want context with your coreference?
            if (sentences != null && options.coreferenceContextSize > 0)
            {
                // If so use sentences to get so context from sentences
                IList <CoreLabel> tokens = sentences[mention.sentNum - 1].Get(typeof(CoreAnnotations.TokensAnnotation));
                int    contextStart      = Math.Max(mention.startIndex - 1 - 5, 0);
                int    contextEnd        = Math.Min(mention.endIndex - 1 + 5, tokens.Count);
                string leftContext       = StringUtils.JoinWords(tokens, " ", contextStart, mention.startIndex - 1);
                string rightContext      = StringUtils.JoinWords(tokens, " ", mention.endIndex - 1, contextEnd);
                SetSingleElement(mentionElem, "leftContext", curNS, leftContext);
                SetSingleElement(mentionElem, "rightContext", curNS, rightContext);
            }
            chainElem.AppendChild(mentionElem);
        }
        /// <summary>Generates the XML content for the coreference chain object.</summary>
        private static bool AddCorefGraphInfo(AnnotationOutputter.Options options, Element corefInfo, IList <ICoreMap> sentences, IDictionary <int, CorefChain> corefChains, string curNS)
        {
            bool foundCoref = false;

            foreach (CorefChain chain in corefChains.Values)
            {
                if (!options.printSingletons && chain.GetMentionsInTextualOrder().Count <= 1)
                {
                    continue;
                }
                foundCoref = true;
                Element chainElem = new Element("coreference", curNS);
                CorefChain.CorefMention source = chain.GetRepresentativeMention();
                AddCorefMention(options, chainElem, curNS, sentences, source, true);
                foreach (CorefChain.CorefMention mention in chain.GetMentionsInTextualOrder())
                {
                    if (mention == source)
                    {
                        continue;
                    }
                    AddCorefMention(options, chainElem, curNS, sentences, mention, false);
                }
                corefInfo.AppendChild(chainElem);
            }
            return(foundCoref);
        }
        /// <exception cref="System.IO.IOException"/>
        private static void Print(Annotation annotation, PrintWriter pw, AnnotationOutputter.Options options)
        {
            IList <ICoreMap> sentences = annotation.Get(typeof(CoreAnnotations.SentencesAnnotation));

            if (sentences != null)
            {
                for (int i = 0; i < sentences.Count; i++)
                {
                    ICoreMap      sentence        = sentences[i];
                    StringBuilder sentenceToWrite = new StringBuilder();
                    foreach (CoreLabel token in sentence.Get(typeof(CoreAnnotations.TokensAnnotation)))
                    {
                        sentenceToWrite.Append(" ");
                        sentenceToWrite.Append(token.Lemma().ToLower());
                        if (token.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).StartsWith("V"))
                        {
                            //verb
                            sentenceToWrite.Append("_V");
                        }
                        else
                        {
                            if (token.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).StartsWith("N"))
                            {
                                //noun
                                sentenceToWrite.Append("_N");
                            }
                        }
                    }
                    pw.Print(sentenceToWrite.ToString());
                }
            }
        }
        /// <exception cref="System.IO.IOException"/>
        public virtual string Print(Annotation ann, AnnotationOutputter.Options options)
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            Print(ann, os, options);
            os.Close();
            return(Sharpen.Runtime.GetStringForBytes(os.ToByteArray()));
        }
Example #5
0
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter writer = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));

            JSONOutputter.JSONWriter l0 = new JSONOutputter.JSONWriter(writer, options);
            if (doc.Get(typeof(CoreAnnotations.SentencesAnnotation)) != null)
            {
                doc.Get(typeof(CoreAnnotations.SentencesAnnotation)).Stream().ForEach(null);
            }
        }
Example #6
0
        /// <summary>Print an Annotation to an output stream.</summary>
        /// <remarks>
        /// Print an Annotation to an output stream.
        /// The target OutputStream is assumed to already by buffered.
        /// </remarks>
        /// <param name="doc"/>
        /// <param name="target"/>
        /// <param name="options"/>
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter writer = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));

            // vv A bunch of nonsense to get tokens vv
            if (doc.Get(typeof(CoreAnnotations.SentencesAnnotation)) != null)
            {
                foreach (ICoreMap sentence in doc.Get(typeof(CoreAnnotations.SentencesAnnotation)))
                {
                    if (sentence.Get(typeof(CoreAnnotations.TokensAnnotation)) != null)
                    {
                        IList <CoreLabel> tokens  = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));
                        SemanticGraph     depTree = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                        for (int i = 0; i < tokens.Count; ++i)
                        {
                            // ^^ end nonsense to get tokens ^^
                            // Try to get the incoming dependency edge
                            int    head   = -1;
                            string deprel = null;
                            if (depTree != null)
                            {
                                ICollection <int> rootSet = depTree.GetRoots().Stream().Map(null).Collect(Collectors.ToSet());
                                IndexedWord       node    = depTree.GetNodeByIndexSafe(i + 1);
                                if (node != null)
                                {
                                    IList <SemanticGraphEdge> edgeList = depTree.GetIncomingEdgesSorted(node);
                                    if (!edgeList.IsEmpty())
                                    {
                                        System.Diagnostics.Debug.Assert(edgeList.Count == 1);
                                        head   = edgeList[0].GetGovernor().Index();
                                        deprel = edgeList[0].GetRelation().ToString();
                                    }
                                    else
                                    {
                                        if (rootSet.Contains(i + 1))
                                        {
                                            head   = 0;
                                            deprel = "ROOT";
                                        }
                                    }
                                }
                            }
                            // Write the token
                            writer.Print(Line(i + 1, tokens[i], head, deprel));
                            writer.Println();
                        }
                    }
                    writer.Println();
                }
            }
            // extra blank line at end of sentence
            writer.Flush();
        }
 /// <summary>Populates options from StanfordCoreNLP pipeline.</summary>
 public static AnnotationOutputter.Options GetOptions(StanfordCoreNLP pipeline)
 {
     AnnotationOutputter.Options options = new AnnotationOutputter.Options();
     options.relationsBeam          = pipeline.GetBeamPrintingOption();
     options.constituentTreePrinter = pipeline.GetConstituentTreePrinter();
     options.encoding           = pipeline.GetEncoding();
     options.printSingletons    = pipeline.GetPrintSingletons();
     options.beamPrintingOption = pipeline.GetBeamPrintingOption();
     options.pretty             = pipeline.GetPrettyPrint();
     options.includeText        = pipeline.GetIncludeText();
     return(options);
 }
        // the namespace is set in the XSLT file
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation annotation, OutputStream os, AnnotationOutputter.Options options)
        {
            Document   xmlDoc = AnnotationToDoc(annotation, options);
            Serializer ser    = new Serializer(os, options.encoding);

            if (options.pretty)
            {
                ser.SetIndent(2);
            }
            else
            {
                ser.SetIndent(0);
            }
            ser.SetMaxLength(0);
            ser.Write(xmlDoc);
            ser.Flush();
        }
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter      writer    = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));
            IList <ICoreMap> sentences = doc.Get(typeof(CoreAnnotations.SentencesAnnotation));

            foreach (ICoreMap sentence in sentences)
            {
                SemanticGraph sg = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                if (sg != null)
                {
                    writer.Print(conllUWriter.PrintSemanticGraph(sg));
                }
                else
                {
                    writer.Print(conllUWriter.PrintPOSAnnotations(sentence));
                }
            }
            writer.Flush();
        }
Example #10
0
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            // It's lying; we need the "redundant" casts (as of 2014-09-08)
            PrintWriter writer = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));

            JSONOutputter.JSONWriter l0 = new JSONOutputter.JSONWriter(writer, options);
            l0.Object(null);
            // Add annotations attached to a Document
            // Add sentences
            // Add a single sentence
            // (metadata)
            // (constituency tree)
            // note the '==' -- we're overwriting the default, but only if it was not explicitly set otherwise
            // strip the trailing newline
            // (dependency trees)
            // (sentiment)
            // (openie)
            // (kbp)
            // (entity mentions)
            //l3.set("originalText", m.get(CoreAnnotations.OriginalTextAnnotation.class));
            //l3.set("lemma", m.get(CoreAnnotations.LemmaAnnotation.class));
            //l3.set("pos", m.get(CoreAnnotations.PartOfSpeechAnnotation.class));
            // Timex
            // (add tokens)
            // Add a single token
            // Timex
            // Add coref values
            // quotes
            // sections
            // Set char start
            // Set char end
            // Set author
            // Set date time
            // add the sentence indexes for the sentences in this section
            l0.Flush();
        }
Example #11
0
 public JSONWriter(PrintWriter writer, AnnotationOutputter.Options options)
 {
     this.writer  = writer;
     this.options = options;
 }
Example #12
0
 /// <exception cref="System.IO.IOException"/>
 public static void JsonPrint(Annotation annotation, OutputStream os, AnnotationOutputter.Options options)
 {
     new JSONOutputter().Print(annotation, os, options);
 }
        /// <summary>The meat of the outputter</summary>
        /// <exception cref="System.IO.IOException"/>
        private static void Print(Annotation annotation, PrintWriter pw, AnnotationOutputter.Options options)
        {
            double           beam      = options.beamPrintingOption;
            IList <ICoreMap> sentences = annotation.Get(typeof(CoreAnnotations.SentencesAnnotation));
            // Display docid if available
            string docId = annotation.Get(typeof(CoreAnnotations.DocIDAnnotation));

            if (docId != null)
            {
                IList <CoreLabel> tokens = annotation.Get(typeof(CoreAnnotations.TokensAnnotation));
                int nSentences           = (sentences != null) ? sentences.Count : 0;
                int nTokens = (tokens != null) ? tokens.Count : 0;
                pw.Printf("Document: ID=%s (%d sentences, %d tokens)%n", docId, nSentences, nTokens);
            }
            // Display doctitle if available
            string docTitle = annotation.Get(typeof(CoreAnnotations.DocTitleAnnotation));

            if (docTitle != null)
            {
                pw.Printf("Document Title: %s%n", docTitle);
            }
            // Display docdate if available
            string docDate = annotation.Get(typeof(CoreAnnotations.DocDateAnnotation));

            if (docDate != null)
            {
                pw.Printf("Document Date: %s%n", docDate);
            }
            // Display doctype if available
            string docType = annotation.Get(typeof(CoreAnnotations.DocTypeAnnotation));

            if (docType != null)
            {
                pw.Printf("Document Type: %s%n", docType);
            }
            // Display docsourcetype if available
            string docSourceType = annotation.Get(typeof(CoreAnnotations.DocSourceTypeAnnotation));

            if (docSourceType != null)
            {
                pw.Printf("Document Source Type: %s%n", docSourceType);
            }
            // display each sentence in this annotation
            if (sentences != null)
            {
                for (int i = 0; i < sz; i++)
                {
                    pw.Println();
                    ICoreMap          sentence  = sentences[i];
                    IList <CoreLabel> tokens    = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));
                    string            sentiment = sentence.Get(typeof(SentimentCoreAnnotations.SentimentClass));
                    string            piece;
                    if (sentiment == null)
                    {
                        piece = string.Empty;
                    }
                    else
                    {
                        piece = ", sentiment: " + sentiment;
                    }
                    pw.Printf("Sentence #%d (%d tokens%s):%n", (i + 1), tokens.Count, piece);
                    string text = sentence.Get(typeof(CoreAnnotations.TextAnnotation));
                    pw.Println(text);
                    // display the token-level annotations
                    string[] tokenAnnotations = new string[] { "Text", "PartOfSpeech", "Lemma", "Answer", "NamedEntityTag", "CharacterOffsetBegin", "CharacterOffsetEnd", "NormalizedNamedEntityTag", "Timex", "TrueCase", "TrueCaseText", "SentimentClass", "WikipediaEntity" };
                    pw.Println();
                    pw.Println("Tokens:");
                    foreach (CoreLabel token in tokens)
                    {
                        pw.Print(token.ToShorterString(tokenAnnotations));
                        pw.Println();
                    }
                    // display the parse tree for this sentence
                    Tree tree = sentence.Get(typeof(TreeCoreAnnotations.TreeAnnotation));
                    if (tree != null)
                    {
                        pw.Println();
                        pw.Println("Constituency parse: ");
                        options.constituentTreePrinter.PrintTree(tree, pw);
                    }
                    // display sentiment tree if they asked for sentiment
                    if (!StringUtils.IsNullOrEmpty(sentiment))
                    {
                        pw.Println();
                        pw.Println("Sentiment-annotated binary tree:");
                        Tree sTree = sentence.Get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));
                        if (sTree != null)
                        {
                            sTree.PennPrint(pw, null);
                            pw.Println();
                        }
                    }
                    // It is possible to turn off the semantic graphs, in which
                    // case we don't want to recreate them using the dependency
                    // printer.  This might be relevant if using CoreNLP for a
                    // language which doesn't have dependencies, for example.
                    if (sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)) != null)
                    {
                        pw.Println();
                        pw.Println("Dependency Parse (enhanced plus plus dependencies):");
                        pw.Print(sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)).ToList());
                    }
                    // display the entity mentions
                    IList <ICoreMap> entityMentions = sentence.Get(typeof(CoreAnnotations.MentionsAnnotation));
                    if (entityMentions != null)
                    {
                        pw.Println();
                        pw.Println("Extracted the following NER entity mentions:");
                        foreach (ICoreMap entityMention in entityMentions)
                        {
                            if (entityMention.Get(typeof(CoreAnnotations.EntityTypeAnnotation)) != null)
                            {
                                pw.Println(entityMention.Get(typeof(CoreAnnotations.TextAnnotation)) + "\t" + entityMention.Get(typeof(CoreAnnotations.EntityTypeAnnotation)));
                            }
                        }
                    }
                    // display MachineReading entities and relations
                    IList <EntityMention> entities = sentence.Get(typeof(MachineReadingAnnotations.EntityMentionsAnnotation));
                    if (entities != null)
                    {
                        pw.Println();
                        pw.Println("Extracted the following MachineReading entity mentions:");
                        foreach (EntityMention e in entities)
                        {
                            pw.Print('\t');
                            pw.Println(e);
                        }
                    }
                    IList <RelationMention> relations = sentence.Get(typeof(MachineReadingAnnotations.RelationMentionsAnnotation));
                    if (relations != null)
                    {
                        pw.Println();
                        pw.Println("Extracted the following MachineReading relation mentions:");
                        foreach (RelationMention r in relations)
                        {
                            if (r.PrintableObject(beam))
                            {
                                pw.Println(r);
                            }
                        }
                    }
                    // display OpenIE triples
                    ICollection <RelationTriple> openieTriples = sentence.Get(typeof(NaturalLogicAnnotations.RelationTriplesAnnotation));
                    if (openieTriples != null && !openieTriples.IsEmpty())
                    {
                        pw.Println();
                        pw.Println("Extracted the following Open IE triples:");
                        foreach (RelationTriple triple in openieTriples)
                        {
                            pw.Println(OpenIE.TripleToString(triple, docId, sentence));
                        }
                    }
                    // display KBP triples
                    ICollection <RelationTriple> kbpTriples = sentence.Get(typeof(CoreAnnotations.KBPTriplesAnnotation));
                    if (kbpTriples != null && !kbpTriples.IsEmpty())
                    {
                        pw.Println();
                        pw.Println("Extracted the following KBP triples:");
                        foreach (RelationTriple triple in kbpTriples)
                        {
                            pw.Println(triple);
                        }
                    }
                }
            }
            else
            {
                IList <CoreLabel> tokens = annotation.Get(typeof(CoreAnnotations.TokensAnnotation));
                pw.Println("Tokens:");
                pw.Println(annotation.Get(typeof(CoreAnnotations.TextAnnotation)));
                foreach (CoreLabel token in tokens)
                {
                    int tokenCharBegin = token.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation));
                    int tokenCharEnd   = token.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation));
                    pw.Println("[Text=" + token.Word() + " CharacterOffsetBegin=" + tokenCharBegin + " CharacterOffsetEnd=" + tokenCharEnd + ']');
                }
            }
            // display the old-style doc-level coref annotations
            // this is not supported anymore!
            //String corefAnno = annotation.get(CorefPLAnnotation.class);
            //if(corefAnno != null) os.println(corefAnno);
            // display the new-style coreference graph
            IDictionary <int, CorefChain> corefChains = annotation.Get(typeof(CorefCoreAnnotations.CorefChainAnnotation));

            if (corefChains != null && sentences != null)
            {
                foreach (CorefChain chain in corefChains.Values)
                {
                    CorefChain.CorefMention representative = chain.GetRepresentativeMention();
                    bool outputHeading = false;
                    foreach (CorefChain.CorefMention mention in chain.GetMentionsInTextualOrder())
                    {
                        if (mention == representative)
                        {
                            continue;
                        }
                        if (!outputHeading)
                        {
                            outputHeading = true;
                            pw.Println();
                            pw.Println("Coreference set:");
                        }
                        // all offsets start at 1!
                        pw.Printf("\t(%d,%d,[%d,%d]) -> (%d,%d,[%d,%d]), that is: \"%s\" -> \"%s\"%n", mention.sentNum, mention.headIndex, mention.startIndex, mention.endIndex, representative.sentNum, representative.headIndex, representative.startIndex, representative
                                  .endIndex, mention.mentionSpan, representative.mentionSpan);
                    }
                }
            }
            // display quotes if available
            if (annotation.Get(typeof(CoreAnnotations.QuotationsAnnotation)) != null)
            {
                pw.Println();
                pw.Println("Extracted quotes: ");
                IList <ICoreMap> allQuotes = QuoteAnnotator.GatherQuotes(annotation);
                foreach (ICoreMap quote in allQuotes)
                {
                    string speakerString;
                    if (quote.Get(typeof(QuoteAttributionAnnotator.CanonicalMentionAnnotation)) != null)
                    {
                        speakerString = quote.Get(typeof(QuoteAttributionAnnotator.CanonicalMentionAnnotation));
                    }
                    else
                    {
                        if (quote.Get(typeof(QuoteAttributionAnnotator.SpeakerAnnotation)) != null)
                        {
                            speakerString = quote.Get(typeof(QuoteAttributionAnnotator.SpeakerAnnotation));
                        }
                        else
                        {
                            speakerString = "Unknown";
                        }
                    }
                    pw.Printf("[QuotationIndex=%d, CharacterOffsetBegin=%d, Text=%s, Speaker=%s]%n", quote.Get(typeof(CoreAnnotations.QuotationIndexAnnotation)), quote.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation)), quote.Get(typeof(CoreAnnotations.TextAnnotation
                                                                                                                                                                                                                                                      )), speakerString);
                }
            }
            pw.Flush();
        }
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation annotation, OutputStream stream, AnnotationOutputter.Options options)
        {
            PrintWriter os = new PrintWriter(IOUtils.EncodedOutputStreamWriter(stream, options.encoding));

            Print(annotation, os, options);
        }
 /// <exception cref="System.IO.IOException"/>
 public abstract void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options);
 /// <exception cref="System.IO.IOException"/>
 public static void XmlPrint(Annotation annotation, OutputStream os, AnnotationOutputter.Options options)
 {
     new Edu.Stanford.Nlp.Pipeline.XMLOutputter().Print(annotation, os, options);
 }
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter os = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));

            Print(doc, os, options);
        }
        /// <summary>Converts the given annotation to an XML document using the specified options</summary>
        public static Document AnnotationToDoc(Annotation annotation, AnnotationOutputter.Options options)
        {
            //
            // create the XML document with the root node pointing to the namespace URL
            //
            Element  root            = new Element("root", NamespaceUri);
            Document xmlDoc          = new Document(root);
            ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", "href=\"" + StylesheetName + "\" type=\"text/xsl\"");

            xmlDoc.InsertChild(pi, 0);
            Element docElem = new Element("document", NamespaceUri);

            root.AppendChild(docElem);
            SetSingleElement(docElem, "docId", NamespaceUri, annotation.Get(typeof(CoreAnnotations.DocIDAnnotation)));
            SetSingleElement(docElem, "docDate", NamespaceUri, annotation.Get(typeof(CoreAnnotations.DocDateAnnotation)));
            SetSingleElement(docElem, "docSourceType", NamespaceUri, annotation.Get(typeof(CoreAnnotations.DocSourceTypeAnnotation)));
            SetSingleElement(docElem, "docType", NamespaceUri, annotation.Get(typeof(CoreAnnotations.DocTypeAnnotation)));
            SetSingleElement(docElem, "author", NamespaceUri, annotation.Get(typeof(CoreAnnotations.AuthorAnnotation)));
            SetSingleElement(docElem, "location", NamespaceUri, annotation.Get(typeof(CoreAnnotations.LocationAnnotation)));
            if (options.includeText)
            {
                SetSingleElement(docElem, "text", NamespaceUri, annotation.Get(typeof(CoreAnnotations.TextAnnotation)));
            }
            Element sentencesElem = new Element("sentences", NamespaceUri);

            docElem.AppendChild(sentencesElem);
            //
            // save the info for each sentence in this doc
            //
            if (annotation.Get(typeof(CoreAnnotations.SentencesAnnotation)) != null)
            {
                int sentCount = 1;
                foreach (ICoreMap sentence in annotation.Get(typeof(CoreAnnotations.SentencesAnnotation)))
                {
                    Element sentElem = new Element("sentence", NamespaceUri);
                    sentElem.AddAttribute(new Attribute("id", int.ToString(sentCount)));
                    int lineNumber = sentence.Get(typeof(CoreAnnotations.LineNumberAnnotation));
                    if (lineNumber != null)
                    {
                        sentElem.AddAttribute(new Attribute("line", int.ToString(lineNumber)));
                    }
                    sentCount++;
                    // add the word table with all token-level annotations
                    Element           wordTable = new Element("tokens", NamespaceUri);
                    IList <CoreLabel> tokens    = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));
                    for (int j = 0; j < tokens.Count; j++)
                    {
                        Element wordInfo = new Element("token", NamespaceUri);
                        AddWordInfo(wordInfo, tokens[j], j + 1, NamespaceUri);
                        wordTable.AppendChild(wordInfo);
                    }
                    sentElem.AppendChild(wordTable);
                    // add tree info
                    Tree tree = sentence.Get(typeof(TreeCoreAnnotations.TreeAnnotation));
                    if (tree != null)
                    {
                        // add the constituent tree for this sentence
                        Element parseInfo = new Element("parse", NamespaceUri);
                        AddConstituentTreeInfo(parseInfo, tree, options.constituentTreePrinter);
                        sentElem.AppendChild(parseInfo);
                    }
                    SemanticGraph basicDependencies = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                    if (basicDependencies != null)
                    {
                        // add the dependencies for this sentence
                        Element depInfo = BuildDependencyTreeInfo("basic-dependencies", sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)), tokens, NamespaceUri);
                        if (depInfo != null)
                        {
                            sentElem.AppendChild(depInfo);
                        }
                        depInfo = BuildDependencyTreeInfo("collapsed-dependencies", sentence.Get(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation)), tokens, NamespaceUri);
                        if (depInfo != null)
                        {
                            sentElem.AppendChild(depInfo);
                        }
                        depInfo = BuildDependencyTreeInfo("collapsed-ccprocessed-dependencies", sentence.Get(typeof(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation)), tokens, NamespaceUri);
                        if (depInfo != null)
                        {
                            sentElem.AppendChild(depInfo);
                        }
                        depInfo = BuildDependencyTreeInfo("enhanced-dependencies", sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)), tokens, NamespaceUri);
                        if (depInfo != null)
                        {
                            sentElem.AppendChild(depInfo);
                        }
                        depInfo = BuildDependencyTreeInfo("enhanced-plus-plus-dependencies", sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)), tokens, NamespaceUri);
                        if (depInfo != null)
                        {
                            sentElem.AppendChild(depInfo);
                        }
                    }
                    // add Open IE triples
                    ICollection <RelationTriple> openieTriples = sentence.Get(typeof(NaturalLogicAnnotations.RelationTriplesAnnotation));
                    if (openieTriples != null)
                    {
                        Element openieElem = new Element("openie", NamespaceUri);
                        AddTriples(openieTriples, openieElem, NamespaceUri);
                        sentElem.AppendChild(openieElem);
                    }
                    // add KBP triples
                    ICollection <RelationTriple> kbpTriples = sentence.Get(typeof(CoreAnnotations.KBPTriplesAnnotation));
                    if (kbpTriples != null)
                    {
                        Element kbpElem = new Element("kbp", NamespaceUri);
                        AddTriples(kbpTriples, kbpElem, NamespaceUri);
                        sentElem.AppendChild(kbpElem);
                    }
                    // add the MR entities and relations
                    IList <EntityMention>   entities  = sentence.Get(typeof(MachineReadingAnnotations.EntityMentionsAnnotation));
                    IList <RelationMention> relations = sentence.Get(typeof(MachineReadingAnnotations.RelationMentionsAnnotation));
                    if (entities != null && !entities.IsEmpty())
                    {
                        Element mrElem  = new Element("MachineReading", NamespaceUri);
                        Element entElem = new Element("entities", NamespaceUri);
                        AddEntities(entities, entElem, NamespaceUri);
                        mrElem.AppendChild(entElem);
                        if (relations != null)
                        {
                            Element relElem = new Element("relations", NamespaceUri);
                            AddRelations(relations, relElem, NamespaceUri, options.relationsBeam);
                            mrElem.AppendChild(relElem);
                        }
                        sentElem.AppendChild(mrElem);
                    }
                    // Adds sentiment as an attribute of this sentence.
                    Tree sentimentTree = sentence.Get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));
                    if (sentimentTree != null)
                    {
                        int sentiment = RNNCoreAnnotations.GetPredictedClass(sentimentTree);
                        sentElem.AddAttribute(new Attribute("sentimentValue", int.ToString(sentiment)));
                        string sentimentClass = sentence.Get(typeof(SentimentCoreAnnotations.SentimentClass));
                        sentElem.AddAttribute(new Attribute("sentiment", sentimentClass.ReplaceAll(" ", string.Empty)));
                    }
                    // add the sentence to the root
                    sentencesElem.AppendChild(sentElem);
                }
            }
            //
            // add the coref graph
            //
            IDictionary <int, CorefChain> corefChains = annotation.Get(typeof(CorefCoreAnnotations.CorefChainAnnotation));

            if (corefChains != null)
            {
                IList <ICoreMap> sentences = annotation.Get(typeof(CoreAnnotations.SentencesAnnotation));
                Element          corefInfo = new Element("coreference", NamespaceUri);
                AddCorefGraphInfo(options, corefInfo, sentences, corefChains, NamespaceUri);
                docElem.AppendChild(corefInfo);
            }
            //
            // save any document-level annotations here
            //
            return(xmlDoc);
        }
 /// <summary>Converts the given annotation to an XML document using options taken from the StanfordCoreNLP pipeline</summary>
 public static Document AnnotationToDoc(Annotation annotation, StanfordCoreNLP pipeline)
 {
     AnnotationOutputter.Options options = GetOptions(pipeline);
     return(AnnotationToDoc(annotation, options));
 }