public RelationExtractorAnnotator(Properties props)
        {
            verbose = bool.Parse(props.GetProperty("sup.relation.verbose", "false"));
            string relationModel = props.GetProperty("sup.relation.model", DefaultPaths.DefaultSupRelationExRelationModel);

            try
            {
                IExtractor             entityExtractor   = new RothEntityExtractor();
                BasicRelationExtractor relationExtractor = BasicRelationExtractor.Load(relationModel);
                log.Info("Loading relation model from " + relationModel);
                mr = MachineReading.MakeMachineReadingForAnnotation(new RothCONLL04Reader(), entityExtractor, relationExtractor, null, null, null, true, verbose);
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
                throw new Exception(e);
            }
        }
Exemple #2
0
        /*
         * FeatureFactory class to use for generating features from relations for relation extraction.
         * Default is "edu.stanford.nlp.ie.machinereading.RelationFeatureFactory"
         */
        //private Class<RelationFeatureFactory> relationFeatureFactoryClass = edu.stanford.nlp.ie.machinereading.RelationFeatureFactory.class;

        /*
         * comma-separated list of feature types to generate for relation extraction
         */
        //private String relationFeatures;
        public override void PrintResults(PrintWriter pw, IList <ICoreMap> goldStandard, IList <ICoreMap> extractorOutput)
        {
            featureFactory = MachineReading.MakeRelationFeatureFactory(MachineReadingProperties.relationFeatureFactoryClass, MachineReadingProperties.relationFeatures, false);
            mentionFactory = new RelationMentionFactory();
            // generic mentions work well in this domain
            ResultsPrinter.Align(goldStandard, extractorOutput);
            IList <RelationMention> relations = new List <RelationMention>();
            IDictionary <RelationMention, string> predictions = new Dictionary <RelationMention, string>();

            for (int i = 0; i < goldStandard.Count; i++)
            {
                IList <RelationMention> goldRelations = AnnotationUtils.GetAllRelations(mentionFactory, goldStandard[i], true);
                Sharpen.Collections.AddAll(relations, goldRelations);
                foreach (RelationMention rel in goldRelations)
                {
                    predictions[rel] = AnnotationUtils.GetRelation(mentionFactory, extractorOutput[i], rel.GetArg(0), rel.GetArg(1)).GetType();
                }
            }
            ICounter <Pair <Pair <string, string>, string> > pathCounts = new ClassicCounter <Pair <Pair <string, string>, string> >();

            foreach (RelationMention rel_1 in relations)
            {
                pathCounts.IncrementCount(new Pair <Pair <string, string>, string>(new Pair <string, string>(rel_1.GetArg(0).GetType(), rel_1.GetArg(1).GetType()), featureFactory.GetFeature(rel_1, "dependency_path_lowlevel")));
            }
            ICounter <string> singletonCorrect   = new ClassicCounter <string>();
            ICounter <string> singletonPredicted = new ClassicCounter <string>();
            ICounter <string> singletonActual    = new ClassicCounter <string>();

            foreach (RelationMention rel_2 in relations)
            {
                if (pathCounts.GetCount(new Pair <Pair <string, string>, string>(new Pair <string, string>(rel_2.GetArg(0).GetType(), rel_2.GetArg(1).GetType()), featureFactory.GetFeature(rel_2, "dependency_path_lowlevel"))) == 1.0)
                {
                    string prediction = predictions[rel_2];
                    if (prediction.Equals(rel_2.GetType()))
                    {
                        singletonCorrect.IncrementCount(prediction);
                    }
                    singletonPredicted.IncrementCount(prediction);
                    singletonActual.IncrementCount(rel_2.GetType());
                }
            }
            // Group together actual relations of a type with relations that were
            // predicted to be that type
            // String rel1group = RelationsSentence.isUnrelatedLabel(rel1.getType())
            // ? prediction1 : rel1.getType();
            // String rel2group = RelationsSentence.isUnrelatedLabel(rel2.getType())
            // ? prediction2 : rel2.getType();
            // int groupComp = rel1group.compareTo(rel2group);
            // int pathComp =
            // getFeature(rel1,"generalized_dependency_path").compareTo(getFeature(rel2,"generalized_dependency_path"));
            // } else if (pathComp != 0) {
            // return pathComp;
            _T1018869951 relComp = new _T1018869951(this);

            relations.Sort(relComp);
            foreach (RelationMention rel_3 in relations)
            {
                string prediction = predictions[rel_3];
                // if (RelationsSentence.isUnrelatedLabel(prediction) &&
                // RelationsSentence.isUnrelatedLabel(rel.getType())) {
                // continue;
                // }
                string type1 = rel_3.GetArg(0).GetType();
                string type2 = rel_3.GetArg(1).GetType();
                string path  = featureFactory.GetFeature(rel_3, "dependency_path_lowlevel");
                if (!((type1.Equals("PEOPLE") && type2.Equals("PEOPLE")) || (type1.Equals("PEOPLE") && type2.Equals("LOCATION")) || (type1.Equals("LOCATION") && type2.Equals("LOCATION")) || (type1.Equals("ORGANIZATION") && type2.Equals("LOCATION")) || (type1
                                                                                                                                                                                                                                                             .Equals("PEOPLE") && type2.Equals("ORGANIZATION"))))
                {
                    continue;
                }
                if (path.Equals(string.Empty))
                {
                    continue;
                }
                pw.Println("\nLABEL: " + prediction);
                pw.Println(rel_3);
                pw.Println(path);
                pw.Println(featureFactory.GetFeatures(rel_3, "dependency_path_words"));
                pw.Println(featureFactory.GetFeature(rel_3, "surface_path_POS"));
            }
        }
        /// <summary>Loads the model from disk.</summary>
        /// <param name="path">The location of model that was saved to disk</param>
        /// <exception cref="System.InvalidCastException">if model is the wrong format</exception>
        /// <exception cref="System.IO.IOException">
        /// if the model file doesn't exist or is otherwise
        /// unavailable/incomplete
        /// </exception>
        /// <exception cref="System.TypeLoadException">this would probably indicate a serious classpath problem</exception>
        public static Edu.Stanford.Nlp.IE.Machinereading.BasicEntityExtractor Load(string path, Type entityClassifier, bool preferDefaultGazetteer)
        {
            // load the additional arguments
            // try to load the extra file from the CLASSPATH first
            InputStream @is = typeof(Edu.Stanford.Nlp.IE.Machinereading.BasicEntityExtractor).GetClassLoader().GetResourceAsStream(path + ".extra");

            // if not found in the CLASSPATH, load from the file system
            if (@is == null)
            {
                @is = new FileInputStream(path + ".extra");
            }
            ObjectInputStream @in = new ObjectInputStream(@is);
            string            gazetteerLocation = ErasureUtils.UncheckedCast <string>(@in.ReadObject());

            if (preferDefaultGazetteer)
            {
                gazetteerLocation = DefaultPaths.DefaultNflGazetteer;
            }
            ICollection <string> annotationsToSkip = ErasureUtils.UncheckedCast <ICollection <string> >(@in.ReadObject());
            bool useSubTypes = ErasureUtils.UncheckedCast <bool>(@in.ReadObject());
            bool useBIO      = ErasureUtils.UncheckedCast <bool>(@in.ReadObject());

            @in.Close();
            @is.Close();
            Edu.Stanford.Nlp.IE.Machinereading.BasicEntityExtractor extractor = (Edu.Stanford.Nlp.IE.Machinereading.BasicEntityExtractor)MachineReading.MakeEntityExtractor(entityClassifier, gazetteerLocation);
            // load the CRF classifier (this works from any resource, e.g., classpath or file system)
            extractor.classifier = CRFClassifier.GetClassifier(path);
            // copy the extra arguments
            extractor.annotationsToSkip = annotationsToSkip;
            extractor.useSubTypes       = useSubTypes;
            extractor.useBIO            = useBIO;
            return(extractor);
        }