Search in sources :

Example 36 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project torodb by torodb.

the class ToroIndexConverter method to.

@Override
public String to(NamedToroIndex userObject) {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    objectBuilder.add(NAME_KEY, userObject.getName());
    if (userObject.isUnique()) {
        objectBuilder.add(UNIQUE_KEY, true);
    }
    JsonArrayBuilder attsBuilder = Json.createArrayBuilder();
    JsonArrayBuilder descBuilder = Json.createArrayBuilder();
    int attPosition = 0;
    boolean hasDescending = false;
    for (Map.Entry<AttributeReference, IndexType> entry : userObject.getAttributes().entrySet()) {
        attsBuilder.add(entry.getKey().toString());
        if (IndexType.desc.equals(entry.getValue())) {
            descBuilder.add(attPosition);
            hasDescending = true;
        }
        attPosition++;
    }
    objectBuilder.add(ATTS_KEY, attsBuilder);
    if (hasDescending) {
        objectBuilder.add(DESCENDING, descBuilder);
    }
    StringWriter stringWriter = new StringWriter(200);
    JsonWriter jsonWriter = Json.createWriter(stringWriter);
    jsonWriter.writeObject(objectBuilder.build());
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) AttributeReference(com.torodb.core.language.AttributeReference) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IndexType(com.torodb.core.model.IndexedAttributes.IndexType) Map(java.util.Map) JsonWriter(javax.json.JsonWriter)

Example 37 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project torodb by torodb.

the class MongoTimestampValueConverter method to.

@Override
public JsonObject to(KvMongoTimestamp userObject) {
    JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
    jsonObjectBuilder.add("secs", userObject.getSecondsSinceEpoch());
    jsonObjectBuilder.add("counter", userObject.getOrdinal());
    return jsonObjectBuilder.build();
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 38 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project CoreNLP by stanfordnlp.

the class NeuralCorefDataExporter method process.

@Override
public void process(int id, Document document) {
    JsonArrayBuilder clusters = Json.createArrayBuilder();
    for (CorefCluster gold : document.goldCorefClusters.values()) {
        JsonArrayBuilder c = Json.createArrayBuilder();
        for (Mention m : gold.corefMentions) {
            c.add(m.mentionID);
        }
        clusters.add(c.build());
    }
    goldClusterWriter.println(Json.createObjectBuilder().add(String.valueOf(id), clusters.build()).build());
    Map<Pair<Integer, Integer>, Boolean> mentionPairs = CorefUtils.getLabeledMentionPairs(document);
    List<Mention> mentionsList = CorefUtils.getSortedMentions(document);
    Map<Integer, List<Mention>> mentionsByHeadIndex = new HashMap<>();
    for (int i = 0; i < mentionsList.size(); i++) {
        Mention m = mentionsList.get(i);
        List<Mention> withIndex = mentionsByHeadIndex.get(m.headIndex);
        if (withIndex == null) {
            withIndex = new ArrayList<>();
            mentionsByHeadIndex.put(m.headIndex, withIndex);
        }
        withIndex.add(m);
    }
    JsonObjectBuilder docFeatures = Json.createObjectBuilder();
    docFeatures.add("doc_id", id);
    docFeatures.add("type", document.docType == DocType.ARTICLE ? 1 : 0);
    docFeatures.add("source", document.docInfo.get("DOC_ID").split("/")[0]);
    JsonArrayBuilder sentences = Json.createArrayBuilder();
    for (CoreMap sentence : document.annotation.get(SentencesAnnotation.class)) {
        sentences.add(getSentenceArray(sentence.get(CoreAnnotations.TokensAnnotation.class)));
    }
    JsonObjectBuilder mentions = Json.createObjectBuilder();
    for (Mention m : document.predictedMentionsByID.values()) {
        Iterator<SemanticGraphEdge> iterator = m.enhancedDependency.incomingEdgeIterator(m.headIndexedWord);
        SemanticGraphEdge relation = iterator.hasNext() ? iterator.next() : null;
        String depRelation = relation == null ? "no-parent" : relation.getRelation().toString();
        String depParent = relation == null ? "<missing>" : relation.getSource().word();
        mentions.add(String.valueOf(m.mentionNum), Json.createObjectBuilder().add("doc_id", id).add("mention_id", m.mentionID).add("mention_num", m.mentionNum).add("sent_num", m.sentNum).add("start_index", m.startIndex).add("end_index", m.endIndex).add("head_index", m.headIndex).add("mention_type", m.mentionType.toString()).add("dep_relation", depRelation).add("dep_parent", depParent).add("sentence", getSentenceArray(m.sentenceWords)).add("contained-in-other-mention", mentionsByHeadIndex.get(m.headIndex).stream().anyMatch(m2 -> m != m2 && m.insideIn(m2)) ? 1 : 0).build());
    }
    JsonArrayBuilder featureNames = Json.createArrayBuilder().add("same-speaker").add("antecedent-is-mention-speaker").add("mention-is-antecedent-speaker").add("relaxed-head-match").add("exact-string-match").add("relaxed-string-match");
    JsonObjectBuilder features = Json.createObjectBuilder();
    JsonObjectBuilder labels = Json.createObjectBuilder();
    for (Map.Entry<Pair<Integer, Integer>, Boolean> e : mentionPairs.entrySet()) {
        Mention m1 = document.predictedMentionsByID.get(e.getKey().first);
        Mention m2 = document.predictedMentionsByID.get(e.getKey().second);
        String key = m1.mentionNum + " " + m2.mentionNum;
        JsonArrayBuilder builder = Json.createArrayBuilder();
        for (int val : CategoricalFeatureExtractor.pairwiseFeatures(document, m1, m2, dictionaries, conll)) {
            builder.add(val);
        }
        features.add(key, builder.build());
        labels.add(key, e.getValue() ? 1 : 0);
    }
    JsonObject docData = Json.createObjectBuilder().add("sentences", sentences.build()).add("mentions", mentions.build()).add("labels", labels.build()).add("pair_feature_names", featureNames.build()).add("pair_features", features.build()).add("document_features", docFeatures.build()).build();
    dataWriter.println(docData);
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) CorefCluster(edu.stanford.nlp.coref.data.CorefCluster) Mention(edu.stanford.nlp.coref.data.Mention) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) ArrayList(java.util.ArrayList) List(java.util.List) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) CoreMap(edu.stanford.nlp.util.CoreMap) HashMap(java.util.HashMap) Map(java.util.Map) CoreMap(edu.stanford.nlp.util.CoreMap) Pair(edu.stanford.nlp.util.Pair)

