You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ss...@apache.org on 2014/05/25 13:40:47 UTC

svn commit: r1597416 - /mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext

Author: ssc
Date: Sun May 25 11:40:47 2014
New Revision: 1597416

URL: http://svn.apache.org/r1597416
Log:
MAHOUT-1536 Update 'Creating vectors from text' page

Modified:
    mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext

Modified: mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext
URL: http://svn.apache.org/viewvc/mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext?rev=1597416&r1=1597415&r2=1597416&view=diff
==============================================================================
--- mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext (original)
+++ mahout/site/mahout_cms/trunk/content/users/basics/creating-vectors-from-text.mdtext Sun May 25 11:40:47 2014
@@ -5,8 +5,7 @@ Title: Creating Vectors from Text
 # Introduction
 
 For clustering and classifying documents it is usually necessary to convert the raw text
-into vectors that can then be consumed by the clustering [Algorithms](algorithms.html)
-.  These approaches are described below.
+into vectors that can then be consumed by the clustering [Algorithms](algorithms.html).  These approaches are described below.
 
 <a name="CreatingVectorsfromText-FromLucene"></a>
 # From Lucene
@@ -29,13 +28,13 @@ McCandless.
 
 To get started, make sure you get a fresh copy of Mahout from [SVN](http://mahout.apache.org/developers/buildingmahout.html)
  and are comfortable building it. It defines interfaces and implementations
-for efficiently iterating over a Data Source (it only supports Lucene
+for efficiently iterating over a data source (it only supports Lucene
 currently, but should be extensible to databases, Solr, etc.) and produces
 a Mahout Vector file and term dictionary which can then be used for
-clustering.   The main code for driving this is the Driver program located
-in the org.apache.mahout.utils.vectors package.  The Driver program offers
+clustering.   The main code for driving this is the driver program located
+in the org.apache.mahout.utils.vectors package.  The driver program offers
 several input options, which can be displayed by specifying the --help
-option.  Examples of running the Driver are included below:
+option.  Examples of running the driver are included below:
 
 <a name="CreatingVectorsfromText-GeneratinganoutputfilefromaLuceneIndex"></a>
 #### Generating an output file from a Lucene Index
@@ -94,13 +93,13 @@ option.  Examples of running the Driver 
                                                as a value between 0 and  
                                                1. The default is 0.  
   
-#### Create 50 Vectors from an Index 
+#### Example: Create 50 Vectors from an Index 
 
     $MAHOUT_HOME/bin/mahout lucene.vector
-        --dir <PATH>/wikipedia/solr/data/index 
+        --dir $WORK_DIR/wikipedia/solr/data/index 
         --field body 
-        --dictOut <PATH>/solr/wikipedia/dict.txt
-        --output <PATH>/solr/wikipedia/out.txt 
+        --dictOut $WORK_DIR/solr/wikipedia/dict.txt
+        --output $WORK_DIR/solr/wikipedia/out.txt 
         --max 50
 
 
@@ -110,13 +109,13 @@ outputs 50 vectors.  If you don't specif
 the index are output.
 
 <a name="CreatingVectorsfromText-50VectorsFromLuceneL2Norm"></a>
-#### Creating 50 Normalized Vectors from a Lucene Index using the [L_2 Norm](http://en.wikipedia.org/wiki/Lp_space)
+#### Example: Creating 50 Normalized Vectors from a Lucene Index using the [L_2 Norm](http://en.wikipedia.org/wiki/Lp_space)
 
     $MAHOUT_HOME/bin/mahout lucene.vector 
-        --dir <PATH>/wikipedia/solr/data/index 
+        --dir $WORK_DIR/wikipedia/solr/data/index 
         --field body 
-        --dictOut <PATH>/solr/wikipedia/dict.txt
-        --output <PATH>/solr/wikipedia/out.txt 
+        --dictOut $WORK_DIR/solr/wikipedia/dict.txt
+        --output $WORK_DIR/solr/wikipedia/out.txt 
         --max 50 
         --norm 2
 
@@ -126,7 +125,7 @@ the index are output.
 Mahout has utilities to generate Vectors from a directory of text
 documents. Before creating the vectors, you need to convert the documents
 to SequenceFile format. SequenceFile is a hadoop class which allows us to
-write arbitary key,value pairs into it. The DocumentVectorizer requires the
+write arbitary (key, value) pairs into it. The DocumentVectorizer requires the
 key to be a Text with a unique document id, and value to be the Text
 content in UTF-8 format.
 
@@ -137,8 +136,6 @@ binary documents to text.
 #### Converting directory of documents to SequenceFile format
 Mahout has a nifty utility which reads a directory path including its
 sub-directories and creates the SequenceFile in a chunked manner for us.
-the document id generated is <PREFIX><RELATIVE PATH FROM
-PARENT>/document.txt
 
     $MAHOUT_HOME/bin/mahout seqdirectory 
         --input (-i) input                       Path to job input directory.   
@@ -169,8 +166,9 @@ PARENT>/document.txt
         --help (-h)                              Print out help                 
         --tempDir tempDir                        Intermediate output directory  
         --startPhase startPhase                  First phase to run             
-        --endPhase endPhase                      Last phase to run  >
+        --endPhase endPhase                      Last phase to run  
 
+The output of seqDirectory will be a Sequence file < Text, Text > of all documents (/sub-directory-path/documentFileName, documentText).
 
 <a name="CreatingVectorsfromText-CreatingVectorsfromSequenceFile"></a>
 #### Creating Vectors from SequenceFile
@@ -230,7 +228,38 @@ generate vectors. 
                                               be logNormalize. If set true else false
 
 
---minSupport is the min frequency for the word to be considered as a feature. --minDF is the min number of documents the word needs to be in --maxDFPercent is the max value of the expression (document frequency of a word/total number of document) to be considered as good feature to be in the document. These options are helpful in removing high frequency features like stop words.
+
+This will create SequenceFiles of tokenized documents < Text, StringTuple >  (docID, tokenizedDoc) and vectorized documents < Text, VectorWritable > (docID, TF-IDF Vector).  
+
+As well, seq2sparse will create SequenceFiles for: a dictionary (wordIndex, word), a word frequency count (wordIndex, count) and a document frequency count (wordIndex, DFCount) in the output directory. 
+
+The --minSupport option is the min frequency for the word to be considered as a feature; --minDF is the min number of documents the word needs to be in; --maxDFPercent is the max value of the expression (document frequency of a word/total number of document) to be considered as good feature to be in the document. These options are helpful in removing high frequency features like stop words.
+
+The vectorized documents can then be used as input to many of Mahout's classification and clustering algorithms.
+
+#### Example: Creating Normalized [TF-IDF](http://en.wikipedia.org/wiki/Tf%E2%80%93idf) Vectors from a directory of text documents using [trigrams](http://en.wikipedia.org/wiki/N-gram) and the [L_2 Norm](http://en.wikipedia.org/wiki/Lp_space)
+Create sequence files from the directory of text documents:
+    
+    $MAHOUT_HOME/bin/mahout seqdirectory 
+        -i $WORK_DIR/reuters 
+        -o $WORK_DIR/reuters-seqdir 
+        -c UTF-8
+        -chunk 64
+        -xm sequential
+
+Vectorize the documents using trigrams, L_2 length normalization and a maximum document frequency cutoff of 85%.
+
+    $MAHOUT_HOME/bin/mahout seq2sparse 
+        -i $WORK_DIR/reuters-out-seqdir/ 
+        -o $WORK_DIR/reuters-out-seqdir-sparse-kmeans 
+        --namedVec
+        -wt tfidf
+        -ng 3
+        -n 2
+        --maxDFPercent 85 
+
+The sequence file in the $WORK_DIR/reuters-out-seqdir-sparse-kmeans/tfidf-vectors directory can now be used as input to the Mahout [k-Means](http://mahout.apache.org/users/clustering/k-means-clustering.html) clustering algorithm.
+
 <a name="CreatingVectorsfromText-Background"></a>
 ## Background
 
@@ -247,7 +276,11 @@ Iterable<Vector> (called VectorIterable 
 reuse the existing VectorWriter classes:
 
 
-    VectorWriter vectorWriter = SequenceFile.createWriter(filesystem, configuration, outfile, LongWritable.class, SparseVector.class);
+    VectorWriter vectorWriter = SequenceFile.createWriter(filesystem,
+                                                          configuration,
+                                                          outfile,
+                                                          LongWritable.class,
+                                                          SparseVector.class);
 
     long numDocs = vectorWriter.write(new VectorIterable(), Long.MAX_VALUE);