Пример #1
0
        public ParserAnnotator(string annotatorName, Properties props)
        {
            string model = props.GetProperty(annotatorName + ".model", LexicalizedParser.DefaultParserLoc);

            if (model == null)
            {
                throw new ArgumentException("No model specified for Parser annotator " + annotatorName);
            }
            this.Verbose = PropertiesUtils.GetBool(props, annotatorName + ".debug", false);
            string[] flags = ConvertFlagsToArray(props.GetProperty(annotatorName + ".flags"));
            this.parser            = LoadModel(model, Verbose, flags);
            this.maxSentenceLength = PropertiesUtils.GetInt(props, annotatorName + ".maxlen", -1);
            string treeMapClass = props.GetProperty(annotatorName + ".treemap");

            if (treeMapClass == null)
            {
                this.treeMap = null;
            }
            else
            {
                this.treeMap = ReflectionLoading.LoadByReflection(treeMapClass, props);
            }
            this.maxParseTime = PropertiesUtils.GetLong(props, annotatorName + ".maxtime", -1);
            this.kBest        = PropertiesUtils.GetInt(props, annotatorName + ".kbest", 1);
            this.keepPunct    = PropertiesUtils.GetBool(props, annotatorName + ".keepPunct", true);
            string buildGraphsProperty = annotatorName + ".buildgraphs";

            if (!this.parser.GetTLPParams().SupportsBasicDependencies())
            {
                if (PropertiesUtils.GetBool(props, buildGraphsProperty))
                {
                    log.Info("WARNING: " + buildGraphsProperty + " set to true, but " + this.parser.GetTLPParams().GetType() + " does not support dependencies");
                }
                this.BuildGraphs = false;
            }
            else
            {
                this.BuildGraphs = PropertiesUtils.GetBool(props, buildGraphsProperty, true);
            }
            if (this.BuildGraphs)
            {
                bool generateOriginalDependencies = PropertiesUtils.GetBool(props, annotatorName + ".originalDependencies", false);
                parser.GetTLPParams().SetGenerateOriginalDependencies(generateOriginalDependencies);
                ITreebankLanguagePack tlp         = parser.GetTLPParams().TreebankLanguagePack();
                IPredicate <string>   punctFilter = this.keepPunct ? Filters.AcceptFilter() : tlp.PunctuationWordRejectFilter();
                this.gsf = tlp.GrammaticalStructureFactory(punctFilter, parser.GetTLPParams().TypedDependencyHeadFinder());
            }
            else
            {
                this.gsf = null;
            }
            this.nThreads = PropertiesUtils.GetInt(props, annotatorName + ".nthreads", PropertiesUtils.GetInt(props, "nthreads", 1));
            bool usesBinary = StanfordCoreNLP.UsesBinaryTrees(props);

            this.saveBinaryTrees   = PropertiesUtils.GetBool(props, annotatorName + ".binaryTrees", usesBinary);
            this.noSquash          = PropertiesUtils.GetBool(props, annotatorName + ".nosquash", false);
            this.extraDependencies = MetaClass.Cast(props.GetProperty(annotatorName + ".extradependencies", "NONE"), typeof(GrammaticalStructure.Extras));
        }
Пример #2
0
        public DependencyParseAnnotator(Properties properties)
        {
            string modelPath = PropertiesUtils.GetString(properties, "model", DependencyParser.DefaultModel);

            parser            = DependencyParser.LoadFromModelFile(modelPath, properties);
            nThreads          = PropertiesUtils.GetInt(properties, "testThreads", DefaultNthreads);
            maxTime           = PropertiesUtils.GetLong(properties, "sentenceTimeout", DefaultMaxtime);
            extraDependencies = MetaClass.Cast(properties.GetProperty("extradependencies", "NONE"), typeof(GrammaticalStructure.Extras));
        }
        private static SemanticGraph GetParse(ICoreMap sentence)
        {
            GrammaticalStructure gs = parser.Predict(sentence);

            GrammaticalStructure.Extras maximal = GrammaticalStructure.Extras.Maximal;
            //        SemanticGraph deps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.ENHANCED, maximal, true, null),
            //                uncollapsedDeps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.BASIC, maximal, true, null),
            //    SemanticGraph ccDeps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.ENHANCED_PLUS_PLUS, maximal, true, null);
            SemanticGraph ccDeps = SemanticGraphFactory.GenerateEnhancedPlusPlusDependencies(gs);

            return(ccDeps);
        }
