Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.mitre.ace2004.callisto.ExportAPF5_0_2.java

License:Open Source License

private void addEvent(Element doc, AWBDocument awbDoc, HasSubordinates aceEvent) {

    String id = toString(aceEvent.getAttributeValue("ace_id"));
    String type = toString(aceEvent.getAttributeValue("type"));
    String subtype = toString(aceEvent.getAttributeValue("subtype"));
    String modality = toString(aceEvent.getAttributeValue("modality"));
    String polarity = toString(aceEvent.getAttributeValue("polarity"));
    String genericity = toString(aceEvent.getAttributeValue("genericity"));
    String tense = toString(aceEvent.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    if ("Other".equals(modality))
        modality = "Unspecified";
    if ("Unspecified".equals(tense))
        tense = null;/*from   www .  ja v  a 2s  .c o  m*/

    if (DEBUG > 0)
        System.err.println("  event: " + aceEvent.getId() + ": " + id);

    Element event = doc.addElement("event").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("MODALITY", modality)
            .addAttribute("POLARITY", polarity).addAttribute("GENERICITY", genericity)
            .addAttribute("TENSE", tense);

    // Promote event_mention_arguments to the event_arguments, while
    // creating the event_mentions.  Add these event_mentions to the
    // event after, so that event_arguments appear first in XML

    // collect event_mentions for later addition
    Vector mentions = new Vector();

    // collect event_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    AWBAnnotation[] aceMentions = aceEvent.getSubordinates(ACE2004Utils.EVENT_MENTION_TYPE);

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the event before adding the mentions to the relation
    for (int i = 0; i < aceMentions.length; i++) {
        TextExtentRegion aceMention = (TextExtentRegion) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String level = toString(aceMention.getAttributeValue("level"));

        if (DEBUG > 0)
            System.err.println("  event-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to event /after/ arguments
        Element mention = DocumentHelper.createElement("event_mention").addAttribute("ID", mentionID)
                .addAttribute("LEVEL", level);

        mentions.add(mention);

        // the text regions
        // TODO: The maia is rather old here. it needs to be fixed!!!
        TextExtentRegion aceExtent = (TextExtentRegion) aceMention.getAttributeValue("extent");

        if (aceExtent != null) {
            Element extent = mention.addElement("extent");
            addCharseq(extent, awbDoc, aceExtent.getTextExtentStart(), aceExtent.getTextExtentEnd());
        } else {
            System.err.println("Event-Mention " + mentionID + " missing extent");
        }

        // TODO: There may be multiple here (there aren't: see comment above)
        Element anchor = mention.addElement("anchor");
        addCharseq(anchor, awbDoc, aceMention.getTextExtentStart(), aceMention.getTextExtentEnd());

        // add mention_arguments and promote to event_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(event, mention, parentArgs, "event", aceArguments);
    } // for (...mention-event...)

    // add the collected (unique) event_aguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(event, "event_argument", role, value);
        }
    }

    // finally add the event_mentions to the event
    Iterator iter = mentions.iterator();
    while (iter.hasNext()) {
        event.add((Element) iter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_0.java

License:Open Source License

private void addRelation(Element doc, AWBDocument awbDoc, HasSubordinates aceRelation) {

    String id = toString(aceRelation.getAttributeValue("ace_id"));
    String type = toString(aceRelation.getAttributeValue("type"));
    String subtype = toString(aceRelation.getAttributeValue("subtype"));
    String modality = toString(aceRelation.getAttributeValue("modality"));
    String tense = toString(aceRelation.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    if ("Other".equals(subtype))
        subtype = null;/*  ww  w.  jav a 2  s . c  o m*/

    if (DEBUG > 0)
        System.err.println("  relation: " + aceRelation.getId() + ": " + id);

    Element relation = doc.addElement("relation").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("TENSE", tense).addAttribute("MODALITY", modality);

    // RK 8/15/05 grab mentions now in case needed to infer arg-1 or arg-2
    AWBAnnotation[] aceMentions = aceRelation.getSubordinates(ACE2004Utils.RELATION_MENTION_TYPE);

    // add arg-1 and arg-2 as argument elements
    AWBAnnotation arg1 = (AWBAnnotation) aceRelation.getAttributeValue("arg1");
    if (arg1 == null) {
        // RK 8/15/05 if arg1 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
            AWBAnnotation mentionArg1Entity = ACE2004Task.getParent(mentionArg1, ACE2004Task.ENTITY_TYPE_NAME);
            arg1 = mentionArg1Entity;
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-1", arg1);

    AWBAnnotation arg2 = (AWBAnnotation) aceRelation.getAttributeValue("arg2");
    if (arg2 == null) {
        // RK 8/15/05 if arg2 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
            AWBAnnotation mentionArg2Entity = ACE2004Task.getParent(mentionArg2, ACE2004Task.ENTITY_TYPE_NAME);
            arg2 = mentionArg2Entity;
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-2", arg2);

    // Promote relation_mention_arguments to the relation_arguments, while
    // creating the relation_mentions.  Add these relation_mentions to the
    // relation after, so that relation_arguments appear first in XML

    // collect relation_mentions for later addition
    Vector mentions = new Vector();

    // collect relation_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the relation before adding the mentions to the
    // relation
    for (int i = 0; i < aceMentions.length; i++) {
        // HasSubordinates aceMention = (HasSubordinates) aceMentions[i];
        AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String condition = toString(aceMention.getAttributeValue("lexicalcondition"));

        if (DEBUG > 0)
            System.err.println("  relation-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to relation /after/ arguments
        Element mention = DocumentHelper.createElement("relation_mention").addAttribute("ID", mentionID)
                .addAttribute("LEXICALCONDITION", condition);

        mentions.add(mention);

        // add arg-1 and arg-2 as argument elements
        AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
        addMentionArgument(mention, "relation_mention_argument", "Arg-1", mentionArg1, awbDoc);

        AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
        addMentionArgument(mention, "relation_mention_argument", "Arg-2", mentionArg2, awbDoc);

        // add mention_extent
        Element relMenExtent = mention.addElement("extent");

        TextExtentRegion relMenAnnotTER = (TextExtentRegion) aceMention
                .getAttributeValue("relation-mention-extent");
        if (DEBUG > 0)
            System.err.println("  relation-mention-extent: " + relMenAnnotTER.getTextExtentStart() + ","
                    + relMenAnnotTER.getTextExtentEnd() + ": " + awbDoc.getSignal().getCharsAt(
                            relMenAnnotTER.getTextExtentStart(), relMenAnnotTER.getTextExtentEnd()));

        addCharseq(relMenExtent, awbDoc, relMenAnnotTER.getTextExtentStart(),
                relMenAnnotTER.getTextExtentEnd());

        // add mention_arguments and promote to relation_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(relation, mention, parentArgs, "relation", aceArguments, awbDoc);
    } // for (...mention-relation...)

    // add the collected (unique) relation_arguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(relation, "relation_argument", role, value);
        }
    }

    // finally add the relation_mentions to the relation
    Iterator mentionIter = mentions.iterator();
    while (mentionIter.hasNext()) {
        relation.add((Element) mentionIter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_0.java

License:Open Source License

private void addEvent(Element doc, AWBDocument awbDoc, HasSubordinates aceEvent) {

    String id = toString(aceEvent.getAttributeValue("ace_id"));
    String type = toString(aceEvent.getAttributeValue("type"));
    String subtype = toString(aceEvent.getAttributeValue("subtype"));
    String modality = toString(aceEvent.getAttributeValue("modality"));
    String polarity = toString(aceEvent.getAttributeValue("polarity"));
    String genericity = toString(aceEvent.getAttributeValue("genericity"));
    String tense = toString(aceEvent.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    if ("Other".equals(modality))
        modality = "Unspecified";
    if ("Unspecified".equals(tense))
        tense = null;//w w  w  .  j a  va2s .  c o  m

    if (DEBUG > 0)
        System.err.println("  event: " + aceEvent.getId() + ": " + id);

    Element event = doc.addElement("event").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("MODALITY", modality)
            .addAttribute("POLARITY", polarity).addAttribute("GENERICITY", genericity)
            .addAttribute("TENSE", tense);

    // Promote event_mention_arguments to the event_arguments, while
    // creating the event_mentions.  Add these event_mentions to the
    // event after, so that event_arguments appear first in XML

    // collect event_mentions for later addition
    Vector mentions = new Vector();

    // collect event_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    AWBAnnotation[] aceMentions = aceEvent.getSubordinates(ACE2004Utils.EVENT_MENTION_TYPE);

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the event before adding the mentions to the relation
    for (int i = 0; i < aceMentions.length; i++) {
        TextExtentRegion aceMention = (TextExtentRegion) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String level = toString(aceMention.getAttributeValue("level"));
        String ldcScopeStartString = toString(aceMention.getAttributeValue("ldcscope-start"));
        String ldcScopeEndString = toString(aceMention.getAttributeValue("ldcscope-end"));
        int ldcScopeStart = -1, ldcScopeEnd = -1;

        if (ldcScopeStartString != null && ldcScopeStartString.length() > 0 && ldcScopeEndString != null
                && ldcScopeEndString.length() > 0) {
            ldcScopeStart = Integer.parseInt(ldcScopeStartString);
            ldcScopeEnd = Integer.parseInt(ldcScopeEndString);
        }
        /*
        System.err.println(" ldcScopeStartString = " + ldcScopeStartString);
        System.err.println(" ldcScopeEndString   = " + ldcScopeEndString);
        System.err.println(" ldcScopeStart       = " + ldcScopeStart);
        System.err.println(" ldcScopeEnd         = " + ldcScopeEnd);
        */

        if (DEBUG > 0)
            System.err.println("  event-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to event /after/ arguments
        Element mention = DocumentHelper.createElement("event_mention").addAttribute("ID", mentionID)
                .addAttribute("LEVEL", level);

        mentions.add(mention);

        // the text regions
        // TODO: The maia is rather old here. it needs to be fixed!!!
        TextExtentRegion aceExtent = (TextExtentRegion) aceMention.getAttributeValue("extent");

        if (aceExtent != null) {
            Element extent = mention.addElement("extent");
            addCharseq(extent, awbDoc, aceExtent.getTextExtentStart(), aceExtent.getTextExtentEnd());
        } else {
            System.err.println("Event-Mention " + mentionID + " missing extent");
        }

        if (ldcScopeStart >= 0 && ldcScopeEnd >= 0) {
            Element ldcscope = mention.addElement("ldc_scope");
            // Note: We don't actually end up using the ldcScopeString!
            // Also, since this was read in directly from APF, we add 1 to
            // the offset, since the addCharseq method automatically subtracts
            // from the end point to create valid charseq values.
            addCharseq(ldcscope, awbDoc, ldcScopeStart, ldcScopeEnd + 1);

        } else {
            System.err.println("Event " + mentionID + " missing ldc_scope");
        }

        // TODO: There may be multiple here (there aren't: see comment above)
        Element anchor = mention.addElement("anchor");
        addCharseq(anchor, awbDoc, aceMention.getTextExtentStart(), aceMention.getTextExtentEnd());

        // add mention_arguments and promote to event_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(event, mention, parentArgs, "event", aceArguments, awbDoc);
    } // for (...mention-event...)

    // add the collected (unique) event_aguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(event, "event_argument", role, value);
        }
    }

    // finally add the event_mentions to the event
    Iterator iter = mentions.iterator();
    while (iter.hasNext()) {
        event.add((Element) iter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_1.java

License:Open Source License

private void addRelation(Element doc, AWBDocument awbDoc, HasSubordinates aceRelation) {

    String id = toString(aceRelation.getAttributeValue("ace_id"));
    String type = toString(aceRelation.getAttributeValue("type"));
    String subtype = toString(aceRelation.getAttributeValue("subtype"));
    String modality = toString(aceRelation.getAttributeValue("modality"));
    String tense = toString(aceRelation.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    if ("Other".equals(subtype))
        subtype = null;/*w ww . ja  v  a2 s.c o m*/

    if (DEBUG > 0)
        System.err.println("  relation: " + aceRelation.getId() + ": " + id);

    if (DEBUG > 2)
        System.err.println("    modality: " + (modality == null ? "null" : modality) + "    tense: "
                + (tense == null ? "null" : tense));

    Element relation = doc.addElement("relation").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("TENSE", tense).addAttribute("MODALITY", modality);

    // RK 8/15/05 grab mentions now in case needed to infer arg-1 or arg-2
    AWBAnnotation[] aceMentions = aceRelation.getSubordinates(ACE2004Utils.RELATION_MENTION_TYPE);

    // add arg-1 and arg-2 as argument elements
    AWBAnnotation arg1 = (AWBAnnotation) aceRelation.getAttributeValue("arg1");
    if (arg1 == null) {
        // RK 8/15/05 if arg1 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
            AWBAnnotation mentionArg1Entity = ACE2004Task.getParent(mentionArg1, ACE2004Task.ENTITY_TYPE_NAME);
            arg1 = mentionArg1Entity;
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-1", arg1);

    AWBAnnotation arg2 = (AWBAnnotation) aceRelation.getAttributeValue("arg2");
    if (arg2 == null) {
        // RK 8/15/05 if arg2 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
            AWBAnnotation mentionArg2Entity = ACE2004Task.getParent(mentionArg2, ACE2004Task.ENTITY_TYPE_NAME);
            arg2 = mentionArg2Entity;
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-2", arg2);

    // Promote relation_mention_arguments to the relation_arguments, while
    // creating the relation_mentions.  Add these relation_mentions to the
    // relation after, so that relation_arguments appear first in XML

    // collect relation_mentions for later addition
    Vector mentions = new Vector();

    // collect relation_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the relation before adding the mentions to the
    // relation
    for (int i = 0; i < aceMentions.length; i++) {
        // HasSubordinates aceMention = (HasSubordinates) aceMentions[i];
        AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String condition = toString(aceMention.getAttributeValue("lexicalcondition"));

        if (DEBUG > 0)
            System.err.println("  relation-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to relation /after/ arguments
        Element mention = DocumentHelper.createElement("relation_mention").addAttribute("ID", mentionID)
                .addAttribute("LEXICALCONDITION", condition);

        mentions.add(mention);

        // add mention_extent
        Element relMenExtent = mention.addElement("extent");

        TextExtentRegion relMenAnnotTER = (TextExtentRegion) aceMention
                .getAttributeValue("relation-mention-extent");
        if (DEBUG > 0)
            System.err.println("  relation-mention-extent: " + relMenAnnotTER.getTextExtentStart() + ","
                    + relMenAnnotTER.getTextExtentEnd() + ": " + awbDoc.getSignal().getCharsAt(
                            relMenAnnotTER.getTextExtentStart(), relMenAnnotTER.getTextExtentEnd()));

        addCharseq(relMenExtent, awbDoc, relMenAnnotTER.getTextExtentStart(),
                relMenAnnotTER.getTextExtentEnd());

        // RK 10/10/05 moved args after extent, as required by current DTD
        // add arg-1 and arg-2 as argument elements
        AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
        addMentionArgument(mention, "relation_mention_argument", "Arg-1", mentionArg1, awbDoc);

        AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
        addMentionArgument(mention, "relation_mention_argument", "Arg-2", mentionArg2, awbDoc);

        // add mention_arguments and promote to relation_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(relation, mention, parentArgs, "relation", aceArguments, awbDoc);
    } // for (...mention-relation...)

    // add the collected (unique) relation_arguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(relation, "relation_argument", role, value);
        }
    }

    // finally add the relation_mentions to the relation
    Iterator mentionIter = mentions.iterator();
    while (mentionIter.hasNext()) {
        relation.add((Element) mentionIter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_1.java

License:Open Source License

private void addEvent(Element doc, AWBDocument awbDoc, HasSubordinates aceEvent) {

    String id = toString(aceEvent.getAttributeValue("ace_id"));
    String type = toString(aceEvent.getAttributeValue("type"));
    String subtype = toString(aceEvent.getAttributeValue("subtype"));
    String modality = toString(aceEvent.getAttributeValue("modality"));
    String polarity = toString(aceEvent.getAttributeValue("polarity"));
    String genericity = toString(aceEvent.getAttributeValue("genericity"));
    String tense = toString(aceEvent.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    // These legacy errors are now the required format for 5.1.1 again, 
    // so remove these "fixes"
    /***********************************************
    if ("Other".equals(modality))/*w w w .j  av a 2  s .  c o m*/
      modality = "Unspecified";
    if ("Unspecified".equals(tense))
      tense = null;
    *********************/

    // instead fix this Tense the other way -- if null, insert as "Unspecified"
    if (tense == null)
        tense = "Unspecified";

    if (DEBUG > 0)
        System.err.println("  event: " + aceEvent.getId() + ": " + id);

    Element event = doc.addElement("event").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("MODALITY", modality)
            .addAttribute("POLARITY", polarity).addAttribute("GENERICITY", genericity)
            .addAttribute("TENSE", tense);

    // Promote event_mention_arguments to the event_arguments, while
    // creating the event_mentions.  Add these event_mentions to the
    // event after, so that event_arguments appear first in XML

    // collect event_mentions for later addition
    Vector mentions = new Vector();

    // collect event_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    AWBAnnotation[] aceMentions = aceEvent.getSubordinates(ACE2004Utils.EVENT_MENTION_TYPE);

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the event before adding the mentions to the relation
    for (int i = 0; i < aceMentions.length; i++) {
        TextExtentRegion aceMention = (TextExtentRegion) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String level = toString(aceMention.getAttributeValue("level"));
        String ldcScopeStartString = toString(aceMention.getAttributeValue("ldcscope-start"));
        String ldcScopeEndString = toString(aceMention.getAttributeValue("ldcscope-end"));
        int ldcScopeStart = -1, ldcScopeEnd = -1;

        if (ldcScopeStartString != null && ldcScopeStartString.length() > 0 && ldcScopeEndString != null
                && ldcScopeEndString.length() > 0) {
            ldcScopeStart = Integer.parseInt(ldcScopeStartString);
            ldcScopeEnd = Integer.parseInt(ldcScopeEndString);
        }
        /*
        System.err.println(" ldcScopeStartString = " + ldcScopeStartString);
        System.err.println(" ldcScopeEndString   = " + ldcScopeEndString);
        System.err.println(" ldcScopeStart       = " + ldcScopeStart);
        System.err.println(" ldcScopeEnd         = " + ldcScopeEnd);
        */

        if (DEBUG > 0)
            System.err.println("  event-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to event /after/ arguments
        Element mention = DocumentHelper.createElement("event_mention").addAttribute("ID", mentionID)
                .addAttribute("LEVEL", level);

        mentions.add(mention);

        // the text regions
        // TODO: The maia is rather old here. it needs to be fixed!!!
        TextExtentRegion aceExtent = (TextExtentRegion) aceMention.getAttributeValue("extent");

        if (aceExtent != null) {
            Element extent = mention.addElement("extent");
            addCharseq(extent, awbDoc, aceExtent.getTextExtentStart(), aceExtent.getTextExtentEnd());
        } else {
            System.err.println("Event-Mention " + mentionID + " missing extent");
        }

        if (ldcScopeStart >= 0 && ldcScopeEnd >= 0) {
            Element ldcscope = mention.addElement("ldc_scope");
            // Note: We don't actually end up using the ldcScopeString!
            // Also, since this was read in directly from APF, we add 1 to
            // the offset, since the addCharseq method automatically subtracts
            // from the end point to create valid charseq values.
            addCharseq(ldcscope, awbDoc, ldcScopeStart, ldcScopeEnd + 1);

        } else {
            System.err.println("Event " + mentionID + " missing ldc_scope");
        }

        // TODO: There may be multiple here (there aren't: see comment above)
        Element anchor = mention.addElement("anchor");
        addCharseq(anchor, awbDoc, aceMention.getTextExtentStart(), aceMention.getTextExtentEnd());

        // add mention_arguments and promote to event_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(event, mention, parentArgs, "event", aceArguments, awbDoc);
    } // for (...mention-event...)

    // add the collected (unique) event_aguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(event, "event_argument", role, value);
        }
    }

    // finally add the event_mentions to the event
    Iterator iter = mentions.iterator();
    while (iter.hasNext()) {
        event.add((Element) iter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_5.java

License:Open Source License

private void addRelation(Element doc, AWBDocument awbDoc, HasSubordinates aceRelation) {

    String id = toString(aceRelation.getAttributeValue("ace_id"));
    String type = toString(aceRelation.getAttributeValue("type"));
    String subtype = toString(aceRelation.getAttributeValue("subtype"));
    String modality = toString(aceRelation.getAttributeValue("modality"));
    String tense = toString(aceRelation.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    if ("Other".equals(subtype))
        subtype = null;//from  w w  w  .  ja v  a 2 s  .c  o m

    if (DEBUG > 0)
        System.err.println("  relation: " + aceRelation.getId() + ": " + id);

    if (DEBUG > 2)
        System.err.println("    modality: " + (modality == null ? "null" : modality) + "    tense: "
                + (tense == null ? "null" : tense));

    Element relation = doc.addElement("relation").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("TENSE", tense).addAttribute("MODALITY", modality);

    // RK 8/15/05 grab mentions now in case needed to infer arg-1 or arg-2
    AWBAnnotation[] aceMentions = aceRelation.getSubordinates(ACE2004Utils.RELATION_MENTION_TYPE);

    // add arg-1 and arg-2 as argument elements
    AWBAnnotation arg1 = (AWBAnnotation) aceRelation.getAttributeValue("arg1");
    if (arg1 == null) {
        // RK 8/15/05 if arg1 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
            if (mentionArg1.getAnnotationType().equals(ACE2004Utils.ENTITY_TYPE)) {
                arg1 = mentionArg1;
            } else {
                AWBAnnotation mentionArg1Entity = ACE2004Task.getParent(mentionArg1,
                        ACE2004Task.ENTITY_TYPE_NAME);
                arg1 = mentionArg1Entity;
            }
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-1", arg1);

    AWBAnnotation arg2 = (AWBAnnotation) aceRelation.getAttributeValue("arg2");
    if (arg2 == null) {
        // RK 8/15/05 if arg2 not found, infer it from a relation mention now
        for (int i = 0; i < aceMentions.length; i++) {
            AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];
            AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
            if (mentionArg2.getAnnotationType().equals(ACE2004Utils.ENTITY_TYPE)) {
                arg2 = mentionArg2;
            } else {
                AWBAnnotation mentionArg2Entity = ACE2004Task.getParent(mentionArg2,
                        ACE2004Task.ENTITY_TYPE_NAME);
                arg2 = mentionArg2Entity;
            }
            break;
        }
    }
    addArgument(relation, "relation_argument", "Arg-2", arg2);

    // Promote relation_mention_arguments to the relation_arguments, while
    // creating the relation_mentions.  Add these relation_mentions to the
    // relation after, so that relation_arguments appear first in XML

    // collect relation_mentions for later addition
    Vector mentions = new Vector();

    // collect relation_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    // Loop over mentions: create standalone, adding structure, but promoting
    // it's arguments to the relation before adding the mentions to the
    // relation
    for (int i = 0; i < aceMentions.length; i++) {
        // HasSubordinates aceMention = (HasSubordinates) aceMentions[i];
        AWBAnnotation aceMention = (AWBAnnotation) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String condition = toString(aceMention.getAttributeValue("lexicalcondition"));

        if (DEBUG > 0)
            System.err.println("  relation-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to relation /after/ arguments
        Element mention = DocumentHelper.createElement("relation_mention").addAttribute("ID", mentionID)
                .addAttribute("LEXICALCONDITION", condition);

        mentions.add(mention);

        // add mention_extent
        Element relMenExtent = mention.addElement("extent");

        TextExtentRegion relMenAnnotTER = (TextExtentRegion) aceMention
                .getAttributeValue("relation-mention-extent");
        if (DEBUG > 0)
            System.err.println("  relation-mention-extent: " + relMenAnnotTER.getTextExtentStart() + ","
                    + relMenAnnotTER.getTextExtentEnd() + ": " + awbDoc.getSignal().getCharsAt(
                            relMenAnnotTER.getTextExtentStart(), relMenAnnotTER.getTextExtentEnd()));

        addCharseq(relMenExtent, awbDoc, relMenAnnotTER.getTextExtentStart(),
                relMenAnnotTER.getTextExtentEnd());

        // RK 10/10/05 moved args after extent, as required by current DTD
        // add arg-1 and arg-2 as argument elements
        AWBAnnotation mentionArg1 = (AWBAnnotation) aceMention.getAttributeValue("arg1");
        // RK 3/14/07 if null, must get the entity arg from the parent
        if (mentionArg1 == null) {
            mentionArg1 = arg1;
        }
        addMentionArgument(mention, "relation_mention_argument", "Arg-1", mentionArg1, awbDoc);

        AWBAnnotation mentionArg2 = (AWBAnnotation) aceMention.getAttributeValue("arg2");
        if (mentionArg2 == null) {
            mentionArg2 = arg2;
        }
        addMentionArgument(mention, "relation_mention_argument", "Arg-2", mentionArg2, awbDoc);

        // add mention_arguments and promote to relation_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(relation, mention, parentArgs, "relation", aceArguments, awbDoc);
    } // for (...mention-relation...)

    // add the collected (unique) relation_arguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(relation, "relation_argument", role, value);
        }
    }

    // finally add the relation_mentions to the relation
    Iterator mentionIter = mentions.iterator();
    while (mentionIter.hasNext()) {
        relation.add((Element) mentionIter.next());
    }
}

From source file:org.mitre.ace2004.callisto.ExportAPF5_1_5.java

License:Open Source License

private void addEvent(Element doc, AWBDocument awbDoc, HasSubordinates aceEvent) {

    String id = toString(aceEvent.getAttributeValue("ace_id"));
    String type = toString(aceEvent.getAttributeValue("type"));
    String subtype = toString(aceEvent.getAttributeValue("subtype"));
    String modality = toString(aceEvent.getAttributeValue("modality"));
    String polarity = toString(aceEvent.getAttributeValue("polarity"));
    String genericity = toString(aceEvent.getAttributeValue("genericity"));
    String tense = toString(aceEvent.getAttributeValue("tense"));

    // Case by case fixes for legacy errors:
    // These legacy errors are now the required format for 5.1.1 again, 
    // so remove these "fixes"
    /***********************************************
    if ("Other".equals(modality))//from   w  w w  . j a  va2  s.  com
      modality = "Unspecified";
    if ("Unspecified".equals(tense))
      tense = null;
    *********************/

    // instead fix this Tense the other way -- if null, insert as "Unspecified"
    if (tense == null)
        tense = "Unspecified";

    if (DEBUG > 0)
        System.err.println("  event: " + aceEvent.getId() + ": " + id);

    Element event = doc.addElement("event").addAttribute("ID", id).addAttribute("TYPE", type)
            .addAttribute("SUBTYPE", subtype).addAttribute("MODALITY", modality)
            .addAttribute("POLARITY", polarity).addAttribute("GENERICITY", genericity)
            .addAttribute("TENSE", tense);

    // Promote event_mention_arguments to the event_arguments, while
    // creating the event_mentions.  Add these event_mentions to the
    // event after, so that event_arguments appear first in XML

    // collect event_mentions for later addition
    Vector mentions = new Vector();

    // collect event_arguments so they are unique
    LinkedHashMap parentArgs = new LinkedHashMap();

    AWBAnnotation[] aceMentions = aceEvent.getSubordinates(ACE2004Utils.EVENT_MENTION_TYPE);

    // Loop over mentions: create standalone, adding structure, but promoting
    // its arguments to the event before adding the mentions to the relation
    for (int i = 0; i < aceMentions.length; i++) {
        TextExtentRegion aceMention = (TextExtentRegion) aceMentions[i];

        String mentionID = toString(aceMention.getAttributeValue("ace_id"));
        String level = toString(aceMention.getAttributeValue("level"));
        String ldcScopeStartString = toString(aceMention.getAttributeValue("ldcscope-start"));
        String ldcScopeEndString = toString(aceMention.getAttributeValue("ldcscope-end"));
        int ldcScopeStart = -1, ldcScopeEnd = -1;

        if (ldcScopeStartString != null && ldcScopeStartString.length() > 0 && ldcScopeEndString != null
                && ldcScopeEndString.length() > 0) {
            ldcScopeStart = Integer.parseInt(ldcScopeStartString);
            ldcScopeEnd = Integer.parseInt(ldcScopeEndString);
        }
        /*
        System.err.println(" ldcScopeStartString = " + ldcScopeStartString);
        System.err.println(" ldcScopeEndString   = " + ldcScopeEndString);
        System.err.println(" ldcScopeStart       = " + ldcScopeStart);
        System.err.println(" ldcScopeEnd         = " + ldcScopeEnd);
        */

        if (DEBUG > 0)
            System.err.println("  event-mention: " + aceMention.getId() + ": " + mentionID);

        // Create standalone mention, and add to event /after/ arguments
        Element mention = DocumentHelper.createElement("event_mention").addAttribute("ID", mentionID)
                .addAttribute("LEVEL", level);

        mentions.add(mention);

        // the text regions
        // TODO: The maia is rather old here. it needs to be fixed!!!
        TextExtentRegion aceExtent = (TextExtentRegion) aceMention.getAttributeValue("extent");

        if (aceExtent != null) {
            Element extent = mention.addElement("extent");
            addCharseq(extent, awbDoc, aceExtent.getTextExtentStart(), aceExtent.getTextExtentEnd());
        } else {
            System.err.println("Event-Mention " + mentionID + " missing extent");
        }

        if (ldcScopeStart >= 0 && ldcScopeEnd >= 0) {
            Element ldcscope = mention.addElement("ldc_scope");
            // Note: We don't actually end up using the ldcScopeString!
            // Also, since this was read in directly from APF, we add 1 to
            // the offset, since the addCharseq method automatically subtracts
            // from the end point to create valid charseq values.
            addCharseq(ldcscope, awbDoc, ldcScopeStart, ldcScopeEnd + 1);

        } else {
            System.err.println("Event " + mentionID + " missing ldc_scope");
        }

        // TODO: There may be multiple here (there aren't: see comment above)
        Element anchor = mention.addElement("anchor");
        addCharseq(anchor, awbDoc, aceMention.getTextExtentStart(), aceMention.getTextExtentEnd());

        // add mention_arguments and promote to event_arguments
        ATLASElementSet aceArguments = aceMention.getRegion()
                .getSubordinateSet(ACE2004Utils.ARGUMENT_MENTION_TYPE);

        addArgumentList(event, mention, parentArgs, "event", aceArguments, awbDoc);
    } // for (...mention-event...)

    // add the collected (unique) event_aguments
    Iterator argIter = parentArgs.keySet().iterator();
    while (argIter.hasNext()) {
        String role = (String) argIter.next();
        // there can be more than one value for a role, but no duplicate values
        // for the same role
        LinkedHashSet roleValues = (LinkedHashSet) parentArgs.get(role);

        Iterator valueIter = roleValues.iterator();
        while (valueIter.hasNext()) {
            AWBAnnotation value = (AWBAnnotation) valueIter.next();
            addArgument(event, "event_argument", role, value);
        }
    }

    // finally add the event_mentions to the event
    Iterator iter = mentions.iterator();
    while (iter.hasNext()) {
        event.add((Element) iter.next());
    }
}

From source file:org.mitre.muc.callisto.session.SessionLogger.java

License:Open Source License

/**
 * Load the specified document.//w  w  w  .jav a  2  s .c  om
 * @throw IOException to wrapper any dom4j errors
 */
private Document parseXML(File aFile) throws IOException {
    Document xdoc = null;
    if (aFile.canRead()) {
        try {
            xdoc = xmlReader.read(aFile);
        } catch (DocumentException de) {
            IOException x = new IOException();
            x.initCause(de);
            throw x;
        }
    } else {
        Element root = DocumentHelper.createElement(ROOT_NAME);
        xdoc = DocumentHelper.createDocument(root);
    }
    return xdoc;
}

From source file:org.neuclear.asset.contracts.AssetGlobals.java

License:Open Source License

public static Element createElement(final String name) {
    return DocumentHelper.createElement(createQName(name));
}

From source file:org.neuclear.xml.AbstractElementProxy.java

License:LGPL

protected AbstractElementProxy(final String name) {
    this.element = DocumentHelper.createElement(name);
}