You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2011/01/19 01:49:10 UTC

svn commit: r1060639 - in /mahout/trunk: math/src/main/java/org/apache/mahout/math/NamedVector.java utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java

Author: srowen
Date: Wed Jan 19 00:49:10 2011
New Revision: 1060639

URL: http://svn.apache.org/viewvc?rev=1060639&view=rev
Log:
MAHOUT-402

Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java
    mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java?rev=1060639&r1=1060638&r2=1060639&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java Wed Jan 19 00:49:10 2011
@@ -19,6 +19,9 @@ package org.apache.mahout.math;
 
 import java.util.Iterator;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
 import org.apache.mahout.math.function.DoubleDoubleFunction;
 import org.apache.mahout.math.function.DoubleFunction;
 
@@ -68,7 +71,10 @@ public class NamedVector implements Vect
   }
 
   public String asFormatString() {
-    return delegate.asFormatString();
+    GsonBuilder builder = new GsonBuilder();
+    builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
+    Gson gson = builder.create();
+    return gson.toJson(this, Vector.class);
   }
 
   public Vector assign(double value) {

Modified: mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java?rev=1060639&r1=1060638&r2=1060639&view=diff
==============================================================================
--- mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java (original)
+++ mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java Wed Jan 19 00:49:10 2011
@@ -33,6 +33,7 @@ import org.apache.hadoop.io.SequenceFile
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
 import org.apache.mahout.common.FileLineIterator;
+import org.apache.mahout.math.NamedVector;
 import org.apache.mahout.math.Vector;
 import org.apache.mahout.math.map.OpenObjectIntHashMap;
 
@@ -43,16 +44,16 @@ public final class VectorHelper {
   private VectorHelper() { }
   
   /**
-   * Create a String from a vector that fills in the values with the appropriate value from a dictionary where
-   * each the ith entry is the term for the ith vector cell..
-   * 
-   * @param vector
-   * @param dictionary
-   *          The dictionary. See
-   * @return The String
+   * @return a String from a vector that fills in the values with the appropriate value from a dictionary where
+   * each the ith entry is the term for the ith vector cell.
    */
   public static String vectorToString(Vector vector, String[] dictionary) {
     StringBuilder bldr = new StringBuilder(2048);
+    
+    if (vector instanceof NamedVector) {
+      bldr.append("name: ").append(((NamedVector) vector).getName()).append('\t');
+    }
+    
     bldr.append("elts: {");
     Iterator<Vector.Element> iter = vector.iterateNonZero();
     boolean first = true;
@@ -65,8 +66,7 @@ public final class VectorHelper {
       Vector.Element elt = iter.next();
       if (dictionary != null) {
         bldr.append(dictionary[elt.index()]);
-      }
-      else {
+      } else {
         bldr.append(elt.index());
       }
       bldr.append(':').append(elt.get());
@@ -80,9 +80,6 @@ public final class VectorHelper {
    * <pre>
    * term DocFreq Index
    * </pre>
-   * 
-   * @param dictFile
-   * @throws IOException
    */
   public static String[] loadTermDictionary(File dictFile) throws IOException {
     return loadTermDictionary(new FileInputStream(dictFile));
@@ -91,12 +88,9 @@ public final class VectorHelper {
   /**
    * Read a dictionary in {@link SequenceFile} generated by
    * {@link org.apache.mahout.vectorizer.DictionaryVectorizer}
-   * 
-   * @param conf
-   * @param fs
+   *
    * @param filePattern
    *          <PATH TO DICTIONARY>/dictionary.file-*
-   * @throws IOException
    */
   public static String[] loadTermDictionary(Configuration conf, FileSystem fs, String filePattern) throws IOException {
     FileStatus[] dictionaryFiles = fs.globStatus(new Path(filePattern));
@@ -129,7 +123,6 @@ public final class VectorHelper {
     FileLineIterator it = new FileLineIterator(is);
     
     int numEntries = Integer.parseInt(it.next());
-    // System.out.println(numEntries);
     String[] result = new String[numEntries];
     
     while (it.hasNext()) {