/// <summary>Find the index of the head of an entity.</summary> /// <param name="ent">The entity mention</param> /// <param name="tree">The Tree for the entire sentence in which it occurs.</param> /// <param name="tokens">The Sentence in which it occurs</param> /// <param name="setHeadSpan">Whether to set the head span in the entity mention.</param> /// <returns>The index of the entity head</returns> public virtual int AssignSyntacticHead(EntityMention ent, Tree tree, IList <CoreLabel> tokens, bool setHeadSpan) { if (ent.GetSyntacticHeadTokenPosition() != -1) { return(ent.GetSyntacticHeadTokenPosition()); } logger.Finest("Finding syntactic head for entity: " + ent + " in tree: " + tree.ToString()); logger.Finest("Flat sentence is: " + tokens); Tree sh = null; try { sh = FindSyntacticHead(ent, tree, tokens); } catch (Exception e) { logger.Severe("WARNING: failed to parse sentence. Will continue with the right-most head heuristic: " + SentenceToString(tokens)); Sharpen.Runtime.PrintStackTrace(e); } int headPos = ent.GetExtentTokenEnd() - 1; if (sh != null) { CoreLabel label = (CoreLabel)sh.Label(); headPos = label.Get(typeof(CoreAnnotations.BeginIndexAnnotation)); } else { logger.Fine("WARNING: failed to find syntactic head for entity: " + ent + " in tree: " + tree); logger.Fine("Fallback strategy: will set head to last token in mention: " + tokens[headPos]); } ent.SetHeadTokenPosition(headPos); if (setHeadSpan) { // set the head span to match exactly the syntactic head // this is needed for some corpora where the head span is not given ent.SetHeadTokenSpan(new Span(headPos, headPos + 1)); } return(headPos); }