Search in sources :

Example 11 with JsonArray

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

the class NeuralCorefDataExporter method getSentenceArray.

private static JsonArray getSentenceArray(List<CoreLabel> sentence) {
    JsonArrayBuilder sentenceBuilder = Json.createArrayBuilder();
    sentence.stream().map(CoreLabel::word).map(w -> w.equals("/.") ? "." : w).map(w -> w.equals("/?") ? "?" : w).forEach(sentenceBuilder::add);
    return sentenceBuilder.build();
}
Also used : SentencesAnnotation(edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation) JsonArrayBuilder(javax.json.JsonArrayBuilder) HashMap(java.util.HashMap) Dictionaries(edu.stanford.nlp.coref.data.Dictionaries) ArrayList(java.util.ArrayList) CorefCluster(edu.stanford.nlp.coref.data.CorefCluster) Mention(edu.stanford.nlp.coref.data.Mention) Map(java.util.Map) Json(javax.json.Json) Pair(edu.stanford.nlp.util.Pair) CoreMap(edu.stanford.nlp.util.CoreMap) PrintWriter(java.io.PrintWriter) JsonObject(javax.json.JsonObject) Dataset(edu.stanford.nlp.coref.CorefProperties.Dataset) CoreLabel(edu.stanford.nlp.ling.CoreLabel) Properties(java.util.Properties) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) Iterator(java.util.Iterator) IOUtils(edu.stanford.nlp.io.IOUtils) JsonArray(javax.json.JsonArray) DocType(edu.stanford.nlp.coref.data.Document.DocType) File(java.io.File) List(java.util.List) StringUtils(edu.stanford.nlp.util.StringUtils) CorefDocumentProcessor(edu.stanford.nlp.coref.CorefDocumentProcessor) CorefProperties(edu.stanford.nlp.coref.CorefProperties) Document(edu.stanford.nlp.coref.data.Document) CorefUtils(edu.stanford.nlp.coref.CorefUtils) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 12 with JsonArray

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

the class ScorePhrases method learnNewPhrasesPrivate.

