示例#1
0
        /// <summary>
        /// Return a string describing the usage of the program this method is called from, given the
        /// options declared in the given set of classes.
        /// </summary>
        /// <remarks>
        /// Return a string describing the usage of the program this method is called from, given the
        /// options declared in the given set of classes.
        /// This will print both the static options, and the non-static options.
        /// </remarks>
        /// <param name="optionsClasses">The classes defining the options being used by this program.</param>
        /// <returns>A String describing the usage of the class.</returns>
        public static string Usage(Type[] optionsClasses)
        {
            string        mainClass = ThreadRootClass();
            StringBuilder b         = new StringBuilder();

            b.Append("Usage: ").Append(mainClass).Append(' ');
            IList <Pair <ArgumentParser.Option, FieldInfo> > options = new List <Pair <ArgumentParser.Option, FieldInfo> >();

            foreach (Type clazz in optionsClasses)
            {
                try
                {
                    Sharpen.Collections.AddAll(options, Arrays.Stream(ScrapeFields(clazz)).Map(null).Filter(null).Collect(Collectors.ToList()));
                }
                catch (Exception)
                {
                    return(b.Append("<unknown>").ToString());
                }
            }
            int longestOptionName = options.Stream().Map(null).Max(IComparer.ComparingInt(null)).OrElse(10);
            int longestOptionType = options.Stream().Map(null).Max(IComparer.ComparingInt(null)).OrElse(10) + 1;

            options.Stream().Filter(null).ForEach(null);
            options.Stream().Filter(null).ForEach(null);
            return(b.ToString());
        }
        /// <summary>
        /// Create a dataset of subject/object pairs, such that a sequence of splits that segments this
        /// subject and object is a correct sequence.
        /// </summary>
        /// <param name="depparse">The dependency parse of the sentence.</param>
        /// <param name="traceTargets">The set of spans corresponding to targets of traces.</param>
        /// <param name="traceSources">The set of indices in a sentence corresponding to the sources of traces.</param>
        /// <returns>A dataset of subject/object spans.</returns>
        private static ICollection <Pair <Span, Span> > SubjectObjectPairs(SemanticGraph depparse, IList <CoreLabel> tokens, IDictionary <int, Span> traceTargets, IDictionary <int, int> traceSources)
        {
            //    log(StringUtils.join(tokens.stream().map(CoreLabel::word), " "));
            IList <Pair <Span, Span> > data = new List <Pair <Span, Span> >();

            foreach (SemgrexPattern vpPattern in segmenter.VpPatterns)
            {
                SemgrexMatcher matcher = vpPattern.Matcher(depparse);
                while (matcher.Find())
                {
                    // Get the verb and object
                    IndexedWord verb    = matcher.GetNode("verb");
                    IndexedWord @object = matcher.GetNode("object");
                    if (verb != null && @object != null)
                    {
                        // See if there is already a subject attached
                        bool hasSubject = false;
                        foreach (SemanticGraphEdge edge in depparse.OutgoingEdgeIterable(verb))
                        {
                            if (edge.GetRelation().ToString().Contains("subj"))
                            {
                                hasSubject = true;
                            }
                        }
                        foreach (SemanticGraphEdge edge_1 in depparse.OutgoingEdgeIterable(@object))
                        {
                            if (edge_1.GetRelation().ToString().Contains("subj"))
                            {
                                hasSubject = true;
                            }
                        }
                        if (!hasSubject)
                        {
                            // Get the spans for the verb and object
                            Optional <IList <IndexedWord> > verbChunk   = segmenter.GetValidChunk(depparse, verb, segmenter.ValidAdverbArcs, Optional.Empty(), true);
                            Optional <IList <IndexedWord> > objectChunk = segmenter.GetValidChunk(depparse, @object, segmenter.ValidObjectArcs, Optional.Empty(), true);
                            if (verbChunk.IsPresent() && objectChunk.IsPresent())
                            {
                                verbChunk.Get().Sort(IComparer.ComparingInt(null));
                                objectChunk.Get().Sort(IComparer.ComparingInt(null));
                                // Find a trace
                                int  traceId   = -1;
                                Span verbSpan  = ToSpan(verbChunk.Get());
                                Span traceSpan = Span.FromValues(verbSpan.Start() - 1, verbSpan.End() + 1);
                                foreach (KeyValuePair <int, int> entry in traceSources)
                                {
                                    if (traceSpan.Contains(entry.Value))
                                    {
                                        traceId = entry.Key;
                                    }
                                }
                                //noinspection StatementWithEmptyBody
                                if (traceId < 0)
                                {
                                }
                                else
                                {
                                    // Register the VP as an unknown VP
                                    //                List<CoreLabel> vpChunk = new ArrayList<>();
                                    //                vpChunk.addAll(verbChunk.get());
                                    //                vpChunk.addAll(objectChunk.get());
                                    //                Collections.sort(vpChunk, (a, b) -> a.index() - b.index());
                                    //                debug("could not find trace for " + vpChunk);
                                    // Add the obj chunk
                                    Span subjectSpan = traceTargets[traceId];
                                    Span objectSpan  = ToSpan(objectChunk.Get());
                                    if (subjectSpan != null)
                                    {
                                        //                  debug("(" +
                                        //                      StringUtils.join(tokens.subList(subjectSpan.start(), subjectSpan.end()).stream().map(CoreLabel::word), " ") + "; " +
                                        //                      verb.word() + "; " +
                                        //                      StringUtils.join(tokens.subList(objectSpan.start(), objectSpan.end()).stream().map(CoreLabel::word), " ") +
                                        //                      ")");
                                        data.Add(Pair.MakePair(subjectSpan, objectSpan));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // Run vanilla pattern splits
            foreach (SemgrexPattern vpPattern_1 in segmenter.VerbPatterns)
            {
                SemgrexMatcher matcher = vpPattern_1.Matcher(depparse);
                while (matcher.Find())
                {
                    // Get the verb and object
                    IndexedWord subject = matcher.GetNode("subject");
                    IndexedWord @object = matcher.GetNode("object");
                    if (subject != null && @object != null)
                    {
                        Optional <IList <IndexedWord> > subjectChunk = segmenter.GetValidChunk(depparse, subject, segmenter.ValidSubjectArcs, Optional.Empty(), true);
                        Optional <IList <IndexedWord> > objectChunk  = segmenter.GetValidChunk(depparse, @object, segmenter.ValidObjectArcs, Optional.Empty(), true);
                        if (subjectChunk.IsPresent() && objectChunk.IsPresent())
                        {
                            Span subjectSpan = ToSpan(subjectChunk.Get());
                            Span objectSpan  = ToSpan(objectChunk.Get());
                            data.Add(Pair.MakePair(subjectSpan, objectSpan));
                        }
                    }
                }
            }
            return(data);
        }