Example 39 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project CoreNLP by stanfordnlp.

the class ConstantsAndVariables method getLearnedWordsAsJson.

//  public Map<String, Counter<CandidatePhrase>> getLearnedWords() {
//    return Counters.flatten(learnedWordsEachIter);
//  }
//public void setLearnedWords(Counter<CandidatePhrase> words, String label) {
//  this.learnedWords.put(label, words);
//}
public String getLearnedWordsAsJson() {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    for (String label : getLabels()) {
        Counter<CandidatePhrase> learnedWords = getLearnedWords(label);
        JsonArrayBuilder arr = Json.createArrayBuilder();
        for (CandidatePhrase k : learnedWords.keySet()) arr.add(k.getPhrase());
        obj.add(label, arr);
    }
    return obj.build().toString();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 40 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project CoreNLP by stanfordnlp.

the class ConstantsAndVariables method getSetWordsAsJson.

public String getSetWordsAsJson(Map<String, Counter<CandidatePhrase>> words) {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    for (String label : getLabels()) {
        JsonArrayBuilder arr = Json.createArrayBuilder();
        for (CandidatePhrase k : words.get(label).keySet()) arr.add(k.getPhrase());
        obj.add(label, arr);
    }
    return obj.build().toString();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)76 JsonArrayBuilder (javax.json.JsonArrayBuilder)31 Map (java.util.Map)19 JsonObject (javax.json.JsonObject)19 JsonException (javax.json.JsonException)14 HashMap (java.util.HashMap)9 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)6 JsonValue (javax.json.JsonValue)6 List (java.util.List)5 JsonString (javax.json.JsonString)5 PrintWriter (java.io.PrintWriter)3 Entry (java.util.Map.Entry)3 JsonArray (javax.json.JsonArray)3 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)2 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 JsonWriter (javax.json.JsonWriter)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2