Example usage for edu.stanford.nlp.time Timex toXmlElement

List of usage examples for edu.stanford.nlp.time Timex toXmlElement

Introduction

In this page you can find the example usage for edu.stanford.nlp.time Timex toXmlElement.

Prototype

public Element toXmlElement() 

Source Link

Usage

From source file:org.sam_agent.csparser.ContinuousParser.java

License:Open Source License

public String stringify(CoreMap words) {

    List<String> posList = new ArrayList<String>();
    List<String> posMap = new ArrayList<String>();
    List<String> lemmaMap = new ArrayList<String>();
    List<String> timexMap = new ArrayList<String>();
    Map<String, List<String>> timexIdMap = new HashMap<String, List<String>>();

    for (CoreLabel token : words.get(CoreAnnotations.TokensAnnotation.class)) {

        String word = token.get(CoreAnnotations.TextAnnotation.class);
        String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
        String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
        String wordToken = esc(word) + "-" + token.index();

        posList.add(String.format("{\"token\":\"%s\",\"pos\":\"%s\"}", esc(word), esc(pos)));
        posMap.add(String.format("\"%s\":\"%s\"", wordToken, esc(pos)));
        lemmaMap.add(String.format("\"%s\":\"%s\"", wordToken, esc(lemma)));

        Timex t = token.get(TimeAnnotations.TimexAnnotation.class);
        if (t != null) {
            String tid = t.tid();
            if (timexIdMap.containsKey(tid)) {
                timexIdMap.get(tid).add("\"" + wordToken + "\"");
                continue;
            }//from w w  w  . ja  va 2 s.  c  om

            List<String> tokens = new ArrayList<String>();
            tokens.add("\"" + wordToken + "\"");
            timexIdMap.put(tid, tokens);

            List<String> attributesList = new ArrayList<String>();
            Element xml = t.toXmlElement();
            NamedNodeMap attrs = xml.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node item = attrs.item(i);
                String name = item.getNodeName();
                String value = item.getNodeValue();
                attributesList.add(String.format("\"%s\":\"%s\"", name, value));
            }
            String json = String.format("\"%s\":{%s}", t.tid(), String.join(",", attributesList));
            timexMap.add(json);
        }
    }

    String posListJSON = "[" + String.join(", ", posList) + "]";
    String posMapJSON = "{" + String.join(", ", posMap) + "}";
    String lemmaMapJSON = "{" + String.join(", ", lemmaMap) + "}";
    String timexMapJSON = "{" + String.join(", ", timexMap) + "}";

    List<String> temp = new ArrayList<String>();
    for (String tid : timexIdMap.keySet()) {
        temp.add(String.format("\"%s\":[%s]", tid, String.join(",", timexIdMap.get(tid))));
    }
    String timexIdMapJSON = "{" + String.join(",", temp) + "}";

    return String.format("\"pos\":{\"map\":%s,\"list\":%s},\"lemma\":%s,\"timex\":%s,\"timexGroups\":%s",
            posMapJSON, posListJSON, lemmaMapJSON, timexMapJSON, timexIdMapJSON);
}