/// <summary>
        /// Returns a new
        /// <c>SemanticGraph</c>
        /// constructed from a given
        /// <see cref="Edu.Stanford.Nlp.Trees.Tree"/>
        /// with given options.
        /// This factory method is intended to replace a profusion of highly similar
        /// factory methods, such as
        /// <c>typedDependencies()</c>
        /// ,
        /// <c>typedDependenciesCollapsed()</c>
        /// ,
        /// <c>allTypedDependencies()</c>
        /// ,
        /// <c>allTypedDependenciesCollapsed()</c>
        /// , etc.
        /// For a fuller explanation of the meaning of the boolean arguments, see
        /// <see cref="Edu.Stanford.Nlp.Trees.GrammaticalStructure"/>
        /// .
        /// </summary>
        /// <param name="tree">A tree representing a phrase structure parse</param>
        /// <param name="includeExtras">
        /// Whether to include extra dependencies, which may
        /// result in a non-tree
        /// </param>
        /// <param name="filter">A filter to exclude certain dependencies; ignored if null</param>
        /// <param name="originalDependencies">
        /// generate original Stanford dependencies instead of new
        /// Universal Dependencies
        /// </param>
        /// <returns>A SemanticGraph</returns>
        public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, GrammaticalStructure.Extras includeExtras, IPredicate <TypedDependency> filter, bool originalDependencies, bool includePunctuationDependencies)
        {
            GrammaticalStructure gs;

            if (originalDependencies)
            {
                IPredicate <string> wordFilt;
                if (includePunctuationDependencies)
                {
                    wordFilt = Filters.AcceptFilter();
                }
                else
                {
                    wordFilt = new PennTreebankLanguagePack().PunctuationWordRejectFilter();
                }
                gs = new EnglishGrammaticalStructure(tree, wordFilt, new SemanticHeadFinder(true));
            }
            else
            {
                IPredicate <string> tagFilt;
                if (includePunctuationDependencies)
                {
                    tagFilt = Filters.AcceptFilter();
                }
                else
                {
                    tagFilt = new PennTreebankLanguagePack().PunctuationTagRejectFilter();
                }
                gs = new UniversalEnglishGrammaticalStructure(tree, tagFilt, new UniversalSemanticHeadFinder(true));
            }
            return(MakeFromTree(gs, mode, includeExtras, filter));
        }
