// nothing to do by default /// <summary> /// Converts NamedEntityTagAnnotation tags into /// <see cref="Edu.Stanford.Nlp.IE.Machinereading.Structure.EntityMention"/> /// s. This /// finds the longest sequence of NamedEntityTagAnnotation tags of the matching /// type. /// </summary> /// <param name="sentence">A sentence, ideally annotated with NamedEntityTagAnnotation</param> /// <param name="nerTag">The name of the NER tag to copy, e.g. "DATE".</param> /// <param name="entityType"> /// The type of the /// <see cref="Edu.Stanford.Nlp.IE.Machinereading.Structure.EntityMention"/> /// objects created /// </param> public virtual void MakeAnnotationFromGivenNERTag(ICoreMap sentence, string nerTag, string entityType) { IList <CoreLabel> words = sentence.Get(typeof(CoreAnnotations.TokensAnnotation)); IList <EntityMention> mentions = sentence.Get(typeof(MachineReadingAnnotations.EntityMentionsAnnotation)); System.Diagnostics.Debug.Assert(words != null); System.Diagnostics.Debug.Assert(mentions != null); for (int start = 0; start < words.Count; start++) { int end; // find the first token after start that isn't of nerType for (end = start; end < words.Count; end++) { string ne = words[end].Get(typeof(CoreAnnotations.NamedEntityTagAnnotation)); if (!ne.Equals(nerTag)) { break; } } if (end > start) { // found a match! EntityMention m = entityMentionFactory.ConstructEntityMention(EntityMention.MakeUniqueId(), sentence, new Span(start, end), new Span(start, end), entityType, null, null); logger.Info("Created " + entityType + " entity mention: " + m); start = end - 1; mentions.Add(m); } } sentence.Set(typeof(MachineReadingAnnotations.EntityMentionsAnnotation), mentions); }
/// <summary> /// Converts NamedEntityTagAnnotation tags into /// <see cref="Edu.Stanford.Nlp.IE.Machinereading.Structure.EntityMention"/> /// s. This /// finds the longest sequence of NamedEntityTagAnnotation tags of the matching /// type. /// </summary> /// <param name="sentence">A sentence annotated with NamedEntityTagAnnotation</param> public virtual void MakeAnnotationFromAllNERTags(ICoreMap sentence) { IList <CoreLabel> words = sentence.Get(typeof(CoreAnnotations.TokensAnnotation)); IList <EntityMention> mentions = sentence.Get(typeof(MachineReadingAnnotations.EntityMentionsAnnotation)); System.Diagnostics.Debug.Assert(words != null); if (mentions == null) { this.logger.Info("mentions are null"); mentions = new List <EntityMention>(); } for (int start = 0; start < words.Count; start++) { int end; // find the first token after start that isn't of nerType string lastneTag = null; string ne = null; for (end = start; end < words.Count; end++) { ne = words[end].Get(typeof(CoreAnnotations.NamedEntityTagAnnotation)); if (ne.Equals(SeqClassifierFlags.DefaultBackgroundSymbol) || (lastneTag != null && !ne.Equals(lastneTag))) { break; } lastneTag = ne; } if (end > start) { // found a match! string entityType = this.GetEntityTypeForTag(lastneTag); EntityMention m = entityMentionFactory.ConstructEntityMention(EntityMention.MakeUniqueId(), sentence, new Span(start, end), new Span(start, end), entityType, null, null); //TODO: changed entityType in the above sentence to nerTag - Sonal logger.Info("Created " + entityType + " entity mention: " + m); start = end - 1; mentions.Add(m); } } sentence.Set(typeof(MachineReadingAnnotations.EntityMentionsAnnotation), mentions); }