Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7244#discussion_r34003752
  
    --- Diff: docs/ml-features.md ---
    @@ -288,6 +288,82 @@ for words_label in wordsDataFrame.select("words", 
"label").take(3):
     </div>
     
     
    +## $n$-gram
    +
    +An [n-gram](https://en.wikipedia.org/wiki/N-gram) is a sequence of $n$ 
tokens (typically words) for some integer $n$. The 
[NGram](api/scala/index.html#org.apache.spark.ml.feature.NGram) class can be 
used to transform input features into $n$-grams.
    +
    +`NGram` takes as input a sequence of strings (e.g. the output of a 
[Tokenizer](api/scala/index.html#org.apache.spark.ml.feature.Tokenizer)).  The 
parameter `n` is used to determine the number of terms in each $n$-gram. The 
output will consist of a sequence of $n$-grams where each $n$-gram is 
represented by a space-delimited string of $n$ consecutive words.  If the input 
sequence contains less than `n` strings, no output is produced.
    +
    +<div class="codetabs">
    +<div data-lang="scala" markdown="1">
    +{% highlight scala %}
    +import org.apache.spark.ml.feature.NGram
    +
    +val wordDataFrame = sqlContext.createDataFrame(Seq(
    +  (0, Array("Hi", "I", "heard", "about", "Spark")),
    +  (1, Array("I", "wish", "Java", "could", "use", "case", "classes")),
    +  (2, Array("Logistic", "regression", "models", "are", "neat"))
    +)).toDF("label", "words")
    +
    +val ngram = new NGram().setInputCol("words").setOutputCol("ngrams")
    +val ngramDataFrame = ngram.transform(wordDataFrame)
    +ngramDataFrame.select("ngrams", "label").take(3).foreach(println)
    +{% endhighlight %}
    +</div>
    +
    +<div data-lang="java" markdown="1">
    +{% highlight java %}
    +import com.google.common.collect.Lists;
    +
    +import org.apache.spark.api.java.JavaRDD;
    +import org.apache.spark.ml.feature.Tokenizer;
    +import org.apache.spark.mllib.linalg.Vector;
    +import org.apache.spark.sql.DataFrame;
    +import org.apache.spark.sql.Row;
    +import org.apache.spark.sql.RowFactory;
    +import org.apache.spark.sql.types.DataTypes;
    +import org.apache.spark.sql.types.Metadata;
    +import org.apache.spark.sql.types.StructField;
    +import org.apache.spark.sql.types.StructType;
    +
    +JavaRDD<Row> jrdd = jsc.parallelize(Lists.newArrayList(
    +  RowFactory.create(0D, Lists.newArrayList("Hi", "I", "heard", "about", 
"Spark")),
    +  RowFactory.create(1D, Lists.newArrayList("I", "wish", "Java", "could", 
"use", "case", "classes")),
    +  RowFactory.create(2D, Lists.newArrayList("Logistic", "regression", 
"models", "are", "neat"))
    +));
    +StructType schema = new StructType(new StructField[]{
    +  new StructField("label", DataTypes.DoubleType, false, Metadata.empty()),
    +  new StructField("words", 
DataTypes.createArrayType(DataTypes.StringType), false, Metadata.empty())
    +});
    +DataFrame wordDataFrame = sqlContext.createDataFrame(jrdd, schema);
    +NGram ngramTransformer = new 
NGram().setInputCol("words").setOutputCol("ngrams");
    +DataFrame ngramDataFrame = ngramTransformer.transform(wordDataFrame);
    +for (Row r : ngramDataFrame.select("ngrams", "label").take(3)) {
    +  java.util.List<String> ngrams = r.getList(0);
    +  for (String ngram : ngrams) System.out.print(ngram + " --- ");
    +    System.out.println();
    --- End diff --
    
    fix indentation


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to