Beispiel #1
0
 /// <summary>
 /// Returns a new CoreLabel instance based on the contents of the given
 /// CoreMap.  It copies the contents of the other CoreMap.
 /// </summary>
 /// <param name="label">The CoreMap to copy</param>
 public CoreLabel(ICoreMap label) : base(label.Size())
 {
     foreach (var key in label.KeySet())
     {
         Set(key, label.Get(key));
     }
 }
 /// <summary>
 /// Returns a new CoreLabel instance based on the contents of the given
 /// label.
 /// </summary>
 /// <remarks>
 /// Returns a new CoreLabel instance based on the contents of the given
 /// label.   Warning: The behavior of this method is a bit disjunctive!
 /// If label is a CoreMap (including CoreLabel), then its entire
 /// contents is copied into this label.
 /// If label is an IndexedWord, then the backing label is copied over
 /// entirely.
 /// But, otherwise, just the
 /// value() and word iff it implements
 /// <see cref="IHasWord"/>
 /// is copied.
 /// </remarks>
 /// <param name="label">Basis for this label</param>
 public CoreLabel(ILabel label)
     : base(0)
 {
     if (label is ICoreMap)
     {
         ICoreMap cl = (ICoreMap)label;
         SetCapacity(cl.Size());
         foreach (Type key in cl.KeySet())
         {
             Set(key, cl.Get(key));
         }
     }
     else
     {
         if (label is IndexedWord)
         {
             ICoreMap cl = ((IndexedWord)label).BackingLabel();
             SetCapacity(cl.Size());
             foreach (Type key in cl.KeySet())
             {
                 Set(key, cl.Get(key));
             }
         }
         else
         {
             if (label is IHasWord)
             {
                 SetWord(((IHasWord)label).Word());
             }
             SetValue(label.Value());
         }
     }
 }
 public static void DumpCoreMapToStringBuilder(ICoreMap cm, StringBuilder sb)
 {
     foreach (Type rawKey in cm.KeySet())
     {
         Type   key       = (Type)rawKey;
         string className = key.GetSimpleName();
         object value     = cm.Get(key);
         sb.Append(className).Append(": ").Append(value).Append("\n");
     }
 }
        /// <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);
        }
        /// <summary>Copy constructor.</summary>
        /// <param name="other">The ArrayCoreMap to copy. It may not be null.</param>
        public ArrayCoreMap(ICoreMap other)
        {
            ICollection <Type> otherKeys = other.KeySet();

            size   = otherKeys.Count;
            keys   = new Type[size];
            values = new object[size];
            int i = 0;

            foreach (Type key in otherKeys)
            {
                this.keys[i]   = key;
                this.values[i] = other.Get(key);
                i++;
            }
        }
Beispiel #6
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The ArrayCoreMap to copy. It may not be null.</param>
        public ArrayCoreMap(ICoreMap other)
        {
            /*Set<Class<?>>*/
            var otherKeys = other.KeySet();

            psize  = otherKeys.Count;
            keys   = new Type[psize];
            values = new Object[psize];

            int i = 0;

            foreach (var key in otherKeys)
            {
                this.keys[i]   = key;
                this.values[i] = other.Get(key);
                i++;
            }
        }
        // static stuff
        /// <summary>
        /// Merge one CoreMap into another -- that is, overwrite and add any keys in
        /// the base CoreMap with those in the one to be merged.
        /// </summary>
        /// <remarks>
        /// Merge one CoreMap into another -- that is, overwrite and add any keys in
        /// the base CoreMap with those in the one to be merged.
        /// This method is functional -- neither of the argument CoreMaps are changed.
        /// </remarks>
        /// <param name="base">The CoreMap to serve as the base (keys in this are lower priority)</param>
        /// <param name="toBeMerged">The CoreMap to merge in (keys in this are higher priority)</param>
        /// <returns>A new CoreMap representing the merge of the two inputs</returns>
        public static ICoreMap Merge(ICoreMap @base, ICoreMap toBeMerged)
        {
            //(variables)
            ICoreMap rtn = new ArrayCoreMap(@base.Size());

            //(copy base)
            foreach (Type key in @base.KeySet())
            {
                rtn.Set(key, @base.Get(key));
            }
            //(merge)
            foreach (Type key_1 in toBeMerged.KeySet())
            {
                rtn.Set(key_1, toBeMerged.Get(key_1));
            }
            //(return)
            return(rtn);
        }
        /// <summary>
        /// Returns a new CoreLabel instance based on the contents of the given
        /// CoreMap.
        /// </summary>
        /// <remarks>
        /// Returns a new CoreLabel instance based on the contents of the given
        /// CoreMap.  It copies the contents of the other CoreMap.
        /// </remarks>
        /// <param name="label">The CoreMap to copy</param>
        public CoreLabel(ICoreMap label)
            : base(label.Size())
        {
            /* , HasContext */
            // /**
            //  * Should warnings be printed when converting from MapLabel family.
            //  */
            // private static final boolean VERBOSE = false;
            IConsumer <Type> savedListener = ArrayCoreMap.listener;

            // don't listen to the clone operation
            ArrayCoreMap.listener = null;
            foreach (Type key in label.KeySet())
            {
                Set(key, label.Get(key));
            }
            ArrayCoreMap.listener = savedListener;
        }
 private void CleanupTags(ICoreMap cm, IDictionary <object, bool> cleaned)
 {
     cm.Remove(typeof(Tags.TagsAnnotation));
     foreach (Type key in cm.KeySet())
     {
         object obj = cm.Get(key);
         if (!cleaned.Contains(obj))
         {
             cleaned[obj] = false;
             if (obj is ICoreMap)
             {
                 CleanupTags((ICoreMap)obj, cleaned);
             }
             else
             {
                 if (obj is ICollection)
                 {
                     CleanupTags((ICollection)obj, cleaned);
                 }
             }
             cleaned[obj] = true;
         }
     }
 }
