예제 #1
0
 private static Element createResults0(Element element) {
   Element newElement = null;
   String tag = element.getLocalName();
   if (ResultsElement.TAG.equals(tag)) {
     newElement = new ResultsElement();
   } else if (ResultElement.TAG.equals(tag)) {
     newElement = new ResultElement();
   } else {
     LOG.error("Unknown element: " + tag);
   }
   XMLUtil.copyAttributes(element, newElement);
   for (int i = 0; i < element.getChildCount(); i++) {
     Node child = element.getChild(i);
     if (child instanceof Text) {
       child = child.copy();
     } else {
       child = ResultsElement.createResults0((Element) child);
     }
     if (newElement != null && child != null) {
       newElement.appendChild(child);
     }
   }
   LOG.trace("XML :" + newElement.toXML());
   return newElement;
 }
 private String childrenToString(Element e) {
   if (e == null) {
     return null;
   }
   String ret = "";
   for (int i = 0; i < e.getChildCount(); i++) {
     Node n = e.getChild(i);
     if (n instanceof Element) {
       ret += getText((Element) n) + ",";
     }
   }
   return ret.substring(0, ret.length() - 1);
 }
 protected static void createSubclassedChildren(
     Element oldElement, AbstractEditorElement newElement) {
   if (oldElement != null) {
     for (int i = 0; i < oldElement.getChildCount(); i++) {
       Node node = oldElement.getChild(i);
       Node newNode = null;
       if (node instanceof Text) {
         String value = node.getValue();
         newNode = new Text(value);
       } else if (node instanceof Comment) {
         newNode = new Comment(node.getValue());
       } else if (node instanceof ProcessingInstruction) {
         newNode = new ProcessingInstruction((ProcessingInstruction) node);
       } else if (node instanceof Element) {
         newNode = createEditorElement((Element) node);
       } else {
         throw new RuntimeException("Cannot create new node: " + node.getClass());
       }
       newElement.appendChild(newNode);
     }
   }
 }
 private static List<CoreMap> toTimexCoreMaps(Element docElem, CoreMap originalDocument) {
   // --Collect Token Offsets
   HashMap<Integer, Integer> beginMap = new HashMap<Integer, Integer>();
   HashMap<Integer, Integer> endMap = new HashMap<Integer, Integer>();
   boolean haveTokenOffsets = true;
   for (CoreMap sent : originalDocument.get(CoreAnnotations.SentencesAnnotation.class)) {
     for (CoreLabel token : sent.get(CoreAnnotations.TokensAnnotation.class)) {
       Integer tokBegin = token.get(CoreAnnotations.TokenBeginAnnotation.class);
       Integer tokEnd = token.get(CoreAnnotations.TokenEndAnnotation.class);
       if (tokBegin == null || tokEnd == null) {
         haveTokenOffsets = false;
       }
       int charBegin = token.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
       int charEnd = token.get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
       beginMap.put(charBegin, tokBegin);
       endMap.put(charEnd, tokEnd);
     }
   }
   // --Set Timexes
   List<CoreMap> timexMaps = new ArrayList<CoreMap>();
   int offset = 0;
   Element textElem = docElem.getFirstChildElement("text");
   for (int i = 0; i < textElem.getChildCount(); i++) {
     Node content = textElem.getChild(i);
     if (content instanceof Text) {
       Text text = (Text) content;
       offset += text.getValue().length();
     } else if (content instanceof Element) {
       Element child = (Element) content;
       if (child.getLocalName().equals("TIMEX3")) {
         Timex timex = new Timex(child);
         if (child.getChildCount() != 1) {
           throw new RuntimeException("TIMEX3 should only contain text " + child);
         }
         String timexText = child.getValue();
         CoreMap timexMap = new ArrayCoreMap();
         // (timex)
         timexMap.set(TimexAnnotation.class, timex);
         // (text)
         timexMap.set(CoreAnnotations.TextAnnotation.class, timexText);
         // (characters)
         int charBegin = offset;
         timexMap.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, charBegin);
         offset += timexText.length();
         int charEnd = offset;
         timexMap.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, charEnd);
         // (tokens)
         if (haveTokenOffsets) {
           Integer tokBegin = beginMap.get(charBegin);
           int searchStep = 1; // if no exact match, search around the character offset
           while (tokBegin == null) {
             tokBegin = beginMap.get(charBegin - searchStep);
             if (tokBegin == null) {
               tokBegin = beginMap.get(charBegin + searchStep);
             }
             searchStep += 1;
           }
           searchStep = 1;
           Integer tokEnd = endMap.get(charEnd);
           while (tokEnd == null) {
             tokEnd = endMap.get(charEnd - searchStep);
             if (tokEnd == null) {
               tokEnd = endMap.get(charEnd + searchStep);
             }
             searchStep += 1;
           }
           timexMap.set(CoreAnnotations.TokenBeginAnnotation.class, tokBegin);
           timexMap.set(CoreAnnotations.TokenEndAnnotation.class, tokEnd);
         }
         // (add)
         timexMaps.add(timexMap);
       } else {
         throw new RuntimeException("unexpected element " + child);
       }
     } else {
       throw new RuntimeException("unexpected content " + content);
     }
   }
   return timexMaps;
 }