Exemplo n.º 1
0
 public virtual void Annotate(Annotation annotation)
 {
     if (annotation.ContainsKey(typeof(CoreAnnotations.SentencesAnnotation)))
     {
         // parse a tree for each sentence
         foreach (ICoreMap sentence in annotation.Get(typeof(CoreAnnotations.SentencesAnnotation)))
         {
             IList <CoreLabel> words = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));
             if (Verbose)
             {
                 log.Info("Parsing: " + words);
             }
             int maxSentenceLength = parser.GetMaxSentenceLength();
             // generate the constituent tree
             Tree tree;
             // initialized below
             if (maxSentenceLength <= 0 || words.Count < maxSentenceLength)
             {
                 tree = parser.GetBestParse(words);
             }
             else
             {
                 tree = ParserUtils.XTree(words);
             }
             IList <Tree> trees = Generics.NewArrayList(1);
             trees.Add(tree);
             ParserAnnotatorUtils.FillInParseAnnotations(Verbose, BuildGraphs, gsf, sentence, trees, GrammaticalStructure.Extras.None);
         }
     }
     else
     {
         throw new Exception("unable to find sentences in: " + annotation);
     }
 }
Exemplo n.º 2
0
        private void FinishSentence(ICoreMap sentence, IList <Tree> trees)
        {
            if (treeMap != null)
            {
                IList <Tree> mappedTrees = Generics.NewLinkedList();
                foreach (Tree tree in trees)
                {
                    Tree mappedTree = treeMap.Apply(tree);
                    mappedTrees.Add(mappedTree);
                }
                trees = mappedTrees;
            }
            ParserAnnotatorUtils.FillInParseAnnotations(Verbose, BuildGraphs, gsf, sentence, trees, extraDependencies);
            if (saveBinaryTrees)
            {
                TreeBinarizer binarizer = TreeBinarizer.SimpleTreeBinarizer(parser.GetTLPParams().HeadFinder(), parser.TreebankLanguagePack());
                Tree          binarized = binarizer.TransformTree(trees[0]);
                Edu.Stanford.Nlp.Trees.Trees.ConvertToCoreLabels(binarized);
                sentence.Set(typeof(TreeCoreAnnotations.BinarizedTreeAnnotation), binarized);
            }
            // for some reason in some corner cases nodes aren't having sentenceIndex set
            // do a pass and make sure all nodes have sentenceIndex set
            SemanticGraph sg = sentence.Get(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation));

            if (sg != null)
            {
                foreach (IndexedWord iw in sg.VertexSet())
                {
                    if (iw.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)) == null && sentence.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)) != null)
                    {
                        iw.SetSentIndex(sentence.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)));
                    }
                }
            }
        }