本文整理汇总了Java中edu.stanford.nlp.ie.machinereading.structure.EntityMention类的典型用法代码示例。如果您正苦于以下问题:Java EntityMention类的具体用法?Java EntityMention怎么用?Java EntityMention使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EntityMention类属于edu.stanford.nlp.ie.machinereading.structure包,在下文中一共展示了EntityMention类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: toXML
点赞 3
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private static Element toXML(EntityMention entity, String curNS) {
Element top = new Element("entity", curNS);
top.addAttribute(new Attribute("id", entity.getObjectId()));
Element type = new Element("type", curNS);
type.appendChild(entity.getType());
top.appendChild(entity.getType());
if (entity.getNormalizedName() != null){
Element nm = new Element("normalized", curNS);
nm.appendChild(entity.getNormalizedName());
top.appendChild(nm);
}
if (entity.getSubType() != null){
Element subtype = new Element("subtype", curNS);
subtype.appendChild(entity.getSubType());
top.appendChild(subtype);
}
Element span = new Element("span", curNS);
span.addAttribute(new Attribute("start", Integer.toString(entity.getHeadTokenStart())));
span.addAttribute(new Attribute("end", Integer.toString(entity.getHeadTokenEnd())));
top.appendChild(span);
top.appendChild(makeProbabilitiesElement(entity, curNS));
return top;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:26,
代码来源:XMLOutputter.java
示例2: extract
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
public static HashMap<String, String> extract(String sentence) {
Annotation doc = new Annotation(sentence);
pipeline.annotate(doc);
r.annotate(doc);
HashMap<String, String> map = new HashMap<String, String>();
for(CoreMap s: doc.get(CoreAnnotations.SentencesAnnotation.class)){
List<RelationMention> rls = s.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);
for(RelationMention rl: rls){
if(rl.getType().equals("Work_For")){
System.out.println(rl);
String organization = "";
String people = "";
for (EntityMention entity: rl.getEntityMentionArgs()){
if(entity.getType().equals("ORGANIZATION")){
organization = entity.getValue();
}
if(entity.getType().equals("PEOPLE")){
people = entity.getValue();
}
}
map.put(people, organization);
}
}
}
return map;
}
开发者ID:intel-analytics,
项目名称:InformationExtraction,
代码行数:27,
代码来源:RelationExtractor.java
示例3: compare
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
@Override
public int compare(EntityMention m1, EntityMention m2){
if(m1.getExtentTokenStart() > m2.getExtentTokenStart()) return 1;
else if(m1.getExtentTokenStart() < m2.getExtentTokenStart()) return -1;
else if(m1.getExtentTokenEnd() > m2.getExtentTokenEnd()) return -1;
else if(m1.getExtentTokenEnd() < m2.getExtentTokenEnd()) return 1;
else return 0;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:9,
代码来源:ACEMentionExtractor.java
示例4: addEntities
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
/**
* Generates the XML content for MachineReading entities
*/
private static void addEntities(List<EntityMention> entities, Element top, String curNS) {
for (EntityMention e: entities) {
Element ee = toXML(e, curNS);
top.appendChild(ee);
}
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:10,
代码来源:XMLOutputter.java
示例5: originalFindSyntacticHead
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
/**
* This is the original version of {@link #findSyntacticHead} before Chris's modifications.
* There's no good reason to use it except for producing historical results.
* It Finds the syntactic head of the given entity mention.
*
* @param ent The entity mention
* @param root The Tree for the entire sentence in which it occurs.
* @param tokens The Sentence in which it occurs
* @return The tree object corresponding to the head. This MUST be a child of root.
* It will be a leaf in the parse tree.
*/
public Tree originalFindSyntacticHead(EntityMention ent, Tree root, List<CoreLabel> tokens) {
logger.fine("Searching for tree matching " + ent);
Tree exactMatch = findTreeWithSpan(root, ent.getExtentTokenStart(), ent.getExtentTokenEnd());
//
// found an exact match
//
if (exactMatch != null) {
logger.fine("Mention \"" + ent + "\" mapped to tree: " + printTree(exactMatch));
return safeHead(exactMatch);
}
//
// no exact match found
// in this case, we parse the actual extent of the mention
//
List<CoreLabel> extentTokens = new ArrayList<CoreLabel>();
for (int i = ent.getExtentTokenStart(); i < ent.getExtentTokenEnd(); i++)
extentTokens.add(tokens.get(i));
Tree tree = parse(extentTokens);
logger.fine("No exact match found. Local parse:\n" + tree.pennString());
convertToCoreLabels(tree);
tree.indexSpans(ent.getExtentTokenStart());
Tree extentHead = safeHead(tree);
assert (extentHead != null);
// extentHead is a child in the local extent parse tree. we need to find the
// corresponding node in the main tree
CoreLabel l = (CoreLabel) extentHead.label();
Tree realHead = findTreeWithSpan(root, l.get(CoreAnnotations.BeginIndexAnnotation.class), l.get(CoreAnnotations.EndIndexAnnotation.class));
assert (realHead != null);
return realHead;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:46,
代码来源:GenericDataSetReader.java
示例6: countMentionTypes
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private void countMentionTypes(CoreMap sent) {
List<EntityMention> mentions = sent.get(MachineReadingAnnotations.EntityMentionsAnnotation.class);
if(mentions != null){
for(EntityMention m: mentions){
mentionTypeCounts.incrementCount(m.getMentionType());
}
}
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:9,
代码来源:AceReader.java
示例7: countNameRelations
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private void countNameRelations(CoreMap sent) {
List<RelationMention> mentions = sent.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);
if(mentions != null){
for(RelationMention m: mentions) {
List<EntityMention> args = m.getEntityMentionArgs();
if(args.size() == 2 && args.get(0).getMentionType().equals("NAM") && args.get(1).getMentionType().equals("NAM")){
nameRelationCounts.incrementCount(m.getType() + "." + m.getSubType());
}
}
}
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:12,
代码来源:AceReader.java
示例8: countAdjacentMentions
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private void countAdjacentMentions(CoreMap sent) {
List<EntityMention> mentions = sent.get(MachineReadingAnnotations.EntityMentionsAnnotation.class);
if(mentions != null){
for(EntityMention m1: mentions){
for(EntityMention m2: mentions){
if(m1 == m2) continue;
if(m1.getHeadTokenEnd() == m2.getHeadTokenStart() && m1.getType().equals(m2.getType())){
adjacentEntityMentions.incrementCount(m1.getType());
}
}
}
}
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:14,
代码来源:AceReader.java
示例9: convertAceRelationMention
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private RelationMention convertAceRelationMention(AceRelationMention aceRelationMention, String docId,
CoreMap sentence, Map<String, EntityMention> entityMap) {
List<AceRelationMentionArgument> args = Arrays.asList(aceRelationMention.getArgs());
List<ExtractionObject> convertedArgs = new ArrayList<ExtractionObject>();
List<String> argNames = new ArrayList<String>();
// the arguments are already stored in semantic order. Make sure we preserve the same ordering!
int left = Integer.MAX_VALUE;
int right = Integer.MIN_VALUE;
for (AceRelationMentionArgument arg : args) {
ExtractionObject o = entityMap.get(arg.getContent().getId());
if(o == null){
logger.severe("READER ERROR: Failed to find relation argument with id " + arg.getContent().getId());
logger.severe("This happens because a few relation mentions illegally span multiple sentences. Will ignore this mention.");
return null;
}
convertedArgs.add(o);
argNames.add(arg.getRole());
if(o.getExtentTokenStart() < left) left = o.getExtentTokenStart();
if(o.getExtentTokenEnd() > right) right = o.getExtentTokenEnd();
}
if(argNames.size() != 2 || ! argNames.get(0).equalsIgnoreCase("arg-1") || ! argNames.get(1).equalsIgnoreCase("arg-2")){
logger.severe("READER ERROR: Invalid succession of arguments in relation mention: " + argNames);
logger.severe("ACE relations must have two arguments. Will ignore this mention.");
return null;
}
RelationMention relation = new RelationMention(
aceRelationMention.getId(),
sentence,
new Span(left, right),
aceRelationMention.getParent().getType(),
aceRelationMention.getParent().getSubtype(),
convertedArgs,
null);
return relation;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:39,
代码来源:AceReader.java
示例10: extractGoldMentions
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
private void extractGoldMentions(CoreMap s, List<List<Mention>> allGoldMentions, EntityComparator comparator) {
List<Mention> goldMentions = new ArrayList<Mention>();
allGoldMentions.add(goldMentions);
List<EntityMention> goldMentionList = s.get(MachineReadingAnnotations.EntityMentionsAnnotation.class);
List<CoreLabel> words = s.get(CoreAnnotations.TokensAnnotation.class);
TreeSet<EntityMention> treeForSortGoldMentions = new TreeSet<EntityMention>(comparator);
if(goldMentionList!=null) treeForSortGoldMentions.addAll(goldMentionList);
if(!treeForSortGoldMentions.isEmpty()){
for(EntityMention e : treeForSortGoldMentions){
Mention men = new Mention();
men.dependency = s.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
men.startIndex = e.getExtentTokenStart();
men.endIndex = e.getExtentTokenEnd();
String[] parseID = e.getObjectId().split("-");
men.mentionID = Integer.parseInt(parseID[parseID.length-1]);
String[] parseCorefID = e.getCorefID().split("-E");
men.goldCorefClusterID = Integer.parseInt(parseCorefID[parseCorefID.length-1]);
men.originalRef = -1;
for(int j=allGoldMentions.size()-1 ; j>=0 ; j--){
List<Mention> l = allGoldMentions.get(j);
for(int k=l.size()-1 ; k>=0 ; k--){
Mention m = l.get(k);
if(men.goldCorefClusterID == m.goldCorefClusterID){
men.originalRef = m.mentionID;
}
}
}
goldMentions.add(men);
if(men.mentionID > maxID) maxID = men.mentionID;
// set ner type
for(int j = e.getExtentTokenStart() ; j < e.getExtentTokenEnd() ; j++){
CoreLabel word = words.get(j);
String ner = e.getType() +"-"+ e.getSubType();
if(Constants.USE_GOLD_NE){
word.set(CoreAnnotations.EntityTypeAnnotation.class, e.getMentionType());
if(e.getMentionType().equals("NAM")) word.set(CoreAnnotations.NamedEntityTagAnnotation.class, ner);
}
}
}
}
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:46,
代码来源:ACEMentionExtractor.java
示例11: findSyntacticHead
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
/**
* Finds the syntactic head of the given entity mention.
*
* @param ent The entity mention
* @param root The Tree for the entire sentence in which it occurs.
* @param tokens The Sentence in which it occurs
* @return The tree object corresponding to the head. This MUST be a child of root.
* It will be a leaf in the parse tree.
*/
public Tree findSyntacticHead(EntityMention ent, Tree root, List<CoreLabel> tokens) {
if (!useNewHeadFinder) {
return originalFindSyntacticHead(ent, root, tokens);
}
logger.fine("Searching for tree matching " + ent);
Tree exactMatch = findTreeWithSpan(root, ent.getExtentTokenStart(), ent.getExtentTokenEnd());
//
// found an exact match
//
if (exactMatch != null) {
logger.fine("Mention \"" + ent + "\" mapped to tree: " + printTree(exactMatch));
return safeHead(exactMatch);
}
// no exact match found
// in this case, we parse the actual extent of the mention, embedded in a sentence
// context, so as to make the parser work better :-)
int approximateness = 0;
List<CoreLabel> extentTokens = new ArrayList<CoreLabel>();
extentTokens.add(initCoreLabel("It"));
extentTokens.add(initCoreLabel("was"));
final int ADDED_WORDS = 2;
for (int i = ent.getExtentTokenStart(); i < ent.getExtentTokenEnd(); i++) {
// Add everything except separated dashes! The separated dashes mess with the parser too badly.
CoreLabel label = tokens.get(i);
if ( ! "-".equals(label.word())) {
extentTokens.add(tokens.get(i));
} else {
approximateness++;
}
}
extentTokens.add(initCoreLabel("."));
// constrain the parse to the part we're interested in.
// Starting from ADDED_WORDS comes from skipping "It was".
// -1 to exclude the period.
// We now let it be any kind of nominal constituent, since there
// are VP and S ones
ParserConstraint constraint = new ParserConstraint();
constraint.start = ADDED_WORDS;
constraint.end = extentTokens.size() - 1;
constraint.state = Pattern.compile(".*");
List<ParserConstraint> constraints = Collections.singletonList(constraint);
Tree tree = parse(extentTokens, constraints);
logger.fine("No exact match found. Local parse:\n" + tree.pennString());
convertToCoreLabels(tree);
tree.indexSpans(ent.getExtentTokenStart() - ADDED_WORDS); // remember it has ADDED_WORDS extra words at the beginning
Tree subtree = findPartialSpan(tree, ent.getExtentTokenStart());
Tree extentHead = safeHead(subtree);
logger.fine("Head is: " + extentHead);
assert(extentHead != null);
// extentHead is a child in the local extent parse tree. we need to find the corresponding node in the main tree
// Because we deleted dashes, it's index will be >= the index in the extent parse tree
CoreLabel l = (CoreLabel) extentHead.label();
// Tree realHead = findTreeWithSpan(root, l.get(CoreAnnotations.BeginIndexAnnotation.class), l.get(CoreAnnotations.EndIndexAnnotation.class));
Tree realHead = funkyFindLeafWithApproximateSpan(root, l.value(), l.get(CoreAnnotations.BeginIndexAnnotation.class), approximateness);
if(realHead != null) logger.fine("Chosen head: " + realHead);
return realHead;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:72,
代码来源:GenericDataSetReader.java
示例12: convertAceEntityMention
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
/**
* Convert an {@link AceEntityMention} to an {@link EntityMention}.
*
* @param entityMention {@link AceEntityMention} to convert
* @param docId ID of the document containing this entity mention
* @param sentence
* @param tokenOffset An offset in the calculations of position of the extent to sentence boundary
* (the ace.reader stores absolute token offset from the beginning of the document, but
* we need token offsets from the beginning of the sentence => adjust by tokenOffset)
* @return entity as an {@link EntityMention}
*/
private EntityMention convertAceEntityMention(AceEntityMention entityMention, String docId, CoreMap sentence, int tokenOffset) {
//System.err.println("TYPE is " + entityMention.getParent().getType());
//System.err.println("SUBTYPE is " + entityMention.getParent().getSubtype());
//System.err.println("LDCTYPE is " + entityMention.getLdctype());
AceCharSeq ext = entityMention.getExtent();
AceCharSeq head = entityMention.getHead();
int extStart = ext.getTokenStart() - tokenOffset;
int extEnd = ext.getTokenEnd() - tokenOffset + 1;
if (extStart < 0) {
logger.severe("READER ERROR: Invalid extent start " + extStart + " for entity mention " + entityMention.getId() + " in document " + docId + " in sentence " + sentence);
logger.severe("This may happen due to incorrect EOS detection. Adjusting entity extent.");
extStart = 0;
}
if (extEnd > sentence.get(CoreAnnotations.TokensAnnotation.class).size()) {
logger.severe("READER ERROR: Invalid extent end " + extEnd + " for entity mention " + entityMention.getId() + " in document " + docId + " in sentence " + sentence);
logger.severe("This may happen due to incorrect EOS detection. Adjusting entity extent.");
extEnd = sentence.get(CoreAnnotations.TokensAnnotation.class).size();
}
int headStart = head.getTokenStart() - tokenOffset;
int headEnd = head.getTokenEnd() - tokenOffset + 1;
if (headStart < 0) {
logger.severe("READER ERROR: Invalid head start " + headStart + " for entity mention " + entityMention.getId() + " in document " + docId + " in sentence " + sentence);
logger.severe("This may happen due to incorrect EOS detection. Adjusting entity head span.");
headStart = 0;
}
if(headEnd > sentence.get(CoreAnnotations.TokensAnnotation.class).size()){
logger.severe("READER ERROR: Invalid head end " + headEnd + " for entity mention " + entityMention.getId() + " in document " + docId + " in sentence " + sentence);
logger.severe("This may happen due to incorrect EOS detection. Adjusting entity head span.");
headEnd = sentence.get(CoreAnnotations.TokensAnnotation.class).size();
}
// must adjust due to possible incorrect EOS detection
if(headStart < extStart){
headStart = extStart;
}
if(headEnd > extEnd){
headEnd = extEnd;
}
assert(headStart < headEnd);
// note: the ace.reader stores absolute token offset from the beginning of the document, but
// we need token offsets from the beginning of the sentence => adjust by tokenOffset
// note: in ace.reader the end token position is inclusive, but
// in our setup the end token position is exclusive => add 1 to end
EntityMention converted = new EntityMention(
entityMention.getId(),
sentence,
new Span(extStart, extEnd),
new Span(headStart, headEnd),
entityMention.getParent().getType(),
entityMention.getParent().getSubtype(),
entityMention.getLdctype());
return converted;
}
开发者ID:benblamey,
项目名称:stanford-nlp,
代码行数:69,
代码来源:AceReader.java
示例13: checkrules
点赞 2
import edu.stanford.nlp.ie.machinereading.structure.EntityMention; //导入依赖的package包/类
public boolean checkrules(final RelationMention relationMention) {
boolean valid = false;
if ((relationMention.getType() != null)
&& (relationMention.getType() != StanfordRelations.NoRelation.toString())) {
final List<EntityMention> entities = relationMention.getEntityMentionArgs();
if (entities.size() != 2) {
LOG.warn("EntityMention for relation is not 2!");
LOG.warn(entities);
} else {
final EntityMention emOne = entities.get(0);
final EntityMention emTwo = entities.get(1);
final StanfordRelations stanfordRelation =
StanfordRelations.fromString(relationMention.getType());
if (LOG.isTraceEnabled()) {
LOG.trace(stanfordRelation + "(" + emOne.getType() + " " + emTwo.getType() + ")" + " ("
+ emOne.getValue() + " " + emTwo.getValue() + ")");
}
switch (stanfordRelation) {
case Live_In:
if (EntityClassMap.P.equals(StanfordENOldVersion.stanford(emOne.getType()))
&& EntityClassMap.L.equals(StanfordENOldVersion.stanford(emTwo.getType()))) {
valid = true;
}
break;
case Work_For:
if (EntityClassMap.P.equals(StanfordENOldVersion.stanford(emOne.getType()))
&& EntityClassMap.O.equals(StanfordENOldVersion.stanford(emTwo.getType()))) {
valid = true;
}
break;
case OrgBased_In:
if (EntityClassMap.O.equals(StanfordENOldVersion.stanford(emOne.getType()))
&& EntityClassMap.L.equals(StanfordENOldVersion.stanford(emTwo.getType()))) {
valid = true;
}
break;
case Located_In:
if (EntityClassMap.L.equals(StanfordENOldVersion.stanford(emOne.getType()))
&& EntityClassMap.L.equals(StanfordENOldVersion.stanford(emTwo.getType()))) {
valid = true;
}
break;
default: {
}
}
}
}
return valid;
}
开发者ID:dice-group,
项目名称:FOX,
代码行数:55,
代码来源:REStanford.java