Beispiel #10
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The ArrayCoreMap to copy. It may not be null.</param>
        public ArrayCoreMap(ICoreMap other)
        {
            /*Set<Class<?>>*/
            var otherKeys = other.KeySet();

            psize = otherKeys.Count;
            keys = new Type[psize];
            values = new Object[psize];

            int i = 0;
            foreach (var key in otherKeys)
            {
                this.keys[i] = key;
                this.values[i] = other.Get(key);
                i++;
            }
        }
        /// <summary>Usage: java -cp "*" StanfordCoreNlpDemo [inputFile [outputTextFile [outputXmlFile]]]</summary>
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            // set up optional output files
            PrintWriter @out;

            if (args.Length > 1)
            {
                @out = new PrintWriter(args[1]);
            }
            else
            {
                @out = new PrintWriter(System.Console.Out);
            }
            PrintWriter xmlOut = null;

            if (args.Length > 2)
            {
                xmlOut = new PrintWriter(args[2]);
            }
            // Create a CoreNLP pipeline. To build the default pipeline, you can just use:
            //   StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            // Here's a more complex setup example:
            //   Properties props = new Properties();
            //   props.put("annotators", "tokenize, ssplit, pos, lemma, ner, depparse");
            //   props.put("ner.model", "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz");
            //   props.put("ner.applyNumericClassifiers", "false");
            //   StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            // Add in sentiment
            Properties props = new Properties();

            props.SetProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            // Initialize an Annotation with some text to be annotated. The text is the argument to the constructor.
            Annotation annotation;

            if (args.Length > 0)
            {
                annotation = new Annotation(IOUtils.SlurpFileNoExceptions(args[0]));
            }
            else
            {
                annotation = new Annotation("Kosgi Santosh sent an email to Stanford University. He didn't get a reply.");
            }
            // run all the selected Annotators on this text
            pipeline.Annotate(annotation);
            // this prints out the results of sentence analysis to file(s) in good formats
            pipeline.PrettyPrint(annotation, @out);
            if (xmlOut != null)
            {
                pipeline.XmlPrint(annotation, xmlOut);
            }
            // Access the Annotation in code
            // The toString() method on an Annotation just prints the text of the Annotation
            // But you can see what is in it with other methods like toShorterString()
            @out.Println();
            @out.Println("The top level annotation");
            @out.Println(annotation.ToShorterString());
            @out.Println();
            // An Annotation is a Map with Class keys for the linguistic analysis types.
            // You can get and use the various analyses individually.
            // For instance, this gets the parse tree of the first sentence in the text.
            IList <ICoreMap> sentences = annotation.Get(typeof(CoreAnnotations.SentencesAnnotation));

            if (sentences != null && !sentences.IsEmpty())
            {
                ICoreMap sentence = sentences[0];
                @out.Println("The keys of the first sentence's CoreMap are:");
                @out.Println(sentence.KeySet());
                @out.Println();
                @out.Println("The first sentence is:");
                @out.Println(sentence.ToShorterString());
                @out.Println();
                @out.Println("The first sentence tokens are:");
                foreach (ICoreMap token in sentence.Get(typeof(CoreAnnotations.TokensAnnotation)))
                {
                    @out.Println(token.ToShorterString());
                }
                Tree tree = sentence.Get(typeof(TreeCoreAnnotations.TreeAnnotation));
                @out.Println();
                @out.Println("The first sentence parse tree is:");
                tree.PennPrint(@out);
                @out.Println();
                @out.Println("The first sentence basic dependencies are:");
                @out.Println(sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)).ToString(SemanticGraph.OutputFormat.List));
                @out.Println("The first sentence collapsed, CC-processed dependencies are:");
                SemanticGraph graph = sentence.Get(typeof(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation));
                @out.Println(graph.ToString(SemanticGraph.OutputFormat.List));
                // Access coreference. In the coreference link graph,
                // each chain stores a set of mentions that co-refer with each other,
                // along with a method for getting the most representative mention.
                // Both sentence and token offsets start at 1!
                @out.Println("Coreference information");
                IDictionary <int, CorefChain> corefChains = annotation.Get(typeof(CorefCoreAnnotations.CorefChainAnnotation));
                if (corefChains == null)
                {
                    return;
                }
                foreach (KeyValuePair <int, CorefChain> entry in corefChains)
                {
                    @out.Println("Chain " + entry.Key);
                    foreach (CorefChain.CorefMention m in entry.Value.GetMentionsInTextualOrder())
                    {
                        // We need to subtract one since the indices count from 1 but the Lists start from 0
                        IList <CoreLabel> tokens = sentences[m.sentNum - 1].Get(typeof(CoreAnnotations.TokensAnnotation));
                        // We subtract two for end: one for 0-based indexing, and one because we want last token of mention not one following.
                        @out.Println("  " + m + ", i.e., 0-based character offsets [" + tokens[m.startIndex - 1].BeginPosition() + ", " + tokens[m.endIndex - 2].EndPosition() + ")");
                    }
                }
                @out.Println();
                @out.Println("The first sentence overall sentiment rating is " + sentence.Get(typeof(SentimentCoreAnnotations.SentimentClass)));
            }
            IOUtils.CloseIgnoringExceptions(@out);
            IOUtils.CloseIgnoringExceptions(xmlOut);
        }