use of javax.json.JsonObjectBuilder in project visualee by Thomas-S-B.
the class GraphCreator method buildJSONNode.
static JsonObjectBuilder buildJSONNode(JavaSource javaSource) {
// this id processing is necessary, because d3.js needs consecutive ids
javaSource.setId(id);
id++;
JsonObjectBuilder node = Json.createObjectBuilder();
node.add("name", javaSource.toString()).add("group", javaSource.getGroup()).add("description", Description.generateDescription(javaSource)).add("sourcecode", javaSource.getEscapedSourceCode()).add("id", javaSource.getId());
return node;
}
use of javax.json.JsonObjectBuilder in project visualee by Thomas-S-B.
the class GraphCreatorTest method testBuildJSONNode.
@Test
public void testBuildJSONNode() {
JavaSourceContainer.getInstance().clear();
DependencyContainer.getInstance().clear();
String name = "MyTestClass";
String sourcecode = "test source code - Matt Pavolka Group - Something People Can Use";
JavaSource javaSource = JavaSourceFactory.getInstance().newJavaSource("MyTestClass");
javaSource.setGroup(2);
javaSource.setPackagePath("de.test.test2");
javaSource.setSourceCode(sourcecode);
JsonObjectBuilder job = GraphCreator.buildJSONNode(javaSource);
JsonObject node = job.build();
assertEquals(name, node.getString("name"));
assertEquals(2, node.getInt("group"));
assertNotNull(node.getString("description"));
assertEquals(sourcecode, node.getString("sourcecode"));
}
use of javax.json.JsonObjectBuilder in project antlr4 by antlr.
the class SwiftTarget method serializeTojson.
//added by janyou -->
public String serializeTojson(ATN atn) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("version", ATNDeserializer.SERIALIZED_VERSION);
builder.add("uuid", ATNDeserializer.SERIALIZED_UUID.toString());
// convert grammar type to ATN const to avoid dependence on ANTLRParser
builder.add("grammarType", atn.grammarType.ordinal());
builder.add("maxTokenType", atn.maxTokenType);
//states
int nedges = 0;
Map<IntervalSet, Integer> setIndices = new HashMap<IntervalSet, Integer>();
List<IntervalSet> sets = new ArrayList<IntervalSet>();
JsonArrayBuilder statesBuilder = Json.createArrayBuilder();
IntegerList nonGreedyStates = new IntegerList();
IntegerList precedenceStates = new IntegerList();
for (ATNState s : atn.states) {
JsonObjectBuilder stateBuilder = Json.createObjectBuilder();
if (s == null) {
// might be optimized away
statesBuilder.addNull();
continue;
}
int stateType = s.getStateType();
stateBuilder.add("stateType", stateType);
//stateBuilder.add("stateNumber",s.stateNumber);
stateBuilder.add("ruleIndex", s.ruleIndex);
if (s instanceof DecisionState && ((DecisionState) s).nonGreedy) {
nonGreedyStates.add(s.stateNumber);
}
if (s instanceof RuleStartState && ((RuleStartState) s).isLeftRecursiveRule) {
precedenceStates.add(s.stateNumber);
}
if (s.getStateType() == ATNState.LOOP_END) {
stateBuilder.add("detailStateNumber", ((LoopEndState) s).loopBackState.stateNumber);
} else if (s instanceof BlockStartState) {
stateBuilder.add("detailStateNumber", ((BlockStartState) s).endState.stateNumber);
}
if (s.getStateType() != ATNState.RULE_STOP) {
// the deserializer can trivially derive these edges, so there's no need to serialize them
nedges += s.getNumberOfTransitions();
}
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
Transition t = s.transition(i);
int edgeType = Transition.serializationTypes.get(t.getClass());
if (edgeType == Transition.SET || edgeType == Transition.NOT_SET) {
SetTransition st = (SetTransition) t;
if (!setIndices.containsKey(st.set)) {
sets.add(st.set);
setIndices.put(st.set, sets.size() - 1);
}
}
}
statesBuilder.add(stateBuilder);
}
builder.add("states", statesBuilder);
// non-greedy states
JsonArrayBuilder nonGreedyStatesBuilder = Json.createArrayBuilder();
for (int i = 0; i < nonGreedyStates.size(); i++) {
nonGreedyStatesBuilder.add(nonGreedyStates.get(i));
}
builder.add("nonGreedyStates", nonGreedyStatesBuilder);
// precedence states
JsonArrayBuilder precedenceStatesBuilder = Json.createArrayBuilder();
for (int i = 0; i < precedenceStates.size(); i++) {
precedenceStatesBuilder.add(precedenceStates.get(i));
}
builder.add("precedenceStates", precedenceStatesBuilder);
JsonArrayBuilder ruleToStartStateBuilder = Json.createArrayBuilder();
int nrules = atn.ruleToStartState.length;
for (int r = 0; r < nrules; r++) {
JsonObjectBuilder stateBuilder = Json.createObjectBuilder();
ATNState ruleStartState = atn.ruleToStartState[r];
stateBuilder.add("stateNumber", ruleStartState.stateNumber);
if (atn.grammarType == ATNType.LEXER) {
// if (atn.ruleToTokenType[r] == Token.EOF) {
// //data.add(Character.MAX_VALUE);
// stateBuilder.add("ruleToTokenType",-1);
// }
// else {
// //data.add(atn.ruleToTokenType[r]);
stateBuilder.add("ruleToTokenType", atn.ruleToTokenType[r]);
// }
}
ruleToStartStateBuilder.add(stateBuilder);
}
builder.add("ruleToStartState", ruleToStartStateBuilder);
JsonArrayBuilder modeToStartStateBuilder = Json.createArrayBuilder();
int nmodes = atn.modeToStartState.size();
if (nmodes > 0) {
for (ATNState modeStartState : atn.modeToStartState) {
modeToStartStateBuilder.add(modeStartState.stateNumber);
}
}
builder.add("modeToStartState", modeToStartStateBuilder);
JsonArrayBuilder nsetsBuilder = Json.createArrayBuilder();
int nsets = sets.size();
//data.add(nsets);
builder.add("nsets", nsets);
for (IntervalSet set : sets) {
JsonObjectBuilder setBuilder = Json.createObjectBuilder();
boolean containsEof = set.contains(Token.EOF);
if (containsEof && set.getIntervals().get(0).b == Token.EOF) {
//data.add(set.getIntervals().size() - 1);
setBuilder.add("size", set.getIntervals().size() - 1);
} else {
//data.add(set.getIntervals().size());
setBuilder.add("size", set.getIntervals().size());
}
setBuilder.add("containsEof", containsEof ? 1 : 0);
JsonArrayBuilder IntervalsBuilder = Json.createArrayBuilder();
for (Interval I : set.getIntervals()) {
JsonObjectBuilder IntervalBuilder = Json.createObjectBuilder();
if (I.a == Token.EOF) {
if (I.b == Token.EOF) {
continue;
} else {
IntervalBuilder.add("a", 0);
//data.add(0);
}
} else {
IntervalBuilder.add("a", I.a);
//data.add(I.a);
}
IntervalBuilder.add("b", I.b);
IntervalsBuilder.add(IntervalBuilder);
}
setBuilder.add("Intervals", IntervalsBuilder);
nsetsBuilder.add(setBuilder);
}
builder.add("IntervalSet", nsetsBuilder);
//builder.add("nedges",nedges);
JsonArrayBuilder allTransitionsBuilder = Json.createArrayBuilder();
for (ATNState s : atn.states) {
if (s == null) {
// might be optimized away
continue;
}
if (s.getStateType() == ATNState.RULE_STOP) {
continue;
}
JsonArrayBuilder transitionsBuilder = Json.createArrayBuilder();
for (int i = 0; i < s.getNumberOfTransitions(); i++) {
JsonObjectBuilder transitionBuilder = Json.createObjectBuilder();
Transition t = s.transition(i);
if (atn.states.get(t.target.stateNumber) == null) {
throw new IllegalStateException("Cannot serialize a transition to a removed state.");
}
int src = s.stateNumber;
int trg = t.target.stateNumber;
int edgeType = Transition.serializationTypes.get(t.getClass());
int arg1 = 0;
int arg2 = 0;
int arg3 = 0;
switch(edgeType) {
case Transition.RULE:
trg = ((RuleTransition) t).followState.stateNumber;
arg1 = ((RuleTransition) t).target.stateNumber;
arg2 = ((RuleTransition) t).ruleIndex;
arg3 = ((RuleTransition) t).precedence;
break;
case Transition.PRECEDENCE:
PrecedencePredicateTransition ppt = (PrecedencePredicateTransition) t;
arg1 = ppt.precedence;
break;
case Transition.PREDICATE:
PredicateTransition pt = (PredicateTransition) t;
arg1 = pt.ruleIndex;
arg2 = pt.predIndex;
arg3 = pt.isCtxDependent ? 1 : 0;
break;
case Transition.RANGE:
arg1 = ((RangeTransition) t).from;
arg2 = ((RangeTransition) t).to;
if (arg1 == Token.EOF) {
arg1 = 0;
arg3 = 1;
}
break;
case Transition.ATOM:
arg1 = ((AtomTransition) t).label;
if (arg1 == Token.EOF) {
arg1 = 0;
arg3 = 1;
}
break;
case Transition.ACTION:
ActionTransition at = (ActionTransition) t;
arg1 = at.ruleIndex;
arg2 = at.actionIndex;
// if (arg2 == -1) {
// arg2 = 0xFFFF;
// }
arg3 = at.isCtxDependent ? 1 : 0;
break;
case Transition.SET:
arg1 = setIndices.get(((SetTransition) t).set);
break;
case Transition.NOT_SET:
arg1 = setIndices.get(((SetTransition) t).set);
break;
case Transition.WILDCARD:
break;
}
transitionBuilder.add("src", src);
transitionBuilder.add("trg", trg);
transitionBuilder.add("edgeType", edgeType);
transitionBuilder.add("arg1", arg1);
transitionBuilder.add("arg2", arg2);
transitionBuilder.add("arg3", arg3);
transitionsBuilder.add(transitionBuilder);
}
allTransitionsBuilder.add(transitionsBuilder);
}
builder.add("allTransitionsBuilder", allTransitionsBuilder);
int ndecisions = atn.decisionToState.size();
//data.add(ndecisions);
JsonArrayBuilder decisionToStateBuilder = Json.createArrayBuilder();
for (DecisionState decStartState : atn.decisionToState) {
//data.add(decStartState.stateNumber);
decisionToStateBuilder.add(decStartState.stateNumber);
}
builder.add("decisionToState", decisionToStateBuilder);
//
// LEXER ACTIONS
//
JsonArrayBuilder lexerActionsBuilder = Json.createArrayBuilder();
if (atn.grammarType == ATNType.LEXER) {
//data.add(atn.lexerActions.length);
for (LexerAction action : atn.lexerActions) {
JsonObjectBuilder lexerActionBuilder = Json.createObjectBuilder();
lexerActionBuilder.add("actionType", action.getActionType().ordinal());
//data.add(action.getActionType().ordinal());
switch(action.getActionType()) {
case CHANNEL:
int channel = ((LexerChannelAction) action).getChannel();
lexerActionBuilder.add("a", channel);
lexerActionBuilder.add("b", 0);
break;
case CUSTOM:
int ruleIndex = ((LexerCustomAction) action).getRuleIndex();
int actionIndex = ((LexerCustomAction) action).getActionIndex();
lexerActionBuilder.add("a", ruleIndex);
lexerActionBuilder.add("b", actionIndex);
break;
case MODE:
int mode = ((LexerModeAction) action).getMode();
lexerActionBuilder.add("a", mode);
lexerActionBuilder.add("b", 0);
break;
case MORE:
lexerActionBuilder.add("a", 0);
lexerActionBuilder.add("b", 0);
break;
case POP_MODE:
lexerActionBuilder.add("a", 0);
lexerActionBuilder.add("b", 0);
break;
case PUSH_MODE:
mode = ((LexerPushModeAction) action).getMode();
lexerActionBuilder.add("a", mode);
lexerActionBuilder.add("b", 0);
break;
case SKIP:
lexerActionBuilder.add("a", 0);
lexerActionBuilder.add("b", 0);
break;
case TYPE:
int type = ((LexerTypeAction) action).getType();
lexerActionBuilder.add("a", type);
lexerActionBuilder.add("b", 0);
break;
default:
String message = String.format(Locale.getDefault(), "The specified lexer action type %s is not valid.", action.getActionType());
throw new IllegalArgumentException(message);
}
lexerActionsBuilder.add(lexerActionBuilder);
}
}
builder.add("lexerActions", lexerActionsBuilder);
// don't adjust the first value since that's the version number
// for (int i = 1; i < data.size(); i++) {
// if (data.get(i) < Character.MIN_VALUE || data.get(i) > Character.MAX_VALUE) {
// throw new UnsupportedOperationException("Serialized ATN data element out of range.");
// }
//
// int value = (data.get(i) + 2) & 0xFFFF;
// data.set(i, value);
// }
JsonObject data = builder.build();
// System.out.print(data.toString());
return data.toString();
}
use of javax.json.JsonObjectBuilder 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");
}
use of javax.json.JsonObjectBuilder in project CoreNLP by stanfordnlp.
the class ConstantsAndVariables method getLearnedWordsAsJsonLastIteration.
public String getLearnedWordsAsJsonLastIteration() {
JsonObjectBuilder obj = Json.createObjectBuilder();
for (String label : getLabels()) {
Counter<CandidatePhrase> learnedWords = getLearnedWordsEachIter(label).lastEntry().getValue();
JsonArrayBuilder arr = Json.createArrayBuilder();
for (CandidatePhrase k : learnedWords.keySet()) arr.add(k.getPhrase());
obj.add(label, arr);
}
return obj.build().toString();
}
Aggregations