Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (c) 2013, Cloudera, Inc. All Rights Reserved.
  *
  * Cloudera, Inc. licenses this file to you under the Apache License,
  * Version 2.0 (the "License"). You may not use this file except in
  * compliance with the License. You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the
 * License.
 */
package com.cloudera.oryx.kmeans.serving.web;

Responds to a GET request to /assign/[datum], or a POST to /assign containing the datum on one line. The input is one data point to cluster, delimited, like "1,-4,3.0". The response body contains the ID of the nearest cluster, on one line.

Author(s):
Sean Owen
public final class AssignServlet extends AbstractKMeansServlet {
  protected void doGet(HttpServletRequest request,
                       HttpServletResponse responsethrows IOException {
    CharSequence pathInfo = request.getPathInfo();
    if (pathInfo == null) {
      response.sendError(."No path");
      return;
    }
    String line = pathInfo.subSequence(1, pathInfo.length()).toString();
    doAssign(lineresponse);
  }
  protected void doPost(HttpServletRequest request,
                        HttpServletResponse responsethrows IOException {
    String line = request.getReader().readLine();
    doAssign(lineresponse);
  }
  private void doAssign(String lineHttpServletResponse responsethrows IOException {
    if (generation == null) {
                         "API method unavailable until model has been built and loaded");
      return;
    }
    RealVector vec = generation.toVector(DelimitedDataUtils.decode(line));
    if (vec == null) {
      response.sendError(."Wrong column count");
      return;
    }
    int assignment = DistanceToNearestServlet.findClosest(generationvec).getClosestCenterId();
    response.getWriter().write(Integer.toString(assignment));
  }
New to GrepCode? Check out our FAQ X