Пример #1
0
        /// <summary>
        /// Parses a tsurgeon script text input and compiles a tregex pattern and a list
        /// of tsurgeon operations into a pair.
        /// </summary>
        /// <param name="reader">Reader to read patterns from</param>
        /// <returns>
        /// A pair of a tregex and tsurgeon pattern read from a file, or
        /// <see langword="null"/>
        /// when the operations present in the Reader have been exhausted
        /// </returns>
        /// <exception cref="System.IO.IOException">If any IO problem</exception>
        public static Pair <TregexPattern, TsurgeonPattern> GetOperationFromReader(BufferedReader reader, TregexPatternCompiler compiler)
        {
            string patternString = GetTregexPatternFromReader(reader);

            // log.info("Read tregex pattern: " + patternString);
            if (patternString.IsEmpty())
            {
                return(null);
            }
            TregexPattern   matchPattern     = compiler.Compile(patternString);
            TsurgeonPattern collectedPattern = GetTsurgeonOperationsFromReader(reader);

            return(new Pair <TregexPattern, TsurgeonPattern>(matchPattern, collectedPattern));
        }
        /// <summary>
        /// Compile the
        /// <see cref="annotations"/>
        /// collection given a
        /// particular head finder. Subclasses should call this method at
        /// least once before the class is used, and whenever the head finder
        /// is changed.
        /// </summary>
        protected internal virtual void CompileAnnotations(IHeadFinder hf)
        {
            TregexPatternCompiler compiler = new TregexPatternCompiler(hf);

            annotationPatterns.Clear();
            foreach (KeyValuePair <string, Pair <string, Func <TregexMatcher, string> > > annotation in annotations)
            {
                TregexPattern compiled;
                try
                {
                    compiled = compiler.Compile(annotation.Value.First());
                }
                catch (TregexParseException e)
                {
                    int nth = annotationPatterns.Count + 1;
                    log.Info("Parse exception on annotation pattern #" + nth + " initialization: " + e);
                    continue;
                }
                Pair <TregexPattern, Func <TregexMatcher, string> > behavior = new Pair <TregexPattern, Func <TregexMatcher, string> >(compiled, annotation.Value.Second());
                annotationPatterns[annotation.Key] = behavior;
            }
        }
Пример #3
0
        // to hold the specific prep or conjunction associated with the grammatical relation

        // TODO document constructor
        // TODO change to put specificString after longName, and then use string... for targetPatterns
        private GrammaticalRelation(Language language,
                                    string shortName,
                                    string longName,
                                    GrammaticalRelation parent,
                                    string sourcePattern,
                                    TregexPatternCompiler tregexCompiler,
                                    string[] targetPatterns,
                                    string specificString)
        {
            this.language  = language;
            this.shortName = shortName;
            this.longName  = longName;
            this.parent    = parent;
            this.specific  = specificString; // this can be null!

            if (parent != null)
            {
                parent.AddChild(this);
            }

            if (sourcePattern != null)
            {
                /*try {*/
                this.sourcePattern = new Regex("^(" + sourcePattern + ")$", RegexOptions.Compiled);

                /*} catch (java.util.regex.PatternSyntaxException e) {
                 * throw new RuntimeException("Bad pattern: " + sourcePattern);
                 * }*/
            }
            else
            {
                this.sourcePattern = null;
            }

            foreach (string pattern in targetPatterns)
            {
                try
                {
                    TregexPattern p = tregexCompiler.Compile(pattern);
                    this.targetPatterns.Add(p);
                }
                catch (TregexParseException pe)
                {
                    throw new SystemException("Bad pattern: " + pattern, pe);
                }
            }

            Dictionary <string, GrammaticalRelation> sToR;

            StringsToRelations.TryGetValue(language, out sToR);
            if (sToR == null)
            {
                sToR = new Dictionary <string, GrammaticalRelation>();
                StringsToRelations.Add(language, sToR);
            }
            if (sToR.ContainsKey(ToString()))
            {
                var previous = sToR[ToString()];
                if (!previous.IsFromString() && !IsFromString())
                {
                    throw new ArgumentException("There is already a relation named " + ToString() + '!');
                }
                else
                {
                    /* We get here if we previously just built a fake relation from a string
                     * we previously read in from a file.
                     */
                    // TODO is it worth copying all of the information from this real
                    //      relation into the old fake one?
                }
                sToR[ToString()] = this;
            }
            else
            {
                sToR.Add(ToString(), this);
            }

            /*GrammaticalRelation previous = sToR.put(ToString(), this);
             * if (previous != null) {
             * if (!previous.isFromString() && !isFromString()) {
             * throw new ArgumentException("There is already a relation named " + ToString() + '!');
             * } else {
             * /* We get here if we previously just built a fake relation from a string
             * we previously read in from a file.
             #1#
             * // TODO is it worth copying all of the information from this real
             * //      relation into the old fake one?
             * }
             * }*/
        }
Пример #4
0
 private GrammaticalRelation(Language language, string shortName, string longName, Edu.Stanford.Nlp.Trees.GrammaticalRelation parent, string sourcePattern, TregexPatternCompiler tregexCompiler, string[] targetPatterns, string specificString)
 {
     /* Non-static stuff */
     // a regexp for node values at which this relation can hold
     // to hold the specific prep or conjunction associated with the grammatical relation
     // TODO document constructor
     // TODO change to put specificString after longName, and then use String... for targetPatterns
     this.language  = language;
     this.shortName = shortName;
     this.longName  = longName;
     this.parent    = parent;
     this.specific  = specificString;
     // this can be null!
     if (parent != null)
     {
         parent.AddChild(this);
     }
     if (sourcePattern != null)
     {
         try
         {
             this.sourcePattern = Pattern.Compile(sourcePattern);
         }
         catch (PatternSyntaxException)
         {
             throw new Exception("Bad pattern: " + sourcePattern);
         }
     }
     else
     {
         this.sourcePattern = null;
     }
     foreach (string pattern in targetPatterns)
     {
         try
         {
             TregexPattern p = tregexCompiler.Compile(pattern);
             this.targetPatterns.Add(p);
         }
         catch (TregexParseException pe)
         {
             throw new Exception("Bad pattern: " + pattern, pe);
         }
     }
     Edu.Stanford.Nlp.Trees.GrammaticalRelation previous;
     lock (stringsToRelations)
     {
         IDictionary <string, Edu.Stanford.Nlp.Trees.GrammaticalRelation> sToR = stringsToRelations[language];
         if (sToR == null)
         {
             sToR = Generics.NewHashMap();
             stringsToRelations[language] = sToR;
         }
         previous = sToR[ToString()] = this;
     }
     if (previous != null)
     {
         if (!previous.IsFromString() && !IsFromString())
         {
             throw new ArgumentException("There is already a relation named " + ToString() + '!');
         }
     }
 }