List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights.java
License:Open Source License
/** * Returns a list of scores that denotes how related an entire corpus is to a list of individual * concepts.// w w w .jav a 2 s . co m * * @param corpus The corpus object * @param concepts Array of concept IDs, each identifying a concept * @return {@link Scores} */ public Scores getCorpusRelationScores(final Corpus corpus, final List<Concept> concepts) { final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); Validate.notEmpty(concepts, "concepts cannot be empty"); final Map<String, Object> queryParameters = new HashMap<String, Object>(); final JsonObject contentJson = new JsonObject(); final JsonArray conceptsJson = new JsonArray(); for (final Concept con : concepts) { conceptsJson.add(new JsonPrimitive(con.getId())); } contentJson.add(CONCEPTS, conceptsJson); queryParameters.put(CONCEPTS, conceptsJson.toString()); return executeRequest(API_VERSION + corpusId + RELATION_SCORES_PATH, queryParameters, Scores.class); }
From source file:com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights.java
License:Open Source License
/** * Returns a list of scores that denotes how related a document is to a list of concepts. * /*w w w . ja v a 2 s .c o m*/ * @param document Document the document object, * @param concepts the concepts * @return {@link Scores} */ public Scores getDocumentRelationScores(final Document document, final List<Concept> concepts) { final String documentId = IDHelper.getDocumentId(document); Validate.notEmpty(concepts, "concepts cannot be empty"); final Map<String, Object> queryParams = new HashMap<String, Object>(); final JsonObject contentJson = new JsonObject(); final JsonArray conceptsJson = new JsonArray(); for (final Concept con : concepts) { conceptsJson.add(new JsonPrimitive(con.getId())); } contentJson.add(CONCEPTS, conceptsJson); queryParams.put(CONCEPTS, conceptsJson.toString()); return executeRequest(API_VERSION + documentId + RELATION_SCORES_PATH, queryParams, Scores.class); }
From source file:com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights.java
License:Open Source License
/** * Searches for graph concepts by using partial matches. * //from w w w . ja v a 2 s . co m * @param graph the graph * @param concepts the concepts * @param parameters The parameters to be used in the service call, graph and concepts are * required. * <ul> * <li>RequestedFields concept_fields - Additional fields to be included in the concept * objects.<br> * <li>Integer level - A number in the range 0 - 3 that represents the level of popularity * of related concepts.<br> * <li>Integer limit - The maximum number of concepts to be returned.<br> * </ul> * @return {@link Concepts} */ public Concepts getGraphRelatedConcepts(final Graph graph, final List<Concept> concepts, final Map<String, Object> parameters) { final String graphId = IDHelper.getGraphId(graph, getFirstAccountId()); Validate.notEmpty(concepts, "concepts cannot be empty"); final Map<String, Object> queryParameters = new HashMap<String, Object>(); final String[] queryParms = new String[] { LEVEL, LIMIT }; for (final String param : queryParms) { if (parameters.containsKey(param)) queryParameters.put(param, parameters.get(param)); } if (parameters.get(CONCEPT_FIELDS) != null) { final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS); if (fields != null && !fields.isEmpty()) queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields())); } final JsonObject contentJson = new JsonObject(); final JsonArray conceptsJson = new JsonArray(); for (final Concept concept : concepts) { conceptsJson.add(new JsonPrimitive(concept.getId())); } contentJson.add(CONCEPTS, conceptsJson); queryParameters.put(CONCEPTS, conceptsJson.toString()); return executeRequest(API_VERSION + graphId + RELATED_CONCEPTS_PATH, queryParameters, Concepts.class); }
From source file:com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights.java
License:Open Source License
public Scores getGraphRelationScores(final String conceptId, final List<String> conceptIds) { Validate.notEmpty(conceptIds, "concepts cannot be empty"); final Map<String, Object> queryParameters = new HashMap<String, Object>(); final JsonObject contentJson = new JsonObject(); final JsonArray conceptsJson = new JsonArray(); for (final String value : conceptIds) { conceptsJson.add(new JsonPrimitive(value)); }// w w w . j ava2s . c o m contentJson.add(CONCEPTS, conceptsJson); queryParameters.put(CONCEPTS, conceptsJson.toString()); return executeRequest(API_VERSION + conceptId + RELATION_SCORES_PATH, queryParameters, Scores.class); }
From source file:com.ibm.watson.developer_cloud.language_translation.v2.LanguageTranslation.java
License:Open Source License
/** * Translate paragraphs of text using a model and or source and target. model_id or source and target needs to be * specified. If both are specified, then only model_id will be used * * @param texts the texts//from ww w.j a va 2s .co m * @param modelId the model id * @param source the source * @param target the target * @return The {@link TranslationResult} */ private ServiceCall<TranslationResult> translateRequest(List<String> texts, String modelId, Language source, Language target) { Validator.isTrue((texts != null) && !texts.isEmpty(), "texts cannot be null or empty"); final JsonObject contentJson = new JsonObject(); // convert the text into a json array final JsonArray paragraphs = new JsonArray(); for (String text : texts) { Validator.notNull(text, "text cannot be null"); paragraphs.add(new JsonPrimitive(text)); } contentJson.add(TEXT, paragraphs); final RequestBuilder requestBuilder = RequestBuilder.post(PATH_TRANSLATE).header(HttpHeaders.ACCEPT, HttpMediaType.APPLICATION_JSON); if (source != null) { contentJson.addProperty(SOURCE, source.toString()); } if (target != null) { contentJson.addProperty(TARGET, target.toString()); } if ((modelId != null) && !modelId.isEmpty()) { contentJson.addProperty(MODEL_ID, modelId); } requestBuilder.bodyJson(contentJson); return createServiceCall(requestBuilder.build(), ResponseConverterUtils.getObject(TranslationResult.class)); }
From source file:com.ibm.watson.developer_cloud.util.DateSerializer.java
License:Open Source License
@Override // DateSerializer.serialize() is NOT thread safe because of the underlying SimpleDateFormats. public synchronized JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { return src == null ? JsonNull.INSTANCE : new JsonPrimitive(utc.format(src)); }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v2.VisualRecognition.java
License:Open Source License
/** * Creates a {@link JsonObject} with an array of classifier ids. * /*ww w . j av a 2s . c o m*/ * @param classifiers the classifiers * @return the classifier ids as {@link JsonObject} */ private JsonObject getClassifierIdsAsJson(VisualClassifier... classifiers) { JsonObject ret = new JsonObject(); JsonArray array = new JsonArray(); ret.add(CLASSIFIER_IDS, array); for (VisualClassifier classifier : classifiers) { array.add(new JsonPrimitive(classifier.getId())); } return ret; }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition.java
License:Open Source License
private JsonObject getParametersAsJson(ClassifyImagesOptions options) { JsonObject ret = new JsonObject(); if ((options.url() != null) && (options.images() == null)) { ret.addProperty(PARAM_URL, options.url().toString()); }/* www . jav a 2 s. c o m*/ if ((options.classifierIds() != null) && !options.classifierIds().isEmpty()) { JsonArray array = new JsonArray(); for (String cId : options.classifierIds()) { array.add(new JsonPrimitive(cId)); } ret.add(PARAM_CLASSIFIER_IDS, array); } if (options.threshold() != null) { ret.addProperty(PARAM_THRESHOLD, options.threshold()); } return ret; }
From source file:com.ibm.watson.self.topics.TopicClient.java
License:Open Source License
/** * Publish data to the topic/*from w w w . jav a 2 s . c om*/ * @param path: the path * @param data: data in the form of key/ val pairs * @param persisted: true if data needs to be persisted */ public void publish(String path, String data, boolean persisted) { logger.entry(); JsonObject wrapperObject = new JsonObject(); JsonArray pathArray = new JsonArray(); pathArray.add(new JsonPrimitive(path)); wrapperObject.add(TopicConstants.TARGETS, pathArray); wrapperObject.addProperty(TopicConstants.MSG, TopicConstants.PUBLISH_AT); wrapperObject.addProperty(TopicConstants.DATA, data); wrapperObject.addProperty(TopicConstants.BINARY, false); wrapperObject.addProperty(TopicConstants.PERSISTED, persisted); this.sendMessage(wrapperObject); logger.exit(); }
From source file:com.ibm.watson.self.topics.TopicClient.java
License:Open Source License
/** * Publish binary data to the topic/* w ww.ja v a 2 s. c o m*/ * @param path: the path * @param data: binary data * @param persisted: true if data needs to be persisted */ public void publish(String path, byte[] data, boolean persisted) { logger.entry(); JsonObject wrapperObject = new JsonObject(); JsonArray pathArray = new JsonArray(); pathArray.add(new JsonPrimitive(path)); wrapperObject.add(TopicConstants.TARGETS, pathArray); wrapperObject.addProperty(TopicConstants.MSG, TopicConstants.PUBLISH_AT); wrapperObject.addProperty(TopicConstants.BINARY, true); wrapperObject.addProperty(TopicConstants.PERSISTED, persisted); this.sendMessage(wrapperObject, data); logger.exit(); }