Search in sources :

Example 6 with JsonReader

use of javax.json.JsonReader 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 7 with JsonReader

use of javax.json.JsonReader 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 8 with JsonReader

use of javax.json.JsonReader 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 9 with JsonReader

use of javax.json.JsonReader 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)

Example 10 with JsonReader

use of javax.json.JsonReader in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManager method httpResponseVerification.

/**
     * Verify Http response using response status
     *
     * @param httpResponse Http response object to verify
     * @throws IotHubBadFormatException This exception is thrown if the response status equal 400
     * @throws IotHubUnathorizedException This exception is thrown if the response status equal 401
     * @throws IotHubTooManyDevicesException This exception is thrown if the response status equal 403
     * @throws IotHubNotFoundException This exception is thrown if the response status equal 404
     * @throws IotHubPreconditionFailedException This exception is thrown if the response status equal 412
     * @throws IotHubTooManyRequestsException This exception is thrown if the response status equal 429
     * @throws IotHubInternalServerErrorException This exception is thrown if the response status equal 500
     * @throws IotHubBadGatewayException This exception is thrown if the response status equal 502
     * @throws IotHubServerBusyException This exception is thrown if the response status equal 503
     * @throws IotHubGatewayTimeoutException This exception is thrown if the response status equal 504
     * @throws IotHubException This exception is thrown if the response status none of them above and greater then 300
     */
public static void httpResponseVerification(HttpResponse httpResponse) throws IotHubBadFormatException, IotHubUnathorizedException, IotHubTooManyDevicesException, IotHubPreconditionFailedException, IotHubTooManyRequestsException, IotHubInternalServerErrorException, IotHubServerBusyException, IotHubBadGatewayException, IotHubNotFoundException, IotHubGatewayTimeoutException, IotHubException {
    int responseStatus = httpResponse.getStatus();
    String errorMessage = "";
    try {
        String jsonString = new String(httpResponse.getErrorReason(), StandardCharsets.UTF_8);
        try (JsonReader jsonReader = Json.createReader(new StringReader(jsonString))) {
            JsonObject jsonObject = jsonReader.readObject();
            if ((jsonObject != JsonObject.NULL) && (jsonObject != null)) {
                errorMessage = Tools.getValueFromJsonObject(jsonObject, "ExceptionMessage");
            }
        }
    } catch (Exception e) {
    /* If it cannot parser the errorReason, just log a error without message.*/
    // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
    }
    // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_001: [The function shall throw IotHubBadFormatException if the Http response status equal 400]
    if (400 == responseStatus) {
        throw new IotHubBadFormatException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_002: [The function shall throw IotHubUnathorizedException if the Http response status equal 401]
    if (401 == responseStatus) {
        throw new IotHubUnathorizedException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_003: [The function shall throw IotHubTooManyDevicesException if the Http response status equal 403]
    if (403 == responseStatus) {
        throw new IotHubTooManyDevicesException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_004: [The function shall throw IotHubNotFoundException if the Http response status equal 404]
    if (404 == responseStatus) {
        throw new IotHubNotFoundException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_005: [The function shall throw IotHubPreconditionFailedException if the Http response status equal 412]
    if (412 == responseStatus) {
        throw new IotHubPreconditionFailedException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_006: [The function shall throw IotHubTooManyRequestsException if the Http response status equal 429]
    if (429 == responseStatus) {
        throw new IotHubTooManyRequestsException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_007: [The function shall throw IotHubInternalServerErrorException if the Http response status equal 500]
    if (500 == responseStatus) {
        throw new IotHubInternalServerErrorException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_008: [The function shall throw IotHubBadGatewayException if the Http response status equal 502]
    if (502 == responseStatus) {
        throw new IotHubBadGatewayException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_009: [The function shall throw IotHubServerBusyException if the Http response status equal 503]
    if (503 == responseStatus) {
        throw new IotHubServerBusyException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_010: [The function shall throw IotHubGatewayTimeoutException if the Http response status equal 504]
    if (504 == responseStatus) {
        throw new IotHubGatewayTimeoutException(errorMessage);
    } else // Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_011: [The function shall throw IotHubException if the Http response status none of them above and greater than 300 copying the error Http reason to the exception]
    if (responseStatus > 300) {
        if (errorMessage.isEmpty()) {
            throw new IotHubException("Unknown error reason");
        } else {
            throw new IotHubException(errorMessage);
        }
    }
// Codes_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_012: [The function shall return without exception if the response status equal or less than 300]
}
Also used : JsonObject(javax.json.JsonObject) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Aggregations

JsonReader (javax.json.JsonReader)30 JsonObject (javax.json.JsonObject)25 StringReader (java.io.StringReader)22 Test (org.junit.Test)18 JsonArray (javax.json.JsonArray)14 JsonString (javax.json.JsonString)12 File (java.io.File)3 JsonValue (javax.json.JsonValue)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonException (javax.json.JsonException)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)1 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)1 AttributeReference (com.torodb.core.language.AttributeReference)1 DefaultNamedToroIndex (com.torodb.core.model.DefaultNamedToroIndex)1 IndexedAttributes (com.torodb.core.model.IndexedAttributes)1