private Counter<CandidatePhrase> learnNewPhrasesPrivate(String label, PatternsForEachToken patternsForEachToken, Counter<E> patternsLearnedThisIter, Counter<E> allSelectedPatterns, Set<CandidatePhrase> alreadyIdentifiedWords, CollectionValuedMap<E, Triple<String, Integer, Integer>> matchedTokensByPat, Counter<CandidatePhrase> scoreForAllWordsThisIteration, TwoDimensionalCounter<CandidatePhrase, E> terms, TwoDimensionalCounter<CandidatePhrase, E> wordsPatExtracted, TwoDimensionalCounter<E, CandidatePhrase> patternsAndWords4Label, String identifier, Set<CandidatePhrase> ignoreWords, boolean computeProcDataFreq) throws IOException, ClassNotFoundException {
    Set<CandidatePhrase> alreadyLabeledWords = new HashSet<>();
    if (constVars.doNotApplyPatterns) {
        // if want to get the stats by the lossy way of just counting without
        // applying the patterns
        ConstantsAndVariables.DataSentsIterator sentsIter = new ConstantsAndVariables.DataSentsIterator(constVars.batchProcessSents);
        while (sentsIter.hasNext()) {
            Pair<Map<String, DataInstance>, File> sentsf = sentsIter.next();
            this.statsWithoutApplyingPatterns(sentsf.first(), patternsForEachToken, patternsLearnedThisIter, wordsPatExtracted);
        }
    } else {
        if (patternsLearnedThisIter.size() > 0) {
            this.applyPats(patternsLearnedThisIter, label, wordsPatExtracted, matchedTokensByPat, alreadyLabeledWords);
        }
    }
    if (computeProcDataFreq) {
        if (!phraseScorer.wordFreqNorm.equals(Normalization.NONE)) {
            Redwood.log(Redwood.DBG, "computing processed freq");
            for (Entry<CandidatePhrase, Double> fq : Data.rawFreq.entrySet()) {
                Double in = fq.getValue();
                if (phraseScorer.wordFreqNorm.equals(Normalization.SQRT))
                    in = Math.sqrt(in);
                else if (phraseScorer.wordFreqNorm.equals(Normalization.LOG))
                    in = 1 + Math.log(in);
                else
                    throw new RuntimeException("can't understand the normalization");
                assert !in.isNaN() : "Why is processed freq nan when rawfreq is " + in;
                Data.processedDataFreq.setCount(fq.getKey(), in);
            }
        } else
            Data.processedDataFreq = Data.rawFreq;
    }
    if (constVars.wordScoring.equals(WordScoring.WEIGHTEDNORM)) {
        for (CandidatePhrase en : wordsPatExtracted.firstKeySet()) {
            if (!constVars.getOtherSemanticClassesWords().contains(en) && (en.getPhraseLemma() == null || !constVars.getOtherSemanticClassesWords().contains(CandidatePhrase.createOrGet(en.getPhraseLemma()))) && !alreadyLabeledWords.contains(en)) {
                terms.addAll(en, wordsPatExtracted.getCounter(en));
            }
        }
        removeKeys(terms, constVars.getStopWords());
        Counter<CandidatePhrase> phraseScores = phraseScorer.scorePhrases(label, terms, wordsPatExtracted, allSelectedPatterns, alreadyIdentifiedWords, false);
        System.out.println("count for word U.S. is " + phraseScores.getCount(CandidatePhrase.createOrGet("U.S.")));
        Set<CandidatePhrase> ignoreWordsAll;
        if (ignoreWords != null && !ignoreWords.isEmpty()) {
            ignoreWordsAll = CollectionUtils.unionAsSet(ignoreWords, constVars.getOtherSemanticClassesWords());
        } else
            ignoreWordsAll = new HashSet<>(constVars.getOtherSemanticClassesWords());
        ignoreWordsAll.addAll(constVars.getSeedLabelDictionary().get(label));
        ignoreWordsAll.addAll(constVars.getLearnedWords(label).keySet());
        System.out.println("ignoreWordsAll contains word U.S. is " + ignoreWordsAll.contains(CandidatePhrase.createOrGet("U.S.")));
        Counter<CandidatePhrase> finalwords = chooseTopWords(phraseScores, terms, phraseScores, ignoreWordsAll, constVars.thresholdWordExtract);
        phraseScorer.printReasonForChoosing(finalwords);
        scoreForAllWordsThisIteration.clear();
        Counters.addInPlace(scoreForAllWordsThisIteration, phraseScores);
        Redwood.log(ConstantsAndVariables.minimaldebug, "\n\n## Selected Words for " + label + " : " + Counters.toSortedString(finalwords, finalwords.size(), "%1$s:%2$.2f", "\t"));
        if (constVars.goldEntities != null) {
            Map<String, Boolean> goldEntities4Label = constVars.goldEntities.get(label);
            if (goldEntities4Label != null) {
                StringBuffer s = new StringBuffer();
                finalwords.keySet().stream().forEach(x -> s.append(x.getPhrase() + (goldEntities4Label.containsKey(x.getPhrase()) ? ":" + goldEntities4Label.get(x.getPhrase()) : ":UKNOWN") + "\n"));
                Redwood.log(ConstantsAndVariables.minimaldebug, "\n\n## Gold labels for selected words for label " + label + " : " + s.toString());
            } else
                Redwood.log(Redwood.DBG, "No gold entities provided for label " + label);
        }
        if (constVars.outDir != null && !constVars.outDir.isEmpty()) {
            String outputdir = constVars.outDir + "/" + identifier + "/" + label;
            IOUtils.ensureDir(new File(outputdir));
            TwoDimensionalCounter<CandidatePhrase, CandidatePhrase> reasonForWords = new TwoDimensionalCounter<>();
            for (CandidatePhrase word : finalwords.keySet()) {
                for (E l : wordsPatExtracted.getCounter(word).keySet()) {
                    for (CandidatePhrase w2 : patternsAndWords4Label.getCounter(l)) {
                        reasonForWords.incrementCount(word, w2);
                    }
                }
            }
            Redwood.log(ConstantsAndVariables.minimaldebug, "Saving output in " + outputdir);
            String filename = outputdir + "/words.json";
            // the json object is an array corresponding to each iteration - of list
            // of objects,
            // each of which is a bean of entity and reasons
            JsonArrayBuilder obj = Json.createArrayBuilder();
            if (writtenInJustification.containsKey(label) && writtenInJustification.get(label)) {
                JsonReader jsonReader = Json.createReader(new BufferedInputStream(new FileInputStream(filename)));
                JsonArray objarr = jsonReader.readArray();
                for (JsonValue o : objarr) obj.add(o);
                jsonReader.close();
            }
            JsonArrayBuilder objThisIter = Json.createArrayBuilder();
            for (CandidatePhrase w : reasonForWords.firstKeySet()) {
                JsonObjectBuilder objinner = Json.createObjectBuilder();
                JsonArrayBuilder l = Json.createArrayBuilder();
                for (CandidatePhrase w2 : reasonForWords.getCounter(w).keySet()) {
                    l.add(w2.getPhrase());
                }
                JsonArrayBuilder pats = Json.createArrayBuilder();
                for (E p : wordsPatExtracted.getCounter(w)) {
                    pats.add(p.toStringSimple());
                }
                objinner.add("reasonwords", l);
                objinner.add("patterns", pats);
                objinner.add("score", finalwords.getCount(w));
                objinner.add("entity", w.getPhrase());
                objThisIter.add(objinner.build());
            }
            obj.add(objThisIter);
            // Redwood.log(ConstantsAndVariables.minimaldebug, channelNameLogger,
            // "Writing justification at " + filename);
            IOUtils.writeStringToFile(StringUtils.normalize(StringUtils.toAscii(obj.build().toString())), filename, "ASCII");
            writtenInJustification.put(label, true);
        }
        if (constVars.justify) {
            Redwood.log(Redwood.DBG, "\nJustification for phrases:\n");
            for (CandidatePhrase word : finalwords.keySet()) {
                Redwood.log(Redwood.DBG, "Phrase " + word + " extracted because of patterns: \t" + Counters.toSortedString(wordsPatExtracted.getCounter(word), wordsPatExtracted.getCounter(word).size(), "%1$s:%2$f", "\n"));
            }
        }
        return finalwords;
    } else if (constVars.wordScoring.equals(WordScoring.BPB)) {
        Counters.addInPlace(terms, wordsPatExtracted);
        Counter<CandidatePhrase> maxPatWeightTerms = new ClassicCounter<>();
        Map<CandidatePhrase, E> wordMaxPat = new HashMap<>();
        for (Entry<CandidatePhrase, ClassicCounter<E>> en : terms.entrySet()) {
            Counter<E> weights = new ClassicCounter<>();
            for (E k : en.getValue().keySet()) weights.setCount(k, patternsLearnedThisIter.getCount(k));
            maxPatWeightTerms.setCount(en.getKey(), Counters.max(weights));
            wordMaxPat.put(en.getKey(), Counters.argmax(weights));
        }
        Counters.removeKeys(maxPatWeightTerms, alreadyIdentifiedWords);
        double maxvalue = Counters.max(maxPatWeightTerms);
        Set<CandidatePhrase> words = Counters.keysAbove(maxPatWeightTerms, maxvalue - 1e-10);
        CandidatePhrase bestw = null;
        if (words.size() > 1) {
            double max = Double.NEGATIVE_INFINITY;
            for (CandidatePhrase w : words) {
                if (terms.getCount(w, wordMaxPat.get(w)) > max) {
                    max = terms.getCount(w, wordMaxPat.get(w));
                    bestw = w;
                }
            }
        } else if (words.size() == 1)
            bestw = words.iterator().next();
        else
            return new ClassicCounter<>();
        Redwood.log(ConstantsAndVariables.minimaldebug, "Selected Words: " + bestw);
        return Counters.asCounter(Arrays.asList(bestw));
    } else
        throw new RuntimeException("wordscoring " + constVars.wordScoring + " not identified");
}
Also used : Entry(java.util.Map.Entry) Counter(edu.stanford.nlp.stats.Counter) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) TwoDimensionalCounter(edu.stanford.nlp.stats.TwoDimensionalCounter) BufferedInputStream(java.io.BufferedInputStream) JsonReader(javax.json.JsonReader) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonValue(javax.json.JsonValue) TwoDimensionalCounter(edu.stanford.nlp.stats.TwoDimensionalCounter) FileInputStream(java.io.FileInputStream) JsonArray(javax.json.JsonArray) File(java.io.File)

