@GET @Path("{datum}") @Produces({MediaType.TEXT_PLAIN, "text/csv", MediaType.APPLICATION_JSON}) public String get(@PathParam("datum") String datum) throws OryxServingException { check(datum != null && !datum.isEmpty(), "Data is needed to cluster"); KMeansServingModel model = (KMeansServingModel) getServingModel(); String[] tokens = TextUtils.parseDelimited(datum, ','); return model.closestCluster(KMeansUtils.featuresFromTokens(tokens, model.getInputSchema())).getSecond().toString(); }
private String nearestClusterID(String datum) throws OryxServingException { check(datum != null && !datum.isEmpty(), "Data is needed to cluster"); String[] tokens = TextUtils.parseDelimited(datum, ','); ClusteringServingModel model = (ClusteringServingModel) getServingModel(); int nearestID; try { nearestID = model.nearestClusterID(tokens); } catch (IllegalArgumentException iae) { throw new OryxServingException(Response.Status.BAD_REQUEST, iae.getMessage()); } return Integer.toString(nearestID); }
private String predict(String datum) throws OryxServingException { check(datum != null && !datum.isEmpty(), "Missing input data"); ClassificationRegressionServingModel model = (ClassificationRegressionServingModel) getServingModel(); String[] parsedDatum = TextUtils.parseDelimited(datum, ','); try { return model.predict(parsedDatum); } catch (IllegalArgumentException iae) { throw new OryxServingException(Response.Status.BAD_REQUEST, iae.getMessage()); } }
/** * @param pmml PMML representation of Clusters * @return List of {@link ClusterInfo} */ public static List<ClusterInfo> read(PMML pmml) { Model model = pmml.getModels().get(0); Preconditions.checkArgument(model instanceof ClusteringModel); ClusteringModel clusteringModel = (ClusteringModel) model; return clusteringModel.getClusters().stream().map(cluster -> new ClusterInfo(Integer.parseInt(cluster.getId()), VectorMath.parseVector(TextUtils.parseDelimited(cluster.getArray().getValue(), ' ')), cluster.getSize()) ).collect(Collectors.toList()); }
private void doPost(BufferedReader buffered) throws IOException, OryxServingException { for (String line; (line = buffered.readLine()) != null;) { String[] tokens = TextUtils.parseDelimited(line, ','); check(tokens.length >= 2, line); String userID = tokens[0];
@GET @Path("{datum}") @Produces({MediaType.TEXT_PLAIN, "text/csv", MediaType.APPLICATION_JSON}) public List<IDValue> get(@PathParam("datum") String datum) throws OryxServingException { check(datum != null && !datum.isEmpty(), "Missing input data"); RDFServingModel model = (RDFServingModel) getServingModel(); InputSchema inputSchema = model.getInputSchema(); check(inputSchema.isClassification(), "Only applicable for classification"); Prediction prediction = model.makePrediction(TextUtils.parseDelimited(datum, ',')); double[] probabilities = ((CategoricalPrediction) prediction).getCategoryProbabilities(); int targetIndex = inputSchema.getTargetFeatureIndex(); CategoricalValueEncodings valueEncodings = model.getEncodings(); Map<Integer,String> targetEncodingName = valueEncodings.getEncodingValueMap(targetIndex); List<IDValue> result = new ArrayList<>(probabilities.length); for (int i = 0; i < probabilities.length; i++) { result.add(new IDValue(targetEncodingName.get(i), probabilities[i])); } return result; }
@Test public void testParseDelimited() throws Exception { assertArrayEquals(new String[] {"a", "1", "foo"}, TextUtils.parseDelimited("a,1,foo", ',')); assertArrayEquals(new String[] {"a", "1", "foo", ""}, TextUtils.parseDelimited("a,1,foo,", ',')); assertArrayEquals(new String[] {"2.3"}, TextUtils.parseDelimited("2.3", ',')); assertArrayEquals(new String[] {"\"a\""}, TextUtils.parseDelimited("\"\"\"a\"\"\"", ',')); assertArrayEquals(new String[] {"\"", "\"\""}, TextUtils.parseDelimited("\"\"\"\" \"\"\"\"\"\"", ' ')); // Different from JSON, sort of: assertArrayEquals(new String[] {""}, TextUtils.parseDelimited("", ',')); assertArrayEquals(new String[] {"a", "1,", ",foo"}, TextUtils.parseDelimited("a\t1,\t,foo", '\t')); assertArrayEquals(new String[] {"a", "1", "foo", ""}, TextUtils.parseDelimited("a 1 foo ", ' ')); assertArrayEquals(new String[] {"-1.0", "a\" \"b"}, TextUtils.parseDelimited("-1.0 a\"\\ \"b", ' ')); assertArrayEquals(new String[] {"-1.0", "a\"b\"c"}, TextUtils.parseDelimited("-1.0 \"a\\\"b\\\"c\"", ' ')); }
Map<String,Integer> valueEncodingMap = categoricalValueEncodings.getValueEncodingMap(featureNumber); String[] categories = TextUtils.parseDelimited(simpleSetPredicate.getArray().getValue(), ' '); BitSet activeCategories = new BitSet(valueEncodingMap.size()); if (operator == SimpleSetPredicate.BooleanOperator.IS_IN) {
Cluster cluster = clusters.get(id); String[] tokens = TextUtils.parseDelimited(cluster.getArray().getValue(), ' '); double[] modelCenter = VectorMath.parseVector(tokens);
/** * @param pmml PMML representation of Clusters * @return List of {@link ClusterInfo} */ public static List<ClusterInfo> read(PMML pmml) { Model model = pmml.getModels().get(0); Preconditions.checkArgument(model instanceof ClusteringModel); ClusteringModel clusteringModel = (ClusteringModel) model; return clusteringModel.getClusters().stream().map(cluster -> new ClusterInfo(Integer.parseInt(cluster.getId()), VectorMath.parseVector(TextUtils.parseDelimited(cluster.getArray().getValue(), ' ')), cluster.getSize()) ).collect(Collectors.toList()); }
Map<String,Integer> valueEncodingMap = categoricalValueEncodings.getValueEncodingMap(featureNumber); String[] categories = TextUtils.parseDelimited(simpleSetPredicate.getArray().getValue(), ' '); BitSet activeCategories = new BitSet(valueEncodingMap.size()); if (operator == SimpleSetPredicate.BooleanOperator.IS_IN) {