You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by dw...@apache.org on 2008/03/09 12:13:53 UTC
svn commit: r635218 - in /lucene/mahout/trunk/src:
main/java/org/apache/mahout/utils/Point.java
test/java/org/apache/mahout/utils/TestPoint.java
Author: dweiss
Date: Sun Mar 9 04:13:52 2008
New Revision: 635218
URL: http://svn.apache.org/viewvc?rev=635218&view=rev
Log:
Comitting https://issues.apache.org/jira/browse/MAHOUT-12 (point format parsing/ emitting).
Added:
lucene/mahout/trunk/src/test/java/org/apache/mahout/utils/TestPoint.java
Modified:
lucene/mahout/trunk/src/main/java/org/apache/mahout/utils/Point.java
Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/utils/Point.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/utils/Point.java?rev=635218&r1=635217&r2=635218&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/utils/Point.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/utils/Point.java Sun Mar 9 04:13:52 2008
@@ -20,8 +20,13 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
+import java.util.regex.Pattern;
public class Point {
+ /**
+ * Split pattern for {@link #decodePoint(String)}.
+ */
+ private final static Pattern splitPattern = Pattern.compile("[,]");
/**
* Format the point for input to a Mapper or Reducer
@@ -30,30 +35,39 @@
* @return a String
*/
public static String formatPoint(Float[] point) {
- String out = "";
- out += "[";
- for (int i = 0; i < point.length; i++)
- out += point[i] + ", ";
- out += "] ";
- String ptOut = out;
- return ptOut;
+ if (point.length == 0) {
+ return "[]";
+ }
+
+ final StringBuilder out = new StringBuilder();
+ out.append('[');
+ for (int i = 0; i < point.length; i++) {
+ if (i > 0) out.append(", ");
+ out.append(point[i]);
+ }
+ out.append(']');
+ return out.toString();
}
/**
* Decodes a point from its string representation.
*
* @param formattedString a comma-terminated String of the form
- * "[v1,v2,...,vn,]"
+ * "[v1,v2,...,vn]"
* @return the Float[] defining an n-dimensional point
*/
public static Float[] decodePoint(String formattedString) {
- String[] pts = formattedString.split(",");
- Float[] point = new Float[pts.length - 1];
- for (int i = 0; i < point.length; i++)
- if (pts[i].startsWith("["))
- point[i] = new Float(pts[i].substring(1));
- else if (!pts[i].startsWith("]"))
- point[i] = new Float(pts[i]);
+ if (formattedString.charAt(0) != '['
+ || formattedString.charAt(formattedString.length() - 1) != ']') {
+ throw new IllegalArgumentException(formattedString);
+ }
+ formattedString = formattedString.substring(1, formattedString.length() - 2);
+
+ final String[] pts = splitPattern.split(formattedString);
+ final Float[] point = new Float[pts.length];
+ for (int i = 0; i < point.length; i++) {
+ point[i] = new Float(pts[i]);
+ }
return point;
}
@@ -65,8 +79,7 @@
* @return
*/
public static String ptOut(String out, Float[] pt) {
- out += formatPoint(pt);
- return out;
+ return out + formatPoint(pt);
}
/**
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/utils/TestPoint.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/utils/TestPoint.java?rev=635218&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/utils/TestPoint.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/utils/TestPoint.java Sun Mar 9 04:13:52 2008
@@ -0,0 +1,30 @@
+package org.apache.mahout.utils;
+
+import junit.framework.TestCase;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License 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.
+ */
+
+public class TestPoint extends TestCase {
+ public void testFormatPoint() {
+ assertEquals("[1.0, 1.5]", Point.formatPoint(new Float [] {1.0f, 1.5f}));
+ }
+
+ public void testPtOut() {
+ assertEquals("abc[1.0, 1.5]", Point.ptOut("abc", new Float [] {1.0f, 1.5f}));
+ }
+}