Example 13 with JsonArray

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

the class ToroIndexConverter method from.

@Override
public NamedToroIndex from(String databaseObject) {
    JsonReader reader = Json.createReader(new StringReader(databaseObject));
    JsonObject object = reader.readObject();
    IndexedAttributes.Builder builder = new IndexedAttributes.Builder();
    JsonArray attsArray = object.getJsonArray(ATTS_KEY);
    Set<Integer> descendingAttPos;
    if (object.containsKey(DESCENDING)) {
        JsonArray descArray = object.getJsonArray(DESCENDING);
        descendingAttPos = Sets.newHashSetWithExpectedSize(descArray.size());
        for (int i = 0; i < descArray.size(); i++) {
            descendingAttPos.add(descArray.getInt(i));
        }
    } else {
        descendingAttPos = Collections.emptySet();
    }
    for (int i = 0; i < attsArray.size(); i++) {
        String att = attsArray.getString(i);
        AttributeReference attRef = parseAttRef(att);
        if (descendingAttPos.contains(i)) {
            builder.addAttribute(attRef, IndexType.desc);
        } else {
            builder.addAttribute(attRef, IndexType.asc);
        }
    }
    return new DefaultNamedToroIndex(object.getString(NAME_KEY), builder.build(), databaseName, collectionName, object.getBoolean(UNIQUE_KEY, false));
}
Also used : DefaultNamedToroIndex(com.torodb.core.model.DefaultNamedToroIndex) IndexedAttributes(com.torodb.core.model.IndexedAttributes) AttributeReference(com.torodb.core.language.AttributeReference) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonObject(javax.json.JsonObject) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Example 14 with JsonArray