Пример #4
0
 public ParserAnnotator(ParserGrammar parser, bool verbose, int maxSent, Func <Tree, Tree> treeMap)
 {
     this.Verbose           = verbose;
     this.BuildGraphs       = parser.GetTLPParams().SupportsBasicDependencies();
     this.parser            = parser;
     this.maxSentenceLength = maxSent;
     this.treeMap           = treeMap;
     this.maxParseTime      = 0;
     this.kBest             = 1;
     this.keepPunct         = true;
     if (this.BuildGraphs)
     {
         ITreebankLanguagePack tlp = parser.GetTLPParams().TreebankLanguagePack();
         this.gsf = tlp.GrammaticalStructureFactory(tlp.PunctuationWordRejectFilter(), parser.GetTLPParams().TypedDependencyHeadFinder());
     }
     else
     {
         this.gsf = null;
     }
     this.nThreads          = 1;
     this.saveBinaryTrees   = false;
     this.noSquash          = false;
     this.extraDependencies = GrammaticalStructure.Extras.None;
 }
 //private static HeadFinder shf = new ChineseHeadFinder();
 protected internal override void CollapseDependencies(IList <TypedDependency> list, bool CCprocess, GrammaticalStructure.Extras includeExtras)
 {
     //      collapseConj(list);
     CollapsePrepAndPoss(list);
 }
 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, GrammaticalStructure.Extras includeExtras, IPredicate <TypedDependency> filter)
 {
     return(MakeFromTree(tree, mode, includeExtras, 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));
        }
        /// <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));
        }
 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));
 }
 public static SemanticGraph GenerateCCProcessedDependencies(GrammaticalStructure gs, GrammaticalStructure.Extras extras)
 {
     return(MakeFromTree(gs, SemanticGraphFactory.Mode.Ccprocessed, extras, null));
 }
 public static SemanticGraph GenerateUncollapsedDependencies(GrammaticalStructure gs, GrammaticalStructure.Extras extras)
 {
     return(MakeFromTree(gs, SemanticGraphFactory.Mode.Basic, extras, null));
 }
Пример #13
0
        // static methods
        /// <summary>
        /// Put the tree in the CoreMap for the sentence, also add any
        /// dependency graphs to the sentence, and fill in missing tag annotations.
        /// </summary>
        /// <remarks>
        /// Put the tree in the CoreMap for the sentence, also add any
        /// dependency graphs to the sentence, and fill in missing tag annotations.
        /// Thread safety note: nothing special is done to ensure the thread
        /// safety of the GrammaticalStructureFactory.  However, both the
        /// EnglishGrammaticalStructureFactory and the
        /// ChineseGrammaticalStructureFactory are thread safe.
        /// </remarks>
        public static void FillInParseAnnotations(bool verbose, bool buildGraphs, IGrammaticalStructureFactory gsf, ICoreMap sentence, IList <Tree> trees, GrammaticalStructure.Extras extras)
        {
            bool first = true;

            foreach (Tree tree in trees)
            {
                // make sure all tree nodes are CoreLabels
                // TODO: why isn't this always true? something fishy is going on
                Edu.Stanford.Nlp.Trees.Trees.ConvertToCoreLabels(tree);
                // index nodes, i.e., add start and end token positions to all nodes
                // this is needed by other annotators down stream, e.g., the NFLAnnotator
                tree.IndexSpans(0);
                if (first)
                {
                    sentence.Set(typeof(TreeCoreAnnotations.TreeAnnotation), tree);
                    if (verbose)
                    {
                        log.Info("Tree is:");
                        tree.PennPrint(System.Console.Error);
                    }
                    SetMissingTags(sentence, tree);
                    if (buildGraphs)
                    {
                        // generate the dependency graph
                        // unfortunately, it is necessary to make the
                        // GrammaticalStructure three times, as the dependency
                        // conversion changes the given data structure
                        SemanticGraph deps                 = SemanticGraphFactory.GenerateCollapsedDependencies(gsf.NewGrammaticalStructure(tree), extras);
                        SemanticGraph uncollapsedDeps      = SemanticGraphFactory.GenerateUncollapsedDependencies(gsf.NewGrammaticalStructure(tree), extras);
                        SemanticGraph ccDeps               = SemanticGraphFactory.GenerateCCProcessedDependencies(gsf.NewGrammaticalStructure(tree), extras);
                        SemanticGraph enhancedDeps         = SemanticGraphFactory.GenerateEnhancedDependencies(gsf.NewGrammaticalStructure(tree));
                        SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.GenerateEnhancedPlusPlusDependencies(gsf.NewGrammaticalStructure(tree));
                        if (verbose)
                        {
                            log.Info("SDs:");
                            log.Info(deps.ToString(SemanticGraph.OutputFormat.List));
                        }
                        sentence.Set(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation), deps);
                        sentence.Set(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation), uncollapsedDeps);
                        sentence.Set(typeof(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation), ccDeps);
                        sentence.Set(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation), enhancedDeps);
                        sentence.Set(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation), enhancedPlusPlusDeps);
                    }
                    first = false;
                }
            }
            if (trees.Count > 1)
            {
                sentence.Set(typeof(TreeCoreAnnotations.KBestTreesAnnotation), trees);
            }
        }