Ejemplo n.º 2
0
        /// <summary>Prints out all matches of a semgrex pattern on a file of dependencies.</summary>
        /// <remarks>
        /// Prints out all matches of a semgrex pattern on a file of dependencies.
        /// <p>
        /// Usage:<br />
        /// java edu.stanford.nlp.semgraph.semgrex.SemgrexPattern [args]
        /// <br />
        /// See the help() function for a list of possible arguments to provide.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            IDictionary <string, int> flagMap = Generics.NewHashMap();

            flagMap[Pattern]            = 1;
            flagMap[TreeFile]           = 1;
            flagMap[Mode]               = 1;
            flagMap[Extras]             = 1;
            flagMap[ConlluFile]         = 1;
            flagMap[OutputFormatOption] = 1;
            IDictionary <string, string[]> argsMap = StringUtils.ArgsToMap(args, flagMap);

            // args = argsMap.get(null);
            // TODO: allow patterns to be extracted from a file
            if (!(argsMap.Contains(Pattern)) || argsMap[Pattern].Length == 0)
            {
                Help();
                System.Environment.Exit(2);
            }
            Edu.Stanford.Nlp.Semgraph.Semgrex.SemgrexPattern semgrex = Edu.Stanford.Nlp.Semgraph.Semgrex.SemgrexPattern.Compile(argsMap[Pattern][0]);
            string modeString = DefaultMode;

            if (argsMap.Contains(Mode) && argsMap[Mode].Length > 0)
            {
                modeString = argsMap[Mode][0].ToUpper();
            }
            SemanticGraphFactory.Mode mode = SemanticGraphFactory.Mode.ValueOf(modeString);
            string outputFormatString      = DefaultOutputFormat;

            if (argsMap.Contains(OutputFormatOption) && argsMap[OutputFormatOption].Length > 0)
            {
                outputFormatString = argsMap[OutputFormatOption][0].ToUpper();
            }
            SemgrexPattern.OutputFormat outputFormat = SemgrexPattern.OutputFormat.ValueOf(outputFormatString);
            bool useExtras = true;

            if (argsMap.Contains(Extras) && argsMap[Extras].Length > 0)
            {
                useExtras = bool.ValueOf(argsMap[Extras][0]);
            }
            IList <SemanticGraph> graphs = Generics.NewArrayList();

            // TODO: allow other sources of graphs, such as dependency files
            if (argsMap.Contains(TreeFile) && argsMap[TreeFile].Length > 0)
            {
                foreach (string treeFile in argsMap[TreeFile])
                {
                    log.Info("Loading file " + treeFile);
                    MemoryTreebank treebank = new MemoryTreebank(new TreeNormalizer());
                    treebank.LoadPath(treeFile);
                    foreach (Tree tree in treebank)
                    {
                        // TODO: allow other languages... this defaults to English
                        SemanticGraph graph = SemanticGraphFactory.MakeFromTree(tree, mode, useExtras ? GrammaticalStructure.Extras.Maximal : GrammaticalStructure.Extras.None);
                        graphs.Add(graph);
                    }
                }
            }
            if (argsMap.Contains(ConlluFile) && argsMap[ConlluFile].Length > 0)
            {
                CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
                foreach (string conlluFile in argsMap[ConlluFile])
                {
                    log.Info("Loading file " + conlluFile);
                    IEnumerator <SemanticGraph> it = reader.GetIterator(IOUtils.ReaderFromString(conlluFile));
                    while (it.MoveNext())
                    {
                        SemanticGraph graph = it.Current;
                        graphs.Add(graph);
                    }
                }
            }
            foreach (SemanticGraph graph_1 in graphs)
            {
                SemgrexMatcher matcher = semgrex.Matcher(graph_1);
                if (!matcher.Find())
                {
                    continue;
                }
                if (outputFormat == SemgrexPattern.OutputFormat.List)
                {
                    log.Info("Matched graph:" + Runtime.LineSeparator() + graph_1.ToString(SemanticGraph.OutputFormat.List));
                    int  i     = 1;
                    bool found = true;
                    while (found)
                    {
                        log.Info("Match " + i + " at: " + matcher.GetMatch().ToString(CoreLabel.OutputFormat.ValueIndex));
                        IList <string> nodeNames = Generics.NewArrayList();
                        Sharpen.Collections.AddAll(nodeNames, matcher.GetNodeNames());
                        nodeNames.Sort();
                        foreach (string name in nodeNames)
                        {
                            log.Info("  " + name + ": " + matcher.GetNode(name).ToString(CoreLabel.OutputFormat.ValueIndex));
                        }
                        log.Info(" ");
                        found = matcher.Find();
                    }
                }
                else
                {
                    if (outputFormat == SemgrexPattern.OutputFormat.Offset)
                    {
                        if (graph_1.VertexListSorted().IsEmpty())
                        {
                            continue;
                        }
                        System.Console.Out.Printf("+%d %s%n", graph_1.VertexListSorted()[0].Get(typeof(CoreAnnotations.LineNumberAnnotation)), argsMap[ConlluFile][0]);
                    }
                }
            }
        }
 public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, bool includeExtras)
 {
     return(MakeFromTree(tree, mode, includeExtras ? GrammaticalStructure.Extras.Maximal : GrammaticalStructure.Extras.None));
 }
 public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, GrammaticalStructure.Extras includeExtras)
 {
     return(MakeFromTree(tree, mode, includeExtras, null, false));
 }
 public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, bool includeExtras, IPredicate <TypedDependency> filter)
 {
     return(MakeFromTree(tree, mode, includeExtras ? GrammaticalStructure.Extras.Maximal : GrammaticalStructure.Extras.None, filter, false));
 }
        // TODO: these booleans would be more readable as enums similar to Mode.
        // Then the arguments would make more sense
        public static SemanticGraph MakeFromTree(GrammaticalStructure gs, SemanticGraphFactory.Mode mode, GrammaticalStructure.Extras includeExtras, IPredicate <TypedDependency> filter)
        {
            ICollection <TypedDependency> deps;

            switch (mode)
            {
            case SemanticGraphFactory.Mode.Enhanced:
            {
                deps = gs.TypedDependenciesEnhanced();
                break;
            }

            case SemanticGraphFactory.Mode.EnhancedPlusPlus:
            {
                deps = gs.TypedDependenciesEnhancedPlusPlus();
                break;
            }

            case SemanticGraphFactory.Mode.CollapsedTree:
            {
                deps = gs.TypedDependenciesCollapsedTree();
                break;
            }

            case SemanticGraphFactory.Mode.Collapsed:
            {
                deps = gs.TypedDependenciesCollapsed(includeExtras);
                break;
            }

            case SemanticGraphFactory.Mode.Ccprocessed:
            {
                deps = gs.TypedDependenciesCCprocessed(includeExtras);
                break;
            }

            case SemanticGraphFactory.Mode.Basic:
            {
                deps = gs.TypedDependencies(includeExtras);
                break;
            }

            default:
            {
                throw new ArgumentException("Unknown mode " + mode);
            }
            }
            if (filter != null)
            {
                IList <TypedDependency> depsFiltered = Generics.NewArrayList();
                foreach (TypedDependency td in deps)
                {
                    if (filter.Test(td))
                    {
                        depsFiltered.Add(td);
                    }
                }
                deps = depsFiltered;
            }
            // there used to be an if clause that filtered out the case of empty
            // dependencies. However, I could not understand (or replicate) the error
            // it alluded to, and it led to empty dependency graphs for very short fragments,
            // which meant they were ignored by the RTE system. Changed. (pado)
            // See also the SemanticGraph constructor.
            //log.info(deps.toString());
            return(new SemanticGraph(deps));
        }
 public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, GrammaticalStructure.Extras includeExtras, IPredicate <TypedDependency> filter, bool originalDependencies)
 {
     return(MakeFromTree(tree, mode, includeExtras, filter, originalDependencies, IncludePunctuationDependencies));
 }