use of javax.json.JsonArray in project mysql_perf_analyzer by yahoo.

the class SNMPSettings method readConfig.

private synchronized boolean readConfig() {
    File cfgFile = new File(this.myperfSnmpConfigPath);
    if (!cfgFile.exists()) {
        logger.info("There is no customized snmp configuration file");
        return true;
    }
    FileInputStream in = null;
    try {
        in = new FileInputStream(cfgFile);
        JsonReader jr = javax.json.Json.createReader(in);
        JsonObject jsonObject = jr.readObject();
        jr.close();
        this.setSiteSetting(readFromJson(jsonObject.getJsonObject("site")));
        JsonArray groups = jsonObject.getJsonArray("group");
        if (groups != null) {
            int len = groups.size();
            for (int i = 0; i < len; i++) {
                JsonObject grp = groups.getJsonObject(i);
                SNMPSetting grpSetting = readFromJson(grp);
                String grpName = grp.getString("dbgroup", null);
                if (grpName != null && grpSetting != null)
                    this.groupSettings.put(grpName, grpSetting);
            }
        }
        JsonArray hosts = jsonObject.getJsonArray("host");
        if (hosts != null) {
            int len = hosts.size();
            for (int i = 0; i < len; i++) {
                JsonObject host = hosts.getJsonObject(i);
                SNMPSetting hostSetting = readFromJson(host);
                String hostName = host.getString("dbhost", null);
                if (hostName != null && hostSetting != null)
                    this.hostSettings.put(hostName, hostSetting);
            }
        }
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to read SNMP configuration file " + cfgFile.getAbsolutePath(), ex);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception fex) {
            }
            ;
        }
    }
    return false;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 15 with JsonArray

use of javax.json.JsonArray in project mysql_perf_analyzer by yahoo.

the class AlertDefinition method createFromJson.

public static AlertDefinition createFromJson(java.io.InputStream in) {
    JsonReader jsonReader = null;
    AlertDefinition alert = null;
    try {
        jsonReader = javax.json.Json.createReader(in);
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();
        alert = new AlertDefinition(jsonObject.getString("name"));
        alert.setSource(jsonObject.getString("source"));
        alert.setMetricName(jsonObject.getString("metricName", null));
        alert.setMetricValueType(jsonObject.getString("metricValueType", null));
        alert.setMetricComparison(jsonObject.getString("metricComparison", null));
        try {
            alert.setDefaultThreshold(Float.parseFloat(jsonObject.getString("defaultThreshold", null)));
        } catch (Exception ex) {
        }
        alert.setSqlId(jsonObject.getString("sqlId", null));
        alert.setSqlText(jsonObject.getString("sqlText", null));
        JsonArray params = jsonObject.getJsonArray("params");
        if (params != null) {
            int mlen = params.size();
            for (int i = 0; i < mlen; i++) {
                JsonObject mobj = params.getJsonObject(i);
                alert.addParam(mobj.getString("name"), mobj.getString("defaultValue"));
            }
        }
        return alert;
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error to parse UDM", ex);
    }
    return null;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) UserDefinedMetricsException(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics.UserDefinedMetricsException)

Aggregations

JsonArray (javax.json.JsonArray)80 JsonObject (javax.json.JsonObject)58 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)24 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 StringReader (java.io.StringReader)15 HashSet (java.util.HashSet)15 JsonReader (javax.json.JsonReader)14 HashMap (java.util.HashMap)12 JsonString (javax.json.JsonString)12 JsonValue (javax.json.JsonValue)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 JsonException (javax.json.JsonException)6 Map (java.util.Map)5 Response (javax.ws.rs.core.Response)5 File (java.io.File)4 JsonArrayBuilder (javax.json.JsonArrayBuilder)4