Example #1
0
        public static void AddNamedEntity(List <Book> books)
        {
            var pipeline = PipelineBuilder.GetPipeLineWithNamedEntities();
            int counter  = 1;

            foreach (var book in books)
            {
                Dictionary <string, int> entities = new Dictionary <string, int>();
                var annotation = new Annotation(book.Text);
                pipeline.annotate(annotation);
                var sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;
                foreach (CoreMap sentence in sentences)
                {
                    var tokens = sentence.get(new
                                              CoreAnnotations.TokensAnnotation().getClass()) as ArrayList;
                    foreach (CoreLabel token in tokens)
                    {
                        var ner = token.get(new
                                            CoreAnnotations.NamedEntityTagAnnotation().getClass()) as string;
                        if (entities.ContainsKey(ner))
                        {
                            entities[ner]++;
                        }
                        else
                        {
                            entities.Add(ner, 1);
                        }
                    }
                }

                foreach (var entity in entities)
                {
                    book.NamedEntities.Add(new NamedEntity()
                    {
                        NamedEntityID = counter, BookID = book.BookID, NamedEntity1 = entity.Key, NumberOfOccurences = entity.Value
                    });
                    counter++;
                }
            }
        }